#include <cstdio>
#include <vector>
#include <utility>
using namespace std;

#define INF 10000000

int n,r,k,t,p,s,d,v;
vector<pair<int,vector<int> > > vec;

int find(int num){
	for(int i = 0; i < n; i++){
		if(vec[i].first == num){
			return i;
		}
	}
	return -1;
}

int sch(int st, int ed){
	int from = find(st);
	int to = find(ed);
	vector<int> vv = vector<int>(n,INF);
	vv[from] = 0;
	for(int i = 1; i <= n; i++){
		for(int vi = 0; vi < n; vi++){
			if(vv[vi] == i - 1){
				for(int j = 0; j < vec[vi].second.size(); j++){
					int tmp = find(vec[vi].second[j]);
					if(vv[tmp] == INF){
						vv[tmp] = i;
					}
				}
			}
		}
	}
	return vv[to];
}

int main(){
	scanf("%d",&n);
	vec = vector<pair<int,vector<int> > >(n);
	for(int i = 0; i < n; i++){
		scanf("%d%d",&r,&k);
		vec[i].first = r;
		vec[i].second = vector<int>(k);
		for(int j = 0; j < k; j++){
			scanf("%d",&t);
			vec[i].second[j] = t;
		}
	}
	scanf("%d",&p);
	for(int i = 0; i < p; i++){
		scanf("%d%d%d",&s,&d,&v);
		int l = sch(s,d);
		l += 1;
		if(l <= v){
			printf("%d\n",l);
		}
		else{
			printf("NA\n");
		}
	}
	return 0;
}