#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> dp(n, vector<int>(n, INF<int>));
    for (int i = 0; i < n; ++i) dp[i][i] = 0;
    for (int i = 0; i < n; ++i) {
        int r, k;
        cin >> r >> k;
        for (int j = 0; j < k; ++j) {
            int x;
            cin >> x;
            dp[r-1][x-1] = 1;
        }
    }
    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);
            }
        }
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--; b--;
        if(dp[a][b] >= c) puts("NA");
        else cout << dp[a][b]+1 << "\n";
    }
    return 0;
}
