#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
//using namespace __gnu_pbds;
//typedef tree< pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>
//ordered_set;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
#define X first
#define Y second
#define int long long
const int N = (int)2e5 + 10;
const int MOD = 1000000007;
void solve(){
int X; cin >> X;
int n; cin >> n;
vector<int> a(n);
for(auto & x: a)
cin >> x;
int m; cin >> m;
vector<int> b(m);
for(auto & x: b)
cin >> x;
if(n == 1){
int ans = 0;
for(auto & x: b){
int differ = x - a[0];
if( abs(differ) <= X )
ans++;
}
cout << ans << '\n';
return;
}
vector<int> diff(n + m - 1), org(n + m + 1);
for(int i = 0; i < n; ++i)
org[i] = a[i];
org[n] = INT_MIN;
for(int i = n + 1, j = 0; j < m; ++i, ++j)
org[i] = b[j];
int id = 0;
for(int i = 0; i + 1 < n; ++i){
diff[id++] = a[i + 1] - a[i];
}
diff[id++] = INT_MIN;
for(int i = 0; i + 1 < m; ++i){
diff[id++] = b[i + 1] - b[i];
}
int sz = diff.size();
vector<int> pi(sz);
pi[0] = 0;
for(int i = 1; i < sz; ++i){
int j = pi[i - 1];
while(j > 0 and diff[i] != diff[j] ){
j = pi[j - 1];
}
if( diff[i] == diff[j])
j++;
pi[i] = j;
}
int ans = 0;
for(int i = n; i < sz; ++i){
if( pi[i] == n - 1){
int currid = i - n + 3;
int x = org[currid] - a[0];
x = abs(x);
if(x <= X)
ans++;
}
}
cout << ans << '\n';
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int t; cin >> t;
while(t--)
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPiAvLyBDb21tb24g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1cm47CiAgICB9CiAgICB2ZWN0b3I8aW50PiBkaWZmKG4gKyBtIC0gMSksIG9yZyhuICsgbSArIDEpOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47ICsraSkKICAgICAgICBvcmdbaV0gPSBhW2ldOwogICAgb3JnW25dID0gSU5UX01JTjsKICAgIGZvcihpbnQgaSA9IG4gKyAxLCBqID0gMDsgaiA8IG07ICsraSwgKytqKQogICAgICAgIG9yZ1tpXSA9IGJbal07CiAgICBpbnQgaWQgPSAwOwogICAgZm9yKGludCBpID0gMDsgaSArIDEgPCBuOyArK2kpewogICAgICAgIGRpZmZbaWQrK10gPSBhW2kgKyAxXSAtIGFbaV07CiAgICB9CiAgICBkaWZmW2lkKytdID0gSU5UX01JTjsKICAgIGZvcihpbnQgaSA9IDA7IGkgKyAxIDwgbTsgKytpKXsKICAgICAgICBkaWZmW2lkKytdID0gYltpICsgMV0gLSBiW2ldOwogICAgfQogICAgaW50IHN6ID0gZGlmZi5zaXplKCk7CiAgICB2ZWN0b3I8aW50PiBwaShzeik7CiAgICBwaVswXSA9IDA7CiAgICBmb3IoaW50IGkgPSAxOyBpIDwgc3o7ICsraSl7CiAgICAgICAgaW50IGogPSBwaVtpIC0gMV07CiAgICAgICAgd2hpbGUoaiA+IDAgYW5kIGRpZmZbaV0gIT0gZGlmZltqXSApewogICAgICAgICAgICBqID0gcGlbaiAtIDFdOwogICAgICAgIH0KICAgICAgICBpZiggZGlmZltpXSA9PSBkaWZmW2pdKQogICAgICAgICAgICBqKys7CiAgICAgICAgcGlbaV0gPSBqOwogICAgfQogICAgaW50IGFucyA9IDA7CiAgICBmb3IoaW50IGkgPSBuOyBpIDwgc3o7ICsraSl7CiAgICAgICAgaWYoIHBpW2ldID09IG4gLSAxKXsKICAgICAgICAgICAgaW50IGN1cnJpZCA9IGkgLSBuICsgMzsKICAgICAgICAgICAgaW50IHggPSAgb3JnW2N1cnJpZF0gLSBhWzBdOwogICAgICAgICAgICB4ID0gYWJzKHgpOwogICAgICAgICAgICBpZih4IDw9IFgpCiAgICAgICAgICAgICAgICBhbnMrKzsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IGFucyA8PCAnXG4nOwogICAgCiAgICAKCgoKCn0KCmludDMyX3QgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7Y2luLnRpZShudWxscHRyKTtjb3V0LnRpZShudWxscHRyKTsgCiAgICBpbnQgdDsgY2luID4+IHQ7CiAgICB3aGlsZSh0LS0pCiAgICAgICAgc29sdmUoKTsKICAgIHJldHVybiAwOwp9