import kotlin.system.exitProcess val br = System.`in`.bufferedReader() fun readLine(): String? = br.readLine() fun readString() = readLine()!! fun readInt() = readString().toInt() fun readStrings() = readLine()?.split(" ")?.filter { it.isNotEmpty() } ?: listOf() const val MAX_STACK_SIZE: Long = 128 * 1024 * 1024 fun main() { val thread = Thread(null, ::run, "solve", MAX_STACK_SIZE) thread.setUncaughtExceptionHandler { _, e -> e.printStackTrace(); exitProcess(1) } thread.start() } fun run() { val N = readInt() val pairs = Array(N) { readStrings() } output(solve(pairs)) } fun solve(pairs: Array>): List { val cheaters = pairs.map { it.first() }.toHashSet() val used = HashSet() val res = mutableListOf() pairs.map { it[1] }.forEach { if (it !in cheaters && !used.contains(it)) { res.add(it) used.add(it) } } return res } fun output(res: List) = res.joinToString("\n") .apply { println(this) }