DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Italian brainrot

Back to problem

Editorial

Fix any total order of the characters. For example, let the character that appears earlier in the input always win.

If character BBB wins against character AAA, then BBB appears earlier than AAA in the input. If character CCC wins against character BBB, then CCC appears earlier than BBB. Therefore CCC also appears earlier than AAA, so CCC wins against AAA.

Thus, printing the earlier character in input order for every query always satisfies the condition.

Store the input index of each name, and for each query print the name with the smaller index. If the two names are equal, print that name.

The time complexity is O(L+Q)O(L + Q)O(L+Q), where LLL is the sum of the lengths of the given character names.