#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int inf = 1 << 28;
int n, m;
int d[102][102];

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      d[i][j] = inf;
    }
  }
  for (int i = 0; i < n; ++i) {
    int r, k, t;
    cin >> r >> k;
    r--;
    for (int j = 0; j < k; ++j) {
      cin >> t;
      t--;
      d[r][t] = 1;
    }
  }
  
  for (int i = 0; i < n; ++i) d[i][i] = 0;
  
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }

  cin >> m;
  for (int i = 0; i < m; ++i) {
    int s, de, v;
    cin >> s >> de >> v;
    s--; de--;
    //printf("(%d,%d) ", s, de);
    if (d[s][de]+1 <= v) {
      cout << d[s][de]+1 << endl;
    } else {
      //cout << d[s][de] << endl;
      cout << "NA" << endl;
    }
  }
  return 0;
}