#include "stdio.h"
#include <bits/stdc++.h>
using namespace std;
int eg[101][101];
int d[101][101];
int const MAX=10000;
int main(){
    int n;
    cin >> n;
    for (int i=0;i<n;i++){
        int nt;
        cin >> nt;
        string str;
        getline(cin,str);
        stringstream ss;
        ss << str;
        while (!ss.eof()){
            int to;
            ss >> to;
            eg[nt][to]=1;
        }
    }
    for(int f=1;f<=n;f++)for(int t=1;t<=n;t++){
        d[f][t]=f==t?0:(eg[f][t]==1?1:MAX);
    }
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    }
    int rn;
    cin >> rn;
    for (int i=0;i<rn;i++){
        int from,to,ttl;
        cin >> from >> to >> ttl;
        if (d[from][to]+1>ttl)cout << "NA";
        else cout << d[from][to]+1;
        cout << endl;
    }
    
}