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

typedef vector<vector<int> > Graph;

const int INF = 1<<28;

int N;
Graph G;
  
int bfs(int src, int dst) {
  queue<int> que;
  vector<int> cost(N, INF);
  que.push(src);
  cost[src] = 0;
  while(que.size()) {
    const int v = que.front();
    que.pop();
    for(int i = 0; i < G[v].size(); ++i) {
      int nv = G[v][i];
      if(cost[nv] != INF) continue;
      cost[nv] = cost[v] + 1;
      que.push(nv);
    }
  }
  return cost[dst];
}

int main() {
  cin >> N;
  G.resize(N);
  vector<vector<int> > to(N);
  map<int,int> id;
  for(int i = 0; i < N; ++i) {
    int r, k;
    cin >> r >> k;
    id[r] = i;
    to[i].resize(k);
    for(int j = 0; j < k; ++j) {
      cin >> to[i][j];
    }
  }
  for(int i = 0; i < N; ++i) {
    for(int j = 0; j < to[i].size(); ++j) {
      G[i].push_back(id[to[i][j]]);
    }
  }

  int p;
  cin >> p;
  while(p--) {
    int s, d, v;
    cin >> s >> d >> v;
    s = id[s];
    d = id[d];
    int res = bfs(s, d);
    if(res < v) {
      cout << res+1 << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}