using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using CHY72;

namespace CHY72{
    
    public class PointXY<T>where T:IComparable{
        public T X , Y;
        public bool Flag = false;
        public PointXY(T _X,T _Y) {
            X = _X; Y = _Y;
        }
        public static bool operator ==(PointXY<T> P1, PointXY<T> P2) {
            return (P1.X.CompareTo( P2.X) == 0 && P1.Y.CompareTo (P2.Y) == 0);
        }
        public static bool operator !=(PointXY<T> P1, PointXY<T> P2) {return !(P1 ==P2);}
        public override int GetHashCode() {return X.GetHashCode() ^ Y.GetHashCode();}
        public override bool Equals(object obj) {
            if (obj == null || this.GetType() != obj.GetType()) {return false;}
            return (this == (PointXY<T>)obj);
        }         

    }
    
}    

public class Program {
    static bool ReadLine_1(ref int num1, bool Fin0 = true) {
        string str = Console.ReadLine();
        num1 = int.Parse(str);
        return (Fin0 && num1 == 0);
    }
    static int  ReadLine_1() {
        return  int.Parse(Console.ReadLine());
    }
    static bool ReadLine_2(ref int num1, ref int num2, char Spliter = ' ', bool Fin0 = true) {
        string str = Console.ReadLine();
        num1 = int.Parse(str.Split(Spliter)[0]);
        num2 = int.Parse(str.Split(Spliter)[1]);
        return (Fin0 && num1 == 0);
    }
    static int[] ReadLine72(char Spliter = ' ') {
        return Console.ReadLine().Split(Spliter).Select(s => int.Parse(s)).ToArray();
    }

    const int DefaultMax = 727272;


    //char '0' == int 48 ;....;char '9' == int 57;
    static int FromMCXI(string MCXIstr) {
        char[] MCXI = MCXIstr.ToCharArray();
        int RESULT = 0;
        int CurrentNum = 1;
        for (int i = 0; i < MCXI.Length; i++) {
            switch (MCXI[i]) {
                case 'm': RESULT += CurrentNum * 1000; CurrentNum = 1; break;
                case 'c': RESULT += CurrentNum * 100; CurrentNum = 1; break;
                case 'x': RESULT += CurrentNum * 10; CurrentNum = 1; break;
                case 'i': RESULT += CurrentNum * 1; CurrentNum = 1; break;
                default: CurrentNum = MCXI[i] - 48 ; break;
            
            }                   
        }
        return RESULT;
    }

    static string ToMCXI(int Num) {
        string RESULT ="";
        int[] KETA = new int [4]{0,0,0,0};
        if (Num >= 1000) { KETA[0] = (int)(Num / 1000); Num -= 1000 * KETA[0]; }
        if (Num >= 100) { KETA[1] = (int)(Num / 100); Num -= 100 * KETA[1]; }
        if (Num >= 10) { KETA[2] = (int)(Num / 10); Num -= 10 * KETA[2]; }
        if (Num >= 1) { KETA[3] = (int)(Num / 1); Num -= 1 * KETA[3]; }
        if (KETA[0] >= 2) RESULT += KETA[0];
        if (KETA[0] >= 1) RESULT += "m";
        if (KETA[1] >= 2) RESULT += KETA[1];
        if (KETA[1] >= 1) RESULT += "c";
        if (KETA[2] >= 2) RESULT += KETA[2];
        if (KETA[2] >= 1) RESULT += "x";
        if (KETA[3] >= 2) RESULT += KETA[3];
        if (KETA[3] >= 1) RESULT += "i";
        return RESULT;
    }

    static void Main(string[] args) {
        int iMax = ReadLine_1();
        for (int i = 0; i < iMax; i ++ ) {
            string str = Console.ReadLine();
            string str1 = str.Split()[0];
            string str2 = str.Split()[1];
            int n1 = FromMCXI(str1);
            int n2 = FromMCXI(str2);
            Console.WriteLine(ToMCXI (n1 + n2 ));

        }

    }
}

Measured memory_bytes: 5820