# -*- coding: utf-8 -*-

"""

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0144



"""

import sys

from sys import stdin

input = stdin.readline

from enum import Enum





class Graph(object):

    """ single source shortest path """

    class Status(Enum):

        """ ?????????????¨??????¶??? """

        white = 1  # ????¨????

        gray = 2  # ?¨???????

        black = 3  #?¨???????



    def __init__(self, n, data):

        num_of_nodes = n+1

        self.color = [Graph.Status.white] * num_of_nodes  # ????????????????¨??????¶???

        self.M = [[float('inf')] * num_of_nodes for _ in range(num_of_nodes)]

        for i in range(num_of_nodes):

            self.M[i][i] = 0

        self._make_matrix(data)  # data????????????????????£??\??????(?????\?¶???¨???????????????????????§????????????)

        self.d = [float('inf')] * num_of_nodes  # ?§???????????????????(?????????)

        self.p = [-1] * num_of_nodes  # ????????????????????????????¨?????????????????????????



    def _make_matrix(self, data):

        for d in data:

            r = d[0]

            for t in d[2:]:

                self.M[r][t] = 1



    def dijkstra(self, start):

        self.d[start] = 0

        self.p[start] = -1

        while True:

            mincost = float('inf')

            # ??\??????????????§??????????????¨?????????????????????u???????????????

            for i in range(len(self.d)):

                if self.color[i] != Graph.Status.black and self.d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????

                    mincost = self.d[i]

                    u = i  # u??????????????????ID

            if mincost == float('inf'):

                break

            self.color[u] = Graph.Status.black  # ?????????u???S????±???????????????´???

            for v in range(len(self.d)):

                if self.color[v] != Graph.Status.black and self.M[u][v] != float('inf'):

                    # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????

                    if self.d[u] + self.M[u][v] < self.d[v]:

                        self.d[v] = self.d[u] + self.M[u][v]

                        self.p[v] = u

                        self.color[v] = Graph.Status.gray





def main(args):

    n = int(eval(input()))

    network = []

    for _ in range(n):

        network.append([int(x) for x in input().split()])



    packets = []

    p = int(eval(input()))

    for _ in range(p):

        packets.append([int(x) for x in input().split()])







    memo = [[0]*(n+1) for _ in range(n+1)]

    for i in range(1, n+1):

        g = Graph(n, network)

        g.dijkstra(i)

        for j in range(1, n+1):

            if i == j:

                continue

            if g.d[j] == float('inf'):

                memo[i][j] = float('inf')

            else:

                path = [j]

                u = j

                while g.p[u] != i:

                    path.append(g.p[u])

                    u = g.p[u]

                path.append(i)

                memo[i][j] = len(path)





    for s, d, v in packets:

        if memo[s][d] <= v:

            print((memo[s][d]))

        else:

            print('NA')







if __name__ == '__main__':

    main(sys.argv[1:])
# -*- coding: utf-8 -*-

"""

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0144



"""

import sys

from sys import stdin

input = stdin.readline

from enum import Enum





class Graph(object):

    """ single source shortest path """

    class Status(Enum):

        """ ?????????????¨??????¶??? """

        white = 1  # ????¨????

        gray = 2  # ?¨???????

        black = 3  #?¨???????



    def __init__(self, n, data):

        self.num_of_nodes = n+1

        self.color = [Graph.Status.white] * self.num_of_nodes  # ????????????????¨??????¶???

        self.M = [[float('inf')] * self.num_of_nodes for _ in range(self.num_of_nodes)]

        for i in range(self.num_of_nodes):

            self.M[i][i] = 0

        self._make_matrix(data)  # data????????????????????£??\??????(?????\?¶???¨???????????????????????§????????????)

        self.d = [float('inf')] * self.num_of_nodes  # ?§???????????????????(?????????)

        self.p = [-1] * self.num_of_nodes  # ????????????????????????????¨?????????????????????????



    def _make_matrix(self, data):

        for d in data:

            r = d[0]

            for t in d[2:]:

                self.M[r][t] = 1



    def dijkstra(self, start):

        self.d[start] = 0

        self.p[start] = -1

        while True:

            mincost = float('inf')

            # ??\??????????????§??????????????¨?????????????????????u???????????????

            for i in range(len(self.d)):

                if self.color[i] != Graph.Status.black and self.d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????

                    mincost = self.d[i]

                    u = i  # u??????????????????ID

            if mincost == float('inf'):

                break

            self.color[u] = Graph.Status.black  # ?????????u???S????±???????????????´???

            for v in range(len(self.d)):

                if self.color[v] != Graph.Status.black and self.M[u][v] != float('inf'):

                    # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????

                    if self.d[u] + self.M[u][v] < self.d[v]:

                        self.d[v] = self.d[u] + self.M[u][v]

                        self.p[v] = u

                        self.color[v] = Graph.Status.gray



    def warshallFloyd(self):

        V = self.num_of_nodes

        for k in range(V):

            for i in range(V):

                for j in range(V):

                    self.M[i][j] = min(self.M[i][j], self.M[i][k] + self.M[k][j])







def main(args):

    n = int(eval(input()))

    network = []

    for _ in range(n):

        network.append([int(x) for x in input().split()])



    packets = []

    p = int(eval(input()))

    for _ in range(p):

        packets.append([int(x) for x in input().split()])



    g = Graph(n, network)

    g.warshallFloyd()



    for s, d, v in packets:

        if g.M[s][d] < v:

            print((g.M[s][d]+1))

        else:

            print('NA')







if __name__ == '__main__':

    main(sys.argv[1:])
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0144
"""
import sys
from sys import stdin

input = stdin.readline
from enum import Enum


class Graph(object):
    """single source shortest path"""

    class Status(Enum):
        """?????????????¨??????¶???"""

        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  # ?¨???????

    def __init__(self, n, data):
        num_of_nodes = n + 1
        self.color = [Graph.Status.white] * num_of_nodes  # ????????????????¨??????¶???
        self.M = [[float("inf")] * num_of_nodes for _ in range(num_of_nodes)]
        for i in range(num_of_nodes):
            self.M[i][i] = 0
        self._make_matrix(
            data
        )  # data????????????????????£??\??????(?????\?¶???¨???????????????????????§????????????)
        self.d = [float("inf")] * num_of_nodes  # ?§???????????????????(?????????)
        self.p = [
            -1
        ] * num_of_nodes  # ????????????????????????????¨?????????????????????????

    def _make_matrix(self, data):
        for d in data:
            r = d[0]
            for t in d[2:]:
                self.M[r][t] = 1

    def dijkstra(self, start):
        self.d[start] = 0
        self.p[start] = -1
        while True:
            mincost = float("inf")
            # ??\??????????????§??????????????¨?????????????????????u???????????????
            for i in range(len(self.d)):
                if (
                    self.color[i] != Graph.Status.black and self.d[i] < mincost
                ):  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                    mincost = self.d[i]
                    u = i  # u??????????????????ID
            if mincost == float("inf"):
                break
            self.color[u] = Graph.Status.black  # ?????????u???S????±???????????????´???
            for v in range(len(self.d)):
                if self.color[v] != Graph.Status.black and self.M[u][v] != float("inf"):
                    # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                    if self.d[u] + self.M[u][v] < self.d[v]:
                        self.d[v] = self.d[u] + self.M[u][v]
                        self.p[v] = u
                        self.color[v] = Graph.Status.gray


def main(args):
    n = int(eval(input()))
    network = []
    for _ in range(n):
        network.append([int(x) for x in input().split()])
    packets = []
    p = int(eval(input()))
    for _ in range(p):
        packets.append([int(x) for x in input().split()])
    memo = [[0] * (n + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        g = Graph(n, network)
        g.dijkstra(i)
        for j in range(1, n + 1):
            if i == j:
                continue
            if g.d[j] == float("inf"):
                memo[i][j] = float("inf")
            else:
                path = [j]
                u = j
                while g.p[u] != i:
                    path.append(g.p[u])
                    u = g.p[u]
                path.append(i)
                memo[i][j] = len(path)
    for s, d, v in packets:
        if memo[s][d] <= v:
            print((memo[s][d]))
        else:
            print("NA")


if __name__ == "__main__":
    main(sys.argv[1:])

# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0144
"""
import sys
from sys import stdin

input = stdin.readline
from enum import Enum


class Graph(object):
    """single source shortest path"""

    class Status(Enum):
        """?????????????¨??????¶???"""

        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  # ?¨???????

    def __init__(self, n, data):
        self.num_of_nodes = n + 1
        self.color = [
            Graph.Status.white
        ] * self.num_of_nodes  # ????????????????¨??????¶???
        self.M = [[float("inf")] * self.num_of_nodes for _ in range(self.num_of_nodes)]
        for i in range(self.num_of_nodes):
            self.M[i][i] = 0
        self._make_matrix(
            data
        )  # data????????????????????£??\??????(?????\?¶???¨???????????????????????§????????????)
        self.d = [float("inf")] * self.num_of_nodes  # ?§???????????????????(?????????)
        self.p = [
            -1
        ] * self.num_of_nodes  # ????????????????????????????¨?????????????????????????

    def _make_matrix(self, data):
        for d in data:
            r = d[0]
            for t in d[2:]:
                self.M[r][t] = 1

    def dijkstra(self, start):
        self.d[start] = 0
        self.p[start] = -1
        while True:
            mincost = float("inf")
            # ??\??????????????§??????????????¨?????????????????????u???????????????
            for i in range(len(self.d)):
                if (
                    self.color[i] != Graph.Status.black and self.d[i] < mincost
                ):  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                    mincost = self.d[i]
                    u = i  # u??????????????????ID
            if mincost == float("inf"):
                break
            self.color[u] = Graph.Status.black  # ?????????u???S????±???????????????´???
            for v in range(len(self.d)):
                if self.color[v] != Graph.Status.black and self.M[u][v] != float("inf"):
                    # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                    if self.d[u] + self.M[u][v] < self.d[v]:
                        self.d[v] = self.d[u] + self.M[u][v]
                        self.p[v] = u
                        self.color[v] = Graph.Status.gray

    def warshallFloyd(self):
        V = self.num_of_nodes
        for k in range(V):
            for i in range(V):
                for j in range(V):
                    self.M[i][j] = min(self.M[i][j], self.M[i][k] + self.M[k][j])


def main(args):
    n = int(eval(input()))
    network = []
    for _ in range(n):
        network.append([int(x) for x in input().split()])
    packets = []
    p = int(eval(input()))
    for _ in range(p):
        packets.append([int(x) for x in input().split()])
    g = Graph(n, network)
    g.warshallFloyd()
    for s, d, v in packets:
        if g.M[s][d] < v:
            print((g.M[s][d] + 1))
        else:
            print("NA")


if __name__ == "__main__":
    main(sys.argv[1:])

-        num_of_nodes = n + 1 -        self.color = [Graph.Status.white] * num_of_nodes  # ????????????????¨??????¶??? -        self.M = [[float("inf")] * num_of_nodes for _ in range(num_of_nodes)] -        for i in range(num_of_nodes): +        self.num_of_nodes = n + 1 +        self.color = [ +            Graph.Status.white +        ] * self.num_of_nodes  # ????????????????¨??????¶??? +        self.M = [[float("inf")] * self.num_of_nodes for _ in range(self.num_of_nodes)] +        for i in range(self.num_of_nodes): -        self.d = [float("inf")] * num_of_nodes  # ?§???????????????????(?????????) +        self.d = [float("inf")] * self.num_of_nodes  # ?§???????????????????(?????????) -        ] * num_of_nodes  # ????????????????????????????¨????????????????????????? +        ] * self.num_of_nodes  # ????????????????????????????¨????????????????????????? +    def warshallFloyd(self): +        V = self.num_of_nodes +        for k in range(V): +            for i in range(V): +                for j in range(V): +                    self.M[i][j] = min(self.M[i][j], self.M[i][k] + self.M[k][j]) + -    memo = [[0] * (n + 1) for _ in range(n + 1)] -    for i in range(1, n + 1): -        g = Graph(n, network) -        g.dijkstra(i) -        for j in range(1, n + 1): -            if i == j: -                continue -            if g.d[j] == float("inf"): -                memo[i][j] = float("inf") -            else: -                path = [j] -                u = j -                while g.p[u] != i: -                    path.append(g.p[u]) -                    u = g.p[u] -                path.append(i) -                memo[i][j] = len(path) +    g = Graph(n, network) +    g.warshallFloyd() -        if memo[s][d] <= v: -            print((memo[s][d])) +        if g.M[s][d] < v: +            print((g.M[s][d] + 1))