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
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 101;

bool ok[N][N];
int n, res, vv;

void dfs(int, int, int);

int main(void){
    cin >> n;

    rep(i, n){
        int r, k; cin >> r >> k;
        while(k--){
            int t; cin >> t;
            ok[r][t] = true;
        }
    }

    int p; cin >> p;
    while(p--){
        int s, d; cin >> s >> d >> vv;
        res = INF;

        dfs(s, d, vv - 1);
     
        if(res == INF) cout << "NA" << endl;
        else cout << res << endl;
    }

    return 0;
}

void dfs(int cur, int z, int v){
    if(cur == z){
        res = vv - v;
        return;
    }
    if(v == 0) return;

    COUNT(i, n) if(ok[cur][i]) dfs(i, z, v - 1);
}

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

using namespace std;

vector<int> hyou[110];

int hyou2[110];

int bfs(int s, int d) {
	queue<int> q;
	q.push(s);
	int k;
	hyou2[s] = 0;
	while (!q.empty()) {
		k = q.front();
		q.pop();
		for (int i = 0; i < hyou[k].size(); i++) {
			int t = hyou[k][i];
			if (hyou2[t] != -1) {
			} else {
				hyou2[t] = hyou2[k]+1;
				if (t == d) {
					return hyou2[t];
				}
				q.push(t);
			}
		}
	}
	return 0;
}

int main() {
	int n;
	cin >> n;
	int a, b, c;
	for (int i = 0; i < n; i++) {
		cin >> a >> b;
		for (int j = 0; j < b; j++) {
			cin >> c;
			hyou[a].push_back(c);
		}
	}
	int k;
	int p, q, r;
	cin >> k;
	for (int i = 0; i < k; i++) {
		cin >> p >> q >> r;
		for (int j = 0; j <= n; j++) {
			hyou2[j] = -1;
		}
		int g = bfs(p, q);
		if (g != 0 && r > g) {
			cout << g+1 << endl;
		} else {
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second
#define IND first
#define Z second.first
#define V second.second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 101;

bool ok[N][N];
int n, res, vv;

int main(void){
    cin >> n;

    rep(i, n){
        int r, k; cin >> r >> k;
        while(k--){
            int t; cin >> t;
            ok[r][t] = true;
        }
    }

    int p; cin >> p;
    while(p--){
        int s, d; cin >> s >> d >> vv;
        res = INF;

        queue< pair<int, pii> > q;
        q.push(mp(s, mp(d, vv - 1)));

        while(!q.empty()){
            pair<int, pii> cur = q.front();
            q.pop();

            if(cur.IND == cur.Z){
                res = vv - cur.V;
                break;
            }
            if(!cur.V) continue;

            COUNT(i, N)
                if(ok[cur.IND][i]){
                    pair<int, pii> next = mp(i, mp(cur.Z, cur.V - 1));
                    q.push(next);
                }
        }
     
        if(res == INF) cout << "NA" << endl;
        else cout << res << endl;
    }

    return 0;
}

C++: #include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N_MAX = 100;

class Node {
public:
	int pos, ttl;
	Node(int p, int t) : pos(p), ttl(t) {}
};

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];
bool visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
	queue<Node> que;
	fill(visited, visited + n + 1, false);
	que.push(Node(src, limit - 1));
	while (!que.empty()) {
		Node current = que.front();
		que.pop();
		if (current.pos == dst) {
			cout << limit - current.ttl << endl;
			return;
		}
		if (visited[current.pos]) continue;
		visited[current.pos] = true;
		if (current.ttl == 0) continue;
		for (int next = 1; next <= n; next++) {
			if (adj[current.pos][next] && !visited[next]) {
				que.push(Node(next, current.ttl - 1));
			}
		}
	}
	cout << "NA" << endl;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int r, k;
		cin >> r >> k;
		fill(adj[r], adj[r] + n + 1, false);
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			adj[r][t] = true;
		}
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		bfs(s, d, v);
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++)
#define MAX 100
#define INF (1<<21)
int n, L[MAX][MAX],S[MAX];

int bfs( int s, int t ){
    queue<int> q; q.push(s);
    int u, v, d[MAX];
    rep(i, n) d[i] = INF;
    d[s] = 1;
    while( !q.empty() ){
	u = q.front(); q.pop();
	rep(i, S[u]){
	    v = L[u][i];
	    if ( d[v] == INF ){
		d[v] = d[u] + 1;
		q.push(v);
	    }
	}
    }
    return d[t];
}

main(){
    int s, t, tl, q, c;
    cin >> n;
    rep(i, n){
	cin >> s; s--; cin >> S[s];
	rep(j, S[s]){
	    cin >> L[s][j]; L[s][j]--;
	}
    }
    cin >> q;
    rep(i, q){
	cin >> s >> t >> tl; s--; t--;
	c = bfs(s, t);
	if ( c <= tl ) cout << c << endl;
	else cout << "NA" << endl;
    }
    
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
  int n,a,b,c,d,k,DATA[101][101];
  for(int i=0;i<=101;i++){
    for(int j=0;j<=101;j++){
      DATA[i][j]=10000000;
    }
  }

  cin >> n;
  for(int i=0;i<n;i++){
    cin >> a >> k;
    for(int j=0;j<k;j++){
      cin >> b;
      DATA[a][b]=1;
    }
  }

  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      for(k=1;k<=n;k++){
	if(DATA[j][k]>DATA[j][i]+DATA[i][k]){
	  DATA[j][k] = DATA[j][i]+DATA[i][k];
	}
      }
    }
  }

  cin >> n;
  for(int i=0;i<n;i++){
    cin >> a >> b >> c;
    if(DATA[a][b]>=c) cout << "NA" << endl;
    else cout << DATA[a][b]+1 << endl;
  }
  return 0;
}