#include <bits/stdc++.h>
using namespace std;
int length_without(const string& s, int i, int j) {
int result = 0;
bool was_not_null = false;
for (int k = 0; k < s.size(); k++) {
if (k != i && k != j) {
was_not_null |= s[k] != '0';
result += was_not_null;
}
}
return result;
}
void write_without(const string& s, int i, int j) {
bool was_not_null = false;
for (int k = 0; k < s.size(); k++) {
if (k != i && k != j) {
was_not_null |= s[k] != '0';
if (was_not_null) {
cout << s[k];
}
}
}
cout << (was_not_null ? "\n" : "-1\n");
}
void task() {
string s;
cin >> s;
int sum = 0;
for (auto e : s) {
sum += e - '0';
}
if (sum % 3 == 0) {
cout << s << '\n';
return;
}
for (int i = 0; i < s.size(); i++) { // one digit - best
if ((s[i] - '0') % 3 == sum % 3 && (i + 1 == s.size() || s[i] < s[i + 1])) {
write_without(s, i, i);
return;
}
}
array<int, 3> best = {-1, 0, 0}; // length, without
for (int i = s.size() - 1; i >= 0; i--) { // one digit - last
if ((s[i] - '0') % 3 == sum % 3) {
best = {length_without(s, i, i), i, i};
break;
}
}
for (int i = 0; i + 1 < s.size(); i++) { // two digits - best
if ((s[i] - '0') % 3 && s[i] < s[i + 1]) {
for (int j = i + 1; j < s.size(); j++) {
if ((s[j] - '0') % 3 && (j + 1 == s.size() || s[j] < s[j + 1])) {
best = max(best, {length_without(s, i, j), i, j});
return;
}
}
for (int j = s.size() - 1; j > i; j--) {
if ((s[j] - '0') % 3) {
best = max(best, {length_without(s, i, j), i, j});
return;
}
}
}
}
for (int i = s.size() - 1; i >= 1; i--) { // two digits - last
if ((s[i] - '0') % 3) {
for (int j = i - 1; j >= 0; j--) {
if ((s[j] - '0') % 3) {
best = max(best, {length_without(s, i, j), i, j});
break;
}
}
break;
}
}
write_without(s, best[1], best[2]);
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
while (n--) task();
}