// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int s = 0, int d = 0, Weight w = 0)
    : src(s), dst(d), weight(w) {
  }
};

using Edges  = vector<Edge>;
using Graph  = vector<Edges>;
using Array  = vector<Weight>;
using Matrix = vector<Array>;

void addArc(Graph &g, const int &a, const int &b, const Weight &w = 1) {
  g[a].emplace_back(a, b, w);
}

void addEdge(Graph &g, const int &a, const int &b, const Weight &w = 1) {
  addArc(g, a, b, w);
  addArc(g, b, a, w);
}

// deps: graph-basic
Matrix warshallFloyd(const Graph &g) {
  int n = g.size();
  Matrix d(n, Array(n, INF));
  rep(i, n) d[i][i] = 0;
  rep(i, n) for (auto &e : g[i]) cmin(d[e.src][e.dst], e.weight);
  rep(k, n) rep(i, n) rep(j, n) {
    if (d[i][k] != INF && d[k][j] != INF) cmin(d[i][j], d[i][k] + d[k][j]);
  }
  return d;
}

void solve() {
  int n;
  cin >> n;
  Graph g(n);
  while (n--) {
    int r, k;
    cin >> r >> k;
    --r;
    while (k--) {
      int t;
      cin >> t;
      --t;
      addArc(g, r, t);
    }
  }
  Matrix t = warshallFloyd(g);
  int p;
  cin >> p;
  while (p--) {
    int s, d, v;
    cin >> s >> d >> v;
    --s, --d;
    if (t[s][d] + 1 > v) {
      cout << "NA" << endl;
      continue;
    }
    cout << t[s][d] + 1 << endl;
  }
}