#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; using pii = pair; bool is_kadomatsu(int a, int b, int c) { return a != b && b != c && c != a && (a < b != b < c); } int main() { int N, Q; cin >> N; vector A(N); vector> edges(N); rep(i, N)cin >> A[i]; rep(i, N - 1) { int x, y; cin >> x >> y; x--; y--; edges[x].push_back(y); edges[y].push_back(x); } vector depth(N), par(N), lim(N); { stack st; st.push({ -1,0 }); while (!st.empty()) { int p, n; tie(p, n) = st.top(); st.pop(); depth[n] = p == -1 ? 0 : depth[p] + 1; par[n] = p; lim[n] = p == -1 ? n : par[p] == -1 ? p : is_kadomatsu(A[n], A[p], A[par[p]]) ? lim[p] : p; for (int next : edges[n])if (next != p)st.push({ n,next }); } } cin >> Q; while (Q--) { int u, v; cin >> u >> v; u--; v--; if (depth[u] < depth[v])swap(u, v); bool res = true; res &= (depth[u] + depth[v]) % 2 == 1; res &= lim[u] == lim[v]; res &= is_kadomatsu(A[par[u]], A[u], A[v]); res &= (par[v] == -1 || is_kadomatsu(A[u], A[v], A[par[v]])); cout << (res ? "YES" : "NO") << endl; } return 0; }