#include <iostream>
#include <vector>

void show(int** a, int n)
{
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) std::cout << a[i][j] << " ";
		std::cout <<std::endl;
	}
}

int main()
{
	int n; std::cin >> n;
	int** D = new int*[n];
	for (int i=0; i<n; i++) D[i] = new int[n];
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) D[i][j] = (i==j) ? 0 : n;
	}
	for (int i=0; i<n; i++) {
		int s; std::cin >> s;
		int k; std::cin >> k;
		for (int j=0; j<k; j++) {
			int t; std::cin >> t;
			D[s-1][t-1] = 1;
		}
	}
	
	//show(D, n);
	// Floyd-Warshall
	for (int k=0; k<n; k++) {
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
				D[i][j] = std::min(D[i][j], D[i][k] + D[k][j]);
			}
		}
		//std::cout << k << std::endl;
		//show(D, n);
	}
	
	int p; std::cin >> p;
	for (int i=0; i<p; i++) {
		int s, t, v;
		std::cin >> s >> t >> v;
		if (D[s-1][t-1] < n && D[s-1][t-1]+1 <= v) std::cout << D[s-1][t-1]+1 << std::endl;
		else std::cout << "NA" << std::endl;
	}
	
	for (int i=0; i<n; i++) delete[] D[i];
	delete[] D;
		
	return 0;
}

