#include<bits/stdc++.h>
using namespace std;

long long nozalbe[75000];
long long zalbe[75000];
int main()
{
	int t;
	cin >> t;
	int pametan[t];
	for (int counter = 0; counter < t;counter++)
	{
		long long n = 0;
		long long m = 0;
		cin >> n;
		cin >> m;
		for (int i=0;i<n;i++)
		{
        		cin >> zalbe[i];
    		}
		for (int i=0;i<m;i++)
		{
        		cin >> nozalbe[i];
    		}
		sort(zalbe, zalbe+n);
		sort(nozalbe, nozalbe+m);
		cout << nozalbe[19];
		long long x = zalbe[1] - zalbe[0];
		if (x != 1)
		{
			long long mod = zalbe[0]%x;
			//cerr << mod << endl;
			//cerr << zalbe[0] << " " <<zalbe[1] << endl;
			for (int i=0;i<n;i++)
			{
				//cerr << zalbe[i] << " " << zalbe[i]%x << " " << x << endl;
				if (zalbe[i]%x != mod)
				{
					pametan[counter] = 0;
				}
				else 
				{
					pametan[counter] = 1;
				}
			}
			for (int i=0;i<m;i++)
			{
				if (nozalbe[i]%x == mod)
				{
					pametan[counter] = 0;
				}
			}
		}
		else
		{
			pametan[counter] = 0;
		}
		
	}
	for(int i=0; i<t; i++)
	{
		cout << pametan[i] << endl;
	}
}