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 <list>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
// ------ Class ------ //
class Graph1 {
public:
	// ------ Variables ------ //
	static const int INF = 1000000000;
	int V; vector<vector<int> > G;

	// ------ Constructors ------ //
	Graph1() : V(0), G(vector<vector<int> >()) {}
	Graph1(int v) : V(v), G(vector<vector<int> >(v)) {}
	Graph1(vector<vector<int> > g) : V(g.size()), G(g) {}

	// ------ Basic Functions ------ //
	void add1(int v1, int v2) { G[v1].push_back(v2); }
	void add2(int v1, int v2) { add1(v1, v2); add1(v2, v1); }

	// ------ Algorithms ------ //
	vector<int> dist(int s) {
		queue<int> que; que.push(s);
		vector<int> d(V, INF); d[s] = 0;
		while (!que.empty()) {
			int u = que.front(); que.pop();
			for (int i = 0; i < G[u].size(); i++) {
				if (d[G[u][i]] == INF) {
					d[G[u][i]] = d[u] + 1;
					que.push(G[u][i]);
				}
			}
		}
		return d;
	}
};
int N, Q, c, u, v; Graph1 G;
int main() {
	cin >> N; G = Graph1(N);
	for (int i = 0; i < N; i++) {
		cin >> u >> c;
		for (int j = 0; j < c; j++) {
			cin >> v; G.add1(u - 1, v - 1);
		}
	}
	cin >> Q;
	for (int i = 0; i < N; i++) {
		cin >> u >> v >> c;
		int d = G.dist(u - 1)[v - 1] + 1;
		if (d > c) cout << "NA" << endl;
		else cout << d << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <climits>
using namespace std;

int main() {
  int n, m, t, u, v;
  int a[100][100];
  cin >> n;
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      a[i][j] = -1;
    }
    a[i][i] = 0;
  }
  for (int i=0; i<n; i++) {
    cin >> t >> u;
    --t;
    for (int j=0; j<u; j++) {
      cin >> v;
      --v;
      a[t][v] = 1;
    }
  }
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      for (int k=0; k<n; k++) {
        if (a[j][i] == -1 || a[i][k] == -1) continue;
        if (a[j][k] == -1) {
          a[j][k] = a[j][i] + a[i][k];
        } else if (a[j][k] > a[j][i] + a[i][k]) {
          a[j][k] = a[j][i] + a[i][k];
        }
      }
    }
  }
  cin >> m;
  for (int i=0; i<m; i++) {
    cin >> t >> u >> v;
    --t;
    --u;
    if (a[t][u] < v) {
      cout << a[t][u]+1 << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

constexpr int INF = 1e9;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> d(n, vector<int>(n, INF));
    for(int i=0; i<n; ++i) {
        d[i][i] = 0;

        int r, k;
        cin >> r >> k;
        for(int j=0; j<k; ++j) {
            int t;
            cin >> t;
            d[r-1][t-1] = 1;
        }
    }
    for(int k=0; k<n; ++k) {
        for(int i=0; i<n; ++i) {
            for(int j=0; j<n; ++j) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
    int p;
    cin >> p;
    for(int i=0; i<p; ++i) {
        int s, g, v;
        cin >> s >> g >> v;
        if(d[s-1][g-1] + 1 <= v) {
            cout << d[s-1][g-1] + 1 << endl;
        } else {
            cout << "NA" << endl;
        }
    }
}

C++: #include<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF=1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const int N=101;

int net[N][N];

int bfs(int n,int s,int d){
  vector<int> time(n);
  queue<int> que;
  rep(i,n)time[i]=INF;
  time[s]=0;
  que.push(s);
  int u;
  while(!que.empty()){
    u=que.front(),que.pop();
    rep(i,n){
      if(time[i]>time[u]+net[u][i]){
        time[i]=time[u]+net[u][i];
        que.push(i);
      }
    }
  }
  return time[d];
}

signed main(){
  int n;
  cin>>n;
  rep(i,n)rep(j,n)net[i][j]=INF;
  rep(i,n){
    int r,k;
    cin>>r>>k;
    r--;
    rep(i,k){
      int t;
      cin>>t;
      t--;
      net[r][t]=1;
    }
  }

  int p;
  cin>>p;
  rep(i,p){
    int s,d,v;
    cin>>s>>d>>v;
    s--;d--;
    int ans=bfs(n,s,d)+1;

    if(ans<=v)cout<<ans<<endl;
    else cout<<"NA"<<endl;
  }
 return 0;
}

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

int md = 10000000;
vector <int> ro[101];
int s,d,v;
int s2,d2;
int kk[101],n;
bool flg[102] ;
int dmap[102][102];

void reset() {

  for(int i=0;i<=n;i++) {
    for(int j=0;j<=n;j++) {
	dmap[i][j] = 10000000;
    }
  }
}

void visit(int sn,int dis) {
  int x;
  
  flg[sn+1] = 1;
  if(sn == d && dis <=100)  dmap[s][d] = min(dmap[s][d],dis);

  else if(dis < dmap[s][d]) {
    for(int i=0;i<ro[sn].size();i++) {
      
      if(flg[ro[sn][i]] == 0){
		cout <<sn+1 << "to"<<ro[sn][i] << endl;
	visit(ro[sn][i]-1,dis+1);
	flg[ro[sn][i]] = 0;
      }
    } 
  }
}
  
int main() {

  cin >> n;
  int a;
    
  for(int i=0;i<n;i++) {
    cin >> kk[i];
    cin >> a;
    ro[i].resize(a);
    for(int j=0;j<a;j++) cin >> ro[kk[i]-1][j];
  }

  reset();
  for(int k=0;k<=n;k++) flg[k] = 0;
  for(int i=0;i<n;i++) {
    for(int j=1;j<=n;j++) {
      cout <<i+1<<"kara"<<j+1<<endl;
      cout <<"-------------------------" <<endl;      
      d = j;
      s = i;
      if(i != j) visit(i,1);
      cout << dmap[i][j] << endl;
      
      for(int i=1;i<=n;i++)cout << flg[i];
      cout <<" ";
   
      
    }
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;

    if(dmap[s-1][d-1] <= v) 
      cout << dmap[s-1][d-1] << endl;
    else cout << "NA" << endl;
  
  }
  
  return 0;
  
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int main(){
  int a,n,m,x,s,d,v,map[200][200][1]={};
  queue <int> A;
  cin>>n;
  for(int i=1;i<=n;i++){
    cin>>a;
    cin>>map[i][0][0];
    for(int j=1;j<=map[i][0][0];j++){
      cin>>map[i][j][0];
    }
  }
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>s>>d>>v;
    A.push(s);
    while(1){
      x=A.front();
      if(x==d)break;
      A.pop();
      for(int j=1;j<=map[x][0][0];j++){
        if(map[map[x][j][0]][0][1]==0){
        A.push(map[x][j][0]);
        map[map[x][j][0]][0][1]=map[x][0][1]+1;
        }
      }
    }
    if(v>map[x][0][1])printf("%d\n",map[x][0][1]);
    else printf("NA\n");
  }
  return 0;
}