#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define it vector<int>::iterator
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
using namespace std;
int main(){
	int n;
	cin>>n;
	vvi cost(n+1,vi(n+1,inf));
	rep(i,n){
		int s,m;
		cin>>s>>m;
		rep(j,m){
			int t;
			cin>>t;
			cost[s][t]=1;
		}
	}
	rep(k,n+1)rep(i,n+1)rep(j,n+1)
		cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
	int m;
	cin>>m;
	rep(i,m){
		int a,b,c;
		cin>>a>>b>>c;
		int t=cost[a][b]+1;
		if(t>c)cout<<"NA"<<endl;
		else cout<<t<<endl;
	}
}