#include <bits/stdc++.h>

using namespace std;

const int N = 1e5;
int parent[N], color[N];
vector<int> adj[N];
map<int, int> cnt_color;
set<int> ans;

void defineParents()
{
    memset(parent, -1, sizeof(parent));
    parent[0] = 0;

    queue<int> fila;
    fila.push(0);

    while(!fila.empty())
    {
        int v = fila.front();
        fila.pop();

        for(auto u: adj[v])
        {
            if(parent[u] == -1)
            {
                parent[u] = v;
                fila.push(u);
            }
        }
    }
}

void dfs(int x)
{
    if(cnt_color[color[x]] == 0) ans.insert(x + 1);
    cnt_color[color[x]]++;
    for(auto u: adj[x])
    {
        if(u != parent[x])
        {
            dfs(u);
        }
    }
    cnt_color[color[x]]--;
}

int main()
{
    int n; cin >> n;
    for(int i = 0; i < n; i++) cin >> color[i];
    for(int i = 1; i < n; i++)
    {
        int u, v; cin >> u >> v;
        u--; v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    defineParents();
    dfs(0);

    for(auto x: ans) cout << x << endl;
    return 0;
}

#include <stdio.h>
#include <set>
#include <utility>
typedef long long ll;

int main(void) {
  ll i, j, k, n, q;
  scanf("%lld", &n);
  ll a, t, b[2] = {(ll)-1e16, (ll)1e16}, s = 0, x;
  for(i = 0; i < n; ++i) {
    scanf("%lld%lld", &a, &t);
    if(t == 1) s += a;
    else if(t == 2) {
      if(b[1] + s < a) b[0] = b[1], s = a - b[0];
      else if(b[0] + s < a) b[0] = a - s;
    } else {
      if(b[0] + s > a) b[0] = b[1], s = a - b[0];
      else if(b[1] + s > a) b[1] = a - s;
    }
  }
  scanf("%lld", &q);
  for(i = 0; i < q; ++i) {
    scanf("%lld", &x);
    if(x < b[0]) printf("%lld\n", b[0] + s);
    else if(x < b[1]) printf("%lld\n", x + s);
    else printf("%lld\n", b[1] + s);
  }
  return 0;
}