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

namespace Contest
{
    class Program
    {
        static int Main(string[] args)
        {
            new Program().Process();

            return 0;
        }

        private CommonInput cin;

        private void Process()
        {
            this.cin = new CommonInput();

            int n;
            string mcxi1, mcxi2, result;

            n = cin.GetInt();

            for (int i = 0; i < n; i++)
            {
                mcxi1 = cin.GetNext();
                mcxi2 = cin.GetNext();

                result = ToMcxi(ToInt(mcxi1) + ToInt(mcxi2));

                Console.WriteLine(result);
            }
        }

        private int ToInt(string mcxi)
        {
            int result = 0;

            var m = new Regex(@"\d?m").Match(mcxi).ToString();
            var c = new Regex(@"\d?c").Match(mcxi).ToString();
            var x = new Regex(@"\d?x").Match(mcxi).ToString();
            var i = new Regex(@"\d?i").Match(mcxi).ToString();

            if (m != "")
            {
                if (m == "m")
                    result += 1000;
                else
                    result += int.Parse(m[0].ToString()) * 1000;
            }

            if (c != "")
            {
                if (c == "c")
                    result += 100;
                else
                    result += int.Parse(c[0].ToString()) * 100;
            }

            if (x != "")
            {
                if (x == "x")
                    result += 10;
                else
                    result += int.Parse(x[0].ToString()) * 10;
            }

            if (i != "")
            {
                if (i == "i")
                    result += 1;
                else
                    result += int.Parse(i[0].ToString());
            }

            return result;
        }

        private string ToMcxi(int num)
        {
            StringBuilder result = new StringBuilder();

            int m, c, x, i;
            m = num / 1000;
            c = (num % 1000) / 100;
            x = (num % 100) / 10;
            i = num % 10;

            if (m > 1)
                result.AppendFormat("{0}m", m);
            else if (m == 1)
                result.Append("m");

            if (c > 1)
                result.AppendFormat("{0}c", c);
            else if (c == 1)
                result.Append("c");

            if (x > 1)
                result.AppendFormat("{0}x", x);
            else if (x == 1)
                result.Append("x");
            
            if (i > 1)
                result.AppendFormat("{0}i", i);
            else if (i == 1)
                result.Append("i");

            return result.ToString();
        }
    }

    public class CommonInput
    {
        public CommonInput()
        {
            this._s = new string[0];
            this._i = 0;
        }

        private string[] _s;
        private int _i;
        private char[] _sep = { ' ', ',' };

        public string GetNext()
        {
            if (this._i < this._s.Length)
                return this._s[this._i++];

            string line;
            this._i = 0;
            do
            {
                line = Console.ReadLine();
            } while (line == "");

            this._s = line.Split(this._sep, StringSplitOptions.RemoveEmptyEntries);

            return this._s[this._i++];
        }

        public int GetInt()
        {
            return int.Parse(this.GetNext());
        }

        public long GetLong()
        {
            return long.Parse(this.GetNext());
        }

        public double GetDouble()
        {
            return double.Parse(this.GetNext());
        }
    }
}

Measured memory_bytes: 7348