import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br
=new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
int t=Integer.parseInt(br.readLine());
for(int i=0;i<t;i++){
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
int p=Integer.parseInt(st.nextToken());
Player[] pls=new Player[n];
for(int j=0;j<n;j++){
st=new StringTokenizer(br.readLine());
pls[j]=new Player(st.nextToken(),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()));
}
Arrays.sort(pls);
LinkedList<PlayerPlaying> team1
=new LinkedList<PlayerPlaying>();
LinkedList<PlayerPlaying> team1Sit
=new LinkedList<PlayerPlaying>();
LinkedList<PlayerPlaying> team2
=new LinkedList<PlayerPlaying>();
LinkedList<PlayerPlaying> team2Sit
=new LinkedList<PlayerPlaying>();
for(int j=n-1,counter=1;j>-1;j--,counter++){
if(j%2==0){
if(team1.size()<p)
team1.add(new PlayerPlaying
(pls[j].getName(),counter, 0));
else
team1Sit.add(new PlayerPlaying
(pls[j].getName(),counter, 0));
}else{
if(team2.size()<p)
team2.add(new PlayerPlaying
(pls[j].getName(),counter, 0));
else
team2Sit.add(new PlayerPlaying
(pls[j].getName(),counter, 0));
}
}
for(int j=0;j<m;j++){
Iterator<PlayerPlaying> it=team1.iterator();
while(it.hasNext()){
it.next().incPlayed();
}
it=team2.iterator();
while(it.hasNext()){
it.next().incPlayed();
}
if(!team1Sit.isEmpty()){
Collections.sort(team1Sit);
PlayerPlaying playteam1=team1Sit.removeLast();
Collections.sort(team1);
PlayerPlaying sitteam1=team1.remove();
team1.add(playteam1);
team1Sit.add(sitteam1);
}
if(!team2Sit.isEmpty()){
Collections.sort(team2Sit);
PlayerPlaying playteam2=team2Sit.removeLast();
Collections.sort(team2);
PlayerPlaying sitteam2=team2.remove();
team2.add(playteam2);
team2Sit.add(sitteam2);
}
}
sb.append("Case #").append(i+1).append(": ");
String[]arrNames=new String[2*p];
for(int j=0;j<p;j++){
arrNames[j]=team1.remove().getName();
arrNames[j+p]=team2.remove().getName();
}
Arrays.sort(arrNames);
for(int j=0;j<arrNames.length;j++){
if(j>0)
sb.append(" ");
sb.append(arrNames[j]);
}
sb.append("\n");
}
System.out.print(sb);
}
}
class Player implements Comparable<Player>{
private String name;
private int height;
private int shotPercentage;
public Player(String str,int s,int h) {
this.name=str;
this.shotPercentage=s;
this.height=h;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getShotPercentage() {
return shotPercentage;
}
public void setShotPercentage(int shotPercentage) {
this.shotPercentage = shotPercentage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Player o) {
if(this.shotPercentage>o.getShotPercentage()){
return 1;
}
else if(this.shotPercentage<o.getShotPercentage()){
return -1;
}else{
if(this.height>o.getHeight()){
return 1;
}
return -1;
}
}
}
class PlayerPlaying implements Comparable<PlayerPlaying> {
private int draft;
private int minPlayed;
private String name;
public PlayerPlaying(String name,int draft, int minPlayed) {
this.name=name;
this.draft = draft;
this.minPlayed = minPlayed;
}
public int getDraft() {
return draft;
}
public void setDraft(int draft) {
this.draft = draft;
}
public int getMinPlayed() {
return minPlayed;
}
public void setMinPlayed(int minPlayed) {
this.minPlayed = minPlayed;
}
public void incPlayed(){
minPlayed++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(PlayerPlaying o) {
if(minPlayed<o.getMinPlayed()){
return 1;
}
if(minPlayed>o.getMinPlayed()){
return -1;
}else{
if(draft<o.getDraft()){
return 1;
}
return -1;
}
}
}