#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<21

using namespace std;

int main()
{	
	int n;
	cin >> n;
	
	int a[101][101];
	rep(i,101) rep(j,101) a[i][j] = INF;

	rep(i,n)
	{
		int r,k,temp;
		cin >> r >> k;
			
		rep(j,k)
		{
			cin >> temp;
			a[r-1][temp-1] = 1;
		}
	}

	rep(k,n)
	{
		rep(i,n)
		{
			rep(j,n)
			{
				a[i][j] = min(a[i][j],a[i][k]+a[k][j]);
			}
		}
	}

	int p;
	cin >> p;
	rep(i,p)
	{
		int from,to,cost;
		cin >> from >> to >> cost;
		
		if(cost > a[from-1][to-1])
		{
			cout << a[from-1][to-1] + 1 << endl;
		}
		else
		{
			cout << "NA" << endl;
		}

	}
	return 0;
}