#include<bits/stdc++.h>
#define S second
#define f first
#define P pair<int,int>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
main(){
  int n,p,m,t,s;
  cin>>n;
  vector<int>a[n+1];
  r(i,n){cin>>p>>m;
    while(m--)cin>>t,a[p].push_back(t);
  }
  cin>>m;
  while(m--){
    cin>>s>>t>>p;p--;
    queue<P>q;
    q.push(make_pair(s,0));
    while(!q.empty()){
      P w=q.front();q.pop();
      if(w.f==t){cout<<w.S+1<<endl;goto L;}
      r(i,a[w.f].size()){
        if(w.S+1<=p)q.push(make_pair(a[w.f][i],w.S+1));
      }
    }
    cout<<"NA"<<endl;
    if(0)L:n++;
  }
}