#C3808. Find the Anagram Pair

    ID: 47276 Type: Default 1000ms 256MiB

Find the Anagram Pair

Find the Anagram Pair

You are given several test cases. In each test case, you are provided with N lines of text. Your task is to find and output the first pair of words that are anagrams of each other from the concatenated text of the test case. Two words are considered anagrams if their sorted characters are identical.

If an anagram pair is found, output the two words separated by a single space in the order they appear. If no anagram pair exists in a test case, output NO ANAGRAMS (in all capital letters).

Input Format: The first line of input contains an integer T representing the number of test cases. For each test case, the first line contains an integer N, the number of lines that follow. Each of the next N lines contains a sequence of words separated by spaces.

Note: When comparing words, consider each word as is. The first valid pair encountered (i.e. when scanning words in order) should be reported.

Example: For the test case containing a single line "listen silent", the output should be "listen silent" because listen and silent are anagrams of each other.

The anagram condition can be expressed in \( \LaTeX \) as:

\[ \text{if } sorted(word_1) = sorted(word_2) \text{ then } word_1 \text{ and } word_2 \text{ are anagrams.} \]

inputFormat

The input is read from standard input (stdin) and has the following structure:

T
N
line_1
line_2
... 
line_N
[Repeat the above for each test case]

Where:

  • T is the number of test cases.
  • For each test case, N indicates how many lines of text follow.
  • Each subsequent line contains a series of words separated by spaces.

outputFormat

For each test case, output one line to standard output (stdout):

  • If an anagram pair is found, output the two words separated by a single space.
  • If no anagram pair exists, output NO ANAGRAMS.
## sample
2
2
listen silent
no anagrams here
3
bat tab
word sword
random words
listen silent

bat tab

</p>