You are given a 2D matrix ‘ARR’ of size ‘N x 3’ having integers, where ‘N’ is the number of rows.
Your task is to find the smallest sum possible while taking one element from each row.
The rules for selecting elements are as follows-
1. In a row, after selecting an element at a given position, you cannot select the element directly below it
2. You can only select elements that are not directly below the previously selected element.
Input format:
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of every test case contains a single integer, ‘N’ denoting the number of rows.
Then each of the ‘N’ rows contains three elements.
Output format:
For each test case, return the smallest sum possible as per the rules.
Output for each test case is printed on a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^3
0 <= ARR[i][j] <= 10^3
Where ‘ARR[i][j]’ denotes the matrix element at the jth column in the ith row
Time Limit: 1 sec