Roman Expressions

No attempts yetTime limit1sMemory limit128 MB

Problem

A marketing company called ACM produces all sorts of quirky promotional items that are handed out to customers and business partners as small gifts. One of its specialties is a calculator that works with Roman numerals.

Roman numerals can express any non-negative integer using uppercase letters:

SymbolIVXLCDM
Value1510501005001000

A number is written by placing letters next to one another, and a letter of higher value must always come before letters of lower value. The only exceptions are the letters "I", "X", and "C": each may be placed before a higher letter to form a value whose last digit is 4 or 9. The only allowed combinations are:

CombinationIVIXXLXCCDCM
Value494090400900

Every Roman numeral must express thousands first, then hundreds, then tens, and finally ones. Therefore 499 must always be written as "CDXCIX", never as "ID".

Although not very practical, this gift is considered extremely cool. Your task is to write the software for this calculator.

Input

The input consists of commands, each written on its own line. The possible commands are assignments, "RESET", and "QUIT".

An assignment command begins with a single digit that names one of the calculator's ten registers (0 through 9). The register number is followed by an equals sign ("=") and an expression. The expression contains only valid Roman numerals, register names (single digits), and the plus ("+") and minus ("-") signs. You may assume that every expression is valid and no longer than 10000 characters.

The "QUIT" command is always the last command in the input.

Output

For each command, print exactly one line.

  • For an assignment, print the register name, an equals sign, and the value assigned to that register written as a Roman numeral. Print the word "Error" instead when the expression refers to a register that has not been assigned yet, or when the result is negative or greater than 10000. In these cases no register is changed.
  • For a "RESET" command, discard all previous register assignments and print the word "Ready".
  • For the "QUIT" command, print the word "Bye" and terminate the program.