#include <iostream>
#include <queue>
#include <climits>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
	int n, p, r, k, t, s, d, v;
	cin >> n;
	int m[100][100] = {};
	rep(i,n) {
		cin >> r >> k;
		rep(j,k) {
			cin >> t;
			m[r-1][t-1] = 1;
		}
	}
	int ret[100][100] = {};
	rep(i,100) {
		ret[i][i] = 1;
	}
	rep(i,n) {
		queue<int> q;
		q.push(i);
		while(!q.empty()) {
			int a = q.front(); q.pop();
			rep(j,n) {
				if(m[a][j] && ret[i][j]==0) {
					q.push(j);
					ret[i][j] = ret[i][a]+1;
				}
			}
		}
	}
	cin >> p;
	rep(i,p) {
		cin >> s >> d >> v;
		--s,--d;
		if(ret[s][d] > v || ret[s][d] == 0) cout<< "NA" << endl;
		else cout << ret[s][d] << endl;
	}
	return 0;
}