Under construction

Still being built. Things may change

Verdis Quo

Time limit1sMemory limit128 MB

Summary
Convert valid Roman numerals to decimal integers using addition and subtraction rules.
Level

Easy3 of 10

Topics
String, Simulation, Implementation
Solved
No attempts yet

Problem

The Romans wrote numbers using seven letters of the alphabet. Each letter and its value are shown below.

  • I = 1
  • V = 5
  • X = 10
  • L = 50
  • C = 100
  • D = 500
  • M = 1000

Using these seven letters and the following rules, the Romans could write any number they wanted.

  • Addition rule: If the letters are written from left to right in non-increasing order of value, add all of the values together. For example, the Roman numeral MMCLVII is 1000 + 1000 + 100 + 50 + 5 + 1 + 1 = 2157.
  • Subtraction rule: Because writing numbers this way makes them too long, there is also a subtraction rule. If a letter's value is smaller than the value of the letter immediately to its right, subtract the value of the left letter. For example, the Roman numeral MCMXIV is 1000 − 100 + 1000 + 10 − 1 + 5 = 1914.
  • I, X, and C cannot be written four or more times in a row. That is, IIII must be written in the shorter form IV.
  • V, L, and D cannot be written two or more times in a row.
  • There is no limit on the number of M's.
  • The subtraction rule applies to only a single left letter. That is, IV is 4, but IIV is not 3 and is not a valid Roman numeral either.
  • Only I, X, and C may appear as the left letter of the subtraction rule.
  • In the subtraction rule, I may appear only before V or X, X only before L or C, and C only before D or M.

Given Roman numerals that satisfy these rules, convert them to decimal integers.

Input

The first line contains N, the number of Roman numerals.

Each of the next N lines contains one Roman numeral. Every Roman numeral follows the rules above and represents a natural number no greater than 10000.

Output

Convert each given Roman numeral to a decimal integer and print the results, one per line.

Hint

Daft Punk - Veridis Quo

Examples1

  1. Example 1

    Input
    3
    IX
    MMDCII
    DXII
    
    Expected output
    9
    2602
    512