import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        TreeMap<String,Integer> mp = new TreeMap<>();
        int maxv = 0;
        while (in.hasNext()) {
            String s = in.next();
            if (mp.containsKey(s)) {
                int x = mp.get(s);
                x++;
                mp.remove(s);
                mp.put(s,x);
                maxv = Math.max(maxv,x);
            }
            else {
                mp.put(s,1);
            }
        }
        for (int v = maxv;v >= 3;v--) {
            for (Map.Entry<String, Integer> cur : mp.entrySet()) {
                if (cur.getValue() == v) {
                    System.out.println(cur.getKey());
                }
            }
        }
    }
}

我使用了TreeMap来保证输出为字典序升序。另外又要保证输出为出现次数降序,我记录了一个最大值,并从最大值开始递减遍历到三,这样子可能复杂度有点大,不过这题数据量没那么大,就过了。