Pompeiian Numerals

input file: pompeii.in

Although you have learned about Roman numerals for years, you have probably not heard about a variation of them, the Pompeiian numerals. You probably remember much of Pompeii was destroyed when Vesuvius exploded, but a few residents survived, and continued to use the Roman numeral system. Unfortunately, these survivors lost their little fingers as a result of the explosion, so their number system is based on a base 8 number system, instead of a base 10 number system, even though it uses the same symbols as Roman numerals. So, the Pompeiian numeral system has the following association of symbols to numbers:
 
Poempeiian Decimal
I 1
V 4
X 8
L 32 (= V * X)
C 64 (= X * X)
D 256 (= V * C)
M 512 (= X * C)

As with Roman numerals, the position of letters is important. If symbols are the same or decrease in value from left to right, their values were added; however, if a symbol had a smaller value than a symbol on its right, it is to be subtracted. For example, CX = 64 + 8 = 72, but XC = 64 - 8 = 56.

No more than two identical symbols are used in succession. For example, IV is used for 3 instead of III. Likewise, there are never more than two symbols involved in the subtractive feature. The only subtractive symbol combinations allowed are IV, IX, XL, XC, CD, and CM.

Write a Pompeiian Numeral Converter that reads Pompeiian numerals from a file and writes their decimal equivalents to a file, one per line.

Input:

The input file will be a list of numbers in Pompeiian form, one per line, with the first digit in the first column of the line and no embedded blanks. End of input will be marked by a line containing only the word ZERO in upper case; your program should not process this case (after all, zero hadn’t been invented yet).

Output:

The output should be the Pompeiian numeral first, followed by an equals sign and the decimal equivalent.

Sample input:

IX
CCI
XXII
XXIV
IV
CXCIX
LXXI
ZERO

Sample output:

IX = 7
CCI = 129
XXII = 18
XXIV = 19
IV = 3
CXCIX = 127
LXXI = 49