#include <iostream>
#include <cmath>
using namespace std;

int main(int argc, char** argv) {
	int t, n, m, k, d, x, mina, maxa, mina2, ost;
	cin >> t;
	for(int i = 0; i<t; i++)
	{
		cin >> n >> m;
		int a[n];
		int b[m];
		cin >> a[0];
		mina = a[0];
        maxa = a[0];
		for(int j = 1; j<n; j++)
		{
			cin >> a[j];
				if(a[j]<mina)
				{
					 mina = a[j];
				}
		}
		mina2 = a[0];
		for(int g = 1; g<n; g++)
		{
			if(a[g]!=mina && a[g] < mina2) mina2 = a[g];
		}
		for(int j = 0; j<m; j++)
		{
			cin >> b[j];
		}
		k = 0;
		d = 2;
		while(d <= mina2 && k == 0)
		{
		bool t = true;
		int c = 1;
		ost = a[0] % d; 
		while(c < n && t)
		{
			if(a[c]%d!=ost) t = false;
			c++;
		}
		if(t)
		{
			c = 0;
			while(c < m && t)
			{
				if(b[c]%d==ost) t = false;
				c++;
			}
		}
		if(t) k = 1;
		else d++;
		}
		cout << k << endl;
	}
	return 0;
}