
#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef long long ll;

const int MAX_SIZE = 10000;

struct edge{
	int to;
	int cost;
};

typedef pair<int,int> P;
int V;

const int MAX_V = 5000;

vector<edge> G[MAX_V];
int d[MAX_V];
const int INF = 100000000;

int prevv[MAX_V];

class Situation{
public:
	int ttl;
	int node;
	int dist;

	Situation();
	Situation(int ttl_,int node_,int dist_){
		ttl=ttl_;
		node=node_;
		dist=dist_;
	}

	bool operator<(const Situation &sit)const{
		return sit.dist > this->dist;
	}
};

void dijkstra(int s,int ttl){
	priority_queue<Situation > que;
	fill(d,d+MAX_V,INF);
	d[s] = 0;
	que.push(Situation(ttl,s,0));
	while(!que.empty()){
		Situation p = que.top();
		que.pop();
		int v = p.node;
		if(d[v] < p.dist)
			continue;
		if(p.ttl<=1)
			continue;
		for(int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if(d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				que.push(Situation(ttl-1,e.to,d[e.to]));
			}
		}
	}
}


int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		int from;
		cin>>from;
		int m;
		cin>>m;
		for(int j = 0; j < m; j++){
			int to;
			cin>>to;
			edge e;
			e.cost=1;
			e.to=to;
			G[from].push_back(e);
		}
	}
	int l;
	cin>>l;
	for(int j = 0; j < l; j++){
		int s,dd,v;
		cin>>s>>dd>>v;
		dijkstra(s,v);
		if(d[dd]==INF)
			cout<<"NA"<<endl;
		else
			cout<<d[dd]+1<<endl;
	}


	return 0;
}