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<string.h>
using namespace std;

const int INF = 100000000;
int adj[110][110];

int main() {
  int n,p,r,k,t,s,d,v;

  for(int i=0; i<110; ++i) {
    for(int j=0; j<110; ++j) {
      adj[i][j] = INF;
    }
  }

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

  for(int k=0; k<n; ++k) {
    for(int i=0; i<n; ++i) {
      for(int j=0; j<n; ++j) {
	adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }
  
  cin>>p;
  for(int i=0; i<p; ++i) {
    cin>>s>>d>>v;
    s--,d--;
    if(adj[s][d]+1 <= v) cout<<adj[s][d]+1<<endl;
    else cout<<"NA"<<endl;
  }
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int s = 0, int d = 0, Weight w = 0)
    : src(s), dst(d), weight(w) {
  }
};

using Edges  = vector<Edge>;
using Graph  = vector<Edges>;
using Array  = vector<Weight>;
using Matrix = vector<Array>;

void addArc(Graph &g, const int &a, const int &b, const Weight &w = 1) {
  g[a].emplace_back(a, b, w);
}

void addEdge(Graph &g, const int &a, const int &b, const Weight &w = 1) {
  addArc(g, a, b, w);
  addArc(g, b, a, w);
}

// deps: graph-basic
Matrix warshallFloyd(const Graph &g) {
  int n = g.size();
  Matrix d(n, Array(n, INF));
  rep(i, n) d[i][i] = 0;
  rep(i, n) for (auto &e : g[i]) cmin(d[e.src][e.dst], e.weight);
  rep(k, n) rep(i, n) rep(j, n) {
    if (d[i][k] != INF && d[k][j] != INF) cmin(d[i][j], d[i][k] + d[k][j]);
  }
  return d;
}

void solve() {
  int n;
  cin >> n;
  Graph g(n);
  while (n--) {
    int r, k;
    cin >> r >> k;
    --r;
    while (k--) {
      int t;
      cin >> t;
      --t;
      addArc(g, r, t);
    }
  }
  Matrix t = warshallFloyd(g);
  int p;
  cin >> p;
  while (p--) {
    int s, d, v;
    cin >> s >> d >> v;
    --s, --d;
    if (t[s][d] + 1 > v) {
      cout << "NA" << endl;
      continue;
    }
    cout << t[s][d] + 1 << endl;
  }
}

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

int main(){
  int n,p;
  int dis[101][101];
  int r,k,t;
  int s,d,v;

  cin >> n;

  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      dis[i][j] = 100;
      if(i==j)dis[i][j] = 0;
    }
  }

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

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

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

C++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;

# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12



template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };

int main()
{
	INT(n);
	int a[110][110];
	FOR(i, 110)FOR(j, 110)a[i][j] = (i != j ? INF:0);
	FOR(i, n)
	{
		INT(r);
		INT(k);
		FOR(j, k)
		{
			INT(t);
			a[r][t] = 1;
		}
	}
	FOR(k, n+1)FOR(i, n+1)FOR(j, n+1)a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
	INT(p);
	FOR(i, p)
	{
		INT(s); 
		INT(g);
		INT(t);
		if (a[s][g]+1 <= t)cout << a[s][g]+1 << endl;
		else cout << "NA" << endl;
	}

}