fun main(args: Array<String>?): Unit {
    while(true) {
        val n = readLine()!!.trim().toInt()
        if (n == 0){
            return
        }
        val groupDefinition = List(n){
            val line = readLine()!!.trim('.').split(':')
            line.first() to line.last().split(',')
        }
        println(findGroup(groupDefinition.first().first, groupDefinition.toMap(), mutableMapOf())!!.size)
    }
}
fun findGroup(name: String, definition: Map<String, List<String>>, cache: MutableMap<String, Set<String>>): Set<String>? {
    val define = definition[name]
    return when(define){
        null -> null
        else -> cache.getOrPut(name){
            define.flatMap{member -> findGroup(member, definition, cache) ?: listOf(member)}.toSet()
        }
    }
}

