#include <iostream>
#include <climits>
using namespace std;

int main() {
  int n, m, t, u, v;
  int a[100][100];
  cin >> n;
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      a[i][j] = -1;
    }
    a[i][i] = 0;
  }
  for (int i=0; i<n; i++) {
    cin >> t >> u;
    --t;
    for (int j=0; j<u; j++) {
      cin >> v;
      --v;
      a[t][v] = 1;
    }
  }
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      for (int k=0; k<n; k++) {
        if (a[j][i] == -1 || a[i][k] == -1) continue;
        if (a[j][k] == -1) {
          a[j][k] = a[j][i] + a[i][k];
        } else if (a[j][k] > a[j][i] + a[i][k]) {
          a[j][k] = a[j][i] + a[i][k];
        }
      }
    }
  }
  cin >> m;
  for (int i=0; i<m; i++) {
    cin >> t >> u >> v;
    --t;
    --u;
    if (a[t][u] != -1 && a[t][u] < v) {
      cout << a[t][u]+1 << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}