#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int d[200][200];
int main(){
    int n;
    cin>>n;
    rep(i,200)rep(j,200)d[i][j]=1<<29;
    rep(i,200)d[i][i]=0;
    rep(i,n){
        int src,k;
        cin>>src>>k;
        src--;
        rep(j,k){
            int dst;cin>>dst;
            dst--;
            d[src][dst] = 1;
        }
    }

    rep(k,n)rep(i,n)rep(j,n)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    int p;cin>>p;
    rep(i,p){
        int s,t,ttl;
        cin>>s>>t>>ttl;--s,--t;
        if(d[s][t] >= ttl) puts("NA");
        else printf("%d\n", d[s][t]+1);
    }
}