题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805392092020736
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e3+10;
const int maxm=4e4+100;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
if(y==0) return x;
return gcd(y,x%y);
}
int n,k,ans[maxn];
vector<int> g[maxn];
int bfs(int u){
int vis[maxn];mst(vis,0);
queue<pii> q;q.push(mk(u,k));
while(!q.empty()){
int tp=q.front().fi,cnt=q.front().se;q.pop();
if(vis[tp]||cnt<0) continue;
vis[tp]=1;
rep(i,0,g[tp].size()){
q.push(mk(g[tp][i],cnt-1));
}
}
int ret=0;rep(i,1,n+1) ret+=vis[i];
return ans[u]=ret;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>k;
rep(i,1,n+1){
int m,x;
cin>>m;
rep(j,0,m){
cin>>x;
g[x].push_back(i);
}
}
int q;cin>>q;
mst(ans,-1);
rep(i,0,q){
int x;cin>>x;
if(ans[x]!=-1) cout<<ans[x];
else cout<<bfs(x)-1<<"\n";
}
return 0;
}