#include<bits/stdc++.h>
using namespace std;
const int MAX = 75010;

unsigned long long a[MAX], b[MAX];

unsigned long long gcd(unsigned long long x[], int n) {
    unsigned long long ret = x[1] - x[0];
    for(int i = 2 ; i < n ; i++) {
        ret = __gcd(ret, x[i] - x[i - 1]);
        if(ret == 1) return 1;
    }
    return ret;
}

int main () {
    int t, n, m;
    scanf("%d", &t);
    while(t--) {
        int res = 1;
        scanf("%d%d", &n, &m);
        for(int i = 0 ; i < n ; i++) scanf("%qu", &a[i]);
        for(int i = 0 ; i < m ; i++) scanf("%qu", &b[i]);
        sort(a, a + n);
        sort(b, b + m);
        unsigned long long step = gcd(a, n);
        for(int i = 0 ; i < m ; i++) {
            if((b[i] - a[0]) % step == 0) {
                res = 0;
                break;
            }
        }
        printf("%d\n", res);
    }
    return 0;
}