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

class Node {
public:
	int pos, ttl;
	Node(int p, int t) : pos(p), ttl(t) {}
};

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

void bfs(int src, int dst, int limit) {
	deque<Node> que;
	que.push_back(Node(src, limit - 1));
	while (!que.empty()) {
		Node current = que.front();
		que.pop_front();
		if (current.pos == dst) {
			cout << limit - current.ttl << endl;
			return;
		}
		if (current.ttl == 0) continue;
		for (int next = 1; next <= n; next++) {
			if (adj[current.pos][next]) {
				que.push_back(Node(next, current.ttl - 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;
}