#include <bits/stdc++.h>
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
bool q = 0;
long long a[76000], b[76000], res;
int t, n, m;
int main() {
FASTIO
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
    cin >> a[i];
}

for (int i = 1; i <= m; i++) {
    cin >> b[i];
}

sort(a+1, a+n+1);
sort(b+1, b+m+1);

if (n == 2) {
res = a[2] - a[1];

for (int i = 1; i <= m; i++) {
    if (b[i] + res != a[i]) {
        continue;
    } else {
    q=true;
    break; }
}
if (!q) {
    cout<<0<<endl;
}else cout<<1<<endl;

}




}
return 0;
}