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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

int cost[100][100];
int n;
void wf(){
  rep(k,n)rep(i,n)rep(j,n)cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
}

main(){
  cin>>n;
  rep(i,n)rep(j,n)cost[i][j]=2<<24;

  rep(i,n){
    int r;
    cin>>r;--r;
    int k;
    cin>>k;
    rep(j,k){
      int t;
      cin>>t;t--;
      cost[r][t]=1;
    }
  }
  wf();
  int p;
  cin>>p;
  rep(i,p){
    int s,d,v;
    cin>>s>>d>>v;--s,--d;
    int c=cost[s][d];
    if(c<v)cout<<c+1<<endl;
    else cout<<"NA"<<endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int inf = 1 << 28;
int n, m;
int d[102][102];

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      d[i][j] = inf;
    }
  }
  for (int i = 0; i < n; ++i) {
    int r, k, t;
    cin >> r >> k;
    r--;
    for (int j = 0; j < k; ++j) {
      cin >> t;
      t--;
      d[r][t] = 1;
    }
  }
  
  for (int i = 0; i < n; ++i) d[i][i] = 0;
  
  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]);
      }
    }
  }

  cin >> m;
  for (int i = 0; i < m; ++i) {
    int s, de, v;
    cin >> s >> de >> v;
    s--; de--;
    printf("(%d,%d) ", s, de);
    if (d[s][de]+1 <= v) {
      cout << d[s][de]+1 << endl;
    } else {
      //cout << d[s][de] << endl;
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

const int INF = 1e7;

int n;
int d[101][101];

void init(int n){
    rep(i, 0, n){
        rep(j, 0, n){
            d[i][j] = INF;
            if(i == j) d[i][j] = 0;
        }
    }
}

void warshall_floyd(){
    rep(k, 0, n){
        rep(i, 0, n){
            rep(j, 0, n){
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

signed main(){
    cin >> n;
    init(n);
    int r, k, t;
    rep(i, 0 ,n){
        cin >> r >> k;
        rep(j, 0, k){
            cin >> t;
            d[r - 1][t - 1] = 1;
        }
    }
    warshall_floyd();
    int fr, to, v, p;
    cin >> p;
    rep(i, 0, p){
        cin >> fr >> to >> v;
        if(d[fr - 1][to - 1] < v){
            cout << d[fr - 1][to - 1] + 1 << endl;
        }else{
            cout << "NA" << endl;
        }
    }
}