eolymp
bolt
Try our new interface for solving problems
Problems

Hexaroman Numbers

Hexaroman Numbers

Roman numerals are numeral system of ancient Rome based on letters of the alphabet, which are combined to signify the sum (or in some cases, the difference) of their values. This system is decimal but not directly positional, since same digits standing at different positions in usual decimal system are represented by different Roman digits, and one decimal digit could be represented by few Roman. There are seven Roman numerals associated to the decimal \textbf{I} = \textbf{1}, \textbf{V} = \textbf{5}, \textbf{X} = \textbf{10_10}, \textbf{L} = \textbf{50_10}, \textbf{C} = \textbf{100_10}, \textbf{D} = \textbf{500_10}, \textbf{M} = \textbf{1000_10}. Generally, Roman numerals are written in descending order from left to right, and are added sequentially, for example \textbf{MMX} (\textbf{2010}) is interpreted as \textbf{1000} + \textbf{1000} + \textbf{10}. Certain combinations employ a subtractive principle, which specifies that where a symbol of smaller value precedes a symbol of larger value, the smaller value is subtracted from the larger value, and the result is added to the total. For example, \textbf{MCMXLIV} equals \textbf{1944}. \textbf{I} may precede \textbf{V} and \textbf{X}, \textbf{X} may precede \textbf{L} or \textbf{C}. The numerals \textbf{V}, \textbf{L}, and \textbf{D} may not be followed by a numeral of greater or equal value. Subtractive principle has been introduced at medevail adges. Originally it allowes only one symbol of smaller value to preced a symbol of larger value. Nowadays this limitation could be skipped to allow shorter notation for some numbers. Also modern computers are much better in hexadecimal numbers and probably it makes sence to introduce "hexadedimal" Roman notation. Let hexadecimal Roman notation be a notation in which Roman numerals are equivalent to the following numbers: \textbf{I} = \textbf{1}, \textbf{V} = \textbf{8}, \textbf{X} = \textbf{10_16}, \textbf{L} = \textbf{80_16}, \textbf{C} = \textbf{100_16}, \textbf{D} = \textbf{800_16}, \textbf{M} = \textbf{1000_16}. Is some hexadecima digit can be represented using addition and subtraction, notation with less symbols is used. If both notations produce same number of symbols addition is used. For example number \textbf{F_16} is written as \textbf{IX_16}, \textbf{5C8_16} becomes \textbf{CCCDLXXXXV_16}. You need to write a program which can perform computation in hexadecimal Roman notation, namely the following operations: addition, subtraction, and multiplication. All source numbers and results will be integers within \[\textbf{1}, \textbf{4FFF_16}\]. \InputFile The first line at input contains one integer \textbf{N} (\textbf{0} < \textbf{N} ≤ \textbf{100}), the number of test lines. Each line contains test data in format "<\textbf{A}><\textbf{O}><\textbf{B}>" whitout any spaces. <\textbf{A}> and <\textbf{B}> are numbers in hexadecimal Roman notation, <\textbf{O}> designates operation: +, -, *. \OutputFile Every line at output should contain hexadecimal Roman notation of calculation result for corresponding test line at the input.
Time limit 1 second
Memory limit 64 MiB
Input example #1
1
XIIV+XXXXII
Output example #1
XXXLV