#include<iostream>
#include<cstdio>
#include<algorithm>
#define INF 9999999
using namespace std;
int n, p;
int r, k;
int t;
int s, d, v, cost[100][100];
void warshall_floyd();
int main(){
  cin >> n;
  for(int i = 0; i <= n; i++){
    for(int j = 0; j <= n; j++){
      if(i == j) cost[i][j] = 0;
      else{
	cost[i][j] = INF;
      }
    }
  }
  for(int i = 1; i <= n; i++){
    cin >> r >> k;
    for(int j = 0; j < k; j++){
      cin >> t;
      cost[i][t] = 1;
    }
  }
  warshall_floyd();
  cin >> p;
  for(int i = 0; i < p;i++){
    cin >> s >> d >> v;
    if(cost[s][d] + 1 <= v) cout << cost[s][d] + 1 << endl;
    else cout << "NA" << endl;
  }
}

void warshall_floyd(){
  for(int k = 1; k <= n; k++){
    for(int i = 1; i <= n; i++){
      for(int j = 1; j <= n; j++){
	cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }
}