//根据特殊字符一个一个排除即可
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<string.h>
using namespace std;
int has[26];
// one two three four five six seven eight nine
// six seven five four two eight three nine one
// 0 1 2 3 4 5 6 7 8 9
// 8 9 0 1 2 3 4 5 6 7
char ch[] = {'X' , 'S' , 'V' , 'F' , 'W' , 'G' , 'R' , 'I' , 'O' };
string str[] = {"SIX" ,"SEVEN","FIVE","FOUR","TWO","EIGHT","THREE","NINE","ONE"};
int num[] = { 8 , 9 , 7 , 6 ,4 ,0 ,5,1,3};
void solve(vector<int> &ans){
for(int i = 0 ; i < 9 ; i ++){
while(has[ch[i] - 'A'] > 0) {
ans.push_back(num[i]);
string tmp = str[i];
for(int j = 0 ; j < tmp.length() ; j ++) has[tmp[j] - 'A'] --;
}
}
}
int main(){
freopen("data.in","r",stdin);
int T;
while(cin>>T){
string str;
for(int i = 0 ; i < T ; i ++){
vector<int> ans;
memset(has, 0 , sizeof has);
cin>>str;
for(int i = 0 ; i < str.length() ; i ++){
has[str[i] - 'A'] ++;
}
solve(ans);
sort(ans.begin() , ans.end());
for(int i = 0 ; i < ans.size() ; i ++){
cout<<ans[i];
}
cout<<endl;
}
}
return 0;
}