Company: Flipkart
Difficulty: medium
Advertisement Arrangement Optimization A media company has been provided with pairs of advertisements that are to be played one after the other. These advertisements are denoted by uppercase letters from the English alphabet. To maximise the revenue, the media company is in need of software that will provide an insight into which arrangement should be selected so that every advertisement is played at least one time. The software will take advertisement pairs as an N × M grid in which every row will denote the pair of advertisements that need to be played one after another, and will return the arrangement in which every advertisement will be played at least one time. As per the prior analysis, the media company found out that if the arrangement starts from \"ABC\" advertisement then maximum revenue is generated. If more than one way is possible, then the software will give the smallest lexicographical arrangement. Input Format The first line consists of two space-separated integers - gr