Converting Romans

시간 제한1초메모리 제한1024 MB

요약
작은 숫자가 큰 숫자보다 앞에 나오면 무조건 빼는 규칙을 따르는 로마 숫자 n개를 읽고 각각 아라비아 숫자로 바꾸어 출력한다.
난이도

보통10점 중 4점

유형
문자열, 수학, 구현
정답자
아직 제출이 없습니다

문제

The Roman numerals are a numeral system that originated in ancient Rome and was widely used throughout Europe well into the Late Middle Ages. It differs from the Arabic system that we mostly use today in that numbers are written with combinations of letters from the Latin alphabet, where each letter is assigned a fixed integer value. Over the years many different letters were used, which was a cause of frequent confusion. But "modern" style uses only these seven:

IVXLCDM
1510501005001000

A very common misunderstanding of the Roman numerals is that they use a universal subtractive syntax. In a subtractive syntax, a lower digit written before a larger digit will be subtracted from the larger digit. 4 can be written as IIII and IV (5−1)(5-1). Romans themselves only really used the subtractive syntax for smaller numbers. For larger numbers, it was largely avoided, to give more 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?

You are given nn numbers written in the Roman numeral system, and your task is to convert them to the regular Arabic number system.

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

입력

The inputs 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 the corresponding Arabic numbers, one number per line.

예제1

  1. 예제 1

    입력
    5
    CDXCIX
    ID
    IV
    IIIIV
    IIIIIIV
    
    예상 출력
    499
    499
    4
    1
    -1