#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
using namespace std;
struct node
{
int id;
int layer;
}tnode;
int K,L;
vector<int> v[1005];
int book[1005]={0};
int cou=0;
void bfs(int id)
{
queue<node> q;
tnode.id=id;
tnode.layer=0;
q.push(tnode);
book[id]=1;
while(!q.empty())
{
node head=q.front();
q.pop();
for(int i=0;i<v[head.id].size();i++)
{
if(book[v[head.id][i]]==0)
{
tnode.id=v[head.id][i];
tnode.layer=head.layer+1;
if(tnode.layer<=L)
{
q.push(tnode);
book[v[head.id][i]]=1;
cou++;
}
}
}
//printf("-\n");
}
}
void init()
{
cou=0;
memset(book,0,sizeof(book));
}
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d %d",&K,&L);
for(int i=1;i<=K;i++)
{
int k;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
int t;
scanf("%d",&t);
v[t].push_back(i);
}
}
int k;
scanf("%d",&k);
while(k--)
{
int id;
scanf("%d",&id);
init();
bfs(id);
printf("%d\n",cou);
}
return 0;
}