using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp
{
class MAXNUM3
{
static void Main(string[] args)
{
char z = '0';
int t = int.Parse(Console.ReadLine());
while (t-- != 0)
{
string str = Console.ReadLine();
List<char> removed = new List<char>(str.ToCharArray());
int sum = 0;
foreach (char c in str)
sum += (c - z);
int mod = sum % 3;
if (((removed.Last() - z) & 1) == 1)
{
removed.RemoveAt(str.Length - 1);
sum = sum - (str[removed.Count()] - z);
if (sum%3!=0 || (((removed.Last() - z) & 1) == 1))
{
Console.WriteLine(-1);
continue;
}
}
//else if (((removed.Last() - z) % 3) == mod && ((removed[str.Length-2] - z) % 2) == 0)
//{
// if (((removed[str.Length - 2] - z) % 3) == mod && (removed[str.Length - 2] < (removed[str.Length - 1])))
// {
// removed.RemoveAt(str.Length - 2);
// }
// else
// {
// removed.RemoveAt(str.Length - 1);
// }
//}
else
{
int last = -1;
for (int i = 0; i < str.Length-1; i++)
{
if (((removed[i] - z) % 3) == mod)
{
last = i;
if (removed[i] <= removed[i + 1])
{
removed.RemoveAt(i);
break;
}
}
}
if (((removed[removed.Count()-1] - z) % 3) == mod && (removed[removed.Count()-2] - z) % 2 == 0)
last = str.Length - 1;
if (str.Length == removed.Count() && last >= 0)
removed.RemoveAt(last);
}
if (str.Length == removed.Count())
Console.WriteLine(-1);
else
{
Console.WriteLine(new string(removed.ToArray()));
}
}
}
}
}