// #define INTERACTIVE
// #define FAST_EXECUTION
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
const long long INF = std::numeric_limits<long long>::max() / 4;
const long long MOD = 998244353;
#ifdef ONLINE_JUDGE
#define debug(x) do { } while (0)
#define debug2(x, y) do { } while (0)
#define debug3(x, y, z) do { } while (0)
#define debugendl() do { } while (0)
#define debugmatrix(m) do { } while (0)
#else
#define debug(x) cerr << #x << " = " << x << endl
#define debug2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl
#define debug3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl
#define debugendl() cerr << endl
#define debugmatrix(m) cerr << #m << " = " << endl; for (auto v: m) { for (auto w: v) cerr << w << " "; cerr << endl; }
#endif
#define LL long long
#define L long
#define ULL unsigned long long
#define I int
#define UI unsigned int
#define D double
#define V(T) std::vector<T>
#define W(T) std::vector<std::vector<T>>
#define S(T) std::set<T>
#define P(T, U) std::pair<T, U>
#define M(T, U) std::map<T, U>
#define ALL(a) a.begin(), a.end()
#define REP(n) for (int t = 0; t < n; ++t)
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FFOR(i, j, n) for (int i = j; i < n; ++i)
#define FOR_S(i, n, k) for (int i = 0; i < n; i += k)
#define RFOR(i, n) for (int i = n-1; i >= 0; --i)
#define RFOR_S(i, n, k) for (int i = n-1; i >= 0; i -= k)
#define IN(a, b) (a.find(b) != a.end())
#define MAX_IN(A) *(max_element(A.begin(), A.end()))
#define MIN_IN(A) *(min_element(A.begin(), A.end()))
#define MAX_AT(A) (max_element(A.begin(), A.end()) - A.begin())
#define MIN_AT(A) (min_element(A.begin(), A.end()) - A.begin())
#define SUM(A) accumulate(A.begin(), A.end(), 0LL)
#define PRO(A) accumulate(A.begin(), A.end(), 1LL, multiplies<long long>())
#define COUNT(A, x) count(A.begin(), A.end(), x)
#define EXISTS(A, cond) any_of(A.begin(), A.end(), cond)
#define FORALL(A, cond) all_of(A.begin(), A.end(), cond)
#define GCD(a, b) std::gcd((a), (b))
#define LCM(a, b) std::lcm((a), (b))
#define SORT(a) sort(ALL(a))
#define DSORT(a) sort(ALL(a), greater<>())
#define ORD(c) (c-'a')
#define mp make_pair
#define test_cases() LL t; cin >> t; while (t--) solve()
#define endl '\n'
#ifndef INTERACTIVE
#define faster_io() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#else
#define faster_io() do { } while (0)
#endif
#ifdef FAST_EXECUTION
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#endif
template<typename A, typename B> std::pair<A, B> operator+(const std::pair<A, B> &a, const std::pair<A, B> &b) { return {a.first + b.first, a.second + b.second}; }
template<typename A, typename B> std::pair<A, B> operator-(const std::pair<A, B> &a, const std::pair<A, B> &b) { return {a.first - b.first, a.second - b.second}; }
template<typename T> std::pair<T, T> operator*(const std::pair<T, T> &a, const T& b) { return {b * a.first, b * a.second}; }
template<typename T> std::pair<T, T> operator*(const T& b, const std::pair<T, T> &a) { return {b * a.first, b * a.second}; }
bool pair_cmp_second(const std::pair<long long, long long> &a, const std::pair<long long, long long> &b) { return a.second < b.second || (a.second == b.second && a.first < b.first); }
template<typename A, typename B> std::ostream& operator<<(std::ostream &out, const std::pair<A, B> &p);
template<typename A, typename B> std::istream& operator>>(std::istream &in, std::pair<A, B> &p);
template<typename T> std::ostream& operator<<(std::ostream &out, const std::vector<T> &v);
template<typename T> std::istream& operator>>(std::istream &in, std::vector<T> &v);
std::istream& operator>>(std::istream &in, bool &b) { char c; in >> c; b = (c == '1'); return in; }
std::ostream& operator<<(std::ostream &out, const bool &b) { return out << (b ? '1' : '0'); }
std::istream& operator>>(std::istream &in, std::vector<bool> &v) { char c; FOR(i, v.size()) { in >> c; v[i] = (c == '1'); } return in; }
template<typename A, typename B> std::ostream& operator<<(std::ostream &out, const std::pair<A, B> &p) { return out << "(" << p.first << ", " << p.second << ")"; }
template<typename A, typename B> std::istream& operator>>(std::istream &in, std::pair<A, B> &p) { return in >> p.first >> p.second; }
template<typename T> std::ostream& operator<<(std::ostream &out, const std::vector<T> &v) { for (const T &x : v) out << x << " "; return out; }
template<typename T> std::istream& operator>>(std::istream &in, std::vector<T> &v) { for (T &x : v) in >> x; return in; }
template<typename F>
LL first_true(LL lo, LL hi, F pred) {
while (lo < hi) {
LL mid = lo + (hi - lo) / 2;
if (pred(mid))
hi = mid;
else
lo = mid + 1;
}
return lo;
}
template<typename F>
LL last_true(LL lo, LL hi, F pred) {
while (lo < hi) {
LL mid = lo + (hi - lo + 1) / 2;
if (pred(mid))
lo = mid;
else
hi = mid - 1;
}
return lo;
}
using namespace std;
void count_routes(int N,int M,int P,int R[][2],int Q,int* G) {
constexpr int LOCAL_INF = 1000000000;
vector<vector<array<int, 2>>> adj(N);
FOR(i, M) {
adj[R[i][0]].push_back({R[i][1], i});
adj[R[i][1]].push_back({R[i][0], i});
}
vector<vector<array<int, 2>>> rev(N);
FOR(i, N) {
if (adj[i].size() > 2)
adj[i].resize(2);
for (const auto &edge : adj[i]) {
int j = edge[0], w = edge[1];
rev[j].push_back({i, w});
}
}
vector<array<int, 2>> dist(N, {LOCAL_INF, LOCAL_INF});
vector<array<int, 2>> to_p(N, {LOCAL_INF, LOCAL_INF});
FOR(tt, 2) {
if (tt == 1 && adj[P].size() == 1)
break;
queue<array<int, 3>> bfs;
bfs.push({0, P, tt});
while (!bfs.empty()) {
auto cur = bfs.front();
bfs.pop();
int t = cur[0], u = cur[1], f = cur[2];
if (dist[u][f] != LOCAL_INF)
continue;
dist[u][f] = t;
int wt = adj[u][0][1];
for (const auto &edge : rev[u]) {
int j = edge[0], w = edge[1];
if (f == 0 && w == wt && adj[u].size() > 1)
continue;
if (f == 1 && w != wt && adj[u].size() > 1)
continue;
int flag = (w != adj[j][0][1]);
bfs.push({t + 1, j, flag});
}
}
FOR(i, N)
to_p[i][tt] = dist[i][0];
dist.assign(N, {LOCAL_INF, LOCAL_INF});
}
auto get_cycle = [&](int f) -> array<int, 2> {
int cycle_len = 0;
int saw = 0;
vector<array<bool, 2>> vis(N, {false, false});
int node = P, flag = f, trav = 0;
while (!vis[node][flag]) {
vis[node][flag] = true;
if (node == P && flag == !f)
saw = trav;
const auto &nxt_edge = adj[node][flag];
int nxt = nxt_edge[0], wt = nxt_edge[1];
if (adj[nxt].size() == 1) {
node = nxt;
flag = 0;
} else {
node = nxt;
flag = (adj[nxt][0][1] == wt);
}
++trav;
}
if (node == P)
cycle_len = trav;
else
cycle_len = INT_MAX, saw = 0;
return {cycle_len, saw};
};
array<int, 2> s1 = get_cycle(0);
array<int, 2> s2 = {INT_MAX, 0};
if (adj[P].size() > 1)
s2 = get_cycle(1);
FOR(t, Q) {
int k = G[t], res = 0;
FOR(i, N) {
if (to_p[i][0] <= k &&
((k - to_p[i][0]) % s1[0] == 0 || (k - to_p[i][0]) % s1[0] == s1[1])) {
++res;
} else if (to_p[i][1] <= k &&
((k - to_p[i][1]) % s2[0] == 0 || (k - to_p[i][1]) % s2[0] == s2[1])) {
++res;
}
}
answer(res);
}
}