fun main(args:Array<String>?): Unit {
    val n = readLine()!!.trim().toInt()
    val books = Array(n){
        val (title, author, date) = readLine()!!.trim().split(' ')
        Book(title, author, date)
    }
    repeat(readLine()!!.trim().toInt()){
        if (it != 0) println()
        val (title, author, from, to) = readLine()!!.trim().split(' ')
        var find: List<Int> = books.indices.toList()
        if (title != "*") find = find.filter{ i -> title in books[i].title }
        if (author != "*") find = find.filter { i -> author in books[i].author }
        if (from != "*") {
            val f = Date.parse(from)
            find = find.filter{ i -> f <= books[i].date}
        }
        if (to != "*") {
            val t = Date.parse(to)
            find = find.filter{ i -> t >= books[i].date}
        }
        if (find.isNotEmpty())
            println(find.joinToString("\n"){i -> books[i].title})
    }
}
data class Book(val title: String, val author: String, val date: Date) {
    constructor(title: String, author: String, date: String):this(title, author, Date.parse(date))
}
data class Date(val year: Int, val month: Int, val day: Int): Comparable<Date> {
    override fun compareTo(other: Date): Int {
        return if (year == other.year) if (month == other.month) day.compareTo(other.day) else month.compareTo(other.month) else year.compareTo(other.year)
    }

    companion object {
        fun parse(str: String): Date {
            val (year, month, day) = str.trim().split('/').map(String::toInt)
            return Date(year, month, day)
        }
    }
}
