You are given two arrays of size n1 and n2. Your task is to find all the elements that are common to both the arrays and sum them print them in the order as they appear in the second array. If there are no common elements the output would be 0.
Note: The arrays may contain duplicate elements. However, you need to sum only unique elements that are common to both arrays.
The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two three lines of input. The first line contains n1 and n2 separated by a space. The second line contains elements of arr1. The third line contains elements of arr2.
For each testcase, in a new line, print the common sum.
Since this is a function problem, you don't need to take any input. Just complete the provided function commonSum that takes two arrays as input.
1 <= T <= 100
1 <= n1,n2 <= 1000
1 <= arr1i,arr2i <= 109
1 2 3 4 5
2 3 4 5 6 7
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.