#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
int n;
cin >> n;
map<ll, ll> m;
for(int i = 0; i < n; i++){
ll x;
cin >> x;
m[x]++;
}
int q;
cin >> q;
while(q--){
ll x, y;
cin >> x >> y;
if(x * x - 4 * y < 0){
cout << 0 << " ";
continue;
}
double p = sqrtl(x * x - 4 * y);
// cout << p << "\n";
double root1 = x + p;
double root2 = x - p;
if((root1 - (ll)root1) <= 1e-6 && (root2 - (ll)root2) <= 1e-6){
ll r1 = root1, r2 = root2;
if(r1 % 2 != 0 || r2 % 2 != 0){
cout << 0 << " ";
}else{
r1 /= 2;
r2 /= 2;
if(r1 + r2 != x || r1 * r2 != y){
cout << 0 << " ";
}
else if(r1 == r2){
cout << m[r1] * (m[r1] - 1) / 2 << " ";
}else{
cout << m[r1] * m[r2] << " ";
}
}
}else{
cout << 0 << " ";
}
}
cout << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
cout << fixed << setprecision(15);
for(int i = 1; i <= t; i++){
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp2b2lkIHNvbHZlKCl7CglpbnQgbjsKCWNpbiA+PiBuOwoJbWFwPGxsLCBsbD4gbTsKCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewoJCWxsIHg7CgkJY2luID4+IHg7CgkJbVt4XSsrOwoJfQoJCglpbnQgcTsKCWNpbiA+PiBxOwoJd2hpbGUocS0tKXsKCQlsbCB4LCB5OwoJCWNpbiA+PiB4ID4+IHk7CgkJaWYoeCAqIHggLSA0ICogeSA8IDApewoJCQljb3V0IDw8IDAgPDwgIiAiOwoJCQljb250aW51ZTsKCQl9CgkJZG91YmxlIHAgPSBzcXJ0bCh4ICogeCA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