#include <iostream>
#include <vector>

using namespace std;

#define INF 1e9

int main() {
	int n; cin >> n;
	int m[100][100]; fill(&m[0][0], &m[0][0]+100*100, INF);
	for (int i = 0; i < n; ++i) m[i][i] = 0;
	for (int i = 0; i < n; ++i) {
		int r, k; cin >> r >> k; --r;
		for (int j = 0; j < k; ++j) {
			int t; cin >> t; --t;
			m[r][t] = 1;
		}
	}
	for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
		m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
	}
	int p; cin >> p;
	for (int k = 0; k < p; ++k) {
		int s, t; cin >> s >> t; --s, --t;
		int th; cin >> th;
		if (m[s][t] < th) cout << m[s][t]+1 << endl;
		else cout << "NA" << endl;
	}
}