#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;



int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit,P[101],D[101],num,min,min_index,current,Distance[101][101];

	for(int i=1; i < 101; i++){
		for(int k=1; k < 101; k++){
			table[i][k] = 0;
			Distance[i][k] = 1000000000;
		}
	}

	scanf("%d",&n);

	for(int i = 0; i < n; i++){
		scanf("%d %d",&r,&k);
		for(int p=0;p < k; p++){
			scanf("%d",&to);
			table[r][to] = 1;
		}
	}

	for(int i = 1; i<= n; i++){
		for(int k=1; k <= n; k++){
			if(i != k){
				start = i, goal = k;

				for(int index=1; index <= n; index++){
					P[index] = 0;
					D[index] = 1000000000;
				}

				P[start] = 1;
				num = 1;

				for(int index = 1; index <= n; index++){
					if(index != start && table[start][index] == 1){
						D[index] = 1;
					}
				}

				while(num < n){
					min = 1000000000;
					for(int index=1;index <= n; index++){
						if(P[index] == 0 && D[index] < min){
							min = D[index];
							min_index =index;
						}
					}

					if(min == 1000000000)break;

					P[min_index] = 1;
					current = min_index;
					num++;
					for(int index=1; index <= n; index++){
						if(table[current][index] == 1){
							D[index] = (D[index] <= (D[current]+1))?D[index]:(D[current]+1);
						}
					}
				}

				Distance[start][goal] = D[goal];
			}
		}
	}

	scanf("%d",&num_of_packet);

	for(int i=0; i < num_of_packet; i++){
		scanf("%d %d %d",&start,&goal,&t_limit);

		if(Distance[start][goal] >= t_limit){
			printf("NA\n");
		}else{
			printf("%d\n",Distance[start][goal]+1);
		}

	}

    return 0;
}