#include<cstdio>
#include<cstring>
#include<utility>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int>P;
vector<int>R[105];

int f(int s,int t,int v)
{
	int flg[105][105];
	memset(flg,0x7f,sizeof(flg));
	priority_queue<P>Q;
	Q.push(P(1,s));
	flg[s][s]=0;
	for(;!Q.empty();)
	{
		P now=Q.top();Q.pop();
		if(now.second==t)return now.first;
		now.first++;
		if(now.first>v)continue;
		int n=now.second;
		for(int i=0;i<R[n].size();i++)
		{
			if(flg[s][R[n][i]]>now.first)
			{
				flg[s][R[n][i]]=now.first;
				Q.push(P(now.first,R[n][i]));
			}
		}
	}
	return -1;
}

int main()
{
	int N,M,s,t,v,r;
	scanf("%d",&N);
	for(;N--;)
	{
		scanf("%d%d",&s,&t);
		for(;t--;)
		{
			scanf("%d",&v);
			R[s].push_back(v);
		}
	}
	scanf("%d",&M);
	for(;M--;)
	{
		scanf("%d%d%d",&s,&t,&v);
		r=f(s,t,v);
		if(r!=-1)printf("%d\n",r);
		else puts("NA");
	}
}