#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#define INF (2000000000)
#define MAX (2000)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;


int n;

int head[100];
int to[10000];
int next[10000];

int d[100];

void Search(int now)
{
	fill(d, d + 100, INF);
	d[now] = 0;
	
	while (true){
		bool f = false;
		
		for (int i = 0; i < n; i++){
			for (int e = head[i]; e != -1; e = next[e]){
				if (d[i] != INF && d[to[e]] > d[i] + 1){
					d[to[e]] = d[i] + 1;
					f = true;
				}
			}
		}
		
		if (!f) break;
	}
}

int main()
{
	memset(head, -1, sizeof(head));
	
	int m = 0;
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++){
		int r, k;
		scanf("%d %d", &r, &k);
		r--;
		
		for (int j = 0; j < k; j++){
			int t;
			scanf("%d", &t);
			t--;
			
			next[m] = head[r];
			head[r] = m;
			to[m] = t;
			m++;
		}
	}
	
	int p;
	scanf("%d", &p);
	
	for (int i = 0; i < p; i++){
		int f, t, v;
		scanf("%d %d %d", &f, &t, &v);
		f--; t--;
		
		Search(f);
		
		if (d[t] + 1 <= v){
			printf("%d\n", d[t] + 1);
		}
		else {
			puts("NA");
		}
	}
}