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 <queue>
#include <algorithm>
using namespace std;

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

void visit(int sn,int dis) {
  int x;
  
  flg[sn+1] = 1;
  
  if(sn == d-1 && dis <=v)  md = min(md,dis);

  else if(dis <= v) {
    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[i][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;


    for(int i=0;i<=n;i++) flg[i] = 0;
    md = 10000000;
   
    visit(s-1,1);
    if(md != 10000000) cout << md <<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];
bool visited[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;
        COUNT(i, N) visited[i] = false;

        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 || visited[cur.IND]) continue;
            visited[cur.IND] = true;

            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<iostream>
#include<queue>
using namespace std;
#define SUB 103

int net[SUB][SUB],n,maxx=0;

typedef pair<int,int> BVB;


int ans(int,int,int);

int main(){

  int i,j,p,num,n1,th;

  cin >> n;



  for(i=0;i<n;i++){ //net's info
    
    cin >> num >> n1;
    if(num >= maxx)maxx = num;

    for(j=0;j<n1;j++){
      cin >> th;
      net[num][th]=1;
    }

  }//net's info
  
  for(int m=0;m<maxx;m++){
    for(int n=0;n<maxx;n++){
      if(net[m][n]!=1)net[m][n]=0;
    }
  }  

  int pkn;

  cin >> pkn;

  int st,ed,c,answer;



  for(i=0;i<pkn;i++){//pack's info
    cin >> st >> ed >> c;


    answer = ans(st,ed,c);

    if(answer != -2)cout << answer << endl;
    else cout << "NA" << endl;

  }//pack's info

}


int ans(int st,int ed,int c){
  queue<BVB> Nari;
  int i,j,k,l,cal=0,mem=0,yes=10001;
  BVB next,fr;

  fr.first=st;
  fr.second=c;
  Nari.push(fr);
  if(st == ed && c != 0)return 1;


  while(1){
    next=Nari.front();
   
    Nari.pop();
    next.second=next.second-1;

    if(fr.second == (next.second+1) && next.second == 0 && next.first != ed)return -2;

    if(next.second < 0)break; 
    
    for(i=1;i<=maxx;i++){
      if(net[next.first][i] == 1)mem++;
      
      if(net[next.first][i] == 1 && i == ed ){ //ツ姪堋的ツ地ツ督楪達

	if(yes >  (c-next.second+1)){
	  yes = (c - next.second)+1;

	}
	
      }
            
      if(net[next.first][i] == 1){
	Nari.push(BVB(i,next.second));

      }
      
    }
       
  }
  if(yes != 10001){return yes;}
  else return -2; 
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int d[100][100];
    int n;
    scanf("%d",&n);
    fill(*d,*d+100*100,1<<25);
    for(int i=0;i<n;i++)d[i][i]=0;
    for(int i=0;i<n;i++){
        int r,k;
        scanf("%d%d",&r,&k);
        r--;
        for(int j=0;j<k;j++){
            int t;
            scanf("%d",&t);
            t--;
            d[r][t]=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 m;
    scanf("%d",&m);
    while(m--){
        int s,g,t;
        scanf("%d%d%d",&s,&g,&t);
        s--;g--;
        int val=d[s][g];
        if(t<=val)puts("NA");
        else printf("%d\n",val+1);
    }
    return 0;

}

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);
		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() {
	scanf("%d", &N); G = Graph1(N);
	for (int i = 0; i < N; i++) {
		scanf("%d%d", &u, &c);
		for (int j = 0; j < c; j++) {
			scanf("%d", &v); G.add1(u - 1, v - 1);
		}
	}
	scanf("%d", &Q);
	for (int i = 0; i < N; i++) {
		scanf("%d%d%d", &u, &v, &c);
		int d = G.dist(u - 1)[v - 1] + 1;
		if (d > c) printf("NA\n");
		else printf("%d\n", d);
	}
}