#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int MAX_N = 103;
vector<int> E[MAX_N];


int solve(int s, int d, int v) {
  int vis[MAX_N];
  queue<int> que;
  fill(vis, vis+MAX_N, -1);
  vis[s] = 1;
  que.push(s);
  while(!que.empty()) {
    int p = que.front();
    que.pop();
    for(int i = 0; i < E[p].size(); ++i) {
      int np = E[p][i];
      if(vis[np] != -1) continue;
      vis[np] = vis[p] + 1;
      if(np == d) {
	if(v >= vis[np]) return vis[np];
	else return -1;
      }
      que.push(np);
    }
  }
  return -1;
}

int main() {
  int n;
  while(cin >> n && n) {
    for(int i = 0; i < MAX_N; ++i) E[i].clear();
    for(int i = 0; i < n; ++i) {
      int r, k;
      cin >> r >> k;
      for(int j = 0; j < k; ++j) {
	int t;
	cin >> t;
	E[r].push_back(t);
      }
    }
    int p;
    cin >> p;
    for(int i = 0; i < p; ++i) {
      int s, d, v;
      cin >> s >> d >> v;
      int ans = solve(s,d,v);
      if(ans == -1) cout << "NA" << endl;
      else cout << ans << endl;
    }
  }
  return 0;
}