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

int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int n, m;
		cin >> n;
		cin >> m;
		long prihvacene[75000];
		long neprihvacene[75000];

		long p=0;
		long np=0;
		int x=0;

		int res = 0;

		for (int i = 0; i < n-1; i++)
		{
			cin >> prihvacene[i];
			p += prihvacene[i];

		}
		for (int i = 0; i < m-1; i++)
		{
			cin >> neprihvacene[i];
			np += neprihvacene[i];
		}
		int a = 0;
		
		for (int i = p; i>=0; i--)
		{
			if ((p-i)%n==0)
			{
				a = i;
				x = (p-a)/n;
				res = 1;
				break;
			}
			
		}
		for (int i = np; i >=0; i--)
		{
			if ((neprihvacene[i]-a)%x==0)
			{
				res = 0;
			}
		}
		cout << res<<endl;

	}

	return 0;
}