#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    long long int t;
    cin >> t;
    int answer[t];
    
    for(long long int k=0; k<t; ++k)
    {
        answer[k] = 1;
        long long int n,m;
        cin >> n >> m;
        
        long long int a[n];
        long long int b[m];
        
        for(long long int i=0; i<n; ++i)
        {
            cin >> a[i];
        }
        sort(a, a+n);
        long long int x = a[1] - a[0];
        
        for(long long int j=0; j<m; ++j)
        {
            cin >> b[j];
            if((abs(a[0] - b[j]) % x) == 0)
            {
                answer[k] = 0;    
            }
        }
    }
    
    for(long long int i=0;i<t;++i)
    {
        cout << answer[i] << endl;    
    }
    return 0;
}