#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, n, m, br, cnt, z, g, siz;
    long long x;
    g = 0;
    br = 1;
    cnt = 0;
    z = 0;
    //cin >> t;
    scanf("%d", &t);
    int res[t];

    for (int i = 0; i < t; i++){
        //cin >> n >> m;
        scanf("%d %d", &n, &m);
        vector<long long> r;
        vector<long long> w;
        vector<long long> diff;
        vector<int> del;
        set<long long> d;

        for (int j = 0; j < n; j++){
            //cin >> x;
            scanf("%lld", &x);
            r.push_back(x);
        }
        for (int j = 0; j < m; j++){
            //cin >> x;
            scanf("%lld", &x);
            w.push_back(x);
        }

        sort(r.begin(), r.end());


        for (int j = 0; j < n-1; j++){
            d.insert(r.at(j+1) - r.at(j));
        }
        cnt = 0;
        for (auto f : d){
            diff.push_back(f);
            ++cnt;
        }
        d.clear();

        //Trazimo zajednicke delioce svih razlika tako sto cemo prvo naci delioce najmanjeg broja, tj diff[0]
        for (int j = 2; j*j <= diff.at(0); j++){
            if(diff.at(0) % j == 0){
                del.push_back(j);
                if(j*j != diff.at(0)){
                    del.push_back(diff.at(0) / j);
                }
            }
        }
        del.push_back(diff.at(0));

        //Proveravamo koji delioci prvog su i delioci za ostale

        br = 1;
        siz = (int)del.size();
        for (int j = 0; j < siz; j++){
            for (int k = 1; k < cnt; k++){
                if(diff.at(k) % del.at(j) == 0) ++br;
            }
            if(br != cnt){
                del.erase(del.begin() + j - z); // ako svaki broj deljiv sa del[j]
                ++z;
            }
            br = 1;
        }
        z = 0;
        siz = (int)del.size();
        br = 0;
        for (int j = 0; j < siz; j++){
            for (int k = 0; k < m; k++){
                if(abs((w.at(k) - r.at(0))) % del.at(j) != 0) ++br;
                else{
                    br = 0;
                    break;
                }
            }
            if(br == m){
                ++g;
                break;
            }
        }
        del.clear();
        r.clear();
        w.clear();
        diff.clear();
        res[i] = g;
        g = 0;

    }

    for (int i = 0; i < t; i++){
        //cout << res[i] << endl;
        printf("%d\n", res[i]);
    }



    return 0;
}