#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

const int INF = 1e7;

int n;
int d[101][101];

void init(int n){
    rep(i, 0, n){
        rep(j, 0, n){
            d[i][j] = INF;
            if(i == j) d[i][j] = 0;
        }
    }
}

void warshall_floyd(){
    rep(k, 0, n){
        rep(i, 0, n){
            rep(j, 0, n){
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

signed main(){
    cin >> n;
    init(n);
    int r, k, t;
    rep(i, 0 ,n){
        cin >> r >> k;
        rep(j, 0, k){
            cin >> t;
            d[r - 1][t - 1] = 1;
        }
    }
    warshall_floyd();
    int fr, to, v, p;
    cin >> p;
    rep(i, 0, p){
        cin >> fr >> to >> v;
        if(d[fr - 1][to - 1] < v){
            cout << d[fr - 1][to - 1] + 1 << endl;
        }else{
            cout << "NA" << endl;
        }
    }
}