MCXI Numeral System

Time limit1sMemory limit128 MB

Summary
Read two MCXI-strings, add their values, and print the canonical MCXI-string for the sum.
Level

Easy3 of 10

Topics
String, Implementation, Math
Solved
No attempts yet

Problem

Prof. Hachioji has devised a new numeral system for integers using four lowercase letters "m", "c", "x", "i" and eight digits "2", "3", "4", "5", "6", "7", "8", "9". The digits "0" and "1" are never used in this system.

The letters "m", "c", "x", and "i" correspond to 1000, 100, 10, and 1 respectively, and the digits "2", ..., "9" correspond to their face values 2, ..., 9. This system has nothing to do with the Roman numeral system.

For example, the strings "5m2c3x4i", "m2c4i", and "5m2c3x" correspond to the integers 5234=5×1000+2×100+3×10+4×15234 = 5\times1000 + 2\times100 + 3\times10 + 4\times1, 1204=1000+2×100+4×11204 = 1000 + 2\times100 + 4\times1, and 5230=5×1000+2×100+3×105230 = 5\times1000 + 2\times100 + 3\times10. The substrings "5m", "2c", "3x", and "4i" represent 50005000, 200200, 3030, and 44.

Each of the letters "m", "c", "x", and "i" may be prefixed by one digit from "2" to "9". In that case the prefix digit and the letter form a pair whose value equals the letter's value multiplied by the prefix digit.

A string is a valid MCXI-string when all of the following hold:

  • Each of the letters "m", "c", "x", "i" occurs at most once.
  • A prefix digit, when present, appears immediately before its letter.
  • The letters appear in the order m, c, x, i from left to right.
  • Every digit is the prefix of the letter that follows it.
  • Any letter may be omitted, but the whole string must not be empty.

The MCXI-value of an MCXI-string is the sum of the values of its letters and pairs. For every integer from 1 to 9999 there is exactly one MCXI-string with that MCXI-value; for instance, the only MCXI-string whose value is 1204 is "m2c4i". The strings "1m2c4i", "mcc4i", "m2c0x4i", and "2cm4i" are invalid, because they use "1", repeat "c", use "0", and place "c" before "m" in the wrong order, respectively.

Read two MCXI-strings, compute the sum of their MCXI-values, and print the MCXI-string corresponding to that sum.

Input

The first line contains a positive integer nn (n≤500n \le 500), the number of the following lines. Each of the next nn lines describes one computation:

MCXI-string1 MCXI-string2

The two MCXI-strings are separated by a single space. For every line, the sum of the two MCXI-values is at most 9999.

Output

For each line of the input, print on its own line a single MCXI-string whose MCXI-value equals the sum of the two given MCXI-values. No other characters should appear in the output.

Examples3

  1. Example 1

    Input
    10
    xi x9i
    i 9i
    c2x2i 4c8x8i
    m2ci 4m7c9x8i
    9c9x9i i
    i 9m9c9x8i
    m i
    i m
    m9i i
    9m8c7xi c2x8i
    
    Expected output
    3x
    x
    6cx
    5m9c9x9i
    m
    9m9c9x9i
    mi
    mi
    mx
    9m9c9x9i
    
  2. Example 2

    Input
    1
    i i
    
    Expected output
    2i
    
  3. Example 3

    Input
    1
    m m
    
    Expected output
    2m