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: 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, 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")

C++: #include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main()
{
	int n;
	int d[101][101];
	REP(i, 101)REP(j, 101) d[i][j] = (i == j ? 0 : INF);
	cin >> n;
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		REP(j, b)
		{
			int k;
			cin >> k;
			k--;
			d[i][k] = 1;
		}
	}
	REP(k, 101)REP(i, 101)REP(j, 101) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	int q;
	cin >> q;
	while (q--)
	{
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if (d[a][b] < c) cout << d[a][b]+1 << endl;
		else puts("NA");
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int dp[110][110];
int n;
void WF(){
	lol(k,n){
		lol(i,n){
			lol(j,n){
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
			}
		}
	}
}
int main(){
	cin>>n;
	lol(i,n)lol(j,n)dp[i][j]=mod;
	lol(i,n){
		int r,k;cin>>r>>k;r--;
		while(k--){
			int a;cin>>a;a--;
			dp[r][a]=1;
		}
	}
	WF();
	int q;cin>>q;
	while(q--){
		int a,b,c;
		cin>>a>>b>>c;a--,b--;
		int tmp=dp[a][b]+1;
		if(tmp<=c)cout<<tmp<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

#define INF 1e9

int main() {
	int n; cin >> n;
	int m[100][100]; fill(&m[0][0], &m[0][0]+100*100, INF);
	for (int i = 0; i < n; ++i) m[i][i] = 0;
	for (int i = 0; i < n; ++i) {
		int r, k; cin >> r >> k; --r;
		for (int j = 0; j < k; ++j) {
			int t; cin >> t; --t;
			m[r][t] = 1;
		}
	}
	for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
		m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
	}
	int p; cin >> p;
	for (int k = 0; k < p; ++k) {
		int s, t; cin >> s >> t; --s, --t;
		int th; cin >> th;
		if (m[s][t] < th) cout << m[s][t]+1 << endl;
		else cout << "NA" << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n;
  int r,k,t;
  int p;
  int s,d,b;
  int ans=0;
  int flag[111][111];
  int box[111][111];
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      box[i][j]=9999;
      flag[i][j]=0;
    }
  }
  for(int i=0;i<n;i++){
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> t;
      box[r-1][t-1]=1;
    }
  }
  for(int z=0;z<n;z++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	box[i][j]=min(box[i][j],box[i][z]+box[z][j]);
      }
    }
  }
  cin >> p;
  for(int i=0;i<p;i++){
    cin >> s >> d >> b;
    ans=box[s-1][d-1]+1;
    if(ans<=b){
      cout << box[s-1][d-1]+1 << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back 

int bfs(int s, int t, int c);

vector<vector<int> > g(101);

int main()
{
    int n;
    cin >> n;
    loop(i, n) {
        int r, k, t;
        cin >> r >> k;
        loop(j, k) {
            cin >> t;
            g[r].pb(t);
        }
    }
    int p;
    cin >> p;
    loop (i, p) {
        int s, d, v;
        cin >> s >> d >> v;
        int ans = bfs(s, d, v);
        if (ans != -1) {
            cout << v-ans << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

int bfs(int s, int t, int c)
{
    vector<bool> check(101, false);
    queue<int> fq, nq, sq;
    fq.push(s);
    while (1) {
        c--;
        while (!fq.empty()) {
            int pos = fq.front(); fq.pop();
            if (check[pos]) {
                continue;
            } else {
                check[pos] = true;
            }
            //cout << pos << endl;
            if (pos == t) {
                return c;
            } else {
                for (int i: g[pos]) {
                    nq.push(i);
                }
            }
        }
        if (c == 0 || nq.empty()) {
            return -1;
        }
        fq = nq;
        nq = sq;
    }
}