#include<Bits/stdc++.h>
using namespace std;
void work()
{
string s;
cin >> s;
deque<int> ans;
for(int i = s.length() - 1; i >= 0; --i){
if(s[i] == 'V'){
if(i && s[i - 1] == 'I'){
ans.push_front(4);
i--;
}
else{
ans.push_front(5);
}
}
else if(s[i] == 'I'){
if(i >= 3 && s[i - 1] == 'I' && s[i - 2] == 'I' && s[i - 3] == 'V'){
ans.push_front(8);
i -= 3;
}
else if(i >= 2 && s[i - 1] == 'I' && s[i - 2] == 'I'){
ans.push_front(3);
i -= 2;
}
else if(i >= 2 && s[i - 1] == 'I' && s[i - 1] == 'V'){
ans.push_front(7);
i--;
}
else if(i && s[i - 1] == 'I'){
ans.push_front(2);
i--;
}
else if(i && s[i - 1] == 'V'){
ans.push_front(6);
i--;
}
else{
ans.push_front(1);
}
}
}
for(auto i : ans){
cout << i;
}
cout << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while(t--)
work();
}