#include <iostream>
#include <algorithm>

using namespace std;

void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

int par (int a[], int low, int high)
{
    int pivot = a[high];
    int i = (low - 1);

    for (int j = low; j <= high- 1; j++) {
        if (a[j] <= pivot)
        {
            i++;
            swap(&a[i], &a[j]);
        }
    }
    swap(&a[i + 1], &a[high]);
    return (i + 1);
}


void quickSort(int a[], int low, int high)
{
    if (low < high) {
        int pivot = par(a, low, high);

        quickSort(a, low, pivot - 1);
        quickSort(a, pivot + 1, high);
    }
}


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

    struct tinf{
        int n;
        int m;
        int tf=2;
        int a[];
    }ti[t];

    for(int i=0; i<t; i++){
        cin >> ti[i].n;
        cin >> ti[i].m;

        for(int j=0; j<ti[i].n; j++){
            cin >> ti[i].a[j];
        }
        quickSort(ti[i].a,0,ti[i].n-1);
        int k=ti[i].n;
        for(int j=0; j<ti[i].m; j++){
            cin >> ti[i].a[j+k];
        }

        int x = ti[i].a[1]-ti[i].a[0];

        for(int j=1; j<ti[i].n-1; j++){
            if((ti[i].a[j+1]-ti[i].a[j])%x != 0){
                ti[i].tf=0;
                continue;
            }
        }

        if(ti[i].tf!=0){

            for(int j=ti[i].n; j<ti[i].m; j++){
                if(ti[i].a[j]%x == ti[i].a[0]%x){
                    ti[i].tf=0;
                    continue;
                }
            }

            if(ti[i].tf!=0) ti[i].tf=1;

        }

    }


    for(int i=0; i<t; i++){
        cout << ti[i].tf << "\n";
    }


    return 0;
}