#include<stdio.h>
#include<math.h>

#define SIZE 10	//??????????????§?????????

int Main();
void SetChar(char *c, int size);
int ConvertMCXItoI(char *str);
int ConvertItoMCXI(char *str, int value);

int main(int argc, char *argv[]){
	int n = 0, i = 0;
	if( scanf("%d", &n) != 1 ){ return -1; }
	for( i = 0; i < n; i++ ){ Main(); }
	return 0;
}

int Main(){
	char ans[SIZE], str1[SIZE], str2[SIZE];	//???, ?????????1, ?????????2???
	SetChar(ans, SIZE);
	SetChar(str1, SIZE);
	SetChar(str2, SIZE);
	if( scanf("%s %s", str1, str2) != 2 ){ return -1; }
	ConvertItoMCXI(ans, ConvertMCXItoI(str1) + ConvertMCXItoI(str2));
	printf("%s\n", ans);
	return 0;
}

void SetChar(char *c, int size){
	int i = 0;
	for( i = 0; i < size; i++ ){
		c[i] = '\0';
	}
	return;
}

int ConvertMCXItoI(char str[SIZE]){
	int i = 0;
	int ans = 0;	//?????????????????????
	int coef = 1;	//?????°???
	for( i = 0; i < SIZE || str[i] != '\0'; i++ ){
		if( '2' <= str[i] && str[i] <= '9' ){
			coef = str[i] - '0';
		}else if( str[i] == 'm' ){
			ans += coef * 1000;
			coef = 1;
		}else if( str[i] == 'c' ){
			ans += coef * 100;
			coef = 1;
		}else if( str[i] == 'x' ){
			ans += coef * 10;
			coef = 1;
		}else if( str[i] == 'i' ){
			ans += coef;
		}
	}
	return ans;
}

int ConvertItoMCXI(char *str, int value){
	int coef = 0;	//?????°???
	int count = 0;	//???????????????????????????
	char mcxi[4] = {'i', 'x', 'c', 'm'};	//??????mxci?????????????????????????????????????????????
	int i = 0;
	//?????????????????????????????????????±??????????MCXI????????????????????°????±??????????
	for( i = 3; i >= 0; i-- ){
		coef = value / (int)pow(10, i);
		if( coef == 0 ){
			continue;
		}else if( coef == 1 ){
			str[count++] = mcxi[i];
		}else if( 2 <= coef && coef <= 9 ){
			str[count++] = coef + '0';
			str[count++] = mcxi[i];
		}
		value -= coef * (int)pow(10, i);
	}
	return 0;
}