
import std.stdio, std.typecons, std.array, std.conv;

alias Tuple!(int, "id", int, "hop") Node;

void main() {
  int number, n; readf("%d\n", &number);
  int[][int] edges;
  foreach(i; 0..number) {
    int from, k;
    scanf("%d %d", &from, &k);
    foreach(j; 0..k) {
      int to;
      scanf(j == k - 1 ? "%d\n" : "%d ", &to);
      edges[from] ~= to;
    }
    if (k == 0) edges[from] = [];
  }
  scanf("%d\n", &n);
  foreach(i; 0..n) {
    int to, from, ttl, ans = -1;
    Node[] queue;
    int[] used = new int[](number + 1);
    scanf("%d %d %d", &from, &to, &ttl);
    queue ~= Node(from, 1);
    used[from] = true; 
    while(!queue.empty) {
      Node node = queue.front;
      if (node.id == to) {ans = node.hop; break;}
      queue.popFront;
      foreach(j; edges[node.id]) {
        if (!used[j]) {
          queue ~= Node(j, node.hop + 1);
          used[j] = true;
        }
      }
    }
    writeln((ans == -1 || ans > ttl) ? "NA" : ans.to!string);
  }
}