*#!/usr/bin/env python*
*# Roman*
*# Phil Bordelon*
**import** os
**import** sys
DEBUG = os.**getenv**("DEBUG", False)
*# A large value to initialize last_val to, to remove silly special cases.*
LARGE_NUMBER = 31337
*# A handy lookup table for the numerals.*
roman_numerals = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
**if** "__main__" == __name__:
dataset_count = **int**(sys.stdin.**readline**())
**for** dataset_loop **in** **range**(dataset_count):
*# Get the Roman numeral.*
roman_number = sys.stdin.**readline**().**strip**()
total_val = 0
last_val = LARGE_NUMBER
*# Loop through the characters, keeping track of the last one; this*
*# is because we need to know if the last one needs to be subtracted*
*# from this one, which is only the case if a smaller value is to the*
*# left of a larger one.*
**for** numeral **in** roman_number:
value = roman_numerals[numeral]
**if** last_val < value:
*# We do, indeed, have to treat the last character as a*
*# subtraction. We already added it, so we have to remove that*
*# value once, and then again to do the actual subtraction.*
total_val += value - 2 * last_val
**else**:
*# Just add it to the total.*
total_val += value
*# Remember the last value for the subtraction check in the next*
*# iteration.*
last_val = value
*# Lastly, print the value we calculated.*
**print** total_val