#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N_MAX = 100;

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];
int visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
	queue<int> que;
	fill(visited, visited + n + 1, -1);
	que.push(src);
	visited[src] = 1;
	while (!que.empty()) {
		int current = que.front();
		que.pop();
		if (current == dst) {
			if (visited[dst] > limit) break;
			cout << visited[dst] << endl;
			return;
		}
		for (int next = 1; next <= n; next++) {
			if (adj[current][next] && visited[next] == -1) {
				que.push(next);
				visited[next] = visited[current] + 1;
			}
		}
	}
	cout << "NA" << endl;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int r, k;
		cin >> r >> k;
		fill(adj[r], adj[r] + n, false);
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			adj[r][t] = true;
		}
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		bfs(s, d, v);
	}
	return 0;
}