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<map>
#include<vector>
#include<queue>
#include<set>
using namespace std;

int main(){
    int n, r, k, t;
    cin >> n;

    map<int, vector<int>> path;
    for(int i = 0; i < n; i++){
        cin >> r >> k;
        for(int j = 0; j < k; j++){
            cin >> t;
            path[r].push_back(t);
        }
    }

    int p, s, d, v;
    cin >> p;
    for(int i = 0; i < p; i++){
        cin >> s >> d >> v;
        queue<pair<int, int>> q;
        set<int> appear;

        q.push({s,1});

        int ans = -1;
        while(!q.empty()){
            pair<int,int> p = q.front();    q.pop();
            if(appear.find(p.first) != appear.end())   continue;
            
            if(p.first == d){
                ans = p.second;
                break;
            }

            appear.insert(p.first);

            for(int j = 0; j < path[p.first].size(); j++){
                if(appear.find(path[p.first][j]) == appear.end()){
                    q.push({path[p.first][j], p.second+1});
                }
            }
        }

        if(ans == -1 || ans > v){
            cout << "NA" << endl;
        }else{
            cout << ans << 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]=INT_MAX;
    }
  }
  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 <string>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> dist(n+1, vector<int>(n+1, 1000));
    for (int i=0; i<n+1; ++i) {
        dist[i][i] = 0;
    }
    int r, k;
    for (int i=0; i<n; ++i) {
        cin >> r >> k;
        int t;
        for (int j=0; j<k; ++j) {
            cin >> t;
            dist[r][t] = 1;
        }
    }
    for (int k=0; k<n+1; ++k) {
        for (int i=0; i<n+1; ++i) {
            for (int j=0; j<n+1; ++j) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
    int p;
    cin >> p;
    int s, d, v;
    for (int i=0; i<p; ++i) {
        cin >> s >> d >> v;
        if (dist[s][d] == 1000 || v < dist[s][d]+1) {
            cout << "NA" << endl;
        } else {
            cout << dist[s][d]+1 << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int INF = 1 << 29;
const int N = 100;
int n;
int as[N+1][N+1];

void floyd() {
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                as[i][j] = min(as[i][j], as[i][k] + as[k][j]);
            }
        }
    }
}

int main() {
    while (cin >> n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                as[i][j] = INF;
            }
        }

        for (int i = 0; i < n; i++) {
            int r, k;
            cin >> r >> k;
            for (int j = 0; j < k; j++) {
                int t;
                cin >> t;
                as[r][t] = 1;
            }
        }
        floyd();
        int p;
        cin >> p;
        for (int i = 0; i < p; i++) {
            int s, d, v;
            cin >> s >> d >> v;
            int dist = as[s][d] + 1;
            if (dist <= v) {
                cout << dist << endl;
            } else {
                cout << "NA" << endl;
            }
        }
    }
}

C++: #include<iostream>
using namespace std;
const int maxv=100000;
int main()
{
	int root[101][101];
	for(int i=0;i<=100;i++){
		for(int i2=0;i2<=100;i2++){
			root[i][i2]=maxv;
		}
	}
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
	int r,k,ch;
	cin>>r>>k;
		for(int i2=0;i2<k;i2++){
			cin>>ch;
			root[r][ch]=1;
		}
	}

	for(int k=1;k<=n;k++){
		for(int i=1;i<=n;i++){
			for(int i2=01;i2<=n;i2++){
				root[i][i2]=min(root[i][i2],root[i][k]+root[k][i2]);
			}
		}
	}

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

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

const int MAX_N = 110;

vector<vector<int>> dist(MAX_N, vector<int>(MAX_N, 1e9));
void warshall_floyd(int n){
    for(int k=0; k<n; k++){
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}

int main(){
    int n; cin>>n;

    for(int i=0; i<n; i++) dist[i][i] = 0;

    for(int i=0; i<n; i++){
        int r; cin>>r;
        r--;
        int k; cin>>k;
        while(k--){
            int t; cin>>t;
            t--;
            dist[r][t] = 1;
        }
    }

    warshall_floyd(n);

    int q; cin>>q;
    while(q--){
        int s, d, v; cin>>s>>d>>v;
        s--, d--;
        if(dist[s][d] + 1 <= v){
            cout << dist[s][d] + 1 << endl;
        }
        else{
            cout << "NA" << endl;
        }
    }
}