first to map the first word to the remaining phrase, and last to map the last word to the remaining phrase.res to store the distinct results.first map that matches the last word of the current phrase, add the combined phrase to res.
c. For each phrase in the last map that matches the first word of the current phrase, add the combined phrase to res.
d. Insert the remaining phrase into the first and last maps with the corresponding first and last words as keys.res to a list, sort it, and return.