

1. Choose any list ‘A’ or ‘B’.
2. Traverse from left to right.
3. After picking a number, if the picked number is present in both the arrays, you are allowed to traverse to the other array.
Since the answer can be very large, print answer % (10^9 + 7).
If the given array are ‘A’ = [1,3,5,7,9] and ‘B’ = [3,5,100]”.The maximum score can be achieved is 109[1+3+5+100].
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two integers, 'N’ and ‘M’, denoting the lengths of the arrays.
The second line contains ‘A’ array.
The third line contains ‘B’ array.
For each test case, print an integer corresponding to the maximum score % (10^9 +7).
Print the output of each test case 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 <= 10
1 <= N,M <= 10^4.
1 <= A[i], B[i] <= 10^6.
Time limit: 1 sec
In this approach, we will create two dp arrays ‘DP_A’ and ‘DP_B’ to calculate the final answer. ’DP_A’[i] represents the maximum answer if we start from any array and finishes at index i-1 of array ‘A’.Similarly, DP_B[j] represents the maximum answer if we start from any array and finish at index i-1 of array ‘B’.
We will calculate the dp table using these conditions:
'DP_A'[i] = 'DP_B'[j] = max('DP_A'[i-1], 'DP_B'[j-1]) + 'A'[i-1] with 'A'[i-1] = 'B'[j-1]
'DP_A'[i] = 'DP_A'[i-1] + 'A'[i-1] with 'A'[i] < 'B'[j]
'DP_B'[j] = 'DP_B'[j-1] + 'B'[j-1] with 'B'[j] < 'A'[i]
At the end we will return the maximum of ‘DP_A’[n] and ‘DP_B’[m].
The main intuition of the solution is that we must take the common elements and won't miss them.And there will be two path between the common elements,and we will take and only take one path with the greater sum.
In this approach, we will iterate through the lists using two pointers ‘i’ and ‘j’ for ‘A’ and ‘B’ array respectively. We will iterate both the arrays always take the step in the smaller element and if we encounter the condition where A[i] is equal to B[j], we have the choice to change our path, we will compare the accumulated sum and pick the greater one.