Hexaroman Numbers#1239

Hexaroman Numbers

1 second
64 megabytes
Very easy

Let us consider the traditional Roman numeral system and construct its generalization.

In the usual (decimal) Roman notation, there are Roman numerals: I , V , X = 10_{10}, L = 50_{10}, C = 100_{10}, D = 500_{10}, M = 1000_{10}.

Usually, Roman numerals are written in decreasing order, and their values are added together (for example, MMX () is interpreted as 1000 + 1000 + 10). If a smaller-value numeral appears before a larger-value numeral, the smaller value is subtracted from the larger and added to the total sum. For example, MCMXLIV equals . For subtractive constructs, the following restrictions apply: I may only precede V and X, X may only precede L or C. The numerals V, L, and D may only be followed by a numeral of greater value.

Now, let's introduce hexadecimal Roman notation, where numerals are assigned the following values: I , V , X = 10_{16}, L = 80_{16}, C = 100_{16}, D = 800_{16}, M = 1000_{16}.

The usage rules for numerals are the same as in the decimal Roman system, except that a notation like IIX is valid. If a number can be represented in multiple valid ways due to this, the notation with fewer symbols is used; if the number of symbols is the same, the notation with fewer subtractions is used (for example, between IIIIX and VIIII for C_{16}, the latter is chosen). For example, F_{16} is written as IX, and 5C8_{16} as CCCDLXXXXV.

Write a program to perform arithmetic operations (addition, subtraction, multiplication) on hexadecimal Roman numerals. The input data and calculation results are guaranteed to be positive integers not exceeding 4FFF_{16}.

Input

The first line contains an integer (0 < N \leq 100) — the number of test cases. Each test case is given in the format \langle\textbf{A}\rangle\langle\textbf{O}\rangle\langle\textbf{B}\rangle, where and are hexadecimal Roman numerals, and is one of the operations: , , or . No test case contains spaces.

Output

For each test case, output the result of the calculation in hexadecimal Roman notation on a separate line.

Examples

Example 1

Loading
Loading