#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

int dist[100][100];

int main() {
	int n;
	cin >> n;
	
	fill(dist[0], dist[100], INF);
	REP(i, n) dist[i][i] = 0;
	REP(i, n) {
		int r, k;
		scanf("%d %d", &r, &k);
		r--;
		REP(j, k) {
			int t;
			scanf("%d", &t);
			t--;
			dist[r][t] = 1;
		}
	}
	
	REP(k, n) REP(i, n) REP(j, n)
		dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
	
	int p;
	cin >> p;
	REP(i, p) {
		int s, d, v;
		scanf("%d %d %d", &s, &d, &v);
		s--; d--;
		if (dist[s][d] == INF || dist[s][d] >= v) puts("NA");
		else printf("%d\n", dist[s][d] + 1);
	}
	return 0;
}