#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair Pr; int N; ll sum; int main() { cin >> N; for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < s.size(); j++) { if (s[j] == 'I') { if (j + 1 < s.size()) { if (s[j + 1] == 'X')sum += 9, j++; else if (s[j + 1] == 'V')sum += 4, j++; else sum++; } else sum++; } else if (s[j] == 'V')sum += 5; else if (s[j] == 'X' ){ if(j + 1 < s.size()) { if (s[j + 1] == 'L')sum += 40, j++; else if (s[j + 1] == 'C')sum += 90, j++; else sum += 10; } else sum += 10; } else if (s[j] == 'L')sum += 50; else if (s[j] == 'C') { if(j + 1 < s.size()) { if (s[j + 1] == 'D')sum += 400, j++; else if (s[j + 1] == 'M')sum += 900, j++; else sum += 100; } else sum += 100; } else if (s[j] == 'D')sum += 500; else if (s[j] == 'M')sum += 1000; } } if (sum > 3999) { cout << "ERROR" << endl; } else { int m = sum / 1000; sum -= m * 1000; for (int i = 0; i < m; i++) cout << "M"; int c = sum / 100; if (c <= 3) { for (int i = 0; i < c; i++) cout << "C"; sum -= c * 100; }else{ if (c == 4)cout << "CD", sum -= 400; else if (c == 5)cout << "D", sum -= 500; else if (c == 6)cout << "DC", sum -= 600; else if (c == 7)cout << "DCC", sum -= 700; else if (c == 8)cout << "DCCC", sum -= 800; else if (c == 9)cout << "CM", sum -= 900; } int x = sum / 10; if (x <= 3) { for (int i = 0; i < x; i++) cout << "X"; sum -= x * 10; } else { if (x == 4)cout << "XL", sum -= 40; else if (x == 5)cout << "L", sum -= 50; else if (x == 6)cout << "LX", sum -= 60; else if (x == 7)cout << "LXX", sum -= 70; else if (x == 8)cout << "LXXX", sum -= 80; else if (x == 9)cout << "XC", sum -= 90; } if (sum <= 3) { for (int i = 0; i < sum; i++) cout << "I"; }else{ if (sum == 4)cout << "IV"; else if (sum == 5)cout << "V"; else if (sum == 6)cout << "VI"; else if (sum == 7)cout << "VII"; else if (sum == 8)cout << "VIII"; else if (sum == 9)cout << "IX"; } cout << endl; } return 0; }