#include <iostream>
#include <cmath>
using namespace std;

int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}

int main() {
int t, n, m;
long long* A = nullptr, *B = nullptr;
int *R = nullptr;

cin >> t;
R = new int[t];

for (int i = 0; i < t; i++) {
cin >> n;
cin >> m;
if (n < 2 || m>75000) return -1;

A = new long long[n];
B = new long long[m];
for (int j = 0; j < n; j++) {
cin >> A[j];
for (int k = 0; k < j; k++) {
if (A[j] == A[k]) return -4;
}
if (A[j] < 1) return -2;
}
for (int j = 0; j < m; j++) {
cin >> B[j];
for (int k = 0; k < j; k++) {
if (B[j] == B[k]) return -5;
}
if (B[j] > pow(10, 18)) return -3;
for (int k = 0; k < n; k++) {
if (B[j] == A[k]) return -6;
}
}

for (int j = 0; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
if (A[j] < A[k]) {
int tmp = A[j];
A[j] = A[k];
A[k] = tmp;
}
}
}
int nzd = 0;
for (int j = 0; j < n - 1; j++) {
nzd = abs(gcd(A[j + 1] - A[j], nzd));
}

if (nzd == 1) {
R[i] = 0;
}
else {
R[i] = 1;
for (int j = 0; j < m; j++) {
if ((abs(B[j] - A[0]) % nzd) == 0) {
R[i] = 0; break;
}
}
}

delete[] A;
delete[] B;


}

for (int i = 0; i < t; i++)
cout << R[i] << endl;

delete[] R;

return 0;

}