#include <iostream>
#include <cstdlib>
#include <cstring>
#include <stdio.h>

using namespace std;

char data[ 1005 ];
int  answ[ 101 ];
int  save[ 21 ];
int  valu[ 30 ] = {
	3000,2000,1000,900,800,700,600,500,400,300,200,
	100,90,80,70,60,50,40,30,20,
	10,9,8,7,6,5,4,3,2,1};

char rome[ 30 ][ 5 ] = {
	"MMM","MM","M","CM","DCCC","DCC","DC","D","CD","CCC",
	"CC","C","XC","LXXX","LXX","LX","L","XL","XXX","XX",
	"X","IX","VIII","VII","VI","V","IV","III","II","I"};

int main()
{
	int  a;
	char bb[ 3 ];
	while ( cin >> a >> bb >> data ) {
		for ( int i = 0 ; i < 20 ; ++ i )
			save[ i ] = 0;
		int le = strlen( data );
		for ( int i = 0 ; i < le ; ++ i ) {
			for ( int j = 0 ; j < 20 ; ++ j )
				save[ j ] *= a;
			if ( data[ i ] >= '0' && data[ i ] <= '9' )
				save[ 0 ] += data[ i ] - '0';
			else
				save[ 0 ] += data[ i ] - 'A'+10;
			for ( int j = 0 ; j < 19 ; ++ j )
				if ( save[ j ] > 9 ) {
					save[ j+1 ] += save[ j ]/10;
					save[  j  ] %= 10;
				}
		}
		int end = 20;
		while ( !save[ end ] ) -- end;
		if ( bb[ 0 ] == 'R' ) {
			int value = 0;
			for ( int i = 0 ; i <= end ; ++ i ) {
				value *= 10;
				value += save[ i ];
			}
			for ( int i = 0 ; i < 30 ; ++ i )
				if ( value >= valu[ i ] ) {
					printf("%s",rome[ i ]);
					value -= valu[ i ];
				}
			printf("\n");
		}else {
			int b = 0;
			int start = 0;
			while ( bb[ start ] ) {
				b *= 10;
				b += bb[ start ++ ] - '0';
			}
			int count = -1;
			for ( int k = 0 ; k < 60 ; ++ k ) {
				for ( int i = end ; i > 0 ; -- i ) {
					save[ i-1 ] += save[ i ]%b*10;
					save[  i  ] /= b;
				}
				answ[ ++ count ] = save[ 0 ]%b;
				save[ 0 ] /= b;
			}
			while ( !answ[ count ] ) -- count;
			while ( count >= 0 )
				if ( answ[ count ] >= 0 && answ[ count ] <= 9 )
					printf("%c",answ[ count -- ]+'0');
				else
					printf("%c",answ[ count -- ]+'A'-10);
			printf("\n");
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <string.h>
#include <string>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;

typedef long long ll;

ll a,b,c;

string w[] = { "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" };
int    v[] = { 1   ,   4  ,  5  ,   9  ,  10 ,  40  , 50  ,  90  , 100 ,  400 , 500 ,  900 , 1000 , 9999 };

string rim( ll n ){
    string ret = "";
    while( n ){
        int i = 0;
        for( ; v[i+1] <= n; ++i );

        ret += w[i];
        n -= v[i];
    }
    return ret;
}

ll digit( char a ){
    if( a >= '0' && a <= '9' ) return a - '0';
    else return a - 'A' + 10;
}

char rev( int a ){
    if( a <= 9 ) return '0' + a;
    else return 'A' + (a - 10);
}

char basea[1000 + 55];

int main(){
    cin >> a;

    char c;
    while( c=getchar() ) if( c >= '0' && c <= '9' || c == 'R' ) break;

    b = c - '0';

    if( c != 'R' ) while( (c=getchar())>='0' && c <='9') b = b * 10 + c - '0';

    scanf("%s",basea);

    int len = strlen(basea);
    ll dec = 0;
    ll stp = 1;

    for( int i = len-1; i >= 0; --i ){
        dec += digit( basea[i] ) * stp;
        stp *= a;
    }

    if( b == 34 ) cout << rim( dec ) <<endl;
    else{
        cout<<rev(10)<<endl;
        string sol = "";
        while( dec ){
            sol += string(1,rev( dec % b ) );
            dec /= b;
        }
        reverse( sol.begin(),sol.end() );
        cout<<sol<<endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <string.h>
#include <string>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;

typedef long long ll;

ll a,b,c;

string w[] = { "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" };
int    v[] = { 1   ,   4  ,  5  ,   9  ,  10 ,  40  , 50  ,  90  , 100 ,  400 , 500 ,  900 , 1000 , 9999 };

string rim( ll n ){
    string ret = "";
    while( n ){
        int i = 0;
        for( ; v[i+1] <= n; ++i );

        ret += w[i];
        n -= v[i];
    }
    return ret;
}

ll digit( char a ){
    if( a >= '0' && a <= '9' ) return a - '0';
    else return a - 'A' + 10;
}

char rev( int a ){
    if( a <= 9 ) return '0' + a;
    else return 'A' + (a - 10);
}

char basea[1000 + 55];

int main(){
    cin >> a;

    char c;
    while( c=getchar() ) if( c >= '0' && c <= '9' || c == 'R' ) break;

    b = c - '0';

    if( c != 'R' ) while( (c=getchar())>='0' && c <='9') b = b * 10 + c - '0';

    scanf("%s",basea);

    int len = strlen(basea);
    ll dec = 0;
    ll stp = 1;

    for( int i = len-1; i >= 0; --i ){
        dec += digit( basea[i] ) * stp;
        stp *= a;
    }

    if( b == 34 ) cout << rim( dec ) <<endl;
    else{
        string sol = "";
        while( dec ){
            sol += string(1,rev( dec % b ) );
            dec /= b;
        }
        reverse( sol.begin(),sol.end() );
        if( sol.size() == 0 ) printf("0\n");
        else cout<<sol<<endl;
    }

    return 0;
}


#include <iostream>
#include <stdio.h>

using namespace std;

void Print(int res)
{
	if(res<=9)
	{
		printf("%d",res);
	}
	else
	{
		res-=10;
		printf("%c",res+'A');
	}
}

void ConvertToBase(long long num,int c)
{
	int base = c;
	while(base*c<=num)
	{
		base*=c;
	}

	while(base>0)
	{
		long long res = num/base;

		num -= res*base;

		Print(res);

		base/=c;
	}

	printf("\n");

}

void ConvertToRoman(long long num)
{
	 string res;

     while (num >= 1000) { num -= 1000; res += "M"; }
     if (num >= 900) { num -= 900; res += "CM"; }
     while (num >= 500) { num -= 500; res += "D"; }
     if (num >= 400) { num -= 400; res += "CD"; }
     while (num >= 100) { num -= 100; res += "C"; }
     if (num >= 90) { num -= 90; res += "XC"; }
     while (num >= 50) { num -= 50; res += "L"; }
     if (num >= 40) { num -= 40; res += "XL"; }
     while (num >= 10) { num -= 10; res += "X"; }
     if (num >= 9) { num -= 9; res += "IX"; }
     while (num >= 5) { num -= 5; res += "V"; }
     if (num >= 4) { num -= 4; res += "IV"; }
     while (num >= 1) { num--; res += "I"; }

     cout<<res<<endl;

}

int main()
{
	int a;
	int b;
	char ch[4];

	scanf("%d %s",&a,ch);

	if(ch[0]=='R')
	{
		b=0;
	}
	else
	{
		sscanf(ch,"%d",&b);
	}

	long long c=0;

	string str;
	cin>>str;

	for(unsigned int n=0;n<str.length();n++)
	{
		c*=a;
		if(str[n]>='0' && str[n]<='9')
		{
			c+=str[n]-'0';
		}
		else
		{
			c+=10+(str[n]-'A');
		}
	}

	if(b==(0))
	{
		ConvertToRoman(c);
	}
	else
	{
		ConvertToBase(c,b);
	}

}


#include <iostream>
#include <stdio.h>

using namespace std;

void Print(int res)
{
	if(res<=9)
	{
		printf("%d",res);
	}
	else
	{
		res-=10;
		printf("%c",res+'A');
	}
}

void ConvertToBase(long long num,int c)
{
	long long base = 1;
	while(base*c<=num)
	{
		base*=c;
	}

	while(base>0)
	{
		long long res = num/base;

		num -= res*base;

		Print(res);

		base/=c;
	}

	printf("\n");

}

void ConvertToRoman(long long num)
{
	 string res;

     while (num >= 1000) { num -= 1000; res += "M"; }
     if (num >= 900) { num -= 900; res += "CM"; }
     while (num >= 500) { num -= 500; res += "D"; }
     if (num >= 400) { num -= 400; res += "CD"; }
     while (num >= 100) { num -= 100; res += "C"; }
     if (num >= 90) { num -= 90; res += "XC"; }
     while (num >= 50) { num -= 50; res += "L"; }
     if (num >= 40) { num -= 40; res += "XL"; }
     while (num >= 10) { num -= 10; res += "X"; }
     if (num >= 9) { num -= 9; res += "IX"; }
     while (num >= 5) { num -= 5; res += "V"; }
     if (num >= 4) { num -= 4; res += "IV"; }
     while (num >= 1) { num--; res += "I"; }

     cout<<res<<endl;

}

int main()
{
	int a;
	int b;
	char ch[4];

	scanf("%d %s",&a,ch);

	if(ch[0]=='R')
	{
		b=0;
	}
	else
	{
		sscanf(ch,"%d",&b);
	}

	long long c=0;

	string str;
	cin>>str;

	for(unsigned int n=0;n<str.length();n++)
	{
		c*=a;
		if(str[n]>='0' && str[n]<='9')
		{
			c+=str[n]-'0';
		}
		else
		{
			c+=10+(str[n]-'A');
		}
	}

	if(b==(0))
	{
		ConvertToRoman(c);
	}
	else
	{
		ConvertToBase(c,b);
	}

}


#include <iostream>
#include <stdio.h>

using namespace std;

void Print(int res)
{
	if(res<=9)
	{
		printf("%d",res);
	}
	else
	{
		res-=10;
		printf("%c",res+'A');
	}
}

void ConvertToBase(long long num,int c)
{
	int base = 1;
	while(base*c<=num)
	{
		base*=c;
	}

	while(base>0)
	{
		long long res = num/base;

		num -= res*base;

		Print(res);

		base/=c;
	}

	printf("\n");

}

void ConvertToRoman(long long num)
{
	 string res;

     while (num >= 1000) { num -= 1000; res += "M"; }
     if (num >= 900) { num -= 900; res += "CM"; }
     while (num >= 500) { num -= 500; res += "D"; }
     if (num >= 400) { num -= 400; res += "CD"; }
     while (num >= 100) { num -= 100; res += "C"; }
     if (num >= 90) { num -= 90; res += "XC"; }
     while (num >= 50) { num -= 50; res += "L"; }
     if (num >= 40) { num -= 40; res += "XL"; }
     while (num >= 10) { num -= 10; res += "X"; }
     if (num >= 9) { num -= 9; res += "IX"; }
     while (num >= 5) { num -= 5; res += "V"; }
     if (num >= 4) { num -= 4; res += "IV"; }
     while (num >= 1) { num--; res += "I"; }

     cout<<res<<endl;

}

int main()
{
	int a;
	int b;
	char ch[4];

	scanf("%d %s",&a,ch);

	if(ch[0]=='R')
	{
		b=0;
	}
	else
	{
		sscanf(ch,"%d",&b);
	}

	long long c=0;

	string str;
	cin>>str;

	for(unsigned int n=0;n<str.length();n++)
	{
		c*=a;
		if(str[n]>='0' && str[n]<='9')
		{
			c+=str[n]-'0';
		}
		else
		{
			c+=10+(str[n]-'A');
		}
	}

	if(b==(0))
	{
		ConvertToRoman(c);
	}
	else
	{
		ConvertToBase(c,b);
	}

}


#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <set>
#include <numeric>
#include <cmath>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <iomanip>

using namespace std;

#ifdef __GNUC__
typedef long long ll;typedef unsigned long long ull;
#else
typedef __int64 ll;  typedef unsigned __int64 ull;
#endif

#define INF 1<<28
#define SIZE 100
#define LEN 3001
#define PI 2*acos(0)
#define fg %I64d

#define rep(i,n) for (i=0;i<n;i++)
#define rev(i,n) for (i=n;i>=0;i--)
#define start(i,p,k) for (i=p; i<k;i++)
#define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)

#define bug(x)    cout<< "->" <<#x<<": "<<x<<endl
#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define MP(a,b) make_pair(a,b)
#define Clear(x,with) memset(x,with,sizeof(x))
#define Copy(c,r)   memcpy(c,r,sizeof(r))
#define SZ(x) (int)x.size()
#define length(x) (int)x.length()
#define All(x) x.begin(),x.end()
#define pb push_back
#define popcount(i) __builtin_popcount(i)
#define gcd(a,b)    __gcd(a,b)
#define fs first
#define sc second
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<double>vd;
typedef vector<ll>vll;
typedef vector<string> vs;
typedef vector<vi>vvi;
typedef vector<vll>vvll;
typedef vector<vd>vvd;
typedef vector<pii>vpii;
typedef map<string,int> msi;
typedef map<int,int>mii;
typedef map<pii,int>mpi;
set<string>SC;
template<class T> inline T sqr(T x){return x*x;}
template<class T> inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No.
template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();}
int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
ll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;}
bool toLower(char ch){if(ch>=97 && ch<=122) return true; return false;}
bool toUpper(char ch){if(ch>=65 && ch<=90) return true;return false;}
bool SCP(char S[],char SS[]){if(!strcmp(S,SS)) return true; return false;}
int Digit(int num){int cn=0;while(num){cn++;num/=10;}return cn;}
string BaseConversion(string xstr,int xbase,int ybase)
{   string str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";long long store[128],i,multi,sum;string ystr;for(i=sum=0;i<str.size();i++)store[str[i]]=i;multi=1;for(i=xstr.size()-1;i>=0;i--){sum+=(store[xstr[i]]*multi);multi*=xbase;
}    while(true){ystr.push_back(str[sum%ybase]);sum/=ybase;if(sum==0)break;}reverse(ystr.begin(),ystr.end());return ystr;}
long long BM ( long long base, long long pow, long long mod )    //Big Mod
{long long temp, result, rem ;temp = base % mod ;result = 1 ;while ( pow > 0 ){rem = pow % 2 ;pow /= 2 ;if ( rem == 1 ) result = ( result * temp ) % mod ;temp = ( temp * temp ) % mod ;}return result ;}
template<class T> void debug(const T& e){cout<<e<<endl;}
template<class T1,class T2> void debug(const T1& e1,const T2& e2){cout<<e1<<"\t"<<e2<<endl;}
template<class T1,class T2,class T3> void debug(const T1& e1,const T2& e2,const T3& e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;}
template<class T1,class T2,class T3,class T4> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;}
template<class T1,class T2,class T3,class T4,class T5> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;}
template<class T> void debug(vector<T>& e){int i;rep(i,SZ(e)) cout<<e[i]<<" ";cout<<endl;}
template<class T> void debug(vector< basic_string<T> >& e){int i,j;rep(i,SZ(e)) {rep(j,SZ(e[i])) cout<<e[i][j];cout<<endl;} cout<<endl;}
template<class T> void debug(vector< vector<T> >& e){int i,j;rep(i,SZ(e)) {rep(j,SZ(e[i])) cout<<e[i][j]<<"\t";cout<<endl;} cout<<endl;}
template<class T> void debug(T e[SIZE][SIZE],int row,int col){int i,j;rep(i,row) {rep(j,col) cout<<e[i][j]<<" ";cout<<endl;}}

ll Pow(ll B,ll P){  ll R=1; while(P>0)  {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
//struct pq{    int cost,node;bool operator<(const pq &b)const{return cost>b.cost;}};// Min Priority Queue
//bool comp(pq a,pq b){    return a.cost > b.cost;}   //Comp Sort

//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
/*struct pq
{
    int cost,node;
    bool operator<(const pq &b)const
    {
        return cost>b.cost;    // Min Priority Queue
    }
};*/
/*void seive()    //prime generate
{
    int i,j,r,c=0,p;for(i=3;i<=SIZE_N;i+=2)flag[i]=true;flag[2]=true;prime[c++]=2;p=SIZE_N+1;
    for(i=3;i<p;i+=2)if(flag[i]){prime[c++]=i;if(SIZE_N/i>=i){r=i*2;for(j=i*i;j<p;j+=r)flag[j]=false;}}
    //printf("Total prime: %d\n",c);
}*/

/*void divisor(long n)           //generate divisor
    {
        long i;
        Count = 0;
        for(i=1;i<sqrt(n);i++)
            if(n % i == 0)
            {
                divisors[Count++] = i;
                divisors[Count++] = n / i;
            }
        if((i * i) == n)
            divisors[Count++] = i;              //  Count is total number of divisor
        sort(&divisors[0],&divisors[Count]);    //  Sorts all the divisor of n
        return ;
    }*/

/*void PF(int nn)
{
    int k,i,j,m=0,cnt=0,n;k=sqrt(nn)+1; for(i=0;prime[i]<k;i++)
    if(nn%prime[i]==0){cnt=0;while(nn%prime[i]==0){nn/=prime[i];cnt++;}fac[m]=prime[i];fre[m++]=cnt;k=sqrt(nn)+1;}
    if(nn>1) {fac[m]=nn;fre[m++]=nn;}
}*/
void arabic(char str[]);
int roman(char str[]);
int convert_num(char str[]);
void convert_roman(int n);
void initial();
//void strrev(char str1[]);

char str[LEN],str1[LEN];
int index1,rvalue[100],rrank[100];

void strev(char str1[])
{
    int i,j;
    char temp;

    for(i=strlen(str1)-1,j=0;i>j;i--,j++)
        temp=str1[i],str1[i]=str1[j],str1[j]=temp;
}
int a[LEN];
int main()
{
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    //cnt=accumulate(scores.begin(),scores.end(),0); //sum
    int j,test,Case=1;
    int i,n,t,s;
    string xstr;int xbase,ybase;
    char S[101],S1[101],S2[101];
    while(scanf("%s %s %s",S,S1,S2)==3)
    {
        if(!strcmp(S1,"R"))
        {
                if(isdigit(S2[0]))
                {
                    arabic(S2);
                    strev(str1);
                    printf("%s\n",str1);
                }
        }
        else
        {
            xstr=S2;xbase=atoi(S);ybase=atoi(S1);
            xstr=BaseConversion(xstr,xbase,ybase);
            cout<<xstr<<endl;
        }

    }

    return 0;
}

void arabic(char str[])
{
    int num,k,part;

    num=convert_num(str);
    k=index1=0;
    while(num)
    {
        part=(num%10)*pow(10,k++);
        num/=10;
        convert_roman(part);
    }
    str1[index1]='\0';
}

int roman(char str[])
{
    int i,sum=0,len;

    len=strlen(str)-1;
    for(i=len;i>-1;i--)
    {
        if(i!=len && rrank[str[i]]<rrank[str[i+1]])
            sum-=rvalue[str[i]];
        else
            sum+=rvalue[str[i]];
    }
    return sum;
}

int convert_num(char str[])
{
    int sum=0,i;

    for(i=0;str[i]!='\0';i++)
        sum=sum*10+(str[i]-'0');
    return sum;
}

void convert_roman(int n)
{
    if(n<10)
    {
        if(n==4)
            str1[index1++]='V',str1[index1++]='I';
        else if(n==9)
            str1[index1++]='X',str1[index1++]='I';
        else if(n==5)
            str1[index1++]='V';
        else if(n>5)
        {
            n-=5;
            while(n--)
                str1[index1++]='I';
            str1[index1++]='V';
        }
        else
        {
            while(n--)
                str1[index1++]='I';
        }
    }
    else if(n<100)
    {
        if(n==40)
            str1[index1++]='L',str1[index1++]='X';
        else if(n==90)
            str1[index1++]='C',str1[index1++]='X';
        else if(n==50)
            str1[index1++]='L';
        else if(n>50)
        {
            n-=50;
            while(n)
            {
                str1[index1++]='X';
                n=n-10;
            }
            str1[index1++]='L';
        }
        else
        {
            while(n)
            {
                str1[index1++]='X';
                n-=10;
            }
        }
    }
    else if(n<1000)
    {
        if(n==400)
            str1[index1++]='D',str1[index1++]='C';
        else if(n==900)
            str1[index1++]='M',str1[index1++]='C';
        else if(n==500)
            str1[index1++]='D';
        else if(n>500)
        {
            n-=500;
            while(n)
            {
                str1[index1++]='C';
                n-=100;
            }
            str1[index1++]='D';
        }
        else
        {
            while(n)
            {
                str1[index1++]='C';
                n-=100;
            }
        }
    }
    else
    {
        while(n)
        {
            str1[index1++]='M';
            n-=1000;
        }
    }
}

void initial()
{
    rrank['I']=1,rrank['V']=2,rrank['X']=3,rrank['L']=4,rrank['C']=5,rrank['D']=6,rrank['M']=7;
    rvalue['I']=1,rvalue['V']=5,rvalue['X']=10,rvalue['L']=50,rvalue['C']=100,rvalue['D']=500,rvalue['M']=1000;
}


#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <set>
#include <numeric>
#include <cmath>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <iomanip>

using namespace std;

#ifdef __GNUC__
typedef long long ll;typedef unsigned long long ull;
#else
typedef __int64 ll;  typedef unsigned __int64 ull;
#endif

#define INF 1<<28
#define SIZE 100
#define LEN 1001
#define PI 2*acos(0)
#define fg %I64d

#define rep(i,n) for (i=0;i<n;i++)
#define rev(i,n) for (i=n;i>=0;i--)
#define start(i,p,k) for (i=p; i<k;i++)
#define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)

#define bug(x)    cout<< "->" <<#x<<": "<<x<<endl
#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define MP(a,b) make_pair(a,b)
#define Clear(x,with) memset(x,with,sizeof(x))
#define Copy(c,r)   memcpy(c,r,sizeof(r))
#define SZ(x) (int)x.size()
#define length(x) (int)x.length()
#define All(x) x.begin(),x.end()
#define pb push_back
#define popcount(i) __builtin_popcount(i)
#define gcd(a,b)    __gcd(a,b)
#define fs first
#define sc second
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

string BaseConversion(string xstr,int xbase,int ybase)
{   string str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";long long store[128],i,multi,sum;string ystr;for(i=sum=0;i<str.size();i++)store[str[i]]=i;multi=1;for(i=xstr.size()-1;i>=0;i--){sum+=(store[xstr[i]]*multi);multi*=xbase;
}    while(true){ystr.push_back(str[sum%ybase]);sum/=ybase;if(sum==0)break;}reverse(ystr.begin(),ystr.end());return ystr;}

/*void arabic(char str[]);
int convert_num(char str[]);
void convert_roman(int n);
void initial();
void strevv(char str1[]);

char str[LEN],str1[LEN];
int index1,rvalue[100],rrank[100];*/

int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
void roman(int e)
{
    int a,b,c,d;

    a = (e/1000)*1000;
    b = ((e/100)%10)*100;
    c = ((e/10)%10)*10;
    d = ((e/1)%10)*1;

    if (a ==1000)
             printf("M");
    else if (a ==2000)
             printf("MM");
    else if (a ==3000)
             printf("MMM");

    if (b == 100)
             printf("C");
    else if (b == 200)
             printf("CC");
    else if (b == 300)
             printf("CCC");
    else if (b == 400)
             printf("CD");
    else if (b ==500)
             printf("D");
    else if (b == 600)
             printf("DC");
    else if (b == 700)
             printf("DCC");
    else if (b ==800)
             printf("DCCC");
    else if (b == 900)
             printf("CM");

    if (c == 10)
             printf("X");
    else if (c == 20)
             printf("XX");
    else if (c == 30)
             printf("XXX");
    else if (c == 40)
             printf("XL");
    else if (c ==50)
             printf("L");
    else if (c == 60)
             printf("LX");
    else if (c == 70)
             printf("LXX");
    else if (c ==80)
             printf("LXXX");
    else if (c == 90)
             printf("XC");

    if (d == 1)
             printf("I");
    else if (d == 2)
             printf("II");
    else if (d == 3)
             printf("III");
    else if (d == 4)
             printf("IV");
    else if (d ==5)
             printf("V");
    else if (d == 6)
             printf("VI");
    else if (d == 7)
             printf("VII");
    else if (d ==8)
             printf("VIII");
    else if (d == 9)
             printf("IX");
    printf("\n");
}
int main()
{
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    //cnt=accumulate(scores.begin(),scores.end(),0); //sum
    int j,test,Case=1;
    int i,n,t,s;
    string xstr;int xbase,ybase;
    char S[101],S1[101],S2[101];

    while(scanf("%s %s %s",S,S1,S2)==3)
    {
        if(!strcmp(S1,"R"))
        {
            xstr=S2;xbase=toInt(S);ybase=toInt(S1);
            xstr=BaseConversion(xstr,xbase,10);j=0;
           /*  rep(i,xstr.size()) S2[j++]=xstr[i];S2[j]=0;
                if(isdigit(S2[0]))
                {
                    arabic(S2);
                    strevv(str1);
                    printf("%s\n",str1);
                }*/
                test=toInt(xstr);
                roman(test);
        }
        else
        {

            xstr=S2;xbase=toInt(S);ybase=toInt(S1);
            xstr=BaseConversion(xstr,xbase,10);
            xstr=BaseConversion(xstr,10,ybase);
            cout<<xstr<<endl;
        }

    }

    return 0;
}

/*void arabic(char str[])
{
	int num,k,part;

	num=convert_num(str);
	k=index1=0;
	while(num)
	{
		part=(num%10)*pow(10,k++);
		num/=10;
		convert_roman(part);
	}
	str1[index1]='\0';
}

int convert_num(char str[])
{
	int sum=0,i;

	for(i=0;str[i]!='\0';i++)
		sum=sum*10+(str[i]-'0');
	return sum;
}

void convert_roman(int n)
{
	if(n<10)
	{
		if(n==4)
			str1[index1++]='V',str1[index1++]='I';
		else if(n==9)
			str1[index1++]='X',str1[index1++]='I';
		else if(n==5)
			str1[index1++]='V';
		else if(n>5)
		{
			n-=5;
			while(n--)
				str1[index1++]='I';
			str1[index1++]='V';
		}
		else
		{
			while(n--)
				str1[index1++]='I';
		}
	}
	else if(n<100)
	{
		if(n==40)
			str1[index1++]='L',str1[index1++]='X';
		else if(n==90)
			str1[index1++]='C',str1[index1++]='X';
		else if(n==50)
			str1[index1++]='L';
		else if(n>50)
		{
			n-=50;
			while(n)
			{
				str1[index1++]='X';
				n=n-10;
			}
			str1[index1++]='L';
		}
		else
		{
			while(n)
			{
				str1[index1++]='X';
				n-=10;
			}
		}
	}
	else if(n<1000)
	{
		if(n==400)
			str1[index1++]='D',str1[index1++]='C';
		else if(n==900)
			str1[index1++]='M',str1[index1++]='C';
		else if(n==500)
			str1[index1++]='D';
		else if(n>500)
		{
			n-=500;
			while(n)
			{
				str1[index1++]='C';
				n-=100;
			}
			str1[index1++]='D';
		}
		else
		{
			while(n)
			{
				str1[index1++]='C';
				n-=100;
			}
		}
	}
	else
	{
		while(n)
		{
			str1[index1++]='M';
			n-=1000;
		}
	}
}

void strevv(char str1[])
{
	int i,j;
	char temp;

	for(i=strlen(str1)-1,j=0;i>j;i--,j++)
		temp=str1[i],str1[i]=str1[j],str1[j]=temp;
}
void initial()
{
	rrank['I']=1,rrank['V']=2,rrank['X']=3,rrank['L']=4,rrank['C']=5,rrank['D']=6,rrank['M']=7;
	rvalue['I']=1,rvalue['V']=5,rvalue['X']=10,rvalue['L']=50,rvalue['C']=100,rvalue['D']=500,rvalue['M']=1000;
}*/


#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <set>
#include <numeric>
#include <cmath>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <iomanip>

using namespace std;

#ifdef __GNUC__
typedef long long ll;typedef unsigned long long ull;
#else
typedef __int64 ll;  typedef unsigned __int64 ull;
#endif

#define INF 1<<28
#define SIZE 100
#define LEN 3001
#define PI 2*acos(0)
#define fg %I64d

#define rep(i,n) for (i=0;i<n;i++)
#define rev(i,n) for (i=n;i>=0;i--)
#define start(i,p,k) for (i=p; i<k;i++)
#define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)

#define bug(x)    cout<< "->" <<#x<<": "<<x<<endl
#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define MP(a,b) make_pair(a,b)
#define Clear(x,with) memset(x,with,sizeof(x))
#define Copy(c,r)   memcpy(c,r,sizeof(r))
#define SZ(x) (int)x.size()
#define length(x) (int)x.length()
#define All(x) x.begin(),x.end()
#define pb push_back
#define popcount(i) __builtin_popcount(i)
#define gcd(a,b)    __gcd(a,b)
#define fs first
#define sc second
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<double>vd;
typedef vector<ll>vll;
typedef vector<string> vs;
typedef vector<vi>vvi;
typedef vector<vll>vvll;
typedef vector<vd>vvd;
typedef vector<pii>vpii;
typedef map<string,int> msi;
typedef map<int,int>mii;
typedef map<pii,int>mpi;
set<string>SC;
template<class T> inline T sqr(T x){return x*x;}
template<class T> inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No.
template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();}
int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
ll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;}
bool toLower(char ch){if(ch>=97 && ch<=122) return true; return false;}
bool toUpper(char ch){if(ch>=65 && ch<=90) return true;return false;}
bool SCP(char S[],char SS[]){if(!strcmp(S,SS)) return true; return false;}
int Digit(int num){int cn=0;while(num){cn++;num/=10;}return cn;}
string BaseConversion(string xstr,int xbase,int ybase)
{   string str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";long long store[128],i,multi,sum;string ystr;for(i=sum=0;i<str.size();i++)store[str[i]]=i;multi=1;for(i=xstr.size()-1;i>=0;i--){sum+=(store[xstr[i]]*multi);multi*=xbase;
}    while(true){ystr.push_back(str[sum%ybase]);sum/=ybase;if(sum==0)break;}reverse(ystr.begin(),ystr.end());return ystr;}
long long BM ( long long base, long long pow, long long mod )    //Big Mod
{long long temp, result, rem ;temp = base % mod ;result = 1 ;while ( pow > 0 ){rem = pow % 2 ;pow /= 2 ;if ( rem == 1 ) result = ( result * temp ) % mod ;temp = ( temp * temp ) % mod ;}return result ;}
template<class T> void debug(const T& e){cout<<e<<endl;}
template<class T1,class T2> void debug(const T1& e1,const T2& e2){cout<<e1<<"\t"<<e2<<endl;}
template<class T1,class T2,class T3> void debug(const T1& e1,const T2& e2,const T3& e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;}
template<class T1,class T2,class T3,class T4> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;}
template<class T1,class T2,class T3,class T4,class T5> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;}
template<class T> void debug(vector<T>& e){int i;rep(i,SZ(e)) cout<<e[i]<<" ";cout<<endl;}
template<class T> void debug(vector< basic_string<T> >& e){int i,j;rep(i,SZ(e)) {rep(j,SZ(e[i])) cout<<e[i][j];cout<<endl;} cout<<endl;}
template<class T> void debug(vector< vector<T> >& e){int i,j;rep(i,SZ(e)) {rep(j,SZ(e[i])) cout<<e[i][j]<<"\t";cout<<endl;} cout<<endl;}
template<class T> void debug(T e[SIZE][SIZE],int row,int col){int i,j;rep(i,row) {rep(j,col) cout<<e[i][j]<<" ";cout<<endl;}}

ll Pow(ll B,ll P){	ll R=1;	while(P>0)	{if(P%2==1)	R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
//struct pq{    int cost,node;bool operator<(const pq &b)const{return cost>b.cost;}};// Min Priority Queue
//bool comp(pq a,pq b){    return a.cost > b.cost;}   //Comp Sort

//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
/*struct pq
{
	int cost,node;
	bool operator<(const pq &b)const
	{
		return cost>b.cost;    // Min Priority Queue
	}
};*/
/*void seive()    //prime generate
{
	int i,j,r,c=0,p;for(i=3;i<=SIZE_N;i+=2)flag[i]=true;flag[2]=true;prime[c++]=2;p=SIZE_N+1;
	for(i=3;i<p;i+=2)if(flag[i]){prime[c++]=i;if(SIZE_N/i>=i){r=i*2;for(j=i*i;j<p;j+=r)flag[j]=false;}}
    //printf("Total prime: %d\n",c);
}*/

/*void divisor(long n)           //generate divisor
	{
		long i;
		Count = 0;
		for(i=1;i<sqrt(n);i++)
			if(n % i == 0)
			{
				divisors[Count++] = i;
				divisors[Count++] = n / i;
			}
		if((i * i) == n)
			divisors[Count++] = i;				//  Count is total number of divisor
		sort(&divisors[0],&divisors[Count]);	//  Sorts all the divisor of n
		return ;
	}*/

/*void PF(int nn)
{
    int k,i,j,m=0,cnt=0,n;k=sqrt(nn)+1; for(i=0;prime[i]<k;i++)
    if(nn%prime[i]==0){cnt=0;while(nn%prime[i]==0){nn/=prime[i];cnt++;}fac[m]=prime[i];fre[m++]=cnt;k=sqrt(nn)+1;}
    if(nn>1) {fac[m]=nn;fre[m++]=nn;}
}*/
void arabic(char str[]);
int roman(char str[]);
int convert_num(char str[]);
void convert_roman(int n);
void initial();
//void strrev(char str1[]);

char str[LEN],str1[LEN];
int index1,rvalue[100],rrank[100];

void strev(char str1[])
{
	int i,j;
	char temp;

	for(i=strlen(str1)-1,j=0;i>j;i--,j++)
		temp=str1[i],str1[i]=str1[j],str1[j]=temp;
}
int a[LEN];
int main()
{
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    //cnt=accumulate(scores.begin(),scores.end(),0); //sum
    int j,test,Case=1;
    int i,n,t,s;
    string xstr;int xbase,ybase;
    char S[101],S1[101],S2[101];
    while(scanf("%s %s %s",S,S1,S2)==3)
    {
        if(!strcmp(S1,"R"))
        {
            xstr=S2;xbase=atoi(S);ybase=atoi(S1);
            xstr=BaseConversion(xstr,xbase,10);j=0;
             rep(i,xstr.size()) S2[j++]=xstr[i];S2[j]=0;
                if(isdigit(S2[0]))
                {
                    arabic(S2);
                    strev(str1);
                    printf("%s\n",str1);
                }
        }
        else
        {

            xstr=S2;xbase=atoi(S);ybase=atoi(S1);
            xstr=BaseConversion(xstr,xbase,10);
            xstr=BaseConversion(xstr,10,ybase);
            cout<<xstr<<endl;
        }

    }

    return 0;
}

void arabic(char str[])
{
	int num,k,part;

	num=convert_num(str);
	k=index1=0;
	while(num)
	{
		part=(num%10)*pow(10,k++);
		num/=10;
		convert_roman(part);
	}
	str1[index1]='\0';
}

int roman(char str[])
{
	int i,sum=0,len;

	len=strlen(str)-1;
	for(i=len;i>-1;i--)
	{
		if(i!=len && rrank[str[i]]<rrank[str[i+1]])
			sum-=rvalue[str[i]];
		else
			sum+=rvalue[str[i]];
	}
	return sum;
}

int convert_num(char str[])
{
	int sum=0,i;

	for(i=0;str[i]!='\0';i++)
		sum=sum*10+(str[i]-'0');
	return sum;
}

void convert_roman(int n)
{
	if(n<10)
	{
		if(n==4)
			str1[index1++]='V',str1[index1++]='I';
		else if(n==9)
			str1[index1++]='X',str1[index1++]='I';
		else if(n==5)
			str1[index1++]='V';
		else if(n>5)
		{
			n-=5;
			while(n--)
				str1[index1++]='I';
			str1[index1++]='V';
		}
		else
		{
			while(n--)
				str1[index1++]='I';
		}
	}
	else if(n<100)
	{
		if(n==40)
			str1[index1++]='L',str1[index1++]='X';
		else if(n==90)
			str1[index1++]='C',str1[index1++]='X';
		else if(n==50)
			str1[index1++]='L';
		else if(n>50)
		{
			n-=50;
			while(n)
			{
				str1[index1++]='X';
				n=n-10;
			}
			str1[index1++]='L';
		}
		else
		{
			while(n)
			{
				str1[index1++]='X';
				n-=10;
			}
		}
	}
	else if(n<1000)
	{
		if(n==400)
			str1[index1++]='D',str1[index1++]='C';
		else if(n==900)
			str1[index1++]='M',str1[index1++]='C';
		else if(n==500)
			str1[index1++]='D';
		else if(n>500)
		{
			n-=500;
			while(n)
			{
				str1[index1++]='C';
				n-=100;
			}
			str1[index1++]='D';
		}
		else
		{
			while(n)
			{
				str1[index1++]='C';
				n-=100;
			}
		}
	}
	else
	{
		while(n)
		{
			str1[index1++]='M';
			n-=1000;
		}
	}
}

void initial()
{
	rrank['I']=1,rrank['V']=2,rrank['X']=3,rrank['L']=4,rrank['C']=5,rrank['D']=6,rrank['M']=7;
	rvalue['I']=1,rvalue['V']=5,rvalue['X']=10,rvalue['L']=50,rvalue['C']=100,rvalue['D']=500,rvalue['M']=1000;
}


#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <set>
#include <numeric>
#include <cmath>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <iomanip>

using namespace std;

#ifdef __GNUC__
typedef long long ll;typedef unsigned long long ull;
#else
typedef __int64 ll;  typedef unsigned __int64 ull;
#endif

#define INF 1<<28
#define SIZE 100
#define LEN 1001
#define PI 2*acos(0)
#define fg %I64d

#define rep(i,n) for (i=0;i<n;i++)
#define rev(i,n) for (i=n;i>=0;i--)
#define start(i,p,k) for (i=p; i<k;i++)
#define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)

#define bug(x)    cout<< "->" <<#x<<": "<<x<<endl
#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define MP(a,b) make_pair(a,b)
#define Clear(x,with) memset(x,with,sizeof(x))
#define Copy(c,r)   memcpy(c,r,sizeof(r))
#define SZ(x) (int)x.size()
#define length(x) (int)x.length()
#define All(x) x.begin(),x.end()
#define pb push_back
#define popcount(i) __builtin_popcount(i)
#define gcd(a,b)    __gcd(a,b)
#define fs first
#define sc second
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

string BaseConversion(string xstr,int xbase,int ybase)
{   string str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";long long store[128],i,multi,sum;string ystr;for(i=sum=0;i<str.size();i++)store[str[i]]=i;multi=1;for(i=xstr.size()-1;i>=0;i--){sum+=(store[xstr[i]]*multi);multi*=xbase;
}    while(true){ystr.push_back(str[sum%ybase]);sum/=ybase;if(sum==0)break;}reverse(ystr.begin(),ystr.end());return ystr;}

/*void arabic(char str[]);
int convert_num(char str[]);
void convert_roman(int n);
void initial();
void strevv(char str1[]);

char str[LEN],str1[LEN];
int index1,rvalue[100],rrank[100];*/

int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
void roman(int e)
{
    int a,b,c,d;

    a = (e/1000)*1000;
    b = ((e/100)%10)*100;
    c = ((e/10)%10)*10;
    d = ((e/1)%10)*1;

    if (a ==1000)
             printf("M");
    else if (a ==2000)
             printf("MM");
    else if (a ==3000)
             printf("MMM");

    if (b == 100)
             printf("C");
    else if (b == 200)
             printf("CC");
    else if (b == 300)
             printf("CCC");
    else if (b == 400)
             printf("CD");
    else if (b ==500)
             printf("D");
    else if (b == 600)
             printf("DC");
    else if (b == 700)
             printf("DCC");
    else if (b ==800)
             printf("DCCC");
    else if (b == 900)
             printf("CM");

    if (c == 10)
             printf("X");
    else if (c == 20)
             printf("XX");
    else if (c == 30)
             printf("XXX");
    else if (c == 40)
             printf("XL");
    else if (c ==50)
             printf("L");
    else if (c == 60)
             printf("LX");
    else if (c == 70)
             printf("LXX");
    else if (c ==80)
             printf("LXXX");
    else if (c == 90)
             printf("XC");

    if (d == 1)
             printf("I");
    else if (d == 2)
             printf("II");
    else if (d == 3)
             printf("III");
    else if (d == 4)
             printf("IV");
    else if (d ==5)
             printf("V");
    else if (d == 6)
             printf("VI");
    else if (d == 7)
             printf("VII");
    else if (d ==8)
             printf("VIII");
    else if (d == 9)
             printf("IX");
    printf("\n");
}
int main()
{
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    //cnt=accumulate(scores.begin(),scores.end(),0); //sum
    int j,test,Case=1;
    int i,n,t,s;
    string xstr;int xbase,ybase;
    string S,S1,S2;

    while(cin>>S>>S1>>S2)
    {
        if(S1=="R")
        {
            xstr=S2;xbase=toInt(S);ybase=toInt(S1);
            xstr=BaseConversion(xstr,xbase,10);j=0;
           /*  rep(i,xstr.size()) S2[j++]=xstr[i];S2[j]=0;
                if(isdigit(S2[0]))
                {
                    arabic(S2);
                    strevv(str1);
                    printf("%s\n",str1);
                }*/
                test=toInt(xstr);
                roman(test);
        }
        else
        {

            xstr=S2;xbase=toInt(S);ybase=toInt(S1);
            xstr=BaseConversion(xstr,xbase,10);
            xstr=BaseConversion(xstr,10,ybase);
            cout<<xstr<<endl;
        }

    }

    return 0;
}

/*void arabic(char str[])
{
	int num,k,part;

	num=convert_num(str);
	k=index1=0;
	while(num)
	{
		part=(num%10)*pow(10,k++);
		num/=10;
		convert_roman(part);
	}
	str1[index1]='\0';
}

int convert_num(char str[])
{
	int sum=0,i;

	for(i=0;str[i]!='\0';i++)
		sum=sum*10+(str[i]-'0');
	return sum;
}

void convert_roman(int n)
{
	if(n<10)
	{
		if(n==4)
			str1[index1++]='V',str1[index1++]='I';
		else if(n==9)
			str1[index1++]='X',str1[index1++]='I';
		else if(n==5)
			str1[index1++]='V';
		else if(n>5)
		{
			n-=5;
			while(n--)
				str1[index1++]='I';
			str1[index1++]='V';
		}
		else
		{
			while(n--)
				str1[index1++]='I';
		}
	}
	else if(n<100)
	{
		if(n==40)
			str1[index1++]='L',str1[index1++]='X';
		else if(n==90)
			str1[index1++]='C',str1[index1++]='X';
		else if(n==50)
			str1[index1++]='L';
		else if(n>50)
		{
			n-=50;
			while(n)
			{
				str1[index1++]='X';
				n=n-10;
			}
			str1[index1++]='L';
		}
		else
		{
			while(n)
			{
				str1[index1++]='X';
				n-=10;
			}
		}
	}
	else if(n<1000)
	{
		if(n==400)
			str1[index1++]='D',str1[index1++]='C';
		else if(n==900)
			str1[index1++]='M',str1[index1++]='C';
		else if(n==500)
			str1[index1++]='D';
		else if(n>500)
		{
			n-=500;
			while(n)
			{
				str1[index1++]='C';
				n-=100;
			}
			str1[index1++]='D';
		}
		else
		{
			while(n)
			{
				str1[index1++]='C';
				n-=100;
			}
		}
	}
	else
	{
		while(n)
		{
			str1[index1++]='M';
			n-=1000;
		}
	}
}

void strevv(char str1[])
{
	int i,j;
	char temp;

	for(i=strlen(str1)-1,j=0;i>j;i--,j++)
		temp=str1[i],str1[i]=str1[j],str1[j]=temp;
}
void initial()
{
	rrank['I']=1,rrank['V']=2,rrank['X']=3,rrank['L']=4,rrank['C']=5,rrank['D']=6,rrank['M']=7;
	rvalue['I']=1,rvalue['V']=5,rvalue['X']=10,rvalue['L']=50,rvalue['C']=100,rvalue['D']=500,rvalue['M']=1000;
}*/


#include<iostream>
#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

typedef long long ll ;

ll values[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
string roman[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"} ;

string toRoman( ll n ){
    string resp = "" ;
    for(int i = 0; i < 13; i++){
        while( n >= values[ i ]){
            n -= values[ i ];
            resp += roman[ i ] ;
        }
    }
    return resp ;
}

ll to10( string s , int b ){
    ll resp = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
        resp *= b ;
        if( isalpha( s[ i ] ) )
            resp += ( s[ i ] - 'A' + 10 ) ;
        else
            resp += ( s[ i ] - '0' ) ;
    }
    return resp ;
}

string toBase( ll n , string x ){
    int b , digit ;
    sscanf( x.c_str() , "%d" , &b ) ;

    string resp = "" ;

    while( n ){
        digit = n % b ;
        n /= b ;
        if( digit >= 10 )
            resp += ( 'A' + digit - 10 ) ;
        else
            resp += ( '0' + digit ) ;
    }
    resp = string( resp.rbegin() , resp.rend() ) ;
    return resp ;
}

int main(){

    int a ;
    string b , c ;
    cin >> a >> b >> c ;

    ll b10 = to10( c , a ) ;
    if( b == "R" )
        cout << toRoman( b10 ) << endl ;
    else
        cout << toBase( b10 , b ) << endl ;

    return 0;
}


#include<iostream>
#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

typedef long long ll ;

ll values[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
string roman[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"} ;

string toRoman( ll n ){
    string resp = "" ;
    for(int i = 0; i < 13; i++){
        while( n >= values[ i ]){
            n -= values[ i ];
            resp += roman[ i ] ;
        }
    }
    return resp ;
}

ll to10( string s , int b ){
    ll resp = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
        resp *= b ;
        if( isalpha( s[ i ] ) )
            resp += ( s[ i ] - 'A' + 10 ) ;
        else
            resp += ( s[ i ] - '0' ) ;
    }
    return resp ;
}

string toBase( ll n , string x ){
    int b , digit ;
    sscanf( x.c_str() , "%d" , &b ) ;

    string resp = "" ;

    while( n ){
        digit = n % b ;
        n /= b ;
        if( digit >= 10 )
            resp += ( 'A' + digit - 10 ) ;
        else
            resp += ( '0' + digit ) ;
    }
    resp = string( resp.rbegin() , resp.rend() ) ;
    return resp ;
}

string dim( string s ){
    string resp = "" ;
    for(int i = 0 ; i < s.size() ; i++){
        if( s[ i ] == '0' ) continue ;
        resp += s[ i ] ;
    }
    return ( resp == "" ? "0" : resp ) ;
}

int main(){

    int a ;
    string b , c ;
    cin >> a >> b >> c ;
    if( dim( c ) == "0" ){
        printf("0\n") ;
        return 0 ;
    }

    ll b10 = to10( c , a ) ;
    if( b == "R" )
        cout << toRoman( b10 ) << endl ;
    else
        cout << toBase( b10 , b ) << endl ;

    return 0;
}


#include<iostream>
#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

typedef long long ll ;

ll values[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
string roman[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"} ;

string toRoman( ll n ){
    string resp = "" ;
    for(int i = 0; i < 13; i++){
        while( n >= values[ i ]){
            n -= values[ i ];
            resp += roman[ i ] ;
        }
    }
    return resp ;
}

ll to10( string s , int b ){
    ll resp = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
        resp *= b ;
        if( isalpha( s[ i ] ) )
            resp += ( s[ i ] - 'A' + 10 ) ;
        else
            resp += ( s[ i ] - '0' ) ;
    }
    return resp ;
}

string toBase( ll n , string x ){
    int b , digit ;
    sscanf( x.c_str() , "%d" , &b ) ;

    string resp = "" ;

    while( n ){
        digit = n % b ;
        n /= b ;
        if( digit >= 10 )
            resp += ( 'A' + digit - 10 ) ;
        else
            resp += ( '0' + digit ) ;
    }
    resp = string( resp.rbegin() , resp.rend() ) ;
    return resp ;
}

int main(){

    int a ;
    string b , c ;
    cin >> a >> b >> c ;
    if( c == "0" ){
        printf("0\n") ;
        return 0 ;
    }

    ll b10 = to10( c , a ) ;
    if( b == "R" )
        cout << toRoman( b10 ) << endl ;
    else
        cout << toBase( b10 , b ) << endl ;

    return 0;
}


#include<iostream>
#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

typedef long long ll ;

ll values[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
string roman[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"} ;

string toRoman( ll n ){
    string resp = "" ;
    for(int i = 0; i < 13; i++){
        while( n >= values[ i ]){
            n -= values[ i ];
            resp += roman[ i ] ;
        }
    }
    return resp ;
}

ll to10( string s , int b ){
    ll resp = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
        resp *= b ;
        if( isalpha( s[ i ] ) )
            resp += ( s[ i ] - 'A' + 10 ) ;
        else
            resp += ( s[ i ] - '0' ) ;
    }
    return resp ;
}

string toBase( ll n , string x ){
    int b , digit ;
    sscanf( x.c_str() , "%d" , &b ) ;

    string resp = "" ;

    while( n ){
        digit = n % b ;
        n /= b ;
        if( digit >= 10 )
            resp += ( 'A' + digit - 10 ) ;
        else
            resp += ( '0' + digit ) ;
    }
    resp = string( resp.rbegin() , resp.rend() ) ;
    return resp ;
}

int main(){

    int a ;
    string b , c ;
    cin >> a >> b >> c ;
    if( c == 0 ){
        printf("0\n") ;
        return 0 ;
    }

    ll b10 = to10( c , a ) ;
    if( b == "R" )
        cout << toRoman( b10 ) << endl ;
    else
        cout << toBase( b10 , b ) << endl ;

    return 0;
}


#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <iterator>
#include <numeric>
#include <deque>
#include <stack>
#include <sstream>

int main()
{
  int a;
  std::cin >> a;
  std::string b;
  std::cin >> b;
  std::string c;
  std::cin >> c;
  long long d = strtoll(c.c_str(), 0, a);
  std::string ans;
  if(b != "R") {
    int base = strtol(b.c_str(), 0, 10);
    std::stringstream ss;
    while(d > 0) {
      int r = d%base;
      if(r >= 10) {
        ss << (char)(r-10+'A');
      } else {
        ss << r;
      }
      d /= base;
    }
    ans = ss.str();
    std::reverse(ans.begin(), ans.end());
  } else {
    std::string t;
    int q;
    q = d/1000;
    ans += std::string(q, 'M');
    d = d%1000;

    if(d >= 900) {
      ans += "CM";
      d -= 900;
    } else if(d >= 500) {
      ans += "D";
      d -= 500;
    } else if(d >= 400) {
      ans += "CD";
      d -= 400;
    }

    q = d/100;
    ans += std::string(q, 'C');
    d = d%100;

    if(d >= 90) {
      ans += "XC";
      d -= 90;
    } else if(d >= 50) {
      ans += "L";
      d -= 50;
    } else if(d >= 40) {
      ans += "XL";
      d -= 40;
    }

    q = d/10;
    ans += std::string(q, 'X');
    d = d%10;

    if(d >= 9) {
      ans += "IX";
      d -= 9;
    } else if(d >= 5) {
      ans += "V";
      d -= 5;
    } else if(d >= 4) {
      ans += "IV";
      d -= 4;
    };
    ans += std::string(d, 'I');
  }
  std::cout << ans << std::endl;
}


#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <iterator>
#include <numeric>
#include <deque>
#include <stack>
#include <sstream>

int main()
{
  int a;
  std::cin >> a;
  std::string b;
  std::cin >> b;
  std::string c;
  std::cin >> c;
  long long d = strtoll(c.c_str(), 0, a);
  std::string ans;
  if(b != "R") {
    if(d == 0) {
      ans = "0";
    } else {
      int base = strtol(b.c_str(), 0, 10);
      std::stringstream ss;
      while(d > 0) {
        int r = d%base;
        if(r >= 10) {
          ss << (char)(r-10+'A');
        } else {
          ss << r;
        }
        d /= base;
      }
      ans = ss.str();
      std::reverse(ans.begin(), ans.end());
    }
  } else {
    std::string t;
    int q;
    q = d/1000;
    ans += std::string(q, 'M');
    d = d%1000;

    if(d >= 900) {
      ans += "CM";
      d -= 900;
    } else if(d >= 500) {
      ans += "D";
      d -= 500;
    } else if(d >= 400) {
      ans += "CD";
      d -= 400;
    }

    q = d/100;
    ans += std::string(q, 'C');
    d = d%100;

    if(d >= 90) {
      ans += "XC";
      d -= 90;
    } else if(d >= 50) {
      ans += "L";
      d -= 50;
    } else if(d >= 40) {
      ans += "XL";
      d -= 40;
    }

    q = d/10;
    ans += std::string(q, 'X');
    d = d%10;

    if(d >= 9) {
      ans += "IX";
      d -= 9;
    } else if(d >= 5) {
      ans += "V";
      d -= 5;
    } else if(d >= 4) {
      ans += "IV";
      d -= 4;
    };
    ans += std::string(d, 'I');
  }
  std::cout << ans << std::endl;
}


#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 1000 * 100 + 10;
vector<int> ng[MAXN], w[MAXN];

long long dfs(int x, int p)
{
	long long ret = 0;
	for (int i = 0; i < ng[x].size(); i++)
	{
		int u = ng[x][i];
		if (u == p)
			continue;
		ret = max(ret, (long long)w[x][i] + dfs(u, x));
	}
	return ret;
}

int main()
{
	int n; long long ans = 0;
	cin >> n;
	for (int i = 0; i < n - 1; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		a--;
		b--;
		ng[a].push_back(b);
		ng[b].push_back(a);
		w[a].push_back(c);
		w[b].push_back(c);
		ans += 2 * c;
	}
	ans -= dfs(0, -1);
	cout << ans << endl;
	return 0;
}


#include <iostream>

using namespace std;

typedef long long ll;

ll dec(string s, int fb)
{
	ll base = 1, ret = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[s.size() - 1 - i] >= 'A')
			ret += base * (s[s.size() - 1 - i] - 'A' + 10);
		else
			ret += base * (s[s.size() - 1 - i] - '0');
		base *= fb;
	}
	return ret;
}

string chn(ll a, int base)
{
	if (a == 0)
		return "";
	string ret = chn(a / base, base);
	if (a % base > 9)
		ret += 'A' + (a % base) - 10;
	else
		ret += '0' + a % base;
	return ret;
}

string roman(int a)
{
	string ret = "";
	string zer[] =
	{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX",
	"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
	"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
	"", "M", "MM", "MMM"};
	int cnt = 0;
	while (a != 0)
	{
		int cur = a % 10;
		ret = zer[cnt * 10 + cur] + ret;
		a /= 10;
		cnt++;
	}
	return ret;
}

int main()
{
	int a;
	string s, b;
	cin >> a >> b >> s;
	ll av = dec(s, a);
	if (b[0] == 'R')
		cout << roman(av) << endl;
	else
	{
		if (av == 0)
		{
			cout << 0 << endl;
			return 0;
		}
		b = '0' + b;
		int base = (b[b.size() - 1] - '0') + (b[b.size() - 2] - '0') * 10;
		cout << chn(av, base) << endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

typedef long long ll;

ll dec(string s, int fb)
{
	ll base = 1, ret = 0;
	for (int i = 0; i < s.size(); i++)
		ret += base * (s[s.size() - 1 - i] - '0'), base *= fb;
	return ret;
}

string chn(ll a, int base)
{
	if (a == 0)
		return "";
	string ret = chn(a / base, base);
	if (a % base > 9)
		ret += 'A' + (a % base) - 10;
	else
		ret += '0' + a % base;
	return ret;
}

string roman(int a)
{
	string ret = "";
	string zer[] =
	{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "IX"
	"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"
	"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"
	"", "M", "MM", "MMM"};
	int cnt = 0;
	while (a != 0)
	{
		int cur = a % 10;
		ret += zer[cnt * 10 + cur];
		a /= 10;
		cnt++;
	}
	return ret;
}

int main()
{
	int a;
	string s, b;
	cin >> a >> b >> s;
	ll av = dec(s, a);
	if (b[0] == 'R')
		cout << roman(av) << endl;
	else
	{
		if (av == 0)
		{
			cout << 0 << endl;
			return 0;
		}
		b = '0' + b;
		int base = (b[b.size() - 1] - '0') + (b[b.size() - 2] - '0') * 10;
		cout << chn(av, base) << endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

typedef long long ll;

ll dec(string s, int fb)
{
	ll base = 1, ret = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[s.size() - 1 - i] >= 'A')
			ret += base * (s[s.size() - 1 - i] - 'A' + 10);
		else
			ret += base * (s[s.size() - 1 - i] - '0');
		base *= fb;
	}
	return ret;
}

string chn(ll a, int base)
{
	if (a == 0)
		return "";
	string ret = chn(a / base, base);
	if (a % base > 9)
		ret += 'A' + (a % base) - 10;
	else
		ret += '0' + a % base;
	return ret;
}

string roman(int a)
{
	string ret = "";
	string zer[] =
	{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "IX"
	"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"
	"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"
	"", "M", "MM", "MMM"};
	int cnt = 0;
	while (a != 0)
	{
		int cur = a % 10;
		ret += zer[cnt * 10 + cur];
		a /= 10;
		cnt++;
	}
	return ret;
}

int main()
{
	int a;
	string s, b;
	cin >> a >> b >> s;
	ll av = dec(s, a);
	if (b[0] == 'R')
		cout << roman(av) << endl;
	else
	{
		if (av == 0)
		{
			cout << 0 << endl;
			return 0;
		}
		b = '0' + b;
		int base = (b[b.size() - 1] - '0') + (b[b.size() - 2] - '0') * 10;
		cout << chn(av, base) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}

string lltostr(long long a, int base)
{
	ostringstream os;
	do { os << a % base; a /= base; } while (a > 0);
	string res = os.str();
	reverse(res.begin(), res.end());
	return res;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		if (a == 0) {
			res = "0";
		}
		else {
			while (a > 0) {
				int k = a % b;
				if (k < 10) os << k;
				else os << (char)('A' + (k - 10));
				a /= b;
			}
			res = os.str();
			reverse(all(res));
		}
	}
	return res;
}

int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		reverse(all(res));
		res = replaceAll(res, "DCCCC", "CM");
		res = replaceAll(res, "CCCC", "CD");
		res = replaceAll(res, "LXXXX", "XC");
		res = replaceAll(res, "XXXX", "XL");
		res = replaceAll(res, "VIIII", "IX");
		res = replaceAll(res, "IIII", "IV");
	}
	else {
		while (a > 0) {
			int k = a % b;
			if (k < 10) os << k;
			else os << (char)('A' + (k - 10));
			a /= b;
		}
		res = os.str();
		reverse(all(res));
	}
	return res;
}

ll dec(string s, ll b)
{
	ll res = 0;
//	reverse(all(s));
	rep(i, s.size()) {
		ll k = 0;
		if (!isdigit(s[i])) k = s[i] - 'A' + 10;
		else k = s[i] - '0';
		res = res * b + k;
	}
	return res;
}
int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		//b = atoll(t.c_str());
		//c = dec(s, a);
		if (t == "R") { b = 0; }
		else b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}

string lltostr(long long a, int base)
{
	ostringstream os;
	do {
		int t = a % base;
		os << (t < 10 ? t : (char)(t - 10 + 'A'));
		a /= base;
	} while (a > 0);
	string res = os.str();
	reverse(res.begin(), res.end());
	return res;
}

string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		res = lltostr(a, b);
		/*
		if (a == 0) {
			res = "0";
		}
		else {
			while (a > 0) {
				int k = a % b;
				if (k < 10) os << k;
				else os << (char)('A' + (k - 10));
				a /= b;
			}
			res = os.str();
			reverse(all(res));
		}
		*/
	}
	return res;
}

int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		res = replaceAll(res, "DCCCC", "CM");
		res = replaceAll(res, "CCCC", "CD");
		res = replaceAll(res, "LXXXX", "XC");
		res = replaceAll(res, "XXXX", "XL");
		res = replaceAll(res, "VIIII", "IX");
		res = replaceAll(res, "IIII", "IV");
		res = replaceAll(res, "CCCC", "CD");
	}
	else {
		while (a > 0) {
			int k = a % b;
			if (k < 10) os << k;
			else os << (char)('A' - (k - 10));
			a /= b;
		}
		res = os.str();
		reverse(all(res));
	}
	return res;
}

ll dec(string s, ll b)
{
	ll res = 0;
//	reverse(all(s));
	rep(i, s.size()) {
		ll k = 0;
		if (!isdigit(s[i])) k = s[i] - 'A' + 10;
		else k = s[i] - '0';
		res = res * b + k;
	}
	return res;
}
int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = dec(s, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}

string lltostr(long long a, int base)
{
	ostringstream os;
	do {
		int t = a % base;
		if (t < 10) os << t; else os << (char)(t - 10 + 'A');
		a /= base;
	} while (a > 0);
	string res = os.str();
	reverse(res.begin(), res.end());
	return res;
}

string toRoman(long long c){
	char o[4] = {'M', 'C', 'X', 'I'};
	char f[4] = {'?', 'D', 'L', 'V'};
	string res;
	int div[4] = {1000, 100, 10, 1};
	rep(i, 4) {
		int cur = (c/div[i])%10;
		if(cur==0) continue;
		if(1<=cur&&cur<=3){ rep(j, cur) res += o[i]; }
		if(cur==4){ res += o[i]; res += f[i]; }
		if(5<=cur&&cur<=8){ res += f[i]; rep(j, cur-5) res += o[i]; }
		if(cur==9){ res += o[i]; res += o[i-1]; }
	}
	return res;
}

string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		res = lltostr(a, b);
		/*
		if (a == 0) {
			res = "0";
		}
		else {
			while (a > 0) {
				int k = a % b;
				if (k < 10) os << k;
				else os << (char)('A' + (k - 10));
				a /= b;
			}
			res = os.str();
			reverse(all(res));
		}
		*/
	}
	return res;
}

int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		if (b == 0)
			cout << toRoman(c) << endl;
		else
			cout << lltostr(c, b) << endl;
//		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}

string lltostr(long long a, int base)
{
	ostringstream os;
	do {
		int t = a % base;
		if (t < 10) os << t; else os << (char)(t - 10 + 'A');
		a /= base;
	} while (a > 0);
	string res = os.str();
	reverse(res.begin(), res.end());
	return res;
}

string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		res = lltostr(a, b);
		/*
		if (a == 0) {
			res = "0";
		}
		else {
			while (a > 0) {
				int k = a % b;
				if (k < 10) os << k;
				else os << (char)('A' + (k - 10));
				a /= b;
			}
			res = os.str();
			reverse(all(res));
		}
		*/
	}
	return res;
}

int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}

string lltostr(long long a, int base)
{
	ostringstream os;
	do {
		int t = a % base;
		os << (t > 10 ? t : (char)(t - 10 + 'A'));
		a /= base;
	} while (a > 0);
	string res = os.str();
	reverse(res.begin(), res.end());
	return res;
}

string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		res = lltostr(a, b);
		/*
		if (a == 0) {
			res = "0";
		}
		else {
			while (a > 0) {
				int k = a % b;
				if (k < 10) os << k;
				else os << (char)('A' + (k - 10));
				a /= b;
			}
			res = os.str();
			reverse(all(res));
		}
		*/
	}
	return res;
}

int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		res = replaceAll(res, "DCCCC", "CM");
		res = replaceAll(res, "CCCC", "CD");
		res = replaceAll(res, "LXXXX", "XC");
		res = replaceAll(res, "XXXX", "XL");
		res = replaceAll(res, "VIIII", "IX");
		res = replaceAll(res, "IIII", "IV");
	}
	else {
		while (a > 0) {
			int k = a % b;
			if (k < 10) os << k;
			else os << (char)('A' + (k - 10));
			a /= b;
		}
		res = os.str();
		reverse(all(res));
	}
	return res;
}

ll dec(string s, ll b)
{
	ll res = 0;
//	reverse(all(s));
	rep(i, s.size()) {
		ll k = 0;
		if (!isdigit(s[i])) k = s[i] - 'A' + 10;
		else k = s[i] - '0';
		res = res * b + k;
	}
	return res;
}
int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		//b = atoll(t.c_str());
		//c = dec(s, a);
		if (t == "R") { b = 0; }
		else b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		while (a > 0) {
			int k = a % b;
			if (k < 10) os << k;
			else os << (char)('A' + (k - 10));
			a /= b;
		}
		res = os.str();
		reverse(all(res));
	}
	return res;
}

ll dec(string s, ll b)
{
	ll res = 0;
//	reverse(all(s));
	rep(i, s.size()) {
		ll k = 0;
		if (!isdigit(s[i])) k = s[i] - 'A' + 10;
		else k = s[i] - '0';
		res = res * b + k;
	}
	return res;
}
int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		//b = atoll(t.c_str());
		//c = dec(s, a);
		if (t == "R") { b = 0; }
		else b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		res = replaceAll(res, "DCCCC", "CM");
		res = replaceAll(res, "CCCC", "CD");
		res = replaceAll(res, "LXXXX", "XC");
		res = replaceAll(res, "XXXX", "XL");
		res = replaceAll(res, "VIIII", "IX");
		res = replaceAll(res, "IIII", "IV");
	}
	else {
		while (a > 0) {
			int k = a % b;
			if (k < 10) os << k;
			else os << (char)('A' + (k - 10));
			a /= b;
		}
		res = os.str();
		reverse(all(res));
	}
	return res;
}

ll dec(string s, ll b)
{
	ll res = 0;
//	reverse(all(s));
	rep(i, s.size()) {
		ll k = 0;
		if (!isdigit(s[i])) k = s[i] - 'A' + 10;
		else k = s[i] - '0';
		res = res * b + k;
	}
	return res;
}
int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		//c = dec(s, a);
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		reverse(all(res));
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		while (a > 0) {
			int k = a % b;
			if (k < 10) os << k;
			else os << (char)('A' + (k - 10));
			a /= b;
		}
		res = os.str();
		reverse(all(res));
	}
	return res;
}

ll dec(string s, ll b)
{
	ll res = 0;
//	reverse(all(s));
	rep(i, s.size()) {
		ll k = 0;
		if (!isdigit(s[i])) k = s[i] - 'A' + 10;
		else k = s[i] - '0';
		res = res * b + k;
	}
	return res;
}
int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		//b = atoll(t.c_str());
		//c = dec(s, a);
		if (t == "R") { b = 0; }
		else b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		res = replaceAll(res, "DCCCC", "CM");
		res = replaceAll(res, "CCCC", "CD");
		res = replaceAll(res, "LXXXX", "XC");
		res = replaceAll(res, "XXXX", "XL");
		res = replaceAll(res, "VIIII", "IX");
		res = replaceAll(res, "IIII", "IV");
	}
	else {
		while (a > 0) {
			int k = a % b;
			if (k < 10) os << k;
			else os << (char)('A' + (k - 10));
			a /= b;
		}
		res = os.str();
		reverse(all(res));
	}
	return res;
}

ll dec(string s, ll b)
{
	ll res = 0;
//	reverse(all(s));
	rep(i, s.size()) {
		ll k = 0;
		if (!isdigit(s[i])) k = s[i] - 'A' + 10;
		else k = s[i] - '0';
		res = res * b + k;
	}
	return res;
}
int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = dec(s, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int rd[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char cd[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

string replaceAll(string s, string a, string b)
{
	string r;
	for (int k = 0; (k = s.find(a)) != s.npos; ) {
		r += s.substr(0, k) + b;
		s = s.substr(k + a.size());
	}
	return r + s;
}

string lltostr(long long a, int base)
{
	ostringstream os;
	do { os << a % base; a /= base; } while (a > 0);
	string res = os.str();
	reverse(res.begin(), res.end());
	return res;
}
string conv(ll a, ll b)
{
	string res;
	ostringstream os;
	if (b == 0) {
		rep(i, 7) {
			while (a >= rd[i]) {
				os << cd[i];
				a -= rd[i];
			}
		}
		res = os.str();
		bool changed = true;
		while (changed) {
			changed = false;
			string t = res;
			res = replaceAll(res, "DCCCC", "CM");
			res = replaceAll(res, "CCCC", "CD");
			res = replaceAll(res, "LXXXX", "XC");
			res = replaceAll(res, "XXXX", "XL");
			res = replaceAll(res, "VIIII", "IX");
			res = replaceAll(res, "IIII", "IV");
			res = replaceAll(res, "DD", "M");
			res = replaceAll(res, "LL", "C");
			res = replaceAll(res, "VV", "X");
			changed = t != res;
		}
	}
	else {
		res = lltostr(a, b);
		/*
		if (a == 0) {
			res = "0";
		}
		else {
			while (a > 0) {
				int k = a % b;
				if (k < 10) os << k;
				else os << (char)('A' + (k - 10));
				a /= b;
			}
			res = os.str();
			reverse(all(res));
		}
		*/
	}
	return res;
}

int main()
{
	ll a, b, c;
	string s, t;
	while (cin >> a >> t >> s) {
		b = atoll(t.c_str());
		c = strtoll(s.c_str(), NULL, a);
		cout << conv(c, b) << endl;
	}
	return 0;
}


#include<iostream>
#include<string>

using namespace std;

long long convertBase10(string num, int base)
{
    long long res = 0;
    for(int i = 0 ; i < num.size() ; i++)
    {
        res *= base;
        if(num[i] >= '0' && num[i] <= '9' )
            res += (num[i] - '0');
        else if(num[i] >= 'A' && num[i] <= 'Z')
            res+= (num[i] - 'A' + 10);
    }
    return res;
}

void convertRoman(long long input)
{
    const string roman[13] = { "M", "CM", "D", "CD", "C","XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    const int decimal[13] = {1000, 900, 500, 400, 100, 90,50, 40, 10, 9, 5, 4, 1};
    string romanvalue = "";
    for (int i = 0; i < 13; i++)
    {
        while (input >= decimal[i])
        {
            input -= decimal[i];
            romanvalue += roman[i];
        }
    }
    cout<<romanvalue;
}

void convertB(long long num, string b)
{
    if(b == "R")
    {
        convertRoman(num);
        return;
    }
    int base = 0;
    for(int i = 0 ; i < b.size() ; i++)
    {
        base *= 10;
        base += (b[i] - '0');
    }

    string s = "";
    if(num == 0)
        s = "0";

    while(num)
    {
        int rem = num%base;
        char ch ;
        if(rem < 10)
            ch = '0' + rem;
        else
            ch = 'A' + rem -10;

        s = string(1,ch) + s;

        num /= base;
    }

    cout<<s;
    return;
}

int main()
{
    int a;
    string b;
    string c;

    cin>>a>>b>>c;

    long long num = convertBase10(c,a);
    convertB(num,b);
    return 0;
}


#include<iostream>
#include<string>
//#include<conio.h>

using namespace std;

int convertBase10(string num, int base)
{
    int res = 0;
    for(int i = 0 ; i < num.size() ; i++)
    {
       res *= base;
       if(num[i] >= '0' && num[i] <= '9' )
          res += (num[i] - '0');
       else if(num[i] >= 'A' && num[i] <= 'Z')
         res+= (num[i] - 'A' + 10);
    }
    return res;
}

void convertRoman(int input)
{
     const string roman[13] = { "M", "CM", "D", "CD", "C",
     "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
     const int decimal[13] = {1000, 900, 500, 400, 100, 90,
      50, 40, 10, 9, 5, 4, 1};
      string romanvalue = "";
      for (int i = 0; i < 13; i++)
      {
          while (input >= decimal[i])
          {
                input -= decimal[i];
                romanvalue += roman[i];
           }
       }
       cout<<romanvalue;
}

void convertB(int num, string b)
{
     if(b == "R")
     {
           convertRoman(num);
           return;
     }
     int base = 0;
     for(int i = 0 ; i < b.size() ; i++)
     {

       base *= 10;
       base += (b[i] - '0');
     }

     string s = "";
     while(num)
     {
           int rem = num%base;
           char ch ;
           if(rem < 10)
                ch = '0' + rem;
           else
             ch = 'A' + rem -10;

           s = string(1,ch) + s;

          num /= base;
     }

     cout<<s;
     return;
}

int main()
{
    int a;
    string b;
    string c;

    cin>>a>>b>>c;

    int num = convertBase10(c,a);
    convertB(num,b);
    //getch();
    return 0;
}


#include<iostream>
#include<string>

using namespace std;

long long convertBase10(string num, int base)
{
    long long res = 0;
    for(int i = 0 ; i < num.size() ; i++)
    {
        res *= base;
        if(num[i] >= '0' && num[i] <= '9' )
            res += (num[i] - '0');
        else if(num[i] >= 'A' && num[i] <= 'Z')
            res+= (num[i] - 'A' + 10);
    }
    return res;
}

void convertRoman(long long input)
{
    const string roman[13] = { "M", "CM", "D", "CD", "C","XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    const int decimal[13] = {1000, 900, 500, 400, 100, 90,50, 40, 10, 9, 5, 4, 1};
    string romanvalue = "";
    for (int i = 0; i < 13; i++)
    {
        while (input >= decimal[i])
        {
            input -= decimal[i];
            romanvalue += roman[i];
        }
    }
    cout<<romanvalue;
}

void convertB(long long num, string b)
{
    if(b == "R")
    {
        convertRoman(num);
        return;
    }
    int base = 0;
    for(int i = 0 ; i < b.size() ; i++)
    {
        base *= 10;
        base += (b[i] - '0');
    }

    string s = "";
    while(num)
    {
        int rem = num%base;
        char ch ;
        if(rem < 10)
            ch = '0' + rem;
        else
            ch = 'A' + rem -10;

        s = string(1,ch) + s;

        num /= base;
    }

    cout<<s;
    return;
}

int main()
{
    int a;
    string b;
    string c;

    cin>>a>>b>>c;

    long long num = convertBase10(c,a);
    convertB(num,b);
    return 0;
}


#include <iostream>
using namespace std;
#include <string>
string s , r;
unsigned long long a , b , c , d;

unsigned long long tonum( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )
        i++;
    for( ; i < s.length(); i++ ){
        temp *= 10;
        temp += ( (int)s[ i ] - 48 );
    }
    return temp;
}

unsigned long long convert( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )i++;

    for( ; i < s.length(); i++ ){
        temp *= a;
        if( (int)s[ i ] < 60 )temp += ( (int)s[ i ] - 48 );
        else temp += (( (int)s[ i ] - 65 ) + 11 );
    }

    return temp;
}

string tostring( int x ){
    string t( 1 , (char)x );
    return t;
}

string decimal2roman(int input) {
  const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  const int decimal[13] =  {1000,  900, 500,  400, 100,   90,  50,   40,  10,   9,    5,    4,   1};
  string romanvalue = "";
  for (int i = 0; i < 13; i++) {
    while (input >= decimal[i]) {
      input -= decimal[i];
      romanvalue += roman[i];
    }
  }
  return romanvalue;
}

int main()
{

    cin>>s;
    a = tonum( s );
    cin>>s;
    if( s[ 0 ] == 'R' ){
        cin>>s;
        c = tonum( s );
        d = convert( s );
        cout<<decimal2roman( d )<<endl;
    }
    else{

        b = tonum( s );
        cin>>s;
        c = tonum( s );
        d = convert( s );

        if( c == 0 ){
            cout<<c<<endl;
            return 0;
        }

        while( d != 0 ){
            r.append( tostring( d % b ) );
            d /= b;
        }

        int i = r.length() - 1;
        while( r[ i ] == '0' )
            i--;

        for( ; i >= 0; i-- ){
            if( (int)r[ i ] < 10 )cout<<(char)(r[ i ] + 48);
            else cout<<(char)(r[ i ] + 54 );
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
using namespace std;
#include <string>
string s , r;
unsigned long long a , b , c , d;

unsigned long long tonum( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )
        i++;
    for( ; i < s.length(); i++ ){
        temp *= 10;
        temp += ( (int)s[ i ] - 48 );
    }
    return temp;
}

unsigned long long convert( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )i++;

    for( ; i < s.length(); i++ ){
        temp *= a;
        if( (int)s[ i ] < 60 )temp += ( (int)s[ i ] - 48 );
        else temp += (( (int)s[ i ] - 65 ) + 11 );
    }

    return temp;
}

string tostring( int x ){
    string t( 1 , (char)x );
    return t;
}

string decimal2roman(int input) {
  const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  const int decimal[13] =  {1000,  900, 500,  400, 100,   90,  50,   40,  10,   9,    5,    4,   1};
  string romanvalue = "";
  for (int i = 0; i < 13; i++) {
    while (input >= decimal[i]) {
      input -= decimal[i];
      romanvalue += roman[i];
    }
  }
  return romanvalue;
}

int main()
{

    cin>>s;
    a = tonum( s );
    cin>>s;
    if( s[ 0 ] == 'R' ){
        cin>>s;
        c = tonum( s );
        d = convert( s );
        cout<<decimal2roman( d )<<endl;
    }
    else{

        b = tonum( s );
        cin>>s;
        c = tonum( s );
        d = convert( s );

        if( c == 0 ){
            cout<<c<<endl;
            return 0;
        }

        while( d != 0 ){
            r.append( tostring( d % b ) );
            d /= b;
        }

        int i = r.length() - 1;
        while( r[ i ] == '0' )
            i--;

        for( i = r.length() - 1; i >= 0; i-- ){
            if( (int)r[ i ] < 10 )cout<<(char)(r[ i ] + 48);
            else cout<<(char)(r[ i ] + 54 );
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
using namespace std;
#include <string>
string s , r;
unsigned long long a , b , c , d;

unsigned long long tonum( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )
        i++;
    for( ; i < s.length(); i++ ){
        temp *= 10;
        temp += ( (int)s[ i ] - 48 );
    }
    return temp;
}

unsigned long long convert( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( i < s.length() && s[ i ] == '0' )i++;

    for( ; i < s.length(); i++ ){
        temp *= a;
        if( (int)s[ i ] < 60 )temp += ( (int)s[ i ] - 48 );
        else temp += (( (int)s[ i ] - 65 ) + 10 );
    }

    return temp;
}

string tostring( int x ){
    string t( 1 , (char)x );
    return t;
}

string decimal2roman(int input) {
  const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  const int decimal[13] =  {1000,  900, 500,  400, 100,   90,  50,   40,  10,   9,    5,    4,   1};
  string romanvalue = "";
  for (int i = 0; i < 13; i++) {
    while (input >= decimal[i]) {
      input -= decimal[i];
      romanvalue += roman[i];
    }
  }
  return romanvalue;
}

int main()
{

    cin>>s;
    a = tonum( s );
    cin>>s;
    if( s[ 0 ] == 'R' ){
        cin>>s;
        c = tonum( s );
        d = convert( s );
        cout<<decimal2roman( d )<<endl;
    }
    else{

        b = tonum( s );
        cin>>s;
        c = tonum( s );
        d = convert( s );

        if( c == 0 ){
            cout<<c<<endl;
            return 0;
        }

        while( d != 0 ){
            r.append( tostring( d % b ) );
            d /= b;
        }

        int i = r.length() - 1;
        while( i >= 0 && r[ i ] == '0' )
            i--;

        for( ; i >= 0; i-- ){
            if( (int)r[ i ] < 10 )cout<<(char)((int)r[ i ] + 48);
            else cout<<(char)((int)r[ i ] + 55 );
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
using namespace std;
#include <string>
string s , r;
unsigned long long a , b , c , d;

unsigned long long tonum( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )
        i++;
    for( ; i < s.length(); i++ ){
        temp *= 10;
        temp += ( (int)s[ i ] - 48 );
    }
    return temp;
}

unsigned long long convert( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )i++;

    for( ; i < s.length(); i++ ){
        temp *= a;
        if( (int)s[ i ] < 60 )temp += ( (int)s[ i ] - 48 );
        else temp += (( (int)s[ i ] - 65 ) + 11 );
    }

    return temp;
}

string tostring( int x ){
    string t( 1 , (char)x );
    return t;
}

string decimal2roman(int input) {
  const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  const int decimal[13] =  {1000,  900, 500,  400, 100,   90,  50,   40,  10,   9,    5,    4,   1};
  string romanvalue = "";
  for (int i = 0; i < 13; i++) {
    while (input >= decimal[i]) {
      input -= decimal[i];
      romanvalue += roman[i];
    }
  }
  return romanvalue;
}

int main()
{

    cin>>s;
    a = tonum( s );
    cin>>s;
    if( s[ 0 ] == 'R' ){
        cin>>s;
        c = tonum( s );
        d = convert( s );
        cout<<decimal2roman( d )<<endl;
    }
    else{

        b = tonum( s );
        cin>>s;
        c = tonum( s );
        d = convert( s );

        if( c == 0 ){
            cout<<c<<endl;
            return 0;
        }

        while( d != 0 ){
            r.append( tostring( d % b ) );
            d /= b;
        }

        for( int i = r.length() - 1; i >= 0; i-- ){
            if( (int)r[ i ] < 10 )cout<<(char)(r[ i ] + 48);
            else cout<<(char)(r[ i ] + 54 );
        }
        cout<<endl;

    }

    return 0;
}