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

#define N 75001

long long NZD(long long a, long long b) {
    if (a % b == 0)
        return b;
    return NZD(b, a % b);
}

int main()
{
    int t;
    cin >> t;
    for (int k = 0; k < t; k++) {
        long long n, m;
        bool resenje = true;
        long long a[N], b[N];
        cin >> n >> m;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        for (int i = 0; i < m; i++)
            cin >> b[i];
        long long X = abs(a[1] - a[0]);
        for (int i = 2; i < n; i++) {
            if (X == 1)
                break;
            long long y = abs(a[i] - a[0]) % X;
            if (y == 0)
                continue;
            X = NZD(X, y);
        }
        for (int i = 0; i < m; i++) {
            if (abs(b[i] - a[0]) % X == 0) {
                resenje = false;
                break;
            }
        }
        cout << resenje << endl;
    }
    return 0;
}