#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll N = 3e5 + 10;

ll a[N];

ll primes[N], cnt = 0;
bool st[N];

void sieve() {
    cnt = 0;
    for (ll i = 2; i < N; ++i) {
        if (!st[i]) primes[++cnt] = i;
        for (ll j = 1; primes[j] * i < N; ++j) {
            st[primes[j] * i] = 1;
            if (i % primes[j] == 0) break;
        }
    }
}

unordered_map<ll, ll> mp;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    sieve();
    ll T;
    cin >> T;
    while (T--) {
        mp.clear();
        ll n;
        cin >> n;
        for (ll i = 1, x; i <= n; ++i) {
            cin >> x;
            ll y = 1;
            for (ll j = 1; j <= cnt; ++j) {
                if (primes[j] > x / primes[j]) break;
                if (x % primes[j] == 0) {
                    ll tmp = 0;
                    while (x % primes[j] == 0) {
                        tmp++;
                        x /= primes[j];

                    }
                    if (tmp & 1) {
                        y *= primes[j];
                    }
                }
            }
            if(x > 1) {
                y *= x;
            }
            mp[y]++;
        }

//        cout << "\n-------------mp:\n";
//
//        for (auto x: mp) {
//            cout << x.first << ' ' << x.second << endl;
//        }
//        cout << "\n----------\n";

        ll ansa = 0, ansb = 0;
        for (auto x : mp) {
            ansa = max(ansa, x.second);
            if ((x.first == 1) || !(x.second & 1)) ansb += x.second;
        }

//        cout << "ans :" << ansa << " " << ansb << endl;

        ll q;
        cin >> q;
        while (q--) {
            ll x;
            cin >> x;
            if (x == 0) {
                cout << ansa << endl;
            } else {
                cout << max(ansa, ansb) << endl;
            }
        }
    }

    return 0;
}