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

struct Node {
    vector<int> to;
    bool isDone;
    int cost;
};

const int MAX_N = 100;
Node nodes[MAX_N + 1];
int n;

int dijkstra(int start, int end) {
    for (int i = 1; i <= n; i++) {
        nodes[i].isDone = false;
        nodes[i].cost = -1;
    }
    nodes[start].cost = 0;
    while (true) {
        queue<int> qu;
        for (int i = 1; i <= n; i++) {
            if (nodes[i].isDone == true || nodes[i].cost < 0) {
                continue;
            }
            qu.push(i);
        }
        if (qu.empty() == true) {
            break;
        }
        while (qu.empty() == false) {
            int idx = qu.front();
            qu.pop();
            nodes[idx].isDone = true;
            for (vector<int>::iterator it = nodes[idx].to.begin(); it != nodes[idx].to.end(); it++) {
                if (nodes[*it].cost < 0) {
                    nodes[*it].cost = nodes[idx].cost + 1;
                }
            }
        }
    }
    return nodes[end].cost;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        int r, k;
        cin >> r >> k;
        for (int j = 0; j < k; j++) {
            int t;
            cin >> t;
            nodes[r].to.push_back(t);
        }
    }
    int p;
    cin >> p;
    for (int i = 0; i < p; i++) {
        int s, d, v;
        cin >> s >> d >> v;
        int cost = 1 + dijkstra(s, d);
        if (v < cost) {
            cout << "NA" << endl;
        } else {
            cout << cost << endl;
        }
    }
}