// Luka Mitrovic
// Zadatak 3

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {

	unsigned long long prihvacene[75005];
	unsigned long long odbijene[75005];

	int t;
	cin >> t;

	while (t--) {
		int n,m;
		cin >> n >> m;

		//cout << endl;
		for (int i = 0; i < n; i++) {
			cin >> prihvacene[i];
			//cout << prihvacene[i] << " ";
		}
		//cout << endl;
		for (int i = 0; i < m; i++) {
			cin >> odbijene[i];
			//cout << odbijene[i] << " ";
		}
		//cout << endl;
		sort(prihvacene, prihvacene + n);

		unsigned long long int min_razmak = prihvacene[n-1];
		unsigned long long int min_broj = prihvacene[0];
		//cout << " min = " << min_broj << endl;

		for (int i = 1; i < n; i++) {
			min_razmak = min(min_razmak, prihvacene[i] - prihvacene[i-1]);
		}

		//cout << "min razmak = " << min_razmak << endl;

		bool not_good = false;

		for (int i = 1; i < n; i++) {
			if ((prihvacene[i] - min_broj) % min_razmak != 0) {
				not_good = true;
			} 
		}
		
		for (int i = 0; i < m; i++) {
			if ((odbijene[i] - min_broj) % min_razmak == 0) {
				not_good = true;
			}
		}

		if (not_good) {
			cout << 0 << endl;
		} else {
			cout << 1 << endl;
		}


	}

	return 0;
} //45