
fun main(args:Array<String>):Unit {
    val n = readLine()!!.toInt()
    val minRoute = Array(n){from -> Array(n){to -> if (from == to) 0 else Int.MAX_VALUE.toLong()}}
    repeat(n){
        val info = readLine()!!.split(' ').map(String::toInt)
        info.drop(2).forEach { to ->
            minRoute[info.first() - 1][to - 1] = 1
        }
    }
    for (i in 0 until n) for (from in 0 until n) for (to in 0 until n){
        if (minRoute[from][to] > minRoute[from][i] + minRoute[i][to])
            minRoute[from][to] = minRoute[from][i] + minRoute[i][to]
    }
    repeat(readLine()!!.toInt()){
        val (s, d, v) = readLine()!!.split(' ').map(String::toInt)
        println(if (minRoute[s - 1][d - 1] >= v) "NA" else minRoute[s - 1][d - 1] + 1)
    }
}
