#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

int findDiff (vector <long long> A, vector <long long> B){
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());

        vector <long long> ::iterator it = A.begin();
        vector <long long> ::iterator bit = --B.end();
        vector <long long> dif; 
    
    for(int i = 0; i < A.size() - 1; i++){
        dif.push_back(A[i+1]-A[i]);
    }

    long long x = *min_element(dif.begin(),dif.end());

    while((*it) - x > 0){
        *it -= x;
    }


    long long prvi = *it;
    A.clear();

    int size=(*bit/x);
    A.resize(size);
    A.clear();

/*        int i = 0;
        while(i < size) {
            A.push_back(prvi + x * i);
            i++;
        }
*/
    for(int i = 0; i < size; i++) {
        A.push_back(prvi + x * i);
    }
    int q;
    it = A.begin();

    for(bit = B.begin(); bit < B.end(); bit++) {
        for(it = A.begin(); it < A.end(); it++) {
            if(*bit == *it) {
                return q = 0;
            }
        }

    }

return q = 1;
}
int main () {
    vector <long long> A;
    vector <long long> B;
    int T, i = 0, j = 0;
    long long c, v, Ai, Bi;
    scanf("%d",&T);
    int mem[T];
    while(i < T) {
        scanf("%lld%lld",&Ai,&Bi);
        j = 0;
        while(j < Ai) {
            scanf("%lld", &c);
                A.push_back(c);
                j++;
        }
        j = 0;
        while(j < Bi) {
            scanf("%lld", &v);
                B.push_back(v);
                j++;
        }
        mem[i] = findDiff(A, B);

        A.clear();
        B.clear();
        A.resize(0);
        B.resize(0);
        i++;
    }

    for(int i = 0; i < T; i++) {
        printf("%d\n",mem[i]);
    }

return 0;
}