public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int q = in.nextInt(); in.nextLine();

        Map<String, String> mimeTypes = new HashMap<String, String>();

        for (int i = 0; i < n; i++) {
            String line = in.nextLine();
            mimeTypes.put(line.split(" ")[0].toUpperCase(), line.split(" ")[1]);
        }

        for (int i = 0; i < q; i++) {
            String fileName = in.nextLine();
            if (!fileName.contains(".")) {
                System.out.println("UNKNOWN");
            } else {
                String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
                if (mimeTypes.containsKey(ext)) {
                    System.out.println(mimeTypes.get(ext));
                } else {
                    System.out.println("UNKNOWN");
                }
            }
        }
    }