#K9081. Most Frequent Consecutive Subsequence

    ID: 37835 Type: Default 1000ms 256MiB

Most Frequent Consecutive Subsequence

Most Frequent Consecutive Subsequence

Given a user's listening history represented as a string and an integer \( k \), identify the most frequently occurring contiguous subsequence (substring) of length \( k \). In the event of a tie, choose the lexicographically smallest subsequence.

For each test case, you are given a string \( S \) and an integer \( k \). You need to consider all substrings \( S[i...i+k-1] \) for \( 0 \leq i \leq |S|-k \) and determine the one that appears most often. If two or more subsequences have the same maximum frequency, output the one that is smallest in lexicographical order.

inputFormat

The input starts with an integer \( T \) representing the number of test cases. Each test case consists of two lines:

  1. The first line contains an integer \( k \), which is the length of the subsequence to consider.
  2. The second line contains a string \( S \) representing the user’s listening history.

Example:

4
2
ababcabcab
3
zzzaaabbbcccaaazzzo
4
aaaaaaa
1
a

outputFormat

For each test case, output the most frequently occurring contiguous subsequence of length \( k \) on a separate line.

## sample
4
2
ababcabcab
3
zzzaaabbbcccaaazzzo
4
aaaaaaa
1
a
ab

aaa aaaa a

</p>