import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br =
            new BufferedReader(new InputStreamReader(System.in));

        int tc = Integer.parseInt(br.readLine().trim());
        while (tc-- > 0) {
            String roman = br.readLine().trim();
            Solution ob = new Solution();
            System.out.println(ob.romanNumeralToDecimal(roman));
        }
    }
}

class Solution {
	
    public int romanNumeralToDecimal(String str) {
        // code here
		int num=0, ans=0, lastDigit=Integer.MAX_VALUE;
		for(int i=0; i<str.length(); i++){
			char ch= str.charAt(i);

			if(ch=='I'){
				num=1;
			}
			else if(ch=='V'){
				num=5;
			}
			else if(ch=='X'){
				num=10;
			}
			else if(ch=='L'){
				num=50;
			}
			else if(ch=='C'){
				num=100;
			}
			else if(ch=='D'){
				num=500;
			}
			else if(ch=='M'){
				num=1000;
			}

              //  if(ans>0 && ans<num){
				//		ans= num-ans;
				//	}
		    //		else{
						ans+=num;
					if(lastDigit<num){
						  
						 ans-=(2*lastDigit);
					 }
					//}
					lastDigit=num;
			
		}

		return ans;

    }
}