#include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
	int n,r,k,t,p,s,d,v,vec[101][101];
	cin>>n;
	rep(i,n){
		vec[i][i]=0;
		rep(j,n)
			vec[i][j]=1000000;
	}
	rep(i,n)
		vec[i][i]=0;
	rep(i,n){
		cin>>r>>k;
		rep(j,k){
			cin>>t;
			vec[r-1][t-1]=1;
		}
	}
	rep(k,n)
	rep(i,n)
	rep(j,n){
		vec[i][j]=min(vec[i][j],vec[i][k]+vec[k][j]);
	}
	cin>>p;
	rep(i,p){
		cin>>s>>d>>v;
		s--;
		d--;
		if(vec[s][d]+1<=v)
		cout<<vec[s][d]+1<<endl;
		else
		cout<<"NA"<<endl;
		
		
		
	}
	return 0;
}