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

const int INF = (int)1e9;
typedef pair<int, int> P;
struct Edge {
    int to, cost;
    Edge(int t, int c) : to(t), cost(c) { }
};

int dijkstra(const vector<vector<Edge> > &edges, const int from, const int to) {
    vector<int> dist(edges.size(), INF);
    priority_queue<P, vector<P>, greater<P> > Q;
    Q.push(P(1, from));
    dist[from] = 1;
    while(!Q.empty()) {
        P p = Q.top(); Q.pop();
        int v = p.second;
        int d = p.first;
        if(v == to) break;
        if(d > dist[v]) continue;
        for(int i = 0; i < (int)edges[v].size(); i++) {
            Edge e = edges[v][i];
            int c = dist[v] + e.cost;
            if(dist[e.to] > c) {
                dist[e.to] = c;
                Q.push(P(c, e.to));
            }
        }
    }
    return dist[to];
}

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