import java.util.*;
class Solution{
    public static void main(String[] args) {
        Map<String, String> dict = new HashMap<>();
        dict.put("be right back", "BRB");
        dict.put("be right there", "BRT");
        dict.put("be right later", "BRL");
        dict.put("be", "B");
        dict.put("be back soon","B back soon");
        dict.put("faster than light","FTL");
        dict.put("later", "L8R");
        
        String[] tests = {
            "I will be right there later",
            "I will be right there",
            "I will be there",
            "I will go right there",
            "I will be there later",
            "I am faster than you",
            "Never faster than light",
            "Faster than light today"
        };

        for(String test_case : tests){
            System.out.println(test_case + " => " + convert(test_case, dict));   
        }
    }

    public static String convert(String s, Map<String, String> dict) {
        
        List<String> dict_words = new ArrayList<>(dict.keySet());
        Map<Integer,String[]> replacement_index = new HashMap<>();
        
        Collections.sort(dict_words,new Comparator<String>(){
            public int compare(String s1,String s2){
                if(s1.length() == s2.length()) return 0; // order doesn't seem to matter for same length strings
                return s2.length() - s1.length(); // return bigger length string first
            }
        });
    
        String temp = s.toLowerCase(); // to perform case insensitive match
        for(String dict_str : dict_words){
            String dict_str_lower = dict_str.toLowerCase(); // to perform case insensitive match
            int index = 0;
            do{
                index = temp.indexOf(dict_str_lower,index);
                if(index != -1){
                    replacement_index.putIfAbsent(index,new String[]{dict.get(dict_str),dict_str});
                    index++;// to get the next match index of the same word in the string.
                }
            }while(index != -1 && index < temp.length());
        }
        
        StringBuilder res = new StringBuilder("");
        
        for(int i = 0;i < s.length(); ++i){
            if(replacement_index.containsKey(i)){
                res.append(replacement_index.get(i)[0]);
                i += replacement_index.get(i)[1].length() - 1;
            }else{
                res.append(s.charAt(i));
            }
        }

        return res.toString();
    }

}