#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int main()
{
    int n, m, t, pom, prvi, drugi, x;
    cin >> t;
    vector<bool> rez;
    bool trrez;
    for (int b1 = 0; b1 < t; b1++)
    {
        cin >> n >> m >> prvi >> drugi;
        x = abs(prvi - drugi);
        for (int i = 0; i < n - 2; i++)
        {
            cin >> pom;
            x = gcd(x, prvi - pom);
        }
        trrez = true;
        for (int i = 0; i < m; i++)
        {
            cin >> pom;
            if (abs(prvi - pom) % x == 0)
            {
                trrez = false;
            }
        }
        rez.push_back(trrez);
    }
    for (int i = 0; i < rez.size(); i++)
    {
        cout << rez[i] << '\n';
    }
}