import scala.util.parsing.combinator._

object MCXIParser extends RegexParsers {
  def parserNum(s: String): Int = if (s.size < 2) s.size else Integer.parseInt(s.substring(0, 1))
  def m = "([2-9]m|m)?".r ^^ {s => parserNum(s) }
  def c = "([2-9]c|c)?".r ^^ {s => parserNum(s) }
  def x = "([2-9]x|x)?".r ^^ {s => parserNum(s) }
  def i = "([2-9]i|i)?".r ^^ {s => parserNum(s) }
  def full = m ~ c ~ x ~ i ^^ {case m ~ c ~ x ~ i => MCXI(m, c, x, i)}
  def parse(input: String) = parseAll(full, input)
}

case class MCXI(m: Int, c: Int, x: Int, i: Int) {

  def +(that: MCXI): MCXI = {
    val i = this.i + that.i
    val x = this.x + that.x + (i / 10)
    val c = this.c + that.c + (x / 10)
    val m = this.m + that.m + (c / 10)
    MCXI(m, c % 10, x % 10, i % 10)
  }

  def transform(v: Int, s: String): String = {
    if (v == 0)
      ""
    else if (v == 1)
      s
    else
      v.toString + s
  }

  override def toString() = transform(m, "m") +
      transform(c, "c") +
      transform(x, "x") +
      transform(i, "i")
}


object Main {

  def main(args: Array[String]) {
    val source = scala.io.Source.fromFile(args(0)).getLines.toSeq

    source.drop(1).foreach(s =>
      println(s.split(" ").foldLeft(MCXI(0, 0, 0, 0))((sum, s) => sum + MCXIParser.parse(s).get))
    )
  }
}