#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

typedef int Weight;
struct Edge {
	int src, dst;
	Weight weight;
	Edge(int src_, int dst_, Weight weight_) :
		src(src_), dst(dst_), weight(weight_) { }
	Edge(int src_, int dst_) :
		src(src_), dst(dst_) {

	}
};
pair<bool, vector<Weight>> spfa(const int v_num, const vector<vector<Edge>>&es, const int start) {
	vector<Weight>diss(v_num, INT_MAX);
	queue<int>que;
	vector<bool>use(v_num);
	vector<int>count(v_num);
	que.emplace(start);
	diss[start] = 0;
	while (!que.empty()) {
		int src(que.front());
		que.pop();
		use[src] = false;
		for (auto e : es[src]) {
			const int d = e.dst;
			if (diss[src] + e.weight < diss[d]) {
				diss[d] = diss[src] + e.weight;
				if (!use[d]) {
					use[d] = true;
					count[d]++;
					if (count[d] >= v_num)return make_pair(false, vector<Weight>());
					que.emplace(d);
				}
			}
		}
	}
	return make_pair(true, diss);
}
struct Compress {
	map<int, int>mp;
	map<int, int>revmp;

	Compress(vector<int>vs) {
		setmp(vs);
	}
	Compress(const set<int>&vs) {
		setmp(vs);
	}
	Compress() :mp(), revmp() {

	}
	void setmp(vector<int>vs) {
		sort(vs.begin(), vs.end());
		vs.erase(unique(vs.begin(), vs.end()), vs.end());
		for (int i = 0; i < static_cast<int>(vs.size()); ++i) {
			mp[vs[i]] = i;
			revmp[i] = vs[i];
		}
	}
	void setmp(const set<int>&vs) {
		int num = 0;
		for (auto v : vs) {
			mp[v] = num;
			revmp[num] = v;
			num++;
		}
	}
};

int main() {
	int N; cin >> N;
	vector<vector<Edge>>es(N);
	map<int, int>mp;
	map<int, int>revmp;
	for (int i = 0; i < N; ++i) {
		int r; cin >> r;
		mp[r] = i;
		revmp[i] = r;
		int k; cin >> k;
		for (int j = 0; j < k; ++j) {
			int t; cin >> t;
			es[i].push_back(Edge(i, t));
		}
	}
	vector<vector<int>>companion(N,vector<int>(N,1e8));
	for (auto& aes : es) {
		for (auto&e : aes) {
			e.dst = mp[e.dst];
			companion[e.src][e.dst] = 1;
		}
	}
	for (int i = 0; i < N; ++i) {
		companion[i][i] = 0;
	}
	for (int k = 0; k < N; ++k) {
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				companion[i][j] = min(companion[i][k] + companion[k][j], companion[i][j]);
			}
		}
	}
	int P; cin >> P;
	while (P--) {
		int s, d, v; cin >> s >> d >> v;
		int ans = companion[mp[s]][mp[d]];
		if (ans >= v)cout << "NA" << endl;
		else {

			cout << ans + 1 << endl;
		}
	}
	

	return 0;
}