#include <bits/stdc++.h>
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return f?x:-x;
}
struct shoupai {
int type;
int size;
friend bool operator < (const shoupai &a, const shoupai &b) {
return a.type == b.type ? a.size < b.size : a.type > b.type;
}
};
struct user {
char name[20];
shoupai pai;
friend bool operator < (const user &a, const user &b) {
if (a.pai < b.pai) return true;
if (b.pai < a.pai) return false;
return strcmp(a.name, b.name) < 0;
}
} u[102];
char p1[20], p2[20], p3[20];
shoupai fenxipai(char* a) {
int size = 0;
if (strlen(a) > 2) {
size = 10;
} else if (a[1] >= '2' && a[1] <= '9') {
size = a[1] - '0';
} else if (a[1] == 'A') {
size = 14;
} else if (a[1] == 'K') {
size = 13;
} else if (a[1] == 'Q') {
size = 12;
} else if (a[1] == 'J') {
size = 11;
}
int type = a[0] - 'a';
return (shoupai) {
type,
size,
};
}
shoupai fenxishoupai() {
shoupai s1 = fenxipai(p1);
shoupai s2 = fenxipai(p2);
shoupai s3 = fenxipai(p3);
if (s1.size > s2.size) swap(s1, s2);
if (s2.size > s3.size) swap(s2, s3);
if (s1.size > s2.size) swap(s1, s2);
// s1 smallest, s3 largest
// 豹子
if (s1.size == s2.size && s2.size == s3.size) {
return (shoupai) { 1, s1.size };
}
// 顺金
if (s1.type == s2.type && s2.type == s3.type) {
if (s2.size - s1.size == 1 && s3.size - s2.size == 1) {
return (shoupai) { 2, s3.size };
}
if (s1.size == 2 && s2.size == 3 && s3.size == 14) {
return (shoupai) { 2, 3 };
}
// 金花
return (shoupai) { 3, s3.size * 10000 + s2.size * 100 + s1.size };
}
// 顺子
if (s2.size - s1.size == 1 && s3.size - s2.size == 1) {
return (shoupai) { 4, s3.size };
}
if (s1.size == 2 && s2.size == 3 && s3.size == 14) {
return (shoupai) { 4, 3 };
}
// 对子
if (s1.size == s2.size) {
return (shoupai) { 5, s1.size * 100 + s3.size };
}
if (s2.size == s3.size) {
return (shoupai) { 5, s2.size * 100 + s1.size };
}
// 单张
return (shoupai) { 6, s3.size * 10000 + s2.size * 100 + s1.size };
}
int main() {
int T = read();
while (T --) {
int n = read();
for (int i = 1; i <= n; ++ i) {
scanf("%s", u[i].name);
scanf("%s%s%s", p1, p2, p3);
u[i].pai = fenxishoupai();
//printf("%s has %d %d\n", u[i].name, u[i].pai.type, u[i].pai.size);
}
sort(u + 1, u + n + 1);
for (int i = n; i; -- i) {
puts(u[i].name);
}
}
}