import java.lang.Exception

fun main(args:Array<String>?): Unit {
    repeat(readLine()!!.trim().toInt()){
        println(readLine()!!.trim().split(' ').map(String::asMCXI).sum().toMCXI())
    }
}
fun String.asMCXI(): Int {
    fun sub(i: Int, digit: Int = 1): Int {
        return when{
            i >= this.length -> 0
            this[i].isDigit() -> sub(i + 1, this[i] - '0')
            else -> when(this[i]) {
                'm' -> 1000 * digit
                'c' -> 100 * digit
                'x' -> 10 * digit
                'i' -> digit
                else -> throw Exception()
            } + sub(i + 1)
        }
    }
    return sub(0)
}
fun Int.toMCXI(): String {
    val digit = listOf('m' to 1000, 'c' to 100, 'x' to 10, 'i' to 1)
    var rest = this
    var result = ""
    for ((c, d) in digit){
        if (rest >= d){
            if (rest / d == 1){
                result += c
            }else {
                result += (rest / d).toString() + c
            }
            rest %= d
        }
    }
    return result
}
