// #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 answer(int x) {
// cout << x << endl;
// }
void count_routes(int N,int M,int P,int R[][2],int Q,int* G) {
V(LL) graph(2 * N);
V(P(LL, LL)) edges(N, {-1, -1});
FOR(i, M) {
if (edges[R[i][0]].first == -1)
edges[R[i][0]].first = R[i][1];
else if (edges[R[i][0]].second == -1)
edges[R[i][0]].second = R[i][1];
if (edges[R[i][1]].first == -1)
edges[R[i][1]].first = R[i][0];
else if (edges[R[i][1]].second == -1)
edges[R[i][1]].second = R[i][0];
}
FOR(i, N) {
LL v1 = edges[i].first, v2 = edges[i].second;
if (edges[v1].first == i)
graph[i] = v1 + N;
else
graph[i] = v1;
if (v2 == -1 && edges[v1].first == i)
graph[i+N] = edges[i].first + N;
else if (v2 == -1)
graph[i+N] = edges[i].first;
else if (edges[v2].first == i)
graph[i+N] = v2 + N;
else
graph[i+N] = v2;
}
V(bool) visited(2 * N, false);
V(LL) dist(2 * N, INF);
dist[P] = dist[P + N] = 0;
visited[P] = visited[P + N] = true;
function<void(LL)> dfs = [&](LL v) {
visited[v] = true;
LL next = graph[v];
if (!visited[next])
dfs(next);
if (dist[next] != INF)
dist[v] = dist[next] + 1;
};
FOR(i, 2 * N) if (!visited[i])
dfs(i);
FOR(i, Q) {
LL g = G[i];
LL count = 0;
FOR(j, N) if (dist[j] == g) ++count;
answer(count);
}
}
// int main() {
// int N, M, P, Q;
// cin >> N >> M >> P;
// int** R = new int*[M];
// for (int i = 0; i < M; i++) {
// R[i] = new int[2];
// cin >> R[i][0] >> R[i][1];
// }
// cin >> Q;
// int* G = new int[Q];
// for (int i = 0; i < Q; i++) {
// cin >> G[i];
// }
// count_routes(N, M, P, R, Q, G);
// }