Equal Arrays
59% Success2197 Attempts30 Points1s Time Limit256MB Memory1024 KB Max Code

You are given two arrays \(A\) and \(B\) of length \(N\) and \(M\) respectively. You can perform the following operation any number of times on \(A\) and \(B\).

  • Replace any subarray of the array with the sum of the elements in the subarray.

You want to make \(A\) and \(B\) equal. Find the maximum possible length of arrays \(A\) and \(B\) such that \(A\) is equal to \(B\). If it is impossible to make \(A\) and \(B\) equal then print \(-1\).

Input format

  • The first line contains an integer \(T\), which denotes the number of test cases.
  • The first line of each test case contains two integers \(N\) and \(M\), denoting the length of the array \(A\) and \(B\).
  • The second line of each test case contains \(N\) space-separated integers, elements of array \(A\).
  • The third line of each test case contains \(M\) space-separated integers, elements of array \(B\).

Output format

For each test case, print the maximum possible length of arrays \(A\) and \(B\) such that \(A\) is equal to \(B\). If it is impossible to make \(A\) and \(B\) equal then print \(-1\) in a new line.

Constraints

\(1 \leq T \leq 10\)
\(1 \leq N, M \leq 10^5\)
\(1 \leq A[i], B[i] \leq 10^5\)

Examples
Input
2
3 4
3 2 2
1 3 1 3
5 3
1 3 2 2 3
4 2 5
Output
-1
3
Explanation

For test case \(1\): It can be proven that we cannot make \(A\) and \(B\) equal. Hence, the answer is \(-1\).

For test case \(2\): We can perform operations on \(A\) from index 1 to 2 and from index 4 to 5 (1-based indexing).
\(A\) and \(B\) after performing the operation is [4, 2, 5]. The maximum possible length is \(3\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...
Results