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 <queue>
using namespace std;

int main(){
  int a,b,c,d,e,f,g,now,v[200],m[201][201]={};
  queue<int> q;
  cin >> a;
  for(int i=1;i<=a;i++){
    cin >>b>>c;
    for(int j=0;j<c;j++){
      cin >> d;
      m[b][d]=1;
    }
  }
  cin >> d;
  for(int i=0;i<d;i++){
    cin >> e >> f >> g;
    q.push(e);
    for(int j=1;j<=a;j++)v[j]=1200;
    v[e]=1;
    while(1){
      now=q.front();q.pop();
      if(now==f){
	if(v[f]>g){
	  cout << "NA" << endl;
	  break;
	}else {
	  cout << v[now] << endl;
	  break;
	}
      }
      for(int j=1;j<=a;j++){
	if(m[now][j]&&v[j]==1200){
	  v[j]=v[now]+1;
	  q.push(j);
	}
      }
      if(q.empty()){
	cout<< "NA" << endl;
	break;
      }
    }
    while(!q.empty())q.pop();
  }
  return 0;
}

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

int main(){
  int a,b,c,d,e,f,g,now,v[101],m[101][101]={};
  queue<int> q;
  cin >> a;
  for(int i=1;i<=a;i++){
    cin >>b>>c;
    for(int j=0;j<c;j++){
      cin >> d;
      m[i][d]=1;
    }
  }
  cin >> d;
  for(int i=0;i<d;i++){
    cin >> e >> f >> g;
    q.push(e);
    for(int j=1;j<=a;j++)v[j]=1200;
    v[e]=1;
    while(1){
      now=q.front();q.pop();
      if(now==f){
	if(v[f]>g){
	  cout << "NA" << endl;
	  break;
	}else {
	  cout << v[now] << endl;
	  break;
	}
      }
      for(int j=1;j<=a;j++){
	if(m[now][j]==1&&v[j]==1200){
	  v[j]=v[now]+1;
	  q.push(j);
	}
      }
      if(q.empty()){
	cout<< "NA" << endl;
	break;
      }
    }
    while(!q.empty())q.pop();
  }
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int g[100][100];
int main(){
	int a;
	scanf("%d",&a);
	for(int i=0;i<100;i++)
		for(int j=0;j<100;j++)
			g[i][j]=1999999999;
	for(int i=0;i<a;i++)
		g[i][i]=0;
	for(int i=0;i<a;i++){
		int b,c;
		scanf("%d%d",&b,&c);
		for(int j=0;j<c;j++){
			int d;
			scanf("%d",&d);
			g[b-1][d-1]=1;
		}
	}
	for(int k=0;k<a;k++)
		for(int i=0;i<a;i++)
			for(int j=0;j<a;j++)
				g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
	scanf("%d",&a);
	while(a--){
		int p,q,r;scanf("%d%d%d",&p,&q,&r);
		if(r<=g[p-1][q-1])printf("NA\n");
		else printf("%d\n",g[p-1][q-1]+1);
	}
}

C++: #include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
	int n,r,k,t,p,s,d,v,vec[101][101];
	cin>>n;
	rep(i,n){
		vec[i][i]=0;
		rep(j,n)
			vec[i][j]=1000000;
	}
	rep(i,n)
		vec[i][i]=0;
	rep(i,n){
		cin>>r>>k;
		rep(j,k){
			cin>>t;
			vec[r-1][t-1]=1;
		}
	}
	rep(k,n)
	rep(i,n)
	rep(j,n){
		vec[i][j]=min(vec[i][j],vec[i][k]+vec[k][j]);
	}
	cin>>p;
	rep(i,p){
		cin>>s>>d>>v;
		s--;
		d--;
		if(vec[s][d]+1<=v)
		cout<<vec[s][d]+1<<endl;
		else
		cout<<"NA"<<endl;
		
		
		
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int d[101][101]={};
int x[101];
int n;
void bfs(int);
int main(){
  int p;
  int na[100];
  int to;
  int th;
  int st,go,ttl;
  //syoki
  for(int i=0;i<n;i++){
    x[i]=10000;
  }
  //in
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> na[i] >> to;
    for(int j=0;j<to;j++){
      cin >> th;
      d[na[i]][th]=1;
    }
  
  }
  //in2
  cin >> p;
  for(int i=0;i<p;i++){
    for(int j=0;j<=n;j++){
      x[j]=10000;
    }
    
    cin>> st >> go >> ttl;
    x[st]=1;
    bfs(st);
    if(x[go]<=ttl)cout << x[go] << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

///bfs///////////////////////////////////////////
void bfs(int s){
  queue<int>q;
  int u;
  q.push(s);
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<=n;v++){
      if(d[u][v]==1&&x[v]==10000){
	x[v]=x[u]+1;
	q.push(v);
      }
    }
  }
  return ;
}

C++: #include<iostream>
#include<cstring>
#include<queue>
#define MAX_N 110
bool A[MAX_N][MAX_N];
int rooter_num;
using namespace std;
int bfs(int,int);
int main()
{
  int n,startNode,goalNode,TTL;
  int r,k,t;
  int a;
  memset(A,false,sizeof(A));
  cin>>rooter_num;
  /*------------input---------------*/
  for(int i=0;i<rooter_num;i++)
    {
      cin>>r>>k;
      for(int j=0;j<k;j++)
	{
	  cin>>t;
	  A[r][t]=true;
	}
    }
  cin>>n;
  /*-------------search--------------*/
  for(int i=0;i<n;i++)
    {
      cin>>startNode>>goalNode>>TTL;
      a=bfs(startNode,goalNode);
      if(a<=TTL)cout<<a<<endl;
      else cout<<"NA"<<endl;
       
    }
  return 0;
}
int bfs(int startNode,int goalNode)
{
  queue<int>Q;
  int visited[rooter_num+1];
  memset(visited,0,sizeof(visited));
  int currentNode=startNode;
  
  Q.push(currentNode);
  visited[currentNode]=1;
  
  while(!Q.empty())
    {

      
      currentNode=Q.front();
      Q.pop();
      for(int nextNode=1;nextNode<=rooter_num;nextNode++)
	{

	  if(A[currentNode][nextNode]==0)continue;

	  if(nextNode==goalNode)return visited[currentNode]+1;
	  
	  if(visited[nextNode]<=0)
	    {
	      visited[nextNode]=visited[currentNode]+1;
	      Q.push(nextNode);
	    }
	}
    }
  return MAX_N;
}

C++: #include <iostream>
#include <algorithm> // max, min

using namespace std;

const int AllaySize = 100;
const int Limit = 999999;

int N, P;
int ConnectR[AllaySize][AllaySize];
int InitTTL;
//int Ans[AllaySize][AllaySize];

void war(void) {
  for(int k=1; k<N+1; k++) {
    for(int i=1; i<N+1; i++) {
      for(int j=1; j<N+1; j++) {
	ConnectR[i][j]=min(ConnectR[i][j], ConnectR[i][k]+ConnectR[k][j]);
      }
    }
  } 
}

int main(void) {
  for(int i=0; i<AllaySize; i++) {
    for(int j=0; j<AllaySize; j++) {
      ConnectR[i][j]=Limit;
      // Ans[i][j]=Limit;
    }
  }

  cin>>N;
  for(int i=0; i<N; i++) { // ルータの接続情報
    int k;
    int from, to;
    cin>>from;
    cin>>k;
    for(int j=0; j<k; j++) {
      cin>>to;
      ConnectR[from][to] = 1; // 通過可能
    }
  }

  war();
  cin>>P;
  for(int i=0; i<P; i++) {
    int from, to;
    cin>>from;
    cin>>to;
    cin>>InitTTL;
    if(ConnectR[from][to] < InitTTL) {
      cout<<ConnectR[from][to]+1<<endl; // ?????
    }
    else {
      cout<<"NA"<<endl;
    }
  }
  /*
  for(int i=0; i<N; i++)
    for(int j=0; j<N; j++)
      cout<<ConnectR[i][j]<<" ";
  */
}