#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define watch(x) cout<<(#x)<<" = "<<x<<endl
#define arr_watch(arr, n) for(int i=0;i<n;++i) cout<<arr[i]<<" \n"[i==n-1]
using namespace std;
const int maxn = 1e5;
vector<int> graph[maxn + 5];
int order[maxn + 5], A[maxn + 5];
list<int> colors[maxn + 5];
int the_end;
void dfs(int u, int p){
colors[A[u]].push_back(u);
for (int child: graph[u]){
if(child == p) continue;
dfs(child, u);
}
order[u] = the_end++;
}
//LM10_Piyush...
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
// freopen("D:/TestFiles/input.txt","r",stdin);
#endif
/*********************** CODE IS HERE *****************************************/
//number of nodes...
int n; cin >> n;
for(int i = 1, x, y; i < n; ++i){
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
//colors input...
for (int i = 1; i <= n; ++i)
cin >> A[i];
the_end = 1;
dfs(1, 0); //here we go....
int ans = 0;
int q; cin >> q;
while(q--){
int x; cin >> x;
int col = A[x]; //color of that x node...
auto it1 = colors[col].begin();
auto it2 = it1;
bool flag = false;
//iterate over subtree with same color(end_time of those nodes <= xth node)
while(it1 != colors[col].end()){
if(order[*it1] > order[x] and flag)
break;
it2++;
if(*it1 == x)
flag = true;
if(order[*it1] <= order[x] and flag){
ans++;
colors[col].erase(it1); //found that node, then remove it
}
it1 = it2;
}
cout << ans << endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBlbmRsICdcbicKI2RlZmluZSB3YXRjaCh4KSBjb3V0PDwoI3gpPDwiID0gIjw8eDw8ZW5kbAojZGVmaW5lIGFycl93YXRjaChhcnIsIG4pIGZvcihpbnQgaT0wO2k8bjsrK2kpIGNvdXQ8PGFycltpXTw8IiBcbiJbaT09bi0xXQp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgbWF4biA9IDFlNTsKdmVjdG9yPGludD4gZ3JhcGhbbWF4biArIDVdOwppbnQgb3JkZXJbbWF4biArIDVdLCBBW21heG4gKyA1XTsKbGlzdDxpbnQ+IGNvbG9yc1ttYXhuICsgNV07CmludCB0aGVfZW5kOwoKdm9pZCBkZnMoaW50IHUsIGludCBwKXsKICAgIGNvbG9yc1tBW3VdXS5wdXNoX2JhY2sodSk7CiAgICBmb3IgKGludCBjaGlsZDogZ3JhcGhbdV0pewogICAgICAgIGlmKGNoaWxkID09IHApIGNvbnRpbnVlOwogICAgICAgIGRmcyhjaGlsZCwgdSk7CiAgICB9CiAgICBvcmRlclt1XSA9IHRoZV9lbmQrKzsKfQoKLy9MTTEwX1BpeXVzaC4uLgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUoTlVMTCk7IGNvdXQudGllKE5VTEwpOwogICAgI2lmbmRlZiBPTkxJTkVfSlVER0UKICAgIAogICAgLy8gZnJlb3BlbigiRDovVGVzdEZpbGVzL2lucHV0LnR4dCIsInIiLHN0ZGluKTsKICAgIAogICAgI2VuZGlmCiAgICAvKioqKioqKioqKioqKioqKioqKioqKiogQ09ERSBJUyBIRVJFICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqLwoKICAgIC8vbnVtYmVyIG9mIG5vZGVzLi4uCiAgICBpbnQgbjsgY2luID4+IG47CiAgICBmb3IoaW50IGkgPSAxLCB4LCB5OyBpIDwgbjsgKytpKXsKICAgICAgICBjaW4gPj4geCA+PiB5OwogICAgICAgIGdyYXBoW3hdLnB1c2hfYmFjayh5KTsKICAgICAgICBncmFwaFt5XS5wdXNoX2JhY2soeCk7CiAgICB9CiAgICAvL2NvbG9ycyBpbnB1dC4uLgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKQogICAgICAgIGNpbiA+PiBBW2ldOyAgICAKCiAgICB0aGVfZW5kID0gMTsKICAgIGRmcygxLCAwKTsgICAgICAgIC8vaGVyZSB3ZSBnby4uLi4KICAgIAogICAgaW50IGFucyA9IDA7CiAgICBpbnQgcTsgY2luID4+IHE7CiAgICB3aGlsZShxLS0pewogICAgICAgIGludCB4OyBjaW4gPj4geDsKICAgICAgICBpbnQgY29sID0gQVt4XTsgICAvL2NvbG9yIG9mIHRoYXQgeCBub2RlLi4uCiAgICAgICAgYXV0byBpdDEgPSBjb2xvcnNbY29sXS5iZWdpbigpOwogICAgICAgIGF1dG8gaXQyID0gaXQxOwogICAgICAgIGJvb2wgZmxhZyA9IGZhbHNlOwogICAgICAgIAogICAgICAgIC8vaXRlcmF0ZSBvdmVyIHN1YnRyZWUgd2l0aCBzYW1lIGNvbG9yKGVuZF90aW1lIG9mIHRob3NlIG5vZGVzIDw9IHh0aCBub2RlKQogICAgICAgIHdoaWxlKGl0MSAhPSBjb2xvcnNbY29sXS5lbmQoKSl7CiAgICAgICAgICAgIGlmKG9yZGVyWyppdDFdID4gb3JkZXJbeF0gYW5kIGZsYWcpCiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgaXQyKys7CiAgICAgICAgICAgIGlmKCppdDEgPT0geCkKICAgICAgICAgICAgICAgIGZsYWcgPSB0cnVlOwoKICAgICAgICAgICAgaWYob3JkZXJbKml0MV0gPD0gb3JkZXJbeF0gYW5kIGZsYWcpewogICAgICAgICAgICAgICAgYW5zKys7CiAgICAgICAgICAgICAgICBjb2xvcnNbY29sXS5lcmFzZShpdDEpOyAgLy9mb3VuZCB0aGF0IG5vZGUsIHRoZW4gcmVtb3ZlIGl0CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaXQxID0gaXQyOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwoKICAgIH0KCgogICAgcmV0dXJuIDA7Cn0K