Under construction

Still being built. Things may change

Converting Romans

Time limit1sMemory limit1024 MB

Summary
Convert each Roman numeral string to its integer value, where any digit left of a larger digit is subtracted.
Level

Easy3 of 10

Topics
String, Simulation
Solved
No attempts yet

Problem

Roman numerals are a numeral system that originated in ancient Rome and was widely used across Europe into the Late Middle Ages. Instead of the Arabic digits most of us use today, it writes numbers with combinations of Latin letters, each with a fixed integer value. Over the years many different letters were used, which often caused confusion. The modern style uses only these seven:

IVXLCDM
1510501005001000

A common misunderstanding is that Roman numerals use a universal subtractive syntax. In a subtractive syntax, a lower digit written before a larger digit is subtracted from the larger digit. 4 can be written as IIII or IV (5−1)(5-1). The Romans only really used subtractive syntax for smaller numbers. For larger numbers they largely avoided it, for clarity. For example, 499499 can be written LDVLIV, XDIX, VDIV or ID, while older sources most likely would use CDXCIX. Ain't Roman numbers fun?

For the purposes of this problem, we subtract any digit written to the left of a larger digit, even if they are not directly adjacent.

Input

The input starts with an integer 0<n≤1030 < n \leq 10^3. Then follow nn lines, each containing up to 3⋅1053 \cdot 10^5 Roman digits. The total number of Roman digits in the input is at most 3⋅1053 \cdot 10^5.

Output

Output the corresponding Arabic numbers, one number per line.

Examples1

  1. Example 1

    Input
    5
    CDXCIX
    ID
    IV
    IIIIV
    IIIIIIV
    
    Expected output
    499
    499
    4
    1
    -1