Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const INF = 1<<31

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	arr := make([][]int, n)
	for i := range arr {
		arr[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			arr[i][j] = INF
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		x, k := input[0]-1, input[1]
		for j := 0; j < k; j++ {
			y := input[j+2]-1
			arr[x][y] = 1
		}
	}
	for k := 0; k < n; k++ {
		arr[k][k] = 0
		for i := 0; i < n; i++ {
			if arr[i][k] >= INF {
				continue
			}
			for j := 0; j < n; j++ {
				if arr[k][j] >= INF {
					continue
				}
				arr[i][j] = min(arr[i][j], arr[i][k]+arr[k][j])
			}
		}
	}
	scanner.Scan()
	p, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < p; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		s, d, v := input[0]-1, input[1]-1, input[2]
		if arr[s][d] < v {
			fmt.Println(arr[s][d]+1)
		} else {
			fmt.Println("NA")
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

Python: # -*- 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)]
        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(input())
    network = []
    for _ in range(n):
        network.append([int(x) for x in input().split()])

    packets = []
    p = int(input())
    for _ in range(p):
        packets.append([int(x) for x in input().split()])

    for s, d, v in packets:
        g = Graph(n, network)
        g.dijkstra(s)
        path = [d]
        u = d
        while g.p[u] != s:
            path.append(g.p[u])
            u = g.p[u]
        path.append(s)

        if len(path) > v:
            print('NA')
        else:
            print(len(path))



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

Python: n=int(raw_input())
g=[[1000000]*n for _ in xrange(n)]
for i in xrange(n):
    g[i][i]=0
for i in xrange(n):
    r=map(int,raw_input().split())
    for j in r[2:]:
        g[r[0]-1][j-1]=1
for k in xrange(n):
    for i in xrange(n):
        for j in xrange(n):
            g[i][j]=min(g[i][j],g[i][k]+g[k][j])
m=int(raw_input())
for i in xrange(m):
    s,d,v=map(int,raw_input().split())
    s-=1
    d-=1
    if g[s][d]<v:
        print(g[s][d]+1)
    else:
        print('NA')

Python: from collections import deque
n = int(input())
rlst = [None] * (n + 1)
#print(rlst)
for _ in range(n):
  lst = list(map(int, input().split()))
  r = lst[0]
  print(r)
  lst = lst[2:]
  rlst[r] = lst

p = int(input())

for _ in range(p):
  s, d, v = map(int, input().split())
  visited = [False] * (n + 1)
  visited[s] = True
  que = deque()
  que.append((s, 1))
  while que:
    node, dist = que.pop()
    if node == d:
      print(dist)
      break
    if dist < v:
      for to in rlst[node]:
        if not visited[to]:
          que.append((to, dist + 1))
  else:
    print("NA")

Python: from functools import lru_cache

n=int(input())
router={}
for _ in range(n):
    t=[int(i) for i in input().split(" ")]
    r=t[0]
    k=t[1]
    t=t[2::]
    router[r]=t

def transport(s,g):
    distance=[10**10 for i in range(n+1)]
    distance[s]=0
    next=[s]
    for i in range(1,n+1):
        _next=[]
        for j in next:
            _next+=router[j]
        next=list(set(_next))
        for j in next:
            distance[j]=min(i,distance[j])
    return distance[g]+1



p=int(input())
for _ in range(p):
    s,d,v=[int(i) for i in input().split(" ")]
    t=transport(s,d)
    if t<=v:
        print(t)
    else:
        print("NA")

Python: from collections import deque
n=input()
r=[map(int,raw_input().split()) for _ in xrange(n)]
m=input()
pa=[map(int,raw_input().split()) for _ in xrange(m)]
for i in xrange(m):
    d=[1]*n
    q=deque([pa[i][0]])
    used=[False]*n
    used[pa[i][0]-1]=True
    while len(q)!=0:
        f=0
        p=q.popleft()
        p-=1
        if d[p]==pa[i][2]:continue
        for j in r[p][2:]:
            j-=1
            if pa[i][1]-1==j:
                f=1
                d[j]+=d[p]
                break
            if used[r[j][0]-1]:continue
            q.append(j+1)
            d[j]=d[p]+1
            used[j]=True
        if f==1:
            break
    if f==1:
        print(d[j])
    else:
        print("NA")

Python: from collections import deque

n = int(input())
rlst = [None] * (n + 1)
for _ in range(n):
  lst = list(map(int, input().split()))
  r = lst[0]
  lst = lst[2:]
  rlst[r] = lst

p = int(input())

for _ in range(p):
  s, d, v = map(int, input().split())
  visited = [False] * (n + 1)
  visited[s] = True
  que = deque()
  que.append((s, 0))
  while que:
    node, dist = que.popleft()
    if node == d:
      if dist < v:
        print(dist + 1)
      else:
        print("NA")
      break
    for to in rlst[node]:
      if not visited[to]:
        que.append((to, dist + 1))
        visited[to] = True
  else:
    print("NA")