1. 

/* Problem 11787-Numeral Hieroglyphs
* Zeit: 0.084
* Status: ACCEPTED
* @author Christoph Miesel
*/

import java.io.*;

public class Hieroglyphs
{
public static String s;

public static int getValue(char ch)
{
switch(ch)
{
case 'B':
return 1;
case 'U':
return 10;
case 'S':
return 100;
case 'P':
return 1000;
case 'F':
return 10000;
case 'T':
return 100000;
case 'M':
return 1000000;
default:
return 1000000;

}
}
public static boolean increasing()
{
for(int i = 1; i < s.length(); i++)
if(getValue(s.charAt(i-1)) > getValue(s.charAt(i)))
return false;
return true;
}

public static boolean decreasing()
{
for(int i = 1; i < s.length(); i++)
if(getValue(s.charAt(i-1)) < getValue(s.charAt(i)))
return false;
return true;
}

public static boolean rightRange()
{
char last, current;
int counter = 1;
last = s.charAt(0);

for(int i = 1; i < s.length(); i++)
if(last == s.charAt(i))
{
counter++;
if(counter > 9)
return false;
}
else
{
counter = 1;
last = s.charAt(i);
}
return true;
}

public static int makeSol()
{
int result = 0;
for(int i = 0; i < s.length(); i++)
result += getValue(s.charAt(i));
return result;
}

public static void main(String[] args) throws IOException
{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(r.readLine());
for(int i = 0; i < cases; i++)
{
s = r.readLine();
if(increasing() || decreasing())
{
//System.out.println(increasing());
//System.out.println(decreasing());
///System.out.println(rightRange());
if(rightRange())
{
//System.out.println(rightRange());
System.out.println(makeSol());
}
else System.out.println("error");
}
else System.out.println("error");
}
}
}