#include <iostream>
#include <queue>
#include <climits>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int main(){
	int n,r,k,t,p,s,d,v;
	int wf[100][100];
	rep(i,100)rep(j,100)wf[i][j] = 1<<21;

	cin >> n;
	for(int i=0;i<n;i++){
		cin >> r >> k;
		r--;
		for(int j=0;j<k;j++){
			cin >> t;
			t--;
			wf[r][t] = 1;
		}
	}
	rep(k,n)rep(i,n)rep(j,n)wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j]);
	
	cin >> p;
	for(int i=0;i<p;i++){
		cin >> s >> d >> v;
		s--;d--;
		if(v > wf[s][d] )cout << wf[s][d]+1 << endl;
		else cout << "NA" << endl;
	}
}