#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  const int INF = 1 << 30;
  int n, dis[110][110];
  cin >> n;
  fill((int*)dis, (int*)(dis + 110), INF);
  for (int i = 1; i <= n; i++) {
    dis[i][i] = 0;
  }
  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      dis[r][t] = 1;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (dis[i][k] == INF || dis[k][j] == INF) continue;
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    if (dis[s][d] + 1 <= v) cout << dis[s][d] + 1 << endl;
    else cout << "NA" << endl;
  }

  return 0;
}
