#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int INF = 1 << 29;
const int N = 100;
int n;
int as[N+1][N+1];

void floyd() {
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                as[i][j] = min(as[i][j], as[i][k] + as[k][j]);
            }
        }
    }
}

int main() {
    while (cin >> n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                as[i][j] = INF;
            }
        }

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