#include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n,r,k,t1,t2;
  cin >> n;
  int graph[n+1][n+1];
  for(int i=0;i<n+1;i++){
    for(int j=0;j<n+1;j++){
      graph[i][j] = 1000000;
    }
  }

  for(int i=0;i<n;i++){
    cin >> r >> k;
    int t;
    for(int j=0;j<k;j++){
      cin >> t;
      graph[r][t] = 1;
    }
  }


  //ワーシャルフロイド
  for(int k=1;k<n+1;k++){
    for(int i=1;i<n+1;i++){
      for(int j=1;j<n+1;j++){
	graph[i][j] = min( graph[i][j] , graph[i][k] + graph[k][j]);
      }
    }
  }


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