#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int nmax = 75000;
int t,n,m;
ll ar[nmax],br[nmax];
int main()
{
	cin >> t;
	for(int k=0;k<t;k++)
	{
		cin >> n >> m;
		for(int i=0;i<n;i++)cin >> ar[i];
		for(int i=0;i<m;i++)cin >> br[i];
		sort(ar,ar+n);
		ll med = ar[1]-ar[0];
		ll modl = ar[0]%med;
		int f=1;
		for(int i=1;i<n;i++)if(ar[i]%med!=modl){cout << "A"<<0<<endl;f=0;break;}
		if(f){
			for(int i=0;i<m;i++)if(br[i]%med==modl){cout << 0<<endl;f=0;break;}
			if(f)cout << f<<endl;	
		}
	}
}