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 <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
typedef pair<int,int> P;
const int INF =100000000;
int n;
int d[110][110];
int main()
{
  int r,k;
  while(cin>>n){
    for(int i = 0;i < 110;i++){
      for(int j = 0;j <110;j++){
	d[i][j]= INF;
      }
    }
    for(int i = 1;i <=n;i++){
      cin>>r>>k;
      for(int j = 0;j < k;j++){
	int x;
	cin>>x;
	d[r][x] = 1;
      }
    }
    for(int i = 1;i <=n;i++){
      for(int j = 1;j <=n;j++){
	for(int k = 1;k <=n;k++){
	  d[i][j] =min(d[i][j],d[i][k]+d[k][j]);
	}
      }
    }
    int p;
    cin>>p;
    int s1,d1,v1;
    for(int i = 0;i <p;i++){
      cin>>s1>>d1>>v1;
      if(d[s1][d1]+1<=v1){
	cout <<d[s1][d1]+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];
bool used[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) used[i] = false;

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

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

            if(cur.IND == cur.Z){
                res = vv - cur.V + 1;
                break;
            }
            cur.V--;
            if(!cur.V || used[cur.IND]) continue;
            used[cur.IND] = true;

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

    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++)
			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<stack>
#include<vector>
#include <map>
#include <queue>
#include<string.h>
using namespace std;
typedef pair<int,int> P;
int node[200][200];

int que(int s,int d,int v)
{
  queue<P> qu;
  int c[200];
  memset(c,-1,sizeof(c));
  for(int i=0;i<200;i++)
    {
      if(v-1>0)
	if(node[s][i]==1)
	  qu.push(P(i,v-1));
    }
  while(!qu.empty())
    {
      P a = qu.front();
      qu.pop();
      //      cout<<a.first<<" "<<a.second<<endl;
      if(a.first==d)
	return v-a.second+1;
      for(int i=0;i<200;i++)
	{
	  if(node[a.first][i]==1)
	    {
	      if(a.second-1>0)
		{
		  if(c[i]==-1)
		    {
		      c[i]=a.second-1;
		      qu.push(P(i,a.second-1));	    
		    }
		}
	    }
	} 
    }
      return -1;
}
int main()
{
  int n;
  cin>>n;
  memset(node,-1,sizeof(node));
  for(;n--;)
    {
      int k,t;
      cin>>k>>t;
      for(int i=0;i<t;i++)
	{
	  int tmp;
	  cin>>tmp;
	  node[k][tmp]=1;
	}
    }
  int p;
  cin>>p;
  for(;p--;)
    {
      int s,d,v;
      cin>>s>>d>>v;
      int ans=que(s,d,v);
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
      
    }
  
}