#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
  int n;
  cin >> n;
  vector<bool>X(n);
  int nzdA;
  for (int i=0; i<n; i++)
  {
    int n1,n2;
    cin >> n1 >> n2;
    vector<int>A(n1);
    vector<int>B(n2);
    vector<int>A_diff(n1-1);
    for (int j=1; j<=n1; j++)
    {
      cin >> A[j];
    }

    for (int j=2; j<=n1; j++)
    {
      A_diff[j-1]=A[j]-A[1];
    }

    if (n1==2)
    {
      nzdA=A_diff[1];
    }
    else
    {
      nzdA=__gcd(A_diff[1], A_diff[2]);
    }
    for (int j=2; j<=n1-2; j++)
    {
      nzdA=__gcd(nzdA, A_diff[j+1]);
    } 
    X[i]=false;
    for (int k=1; k<=n2; k++)
    {
      int b;
      cin >> b;
      if (b-A[1]%nzdA==0)
      {
          X[i]=true;
      }
    }
  }
  for (int i=0; i<n; i++)
  {
    cout << X[i] << endl;
  }
  return 0;
}