#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int nzd(int a, int b)
{
  if (a<b) swap(a,b);
  if (a%b==0) return b;
  return nzd(b,a%b);
}
int main()
{
  int n;
  cin >> n;
  vector<bool>X(n);
  for (int i=0; i<n; i++)
  {
    int n1,n2;
    vector<int>A(n1);
    vector<int>B(n2);
    vector<int>A_diff(n1-1);
    vector<int>B_diff(n2-1);
    for (int j=1; j<=n1; j++)
    {
      cin >> A[j];
    }
    sort(A.begin(), A.end());
    for (int k=1; k<=n2; k++)
    {
      cin >> B[k];
    }
    sort(B.begin(), B.end());
    for (int j=1; j<n1; j++)
    {
      A_diff[j]=A[j+1]-A[j];
    }
    for (int k=1; k<n2; k++)
    {
      B_diff[k]=B[k+1]-B[k];
    }
    int nzdA=nzd(A_diff[1], A_diff[2]);
    for (int j=2; j<=n1-2; j++)
    {
      nzdA=nzd(nzdA, A_diff[j+1]);
    }
    int nzdB=nzd(B_diff[1], B_diff[2]);
    for (int k=2; k<=n2-2; k++)
    {
      nzdB=nzd(nzdB, B_diff[k+1]);
    }
    X[i]=!(nzdB%nzdA==0);
  }
  for (int i=0; i<n; i++)
  {
    cout << X[i] << endl;
  }
  return 0;
}