#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_NODE = 110;

vector<int> makeInput(string input){
    vector<int> ret;
    stringstream ss(input);

    while(ss >>input) ret.push_back(atoi(input.c_str()));
    return ret;
}

int main(){
    int n;
    while(cin >>n && n){
        int d[MAX_NODE][MAX_NODE];
        REP(i, MAX_NODE) REP(j, MAX_NODE) d[i][j] = INF;
        REP(i, MAX_NODE) d[i][i] = 0;
        vector< vector<int> > graph(MAX_NODE, vector<int>(MAX_NODE));
        string dust_str;
        getline(cin, dust_str);
        //make graph
        REP(i, n){
            string input;
            getline(cin, input);
            vector<int> sum_list = makeInput(input);

            //make
            REP(j, sum_list[1]){
                graph[sum_list[0]].push_back(sum_list[j + 2]);
                d[sum_list[0]][sum_list[j + 2]] = 1;
            }
        }

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

        //cal ans
        int m;
        cin >>m;
        getline(cin, dust_str);
        REP(i, m){
            string input;
            getline(cin, input);
            vector<int> sum_list = makeInput(input);
            int from = sum_list[0], to = sum_list[1], ttl = sum_list[2];
            if(ttl < (d[from][to] + 1)) cout <<"NA" <<endl;
            else cout <<d[from][to] + 1 <<endl;
        }
    }
    return 0;
}