#include<stdio.h>
#include<string.h>
char s[1100];
int digitCount[10];
int main() {
int t,l,last,i,flag;
scanf("%d",&t);
while(t--){
scanf("%s",s);
l=strlen(s);
flag=0;
int sum=0;
for(i=0;i<10;i++) digitCount[i]=0;
for(i=0;i<l;i++){
digitCount[s[i]-'0']++;//count the numbers
sum+=s[i]-'0'; // storing the sum
}
if(digitCount[0]>0){
last=0;
digitCount[0]--;
}
else if(digitCount[5]>0){
last=5;
digitCount[5]--;
}
else{
printf("impossible\n");continue;
} //to check the divisibility by 5
if(sum%3==1){
// if remainder is 1 then number can be 1 4 7
if(digitCount[1]>0){
digitCount[1]--;
}
else if(digitCount[4]>0){
digitCount[4]--;
}
else if(digitCount[7]>0){
digitCount[7]--;
}
// (2*2)%3=1
// as removing 2 single digit is costlier than removing single digit so
// i have put this condition at last
else if(digitCount[2]>=2){
digitCount[2]-=2;
}
else if(digitCount[2]>=1&&digitCount[5]>=1){
digitCount[2]--;digitCount[5]--;
}
else if(digitCount[5]>=2){
digitCount[5]-=2;
}
else if(digitCount[8]>=1&&digitCount[2]>=1){
digitCount[8]--;digitCount[2]--;
}
else if(digitCount[8]>=1&&digitCount[5]>=1){
digitCount[8]--;digitCount[5]--;
}
else if(digitCount[8]>=2){
digitCount[8]-=2;
}
else{
printf("impossible\n");continue;
}
}
else if(sum%3==2){
// if remainder is 2 then number can be 2 5 8
if(digitCount[2]>0){
digitCount[2]--;
}
else if(digitCount[5]>0){
digitCount[5]--;
}
else if(digitCount[8]>0){
digitCount[8]--;
}
else if(digitCount[1]>=2){
digitCount[1]-=2;
}
else if(digitCount[4]>=1&&digitCount[1]>=1){
digitCount[4]--;digitCount[1]--;
}
else if(digitCount[4]>=2){
digitCount[4]-=2;
}
else if(digitCount[7]>=1&&digitCount[1]>=1){
digitCount[7]--;digitCount[1]--;
}
else if(digitCount[7]>=1&&digitCount[4]>=1){
digitCount[7]--;digitCount[4]--;
}
else if(digitCount[7]>=2){
digitCount[7]-=2;
}
else{
printf("impossible\n");continue;
}
sum-=2;
}
//now sum%3=0
// now selecting the last digit
if(last==0){
for(i=1;i<10;i++){
if(digitCount[i]>0) break;
}
}
if(i==10) {printf("0\n");continue;}
for(i=9;i>=0;i--){
while(digitCount[i]>0){
printf("%d",i);
digitCount[i]--;
}
}
// now printing the last digit
printf("%d\n",last);
}
return 0;
}