#include <iostream>
#include <cmath>
#include <algorithm>
#define forn(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) {
    if (b==0) return a;
    return gcd(b, a%b);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        scanf("%d%d", &n, &m);
        ll gici = 0;
        ll a[n];
        forn(i, n) scanf("%lld", &a[i]);
        sort(a, a+n);
        ll lastnum = a[0];
        for (int i = 1; i < n; i++) {
            gici=gcd(gici, a[i]-lastnum);
            lastnum=a[i];
        }
        ll b[m];
        forn(i, m) scanf("%lld", &b[i]);
        forn(i, m) {
            if (abs(b[i]-a[0])%gici==0) {
                printf("0\r\n");
                goto endit;
            }
        }
        printf("1\r\n");
        endit: {}
    }
}