#include <iostream>
#include <string>
#include <vector>
using namespace std;

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