
For the board of size 3 * 2 given below, with horizontal = [2] and vertical = [1, 3], the minimum cost of cutting it is explained below:

1st cut: Horizontal cut with cost 3 cutting 1 segment. Cost = 3 * 1.
2nd cut: Vertical cut with cost 2 cutting 2 segments. Cost = 2 * 2.
3rd cut: Horizontal cut with cost 1 cutting 2 segments. Cost = 1 * 2.

Above is the pictorial representation.
Cost = 3 + 4 + 2 = 9.
As the answer can be large, return your answer modulo 10^9 + 7.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case or query contains two space-separated integers 'M' and ‘N’ denoting the number of rows and columns in the board.
The second line contains ‘M-1’ single space-separated integers, representing the costs of cutting Horizontal edges.
The second line contains 'N-1' single space-separated integers, representing the costs of cutting Vertical edges.
For each test case, print an integer denoting the minimum possible cost modulo 10^9+7 in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
2 <= M <= 10^3
2 <= N <= 10^3
1 <= horizontal[i] <= 10^9
1 <= vertical[i] <= 10^9
Time limit: 1 sec
The idea is to solve the problem with a greedy approach. The total cost will be the sum of C1.S1 + C2.S2 + … C(M + N).S(M + N) where Ci is the cost of cutting a distinct edge and Si is the number of segments present when cutting that particular edge.
Declare two variables, one to store the count of horizontal segments, say ‘hori’ and second to store the count of vertical segments, say ‘verti’. Initialize both with 1.
Now, to achieve the minimum cost, we need to cut the edge with maximum cutting cost first to reduce the total cost, i.e. edge cost * the number of segments.
Here is the algorithm: