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<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_NODE = 100;

vector<int> makeInput(string input){
    vector<int> ret;
    stringstream ss(input);

    while(ss >>input) ret.push_back(atoi(input.c_str()));
    return ret;
}

int main(){
    int n;
    while(cin >>n && n){
        int d[MAX_NODE][MAX_NODE];
        REP(i, MAX_NODE) REP(j, MAX_NODE) d[i][j] = INF;
        REP(i, MAX_NODE) d[i][i] = 0;
        vector< vector<int> > graph(MAX_NODE, vector<int>(MAX_NODE));
        string dust_str;
        getline(cin, dust_str);
        //make graph
        REP(i, n){
            string input;
            getline(cin, input);
            vector<int> sum_list = makeInput(input);

            //make
            REP(j, sum_list[1]){
                graph[sum_list[0]].push_back(sum_list[j + 2]);
                d[sum_list[0]][sum_list[j + 2]] = 1;
            }
        }

        //ワーシャルフロイド
        int v = n + 1;
        REP(k, v){
            REP(i, v){
                REP(j, v){
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }

        //cal ans
        int m;
        cin >>m;
        getline(cin, dust_str);
        REP(i, m){
            string input;
            getline(cin, input);
            vector<int> sum_list = makeInput(input);
            int from = sum_list[0], to = sum_list[1], ttl = sum_list[2];
            if(ttl < (d[from][to] + 1)) cout <<"NA" <<endl;
            else cout <<d[from][to] + 1 <<endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n;
  int r,k,t;
  int p;
  int s,d,b;
  int ans=0;
  int box[111][111];
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      box[i][j]=(1<<21);
    }
  }
  for(int i=0;i<n;i++){
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> t;
      box[r-1][t-1]=1;
    }
  }
  for(int z=0;z<n;z++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	box[i][j]=min(box[i][j],box[i][z]+box[z][j]);
      }
    }
  }
  cin >> p;
  for(int i=0;i<p;i++){
    cin >> s >> d >> b;
    ans=box[s-1][d-1]+1;
    if(ans<=b){
      cout << box[s-1][d-1]+1 << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> ro[101];
int s,d,v;
int s2,d2;
int kk,n,r,md;
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;
    
  for(int i=0;i<n;i++) {
    cin >> r >> kk;
    ro[i].resize(kk);
    for(int j=0;j<kk;j++) cin >> ro[r-1][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;
    
    for(int j=1;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 <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

const int INF = (int)1e9;
typedef pair<int, int> P;
struct Edge {
    int to, cost;
    Edge(int t, int c) : to(t), cost(c) { }
};

int dijkstra(const vector<vector<Edge> > &edges, const int from, const int to) {
    vector<int> dist(edges.size(), INF);
    priority_queue<P, vector<P>, greater<P> > Q;
    Q.push(P(1, from));
    dist[from] = 1;
    while(!Q.empty()) {
        P p = Q.top(); Q.pop();
        int v = p.second;
        int d = p.first;
        if(v == to) break;
        if(d > dist[v]) continue;
        for(int i = 0; i < (int)edges[v].size(); i++) {
            Edge e = edges[v][i];
            int c = dist[v] + e.cost;
            if(dist[e.to] > c) {
                dist[e.to] = c;
                Q.push(P(c, e.to));
            }
        }
    }
    return dist[to];
}

int main() {
    int n; cin >> n;
    vector<vector<Edge> > edges(n + 1);
    for(int i = 0; i < n; i++) {
        int r, k; cin >> r >> k;
        for(int j = 0; j < k; j++) {
            int t; cin >> t;
            edges[r].push_back(Edge(t, 1));
        }
    }
    int p; cin >> p;
    for(int i = 0; i < p; i++) {
        int from, to, ttl;
        cin >> from >> to >> ttl;
        int d = dijkstra(edges, from, to);
        if(d > ttl) {
            cout << "NA" << endl;
        }
        else {
            cout << d << endl;
        }
    }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n;
  int r,k,t;
  int p;
  int s,d,b;
  int ans=0;
  int flag[111][111];
  int box[111][111];
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      box[i][j]=9999;
      flag[i][j]=0;
    }
  }
  for(int i=0;i<n;i++){
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> t;
      box[r-1][t-1]=1;
    }
  }
  for(int z=0;z<n;z++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	box[i][j]=min(box[i][j],box[i][z]+box[z][j]);
      }
    }
  }
  cin >> p;
  for(int i=0;i<p;i++){
    cin >> s >> d >> b;
    ans=box[s-1][d-1]+1;
    if(b-ans>=0){
      cout << box[s-1][d-1]+1 << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}