import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) A = (0, ) + tuple(map(int, readline().split())) G = [[] for _ in range(N + 1)] for _ in range(N - 1): x, y = map(int, readline().split()) G[x].append(y) G[y].append(x) Q = int(readline()) m = map(int, read().split()) query = tuple(zip(m, m)) parent = [0] * (N + 1) depth = [0] * (N + 1) stack = [] order = [] root = 1 stack = [root] while stack: v = stack.pop() order.append(v) for w in G[v]: if w == parent[v]: continue parent[w] = v depth[w] = depth[v] + 1 stack.append(w) K = 18 P = [parent] for _ in range(K): p = P[-1] P.append([p[v] for v in p]) def LCA(u, v): du, dv = depth[u], depth[v] if du < dv: du, dv = dv, du u, v = v, u n = du - dv for i in range(K): if n & 1: u = P[i][u] n >>= 1 if u == v: return u for i in range(K - 1, -1, -1): u1, v1 = P[i][u], P[i][v] if u1 == v1: continue u, v = u1, v1 return parent[u] def is_kadomatsu(a, b, c): if a == c: return False return (a > b < c) or (a < b > c) longest_kadomatsu = [0] * (N + 1) for v in order[1:]: p = parent[v] pp = parent[p] if not p: longest_kadomatsu[v] = 0 elif not pp: longest_kadomatsu[v] = 1 else: a, b, c = A[v], A[p], A[pp] if is_kadomatsu(a, b, c): longest_kadomatsu[v] = longest_kadomatsu[p] + 1 else: longest_kadomatsu[v] = 1 def ascend(v, k): for i in range(K): if k & 1: v = P[i][v] k >>= 1 return v longest_kadomatsu def solve(u,v): w = LCA(u,v) if v == w: u,v = v,u du, dv, dw = depth[u], depth[v], depth[w] if longest_kadomatsu[v] < dv - dw: return False if longest_kadomatsu[u] < du - dw: return False if u == w: v1 = parent[v] v2 = ascend(v, dv-dw-1) return is_kadomatsu(A[v2], A[w], A[v]) and is_kadomatsu(A[w], A[v], A[v1]) v1 = parent[v] v2 = ascend(v, dv-dw-1) u1 = parent[u] u2 = ascend(u, du-dw-1) return is_kadomatsu(A[v2], A[w], A[u2]) and is_kadomatsu(A[v], A[u], A[u1]) and is_kadomatsu(A[u], A[v], A[v1]) query for a,b in query: print('YES' if solve(a,b) else 'NO')