toInt :: Char -> Int
toInt 'I' = 1
toInt 'V' = 5
toInt 'X' = 10
toInt 'L' = 50
toInt 'C' = 100
toInt 'D' = 500
toInt 'M' = 1000
toInt _ = undefined

plus :: Int -> (Int , Int) -> (Int , Int)
plus c (p,n)
    | c < p = (c , n - c)
    | otherwise = (c , n + c)

solve :: [Char] -> IO ()
solve = print . snd . foldr plus (0 , 0) . map toInt

main :: IO ()
main = getContents
    >>= mapM_ solve . lines