#include <bits/stdc++.h>
#define ll long long

using namespace std;

long long nzd(long long a, long long b)
{
    if (b == 0) return a;
    return (nzd(b, a%b));
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);
    int t, n, m;
    ll korak, a, mod;
    bool b;
    cin >> t;
    for (int i = 0; i < t; i++)
    {
        cin >> n >> m >> korak >> a;
        mod = min(a, korak);
        korak = abs(a-korak);
        b = true;
        for (int j = 2; j < n; j++)
        {
            cin >> a;
            korak = nzd(korak, abs(a%korak - mod));
            mod = a % korak;
        }
        for (int j = 0; j < m; j++)
        {
            cin >> a;
            b = b && a % korak != mod;
        }
        cout << b << "\n";
    }
    return 0;
}