#include <iostream>
#include <stdlib.h>
using namespace std;

int compare(const void* a, const void* b)
{
    return (*(int*)a - *(int*)b);
}

int main()
{

    long long int t;
    cin >> t;
    long long int n, m, min, k, o,x;
    long long int b[100];
    long long int a[100];
    long long int c[100];
    for (int p = 0; p < t;p++)
    {
       
        
        o = 1;
        cin >> n >> m;
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int i = 0; i < m; i++) cin >> b[i];
        qsort(a, n, sizeof(long long int), compare);
        qsort(b, m, sizeof(long long int), compare);
        int pl = 0;
        int y = 1;
        min = a[y] - a[pl];
        long long int max = a[n-1];
        if (b[m-1] > max) max = b[m-1];
        while (y < n)
        {
            if (a[y] - a[pl] < min) min = a[y] - a[pl];
            y++;
            pl++;
        }
        k = min;
        x = a[0];
        while (x <= max)
        {
            if (o == 0) break;
            int l = 1;
           
            for (int i = l; i < n; i++)
            {
                if ((a[i] - x) % k != 0)
                {
                    o = 0;
                    
                }
            }
             for (int j = 0; j < m; j++)
               {
                        if (x == b[j]) o = 0;
                        
               }                
             if (x == max) break;
            l++;
            x += k;
            
        }
        c[p] = o;
    }
    for (int i = 0; i < t; i++) cout << c[i] << endl;

    return 0;
}