#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;

const int INF = 1e9;

void WarshallFloyd(vector<vector<int>> &d) {
    int n = d.size();

    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 main()
{
    int n, k, t, s, d, v;

    cin >> n;
    vector<vector<int>> g(n, vector<int>(n, INF));

    for (int i = 0; i < n; ++i)
        g[i][i] = 0;

    for (int i = 0; i < n; ++i) {
        cin >> s >> k;
        --s;
        while (k--) {
            cin >> t;
            g[s][t - 1] = 1;
        }
    }

    WarshallFloyd(g);

    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> s >> d >> v;
        --s, --d;
        if (g[s][d] + 1 > v)
            cout << "NA\n";
        else
            cout << g[s][d] + 1 << '\n';
    }

    return 0;
}