#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;
}