#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

void shortestPath(const vector<vector<int> >& edges, int start, vector<int>& dist)
{
    dist.assign(edges.size(), -1);
    dist[start] = 0;
    queue<int> q;
    q.push(start);

    while(!q.empty()){
        int v = q.front();
        q.pop();
        for(unsigned i=0; i<edges[v].size(); ++i){
            int u = edges[v][i];
            if(dist[u] == -1){
                dist[u] = dist[v] + 1;
                q.push(u);
            }
        }
    }
}

void shortestPath(const vector<vector<int> >& edges, vector<vector<int> >& dist)
{
    dist.resize(edges.size());
    for(unsigned i=0; i<edges.size(); ++i)
        shortestPath(edges, i, dist[i]);
}


int main()
{
    int n;
    cin >> n;
    vector<vector<int> > edges(n+1);
    for(int i=0; i<n; ++i){
        int r, k;
        cin >> r >> k;
        edges[r].resize(k);
        for(int j=0; j<k; ++j)
            cin >> edges[r][j];
    }
    vector<vector<int> > dist;
    shortestPath(edges, dist);

    int p;
    cin >> p;
    for(int i=0; i<p; ++i){
        int s, d, v;
        cin >> s >> d >> v;
        int num = dist[s][d];
        if(num == -1 || num >= v)
            cout << "NA" << endl;
        else
            cout << (num+1) << endl;
    }

    return 0;
}