#include <stdio.h>
#include <list>
#include <set>

using namespace std;

typedef struct {
  int *ks;
  int length;
} node;

node *nodes;


int dikstra(int s, int e, int deep){
  int c=0;

  list<int> que;
  set<int> done;
  set<int> done2;
  que.push_back(s);
  done.insert(s);

  while(1){
    c++;
    if(c >= deep) return -1;

    done2.clear();

    int len = que.size();
    for(int i=0;i<len;i++){

      int v = que.front();
      que.pop_front();

      for(int j=0;j<nodes[v].length;j++){
        if(nodes[v].ks[j]==e){
          return c+1;
        }else{
          int k = nodes[v].ks[j];
          if(done.find(k) == done.end()){
            que.push_back(k);
            done2.insert(k);
          }
        }
      }
    }
    done.insert(done2.begin(), done2.end());
  }
}

int main(){
  int n;
  scanf("%d", &n);

  nodes = new node[n];

  int i,j;
  int t,u,v;
  for(i=0;i<n;i++){
    scanf("%d%d", &t,&u);
    nodes[t-1].ks = new int[u];
    nodes[t-1].length = u;
    for(j=0;j<u;j++){
      scanf("%d", &v);
      nodes[t-1].ks[j] = v-1;
    }
  }

  int m;
  scanf("%d", &m);
  for(i=0;i<m;i++){
    scanf("%d%d%d", &t, &u, &v);
    int r;
    if((r=dikstra(t-1, u-1,v)) != -1) printf("%d\n", r);
    else puts("NA");
  }
}
    