#K39087. Most Frequent Songs

    ID: 26342 Type: Default 1000ms 256MiB

Most Frequent Songs

Most Frequent Songs

You are given T test cases. For each test case, there are N unique songs and M users. The list of song names is provided, and each user has played each song a certain number of times, given by a frequency matrix. Your task is to determine the most frequently played song for each user. In case of a tie, output the song with the lexicographically smallest name.

Formally, for each user i (1 \leq i \leq M), let (F_{ij}) be the frequency of song j (1 \leq j \leq N). You must choose a song k such that [ F_{ik} = \max_{1 \leq j \leq N} F_{ij} ] If there exists more than one k with the same maximum frequency, choose the song with the smallest name in lexicographical order.

Note: Song names consist of alphanumeric characters.

inputFormat

The input is read from standard input (stdin). The first line contains a single integer T, denoting the number of test cases. Each test case is described as follows:

  1. The first line of each test case contains two integers N and M, where N is the number of songs and M is the number of users.
  2. The second line contains N space-separated strings representing the song names.
  3. The following M lines each contain N space-separated integers representing the frequency with which a user played each song.

outputFormat

For each test case, print M lines to standard output (stdout). Each line should contain the name of the most frequently played song for the corresponding user.## sample

4
3 2
songA songB songC
2 1 0
1 3 2
4 3
song1 song2 song3 song4
2 2 2 2
0 0 0 1
5 5 5 4
3 2
songX songY songZ
3 1 3
1 2 3
2 1
songA songB
2 2
songA

songB song1 song4 song1 songX songZ songA

</p>