#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#define fi first
#define se second
#define endl '\n'
using namespace std;
using pii = pair<int, int>;
string ss[7] = {"012345", "6789AB", "CDEFGH", "IJKLMN", "OPQRST", "UVWXYZ"}; // 打表
//bool cmp(pii& u, pii& v) { // 行不同按行排, 列不同按列排
// if(u.fi != v.fi) return u.fi < v.fi;
// return u.se < v.se;
//}
// 查找 - 可用哈希表优化
pii bi(char ch) {
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {
if(ch == ss[i][j]) return {i, j};
}
}
return {-1, -1};
}
// 计算结果
string calc(string& s) {
int ii[7] = {0}, jj[7] = {0}; // 计数
int mi = 0, mj = 0; // 最大出现次数
for(char ch : s) {
pii t = bi(ch);
ii[t.fi]++;
jj[t.se]++;
mi = max(ii[t.fi], mi);
mj = max(jj[t.se], mj);
}
vector<pii> co; // 存放最大出现次数的行和列 - 已经按输出要求的顺序来存了
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {
if(ii[i] == mi && jj[j] == mj) {
co.push_back({i, j});
}
}
}
//sort(co.begin(), co.end(), cmp); // 按输出要求排序
string ret = ""; // 统计结果
for(auto e : co) {
int x = e.fi, y = e.se;
ret += ss[x][y];
}
return ret;
}
void solve() {
string s; cin >> s;
cout << calc(s) << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while(T--) {
solve();
}
return 0;
}