#K4681. Lexicographically Smallest ID
Lexicographically Smallest ID
Lexicographically Smallest ID
You are given an integer N and a string letters representing a set of distinct characters. Your task is to generate the lexicographically smallest possible ID of length N following these rules:
- The first character of the ID is the smallest character from the set (letters) when the characters are sorted in ascending order.
- For each subsequent position, choose the smallest character (in sorted order) that is different from the character immediately preceding it.
For example, if N = 5
and letters = "ABC"
, after sorting the letters we get "ABC". Start with 'A', then choose the smallest letter not equal to 'A', which is 'B', then again the smallest letter that is not 'B' (i.e. 'A'), and so on. The resulting ID is ABABA
.
You will be given multiple test cases. For each test case, output the corresponding lexicographically smallest ID.
inputFormat
The first line of input contains an integer T — the number of test cases.
Each of the next T lines contains a test case with an integer N and a string letters separated by a space.
outputFormat
For each test case, output a single line containing the lexicographically smallest ID that meets the rules.
## sample3
5 ABC
3 QV
4 XYZ
ABABA
QVQ
XYXY
</p>