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

map< int,vector<int> > info;

int bfs(int s,int d){
	queue<NODE> Q;
	
	map<int,bool>done;
	
	
	Q.push( make_pair(s,0) );

	while(!Q.empty()){
		NODE now = Q.front();
		Q.pop();
		done[now.first] = true;
		if(now.first == d){
			return now.second;
		}
		for(int i=0;i<info[now.first].size();i++){
			if(!done[info[now.first][i]])
				Q.push( make_pair(info[now.first][i],now.second+1));
		}
	}
	return INT_MAX;
}

int main(){
	int n,r,k,t;
	int p,s,d,v;
	
	cin >> n;
	
	for(int i=0;i<n;i++){
		cin >> r >> k;
		for(int j=0;j<k;j++){
			cin >> t;
			info[r].push_back(t);
		}
	}
	
	cin >> p;
	
	for(int i=0;i<p;i++){
		cin >> s >> d >> v;
		if(v > bfs(s,d) )cout << bfs(s,d)+1 << endl;
		else cout << "NA" << endl;
	}
}