// TakProg3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <set>
#include <iterator>
using namespace std;


int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int gcdmul(vector<int> arr, int n)
{
    int 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::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    int n, m;
    cin >> t;
    
    
    int neki;
    int gc;
    for (int i = 1; i <= t; i++) {
        bool mark = 0;
        vector<int>niz;

        cin >> n >> m;

        int br,pre;
        cin >> pre;
        neki = pre;
        for (int j = 1; j < n; j++)

        {
           
            cin >> br;
            niz.push_back(abs(br-pre));
            pre = br;
        }
        
        
        int gc = gcdmul(niz, niz.size());
        //for (int p = 0; p < niz.size(); p++) {
       //     cout << niz[p];
       // }
      //  cout << gc;
        if (gc == 1) {
            cout << 0 << endl;
        }
        else {
        
            for (int l = 0; l < m; l++) {
                int boj;
                cin >> boj;
                int razlika = abs(boj - neki);
                if (razlika % gc == 0 && mark==false) {
                    cout << 0 << endl;
                    mark = true;
                }
               
                if (l == m - 1 && mark == false) {
                    cout << 1 << endl;
                }
            }
        }
            }
        }
    
       
    
    
   


// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file