#include <bits/stdc++.h>
using namespace std;

constexpr int INF = 1e9;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> d(n, vector<int>(n, INF));
    for(int i=0; i<n; ++i) {
        d[i][i] = 0;

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