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):
n=int(input())
for i in range(n):
    s0,s1=map(str,input().split())
    ans=""
    ans0=0
    ans1=0
    ans2=0
    p=1
    for j in range(len(s0)):
        if s0[j]!="m" and s0[j]!="c" and s0[j]!="x" and s0[j]!="i" :
            p=int(s0[j])
            continue
        if s0[j]=="m":
            ans0+=p*1000
            p=1 
        if s0[j]=="c":
            ans0+=p*100
            p=1
        if s0[j]=="x":
            ans0+=p*10
            p=1
        if s0[j]=="i":
            ans0+=p*1
            p=1
    for k in range(len(s1)):
        if s1[k]!="m" and s1[k]!="c" and s1[k]!="x" and s1[k]!="i" :
            p=int(s1[k])
            continue
        if s1[k]=="m":
            ans1+=p*1000 
            p=1
        if s1[k]=="c":
            ans1+=p*100
            p=1
        if s1[k]=="x":
            ans1+=p*10
            p=1
        if s1[k]=="i":
            ans1+=p*1
            p=1
    print(ans0)
    print(ans1)
    ans2=ans0+ans1
    while ans2>0:
        if ans2>=1000:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"m"
            else:
                ans=ans+sans2[0]+"m"
            ans2=int(sans2[1:])
            continue
        if ans2>=100:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"c"
            else:
                ans=ans+sans2[0]+"c"
            ans2=int(sans2[1:])
            continue
        if ans2>=10:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"x"
            else:
                ans=ans+sans2[0]+"x"
            ans2=int(sans2[1:])
            continue
        if ans2>=1:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"i"
            else:
                ans=ans+sans2[0]+"i"
            break
    print(ans)


Fixed, accepted submission:
n=int(input())
for i in range(n):
    s0,s1=map(str,input().split())
    ans=""
    ans0=0
    ans1=0
    ans2=0
    p=1
    for j in range(len(s0)):
        if s0[j]!="m" and s0[j]!="c" and s0[j]!="x" and s0[j]!="i" :
            p=int(s0[j])
            continue
        if s0[j]=="m":
            ans0+=p*1000
            p=1 
        if s0[j]=="c":
            ans0+=p*100
            p=1
        if s0[j]=="x":
            ans0+=p*10
            p=1
        if s0[j]=="i":
            ans0+=p*1
            p=1
    for k in range(len(s1)):
        if s1[k]!="m" and s1[k]!="c" and s1[k]!="x" and s1[k]!="i" :
            p=int(s1[k])
            continue
        if s1[k]=="m":
            ans1+=p*1000 
            p=1
        if s1[k]=="c":
            ans1+=p*100
            p=1
        if s1[k]=="x":
            ans1+=p*10
            p=1
        if s1[k]=="i":
            ans1+=p*1
            p=1
    ans2=ans0+ans1
    while ans2>0:
        if ans2>=1000:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"m"
            else:
                ans=ans+sans2[0]+"m"
            ans2=int(sans2[1:])
            continue
        if ans2>=100:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"c"
            else:
                ans=ans+sans2[0]+"c"
            ans2=int(sans2[1:])
            continue
        if ans2>=10:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"x"
            else:
                ans=ans+sans2[0]+"x"
            ans2=int(sans2[1:])
            continue
        if ans2>=1:
            sans2=str(ans2)
            if sans2[0]=="1":
                ans=ans+"i"
            else:
                ans=ans+sans2[0]+"i"
            break
    print(ans)