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

#define N 75001

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;
        long long X = LLONG_MAX;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        for (int i = 0; i < m; i++)
            cin >> b[i];
        for (int i = 1; i < n; i++)
            X = min(X, abs(a[i] - a[0]));
        for (int i = 1; i < n; i++) {
            if (abs(a[i] - a[0]) % X != 0) {
                resenje = false;
                break;
            }
        }
        for (int i = 0; i < m; i++) {
            if (abs(b[i] - a[0]) % X == 0) {
                resenje = false;
                break;
            }
        }
        cout << resenje << endl;
    }
    return 0;
}