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 <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

const int INF = 1 << 29;

int main()
{
  int g[128][128];
  rep(i, 128) rep(j, 128) g[i][j] = INF;
  rep(i, 128) g[i][i] = 0;
  int n = in();
  rep(i, n){
    int f = in() - 1;
    rep(j, in()){
      g[f][in() - 1] = 1;
    }
  }
  rep(k, n){
    rep(i, n){
      rep(j, n){
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  rep(p, in()){
    int f, t, v;
    f = in() - 1;
    t = in() - 1;
    v = in();
    if (g[f][t] + 1 <= v) printf("%d\n", g[f][t] + 1);
    else puts("NA");
  }
  return 0;
}

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

using namespace std;
typedef pair<int,int> PII;
typedef long long ll;

int main(){
  int n;
	cin>>n;
	int graph[n][n];
	for(int j=0;j<n;j++)
		for(int i=0;i<n;i++){
			if(i==j)
				graph[j][i] = 1;
			else
				graph[j][i] = 1000;
		}
	for(int i=0;i<n;i++){
		int k,m,l;
		cin>>k>>m;
		for(int j=0;j<m;j++){
			cin>>l;
			graph[k-1][l-1] = 1;
		}
	}
	
	for(int j = 0;j<n;j++)
		for(int i = 0;i<n;i++)
			for(int k=0;k<n;k++)
				if(graph[i][j]>graph[i][k]+graph[k][j])
					graph[i][j] = graph[i][k]+graph[k][j];
	int p;
	cin>>p;
	for(int i=0;i<p;i++){
		int s,d,v;
		cin>>s>>d>>v;
		if(graph[s-1][d-1]<v)
			cout<<graph[s-1][d-1]+1<<endl;
		else
			cout<<"NA"<<endl;
	}
  return 0;
}

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

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


void visit(int sn,int dis) {
  //  for(int i=1;i<=n;i++)cout << flg[i];
  // cout <<" ";
  
  if(sn == d-1 && dis <=v)  md = min(md,dis);
  else if(dis < v && dis < md) {
    for(int i=0;i<ro[sn].size();i++) {
      if(flg[ro[sn][i]] == 0){
	//	cout <<sn+1 << "to"<<ro[sn][i] << endl;
	flg[ro[sn][i]] = 1;
	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;
    cin >> a;
    ro[i].resize(a);
    for(int j=0;j<a;j++) cin >> ro[kk-1][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;
    
    for(int j=0;j<=n;j++) flg[j] = 0;
    md = 10000000;
    flg[s]  = 1;   
    visit(s-1,1);
    if(md != 10000000) cout << md <<endl;
    else cout << "NA" <<endl;
  }
  
  return 0;
  
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

struct Packet{
	Packet(){}

	Packet(int arg_ttl,int loc){
		ttl = arg_ttl;
		went_num = 1;
		for(int i=0; i < 101; i++)checkTable[i] = 0;
		checkTable[loc] = 1;
		current = loc;
	}

	void set(int loc){
		checkTable[loc] = 1;
		current = loc;
		went_num++;
	}

	int ttl,checkTable[101],went_num,current;
};

int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit,memo[101][101];
	Packet tmp;

	for(int i=1; i <= n; i++){
		for(int k=1; k <= n; k++){
			table[i][k] = 0;
			memo[i][k] = 0;
		}
	}

	scanf("%d",&n);

	for(int i = 0; i < n; i++){
		scanf("%d %d",&r,&k);
		for(int p=0;p < k; p++){
			scanf("%d",&to);
			table[r][to] = 1;
		}
	}

	scanf("%d",&num_of_packet);

	queue<Packet> Q;
	bool FLG;

	for(int i=0; i < num_of_packet; i++){
		scanf("%d %d %d",&start,&goal,&t_limit);

		FLG = false;

		if(memo[start][goal] != 0){
			if(memo[start][goal] <= t_limit){
				printf("%d\n",memo[start][goal]);
			}else{
				printf("NA\n");
			}
			continue;
		}

		for(int i = 1; i <= n; i++){
			if(i != start && table[start][i] == 1){
				if(memo[i][goal] > 0 && memo[i][goal] < t_limit){
					printf("%d\n",memo[i][goal]+1);
					memo[start][goal] = memo[i][goal]+1;
					FLG = true;
					break;
				}
				Packet p(t_limit-1,start);
				p.set(i);
				Q.push(p);
			}
		}



		while(!Q.empty() && FLG == false){
			tmp = Q.front();
			Q.pop();

			if(tmp.current == goal){
				printf("%d\n",t_limit - tmp.ttl+1);
				FLG = true;

				while(!Q.empty())Q.pop();

				memo[start][goal] = t_limit - tmp.ttl+1;

				break;
			}

			if(tmp.ttl > 0 && tmp.went_num < n){
				for(int i=1; i <= n; i++){
					if(i != tmp.current && tmp.checkTable[i] == 0 && table[tmp.current][i] == 1){
						if(memo[i][goal] > 0 && memo[i][goal] < tmp.ttl){
							printf("%d\n",t_limit-tmp.ttl+memo[i][goal]+1);
							memo[start][goal] = t_limit-tmp.ttl+memo[i][goal]+1;
							FLG = true;
							while(!Q.empty())Q.pop();
							break;
						}

						Packet p = tmp;
						p.ttl--;
						p.set(i);
						Q.push(p);
					}
				}
			}
		}

		if(!FLG){
			printf("NA\n");
		}

	}

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define INF (1<<24)
int n;
int r[101][101];
main(){
  scanf("%d",&n);
  for(int i=0;i<101;i++)
    for(int j=0;j<101;j++) r[i][j]=INF;
  for(int i=0;i<n;i++){
    int rr,k;
    scanf("%d %d",&rr,&k);
    for(int j=0;j<k;j++){
      int t;
      scanf("%d",&t);
      r[rr][t]=1;
    }
  }
  for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
      for(int l=1;l<=n;l++)
	r[j][l]=min(r[j][l],r[j][i]+r[i][l]);
  int p;
  scanf("%d",&p);
  for(int i=0;i<p;i++){
    int s,d,v;
    scanf("%d %d %d",&s,&d,&v);
    int res=r[s][d]+1;
    if(res>v) printf("NA\n");
    else printf("%d\n",res);
  }
}