#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <stack>
#include <utility>
#include <algorithm>
#include <cmath>
using namespace std;

int binarySearch(long long arr[], int l, int r, long long x) //https://www.geeksforgeeks.org/binary-search/
{ 
    if (r >= l) 
	{ 
        int mid = l + (r - l) / 2; 
  
        if (arr[mid] == x) 
            return mid; 
        if (arr[mid] > x) 
            return binarySearch(arr, l, mid - 1, x); 
        return binarySearch(arr, mid + 1, r, x); 
    } 
  
    return -1; 
}

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n,m,br = 0;;
		cin >> n >> m;
		long long a[n];
		long long b[m];
		for(int i = 0; i < n; ++i)cin >> a[i];
		for(int i = 0; i < m; ++i)cin >> b[i];
		sort(a, a + n);
		
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < m; ++j)
			{
				long long temp = abs(a[i]-b[j]);
				for(int k = 0; k < n; ++k)
				{
					int res = binarySearch(a,i,n-1,a[k] + temp);
					if(res != -1)
					{
						cout << "0" << endl;
						goto label;
					}
				}
			}
		}
		
		cout << "1" << endl;
		
		label:
			continue;
	}
	
	return 0;
}