#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const int INF = 1<<29;

int main()
{
	int n;
	scanf("%d", &n);
	vvi dist(n, vi(n, INF));
	
	rep(i, n){
		int r, k;
		scanf("%d%d", &r, &k);
		--r;
		rep(j, k){
			int a;
			scanf("%d", &a);
			dist[r][a-1] = 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;
	scanf("%d", &p);
	rep(i, p){
		int s, d, v;
		scanf("%d%d%d", &s, &d, &v);
		--s;
		--d;
		if(dist[s][d]+1 <= v)
			printf("%d\n", dist[s][d]+1);
		else
			puts("NA");
	}
	
	return 0;
}