// 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;


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

long long int gcdmul(vector<long long int> arr, int n)
{
   long long 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;
    
    
   long long int neki;
   bool mark;
   long long int br, pre;
   long long int gc;
   long long  int bt;
   long long int boj;
   long long int razlika;
    for (int i = 1; i <= t; i++) {
         mark = 0;
        vector<long long int>niz;

        cin >> n >> m;

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

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


// 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