# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
@files = []
@extensions = {}
@n = gets.to_i # Number of elements which make up the association table.
@q = gets.to_i # Number Q of file names to be analyzed.
@n.times do
    # ext: file extension
    # mt: MIME type.
    ext, mt = gets.split(" ")
    @extensions[ext.downcase] = mt
end
@q.times do
    fname = gets.chomp # One file name per line.
    @files << fname
end
STDERR.puts @files.to_s
STDERR.puts @extensions.to_s
@files = @files.map do |file|
    if file.slice(file.length - 1) == "."
        file = ["398484843"]
    elsif file.include?(".")
        file = file.split(".")
    else
        file = ["43843839"]
    end
end
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
STDERR.puts @files.to_s
@files.each do |file|
    if @extensions.has_key?(file.last.downcase)
        puts @extensions[file.last.downcase]
    elsif @extensions.has_key?(file.last.downcase) == false
        puts "UNKNOWN"
    else
        puts "UNKNOWN"
    end
end
