#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int main(){
	int n; cin>>n;
	vector<vector<int> > e;
	vector<int> id(n);
	FOR(i,n){
		cin>>id[i];
		int x; cin>>x;
		e.push_back(vector<int>(x));
		FOR(j,x) cin>>e[i][j];
	}

	map<int,int> mp;
	FOR(i,n) mp[id[i]] = i;
	FOR(i,n) FOR(j,sz(e[i])) e[i][j] = mp[e[i][j]];

	vector<vector<int> > dp(n,vector<int>(n,INF));
	FOR(i,n) FOR(j,sz(e[i])) dp[i][e[i][j]] = 1;

	FOR(k,n) FOR(i,n) FOR(j,n) dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);

	int m; cin>>m;
	while(m--){
		int s,t,tll; cin>>s>>t>>tll;
		s = mp[s],t = mp[t];
		int ans = dp[s][t] + 1;
		if(ans > tll) printf("NA");
		else printf("%d",ans);
		
		puts("");
	}

	return 0;
}