#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);

    int t;
    cin >> t;

    for(size_t i = 0; i < t; i++)
    {
        uint16_t n, m;
        cin >> n >> m;
        vector<uint64_t> odobrene(n);
        map<uint64_t, bool> odbijene;
        for(size_t j = 0; j < n; j++)
            cin >> odobrene[j];
        for(size_t j = 0; j < m; j++) {
            uint64_t x;
            cin >> x;
            odbijene[x] = true;
        }

        sort(begin(odobrene), end(odobrene));

        bool zalba = true;
        uint64_t mast = -1;

        for(size_t j = 1; j < odobrene.size(); j++)
        {
            if(mast == -1) mast = odobrene[j] - odobrene[j - 1];

            if(odobrene[j] - odobrene[j - 1] > mast)
            {
                if(odbijene.find(odobrene[j] - mast) != end(odbijene))
                {
                    zalba = false;
                    break;
                }
            }

            if((max(mast, odobrene[j] - odobrene[j - 1]) % min(odobrene[j] - odobrene[j - 1], mast)) != 0) {
                zalba = false;
                break;
            }

            mast = min(odobrene[j] - odobrene[j - 1], mast);
        }

        cout << zalba << '\n';
    }


    cout.flush();
    return 0;
}