#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

const int INF = 100000000;

int main()
{
    int n, p;
    cin >> n;
    int w[n+1][n+1];
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= n; j++) 
            w[i][j] = (i==j) ? 0 : INF;

    int r,k,t;
    for(int i = 1; i <= n; i++) {
        cin >> r >> k;
        for(int j = 0; j < k; j++) {
            cin >> t;
            w[r][t] = 1;
        }
    }

    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
            }
        }
    }
    
    cin >> p;
    int s,d,v;
    for(int i = 0; i < p; i++) {
        cin >> s >> d >> v;
        if(w[s][d] + 1 <= v) {
            cout << w[s][d] + 1 << endl;
        } else {
            cout << "NA" << endl;
        }
    }
}