#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef pair<int,int> P;
int N,M,v[3000],to[3000][2];
bool done[3000][3000];
vector<int> rG[3000][2];
int main(){
	cin>>N>>M;
	rep(i,N) cin>>v[i]>>to[i][0]>>to[i][1];
	rep(i,N) rG[to[i][0]][0].pb(i),rG[to[i][1]][1].pb(i);
	queue<P> que;
	rep(i,N) rep(j,N) if(v[i]!=v[j]) que.push(P(i,j)),done[i][j]=1;
	while(!que.empty()){
		P p=que.front();que.pop();
		int u=p.fs,v=p.sc;
		rep(k,2){
			for(int a:rG[u][k]) for(int b:rG[v][k]){
				if(!done[a][b]){
					que.push(P(a,b));
					done[a][b]=1;
				}
			}
		}
	}
	int c[3000]={};
	rep(i,N){
		rep(j,N) if(!done[i][j]) c[i]++;
	}
	rep(i,M){
		int a;
		cin>>a;
		cout<<c[a]<<endl;
	}
}