#include <iostream>
#include <set>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

const int SIZE = 101;

int solve( int table[ SIZE ][ SIZE ], int s, int e, int initTtl, int numRooter )
{
	if( initTtl < 2 ){
		return 0;
	}
	bool bPast[ SIZE ];
	memset( bPast, 0, sizeof( bPast ) );
	bPast[ s ] = true;
	vector< int > pList;
	for( int i = 1; i <= numRooter; ++i ){
		if( table[ s ][ i ] ){
			if( i == e ){
				return 2;
			}
			bPast[ i ] = true;
			pList.push_back( i );
		}
	}

	int ttl = initTtl;
	while( pList.size() > 0 ){
		--ttl;
		if( ttl < 1 ){
			return 0;
		}
		vector< int > pList2;
		for( int i = 0; i < pList.size(); ++i ){
			int p = pList[ i ];
			if( p == e ){
				return initTtl - ttl + 1;
			}
			for( int j = 1; j <= numRooter; ++j ){
				if( table[ p ][ j ] && !bPast[ j ] ){
					bPast[ j ] = true;
					pList2.push_back( j );
				}
			}
		}
		pList = pList2;
	}

	return 0;
}

int main()
{
	int table[ SIZE ][ SIZE ];

	int n;
	while( cin >> n ){
		cin.ignore( 100, '\n' );
		memset( table, 0, sizeof(table) );
		string s;
		for( int i = 0; i < n; ++i ){
			getline( cin, s );
			int o = s[ 0 ] - '0';
			for( int j = 4; j < s.size(); j += 2 ){
				table[ o ][ s[ j ] - '0' ] = true;
			}
		}
		int p;
		cin >> p;
		for( int i = 0; i < p; ++i ){
			int from, to, ttl;
			cin >> from >> to >> ttl;
			int result = solve( table, from, to, ttl, n );
			if( result > 0 ){
				cout << result << endl;
			}else{
				cout << "NA" << endl;
			}
		}
	}

	return 0;
}