#include <iostream>

using namespace std;

// https://www.geeksforgeeks.org/gcd-two-array-numbers/

long long gcd(long long a, long long b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
long long findGCD(long long arr[], int n)
{
    long long result = arr[0];
    for (int i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);
  
        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    int t, n, m, test;
    bool flag;
    
    long long x, temp1, temp2, f;
    
    cin >> t;
    bool res[t];
    
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;
        
        long long arr[n - 1];
        flag = true;
        
        cin >> temp1;
        f = temp1;
        
        for (int j = 1; j < n; ++j) {
            
            cin >> temp2;
            arr[j - 1] = abs(temp1 - temp2);
            
            temp1 = temp2;
        }
        
        x = findGCD(arr, n - 1);
        
        for (int j = 0; j < m; ++j) {
            cin >> test;
            
            temp1 = abs(test - f);
            
            if (temp1 % x == 0) {
                flag = false;
            }
        }
        
        res[i] = flag;
    }
    
    for (int i = 0; i < t; ++i) {
        cout << res[i] << "\n";
    }
}