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 <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>
#include <cmath>
#include <string.h>
#include <set>


using namespace std;



int main(){
    int n;
    while(cin>>n){
        int graph[101][101]={0};
        for(int roop=0; roop<n; roop++){
            int r;
            cin>>r;
            int k;
            cin>>k;
            for(int roop2=0; roop2<k; roop2++){
                int t;
                cin>>t;
                graph[r][t]=1;
            }
        }
        
        int q;
        cin>>q;
        for(int roop=0; roop<q; roop++){
            int s,d,v;
            cin>>s>>d>>v;
            int visit[101]={0};
            visit[s]=1;
            queue<pair<int,int>> qu;
            pair<int,int> now;
            now.first=s;
            now.second=v;
            qu.push(now);
            bool find=false;
            
            
            while(!qu.empty()){
                
                now=qu.front();
                //cout<<now.first<<" "<<now.second<<endl;
                qu.pop();
                if(now.first==d){
                    find=true;
                    break;
                }
                if(now.second>1){
                    for(int i=1; i<=n; i++){
                        if(graph[now.first][i]==1&&visit[i]==0){
                            visit[i]=1;
                            pair<int,int> temp;
                            temp.first=i;
                            temp.second=now.second-1;
                            qu.push(temp);
                        }
                    }
                }
            }
            if(find)cout<<v-now.second+1<<endl;
            else cout<<"NA"<<endl;
            
        }
    }
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	int d[101][101];
	const int Inf = 100000000;
	for (int i = 1; i < 101; ++i)
		for (int j = 1; j < 101; ++j)
			d[i][j] = Inf;
	for (int i = 1; i < 101; ++i)
		d[i][i] = 0;

	int n;
	scanf("%d", &n);
	while (n--)
	{
		int no, m;
		scanf("%d %d", &no, &m);
		while (m--)
		{
			int t;
			scanf("%d", &t);
			d[no][t] = 1;
		}
	}
	for (int k = 1; k < 101; ++k)
		for (int i = 1; i < 101; ++i)
			for (int j = 1; j < 101; ++j)
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

	int p;
	scanf("%d", &p);
	while (p--)
	{
		int src, dest, ttl;
		scanf("%d %d %d", &src, &dest, &ttl);
		int r = d[src][dest] + 1;
		if (ttl >= r)
			printf("%d\n", r);
		else
			puts("NA");
	}

	return 0;
}

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

int router[100][100];

int main(void){
  int N;
  cin >> N;
  for(int n=0; n < N; n++){
    int r, k;
    cin >> r >> k;
    for(int i=0; i < k; i++){
      int tmp; cin >> tmp;
      router[r][tmp] = 1;
    }
  }


  int pac_n;
  cin >> pac_n;
  for(int p=0; p < pac_n; p++){
    int start, des, ttl; cin >> start >> des >> ttl;
    
    queue<pair<int, int> > que;   //<id, now_ttl>
    que.push(make_pair(start, ttl));
    bool flg = false;
    while(!que.empty()){
      pair<int, int> q = que.front(); que.pop();
      if(q.first == des){
        cout << ttl - q.second + 1<< endl;
        flg = true;
        break;
      }
      if(q.second != 1){
        for(int i=0; i <= N; i++){
          if(router[q.first][i] == 1){
            que.push(make_pair(i, q.second - 1));
          }
        }
      }
    }
    if(!flg)
      cout << "NA" << 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 num,n1,th;

  cin >> n;



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

    for(int 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;



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


    if(ans(st,ed,c) != -2)cout << ans(st,ed,c) << endl;
    else cout << "NA" << endl;

  }//pack's info

}


int ans(int st,int ed,int c){
  queue<BVB> Nari;
  int 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(int i=1;i<=maxx;i++){
      
      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 <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

vector<int> con[101];

#define INF ((int)1e9)

int bfs(int const S, int const T) {
  queue<pair<int, int> > q;
  int cost[101]; fill(cost, cost+100, INF);
  cost[S] = 0;
  q.push(make_pair(0, S));
  while(!q.empty()) {
    int now = q.front().second;
    int c = q.front().first; q.pop();
    int const size = con[now].size();
    for(int i=0; i<size; i++) {
      if(cost[con[now][i]]<INF) continue;
      cost[con[now][i]] = c + 1;
      //cout <<   "cost: " <<   cost[con[now][i]] << endl;
      q.push(make_pair(c + 1, con[now][i]));
    }
  }
  
  return cost[T];
}

int main() {
  
  int n; cin >> n;
  for(int I = 0; I < n; I++) {
    int r, k;
    cin >> r >> k; r--;
    for(int i=0; i<k; i++) {
      int t; cin >> t; t --;
      con[r].push_back(t);
    }
    // cout << "con["<<r<<"].size() " << con[r].size() << endl;
  }
  
  int m; cin >> m;
  for(int i=0; i<m; i++) {
    int a, b, ttl; cin >> a >> b >> ttl; a--; b--;
    int ans;
    if((ans = bfs(a, b)+1) <= ttl) {
      cout << ans << endl;
    }
    else {
      cout << "NA" << endl;
    }
  }
  
  return 0;
}