Problem:
Prof. Hachioji has devised a new numeral system of integral numbers with four lowercase letters "m", "c", "x", "i" and with eight digits "2", "3", "4", "5", "6", "7", "8", "9". He doesn't use digit "0" nor digit "1" 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 2, ..., 9, respectively. This system has nothing to do with the Roman numeral system.  For example, character strings  > "5m2c3x4i", "m2c4i" and "5m2c3x"  correspond to the integral numbers 5234 (=5*1000+2*100+3*10+4*1), 1204 (=1000+2*100+4*1), and 5230 (=5*1000+2*100+3*10), respectively. The parts of strings in the above example, "5m", "2c", "3x" and "4i" represent 5000 (=5*1000), 200 (=2*100), 30 (=3*10) and 4 (=4*1), respectively.  Each of the letters "m", "c", "x" and "i" may be prefixed by one of the digits "2", "3", ..., "9". In that case, the prefix digit and the letter are regarded as a pair. A pair that consists of a prefix digit and a letter corresponds to an integer that is equal to the original value of the letter multiplied by the value of the prefix digit.  For each letter "m", "c", "x" and "i", the number of its occurrence in a string is at most one. When it has a prefix digit, it should appear together with the prefix digit. The letters "m", "c", "x" and "i" must appear in this order, from left to right. Moreover, when a digit exists in a string, it should appear as the prefix digit of the following letter. Each letter may be omitted in a string, but the whole string must not be empty. A string made in this manner is called an _MCXI-string_.  An MCXI-string corresponds to a positive integer that is the sum of the values of the letters and those of the pairs contained in it as mentioned above. The positive integer corresponding to an MCXI-string is called its MCXI-value. Moreover, given an integer from 1 to 9999, there is a unique MCXI-string whose MCXI-value is equal to the given integer. For example, the MCXI-value of an MCXI-string "m2c4i" is 1204 that is equal to `1000 + 2*100 + 4*1`. There are no MCXI-strings but "m2c4i" that correspond to 1204. Note that strings "1m2c4i", "mcc4i", "m2c0x4i", and "2cm4i" are not valid MCXI-strings. The reasons are use of "1", multiple occurrences of "c", use of "0", and the wrong order of "c" and "m", respectively.  Your job is to write a program for Prof. Hachioji that reads two MCXI-strings, computes the sum of their MCXI-values, and prints the MCXI-string corresponding to the result.

Incorrect submission (verdict: Wrong Answer):
ans_list = []
n = int(input())

def mcxl2digit(s):
    ans = 0
    dig = 1
    for i in range(len(s)):
        if "2" <= s[i] <= "9":
            dig = int(s[i])
        else:
            if s[i] == "m":
                key = 100
            elif s[i] == "c":
                key = 100
            elif s[i] == "x":
                key = 10
            else:
                key = 1
            ans += key * dig
            dig = 1
    return ans

def digit2mcxl(i):
    return_list = []
    m = i // 1000
    if m != 0 and m != 1:
        return_list.append(str(m))
    if m != 0:
        return_list.append("m")
    i = i % 1000

    c = i // 100
    if c != 0 and c != 1:
        return_list.append(str(c))
    if c != 0:
        return_list.append("c")
    i = i % 100

    x = i // 10
    if x != 0 and x != 1:
        return_list.append(str(x))
    if x != 0:
        return_list.append("x")
    i = i % 10

    l = i
    if l != 0 and l != 1:
        return_list.append(str(l))
    if l != 0:
        return_list.append("l")

    return return_list

for i in range(n):
    a, b = input().split()
    ans = (mcxl2digit(a) + mcxl2digit(b))
    ans = digit2mcxl(ans)
    ans_list.append(ans)
for i in ans_list:
    print("".join(i))


Fixed, accepted submission:
ans_list = []
n = int(input())

def mcxl2digit(s):
    ans = 0
    dig = 1
    for i in range(len(s)):
        if "2" <= s[i] <= "9":
            dig = int(s[i])
        else:
            if s[i] == "m":
                key = 1000
            elif s[i] == "c":
                key = 100
            elif s[i] == "x":
                key = 10
            else:
                key = 1
            ans += key * dig
            dig = 1
    return ans

def digit2mcxl(i):
    return_list = []
    m = i // 1000
    if m != 0 and m != 1:
        return_list.append(str(m))
    if m != 0:
        return_list.append("m")
    i = i % 1000

    c = i // 100
    if c != 0 and c != 1:
        return_list.append(str(c))
    if c != 0:
        return_list.append("c")
    i = i % 100

    x = i // 10
    if x != 0 and x != 1:
        return_list.append(str(x))
    if x != 0:
        return_list.append("x")
    i = i % 10

    l = i
    if l != 0 and l != 1:
        return_list.append(str(l))
    if l != 0:
        return_list.append("i")

    return return_list

for i in range(n):
    a, b = input().split()
    ans = (mcxl2digit(a) + mcxl2digit(b))
    
    #print(ans)
    ans = digit2mcxl(ans)
    ans_list.append(ans)
for i in ans_list:
    print("".join(i))