#include<iostream>
using namespace std;

const int INF = 10000;
const int VMAX = 108;

int main() {
  int n;
  cin >> n;
  int G[VMAX][VMAX];
  for (int i = 0; i < VMAX; i++) {
    for (int j = 0; j < VMAX; j++) {
      G[i][j] = (i == j ? 0 : INF);
    }
  }

  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    while (k > 0) {
      int t;
      cin >> t;
      G[r][t] = 1;
      k--;
    }
  }

  for (int k = 0; k < VMAX; k++) {
    for (int i = 0; i < VMAX; i++) {
      for (int j = 0; j < VMAX; j++) {
        G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
      }
    }
  }

  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    if (G[s][d] + 1 > v) {
      cout << "NA" << endl;
    } else {
      cout << G[s][d] + 1 << endl;
    }
  }
 
  return 0;
}