#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

vector<ll> pow2;

void solve(){
    ll n, ans=0;
    cin>>n;
    vector<ll> list;
    for(int i=1;i<=n;i++){
        ll temp;
        cin>>temp;
        list.push_back(temp);
    }

    sort(list.begin(), list.end());

    // for(int i=0;i<list.size();i++){
    //     cout<<list[i]<<endl;
    // }

    ll begin=0, end=-1, flag;
    for(int i=0;i<pow2.size();i++){
        if(list[begin]<pow2[i]){
            flag=i;
            break;
        }
    }
    //cout<<flag<<endl;
    while(begin<n){
        for(int i=begin;list[i]<pow2[flag] && i<n;i++){
            end=i;
        }

        // for(int i=begin;i<end;i++){
        //     for(int j=i+1;j<=end;j++){
        //         if(list[i]<=list[j]) ans++;
        //     }
        // }

        ll dis=end-begin+1;
        ans+=dis*(dis-1)/2;

        for(int i=flag+1;i<pow2.size();i++){
            if(list[begin]<pow2[i]){
                flag=i;
                break;
            }
        }
        begin=end+1;
        end=-1;
    }

    cout<<ans<<endl;
}

int main(){
    int t;
    cin>>t;

    ll pow=1;
    while(true){
        pow*=2;
        pow2.push_back(pow);
        if(pow>1e9) break;
    }

    for(int i=1;i<=t;i++){
        solve();
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

vector<ll> pow2;

void solve(){
    ll n, ans=0;
    cin>>n;

    // for(int i=0;i<pow2.size();i++){
    //     cout<<pow2[i]<<endl;
    // }

    vector<ll> list;
    for(int i=1;i<=n;i++){
        ll temp;
        cin>>temp;
        list.push_back(temp);
    }

    sort(list.begin(), list.end());

    ll begin=0, end=-1, flag;
    for(int i=0;i<pow2.size();i++){
        if(list[begin]<pow2[i]){
            flag=i;
            break;
        }
    }
    //cout<<flag<<endl;
    while(begin<n){
        for(int i=begin;list[i]<pow2[flag] && i<n;i++){
            end=i;
        }

        // for(int i=begin;i<end;i++){
        //     for(int j=i+1;j<=end;j++){
        //         if(list[i]<=list[j]) ans++;
        //     }
        // }

        //cout<<begin<<" "<<end<<" ";

        ll dis=end-begin+1;
        ans+=dis*(dis-1)/2;

        for(int i=flag+1;i<pow2.size();i++){
            if(list[end+1]<pow2[i]){
                flag=i;
                break;
            }
        }

        //cout<<flag<<endl;
        begin=end+1;
        end=-1;
    }

    cout<<ans<<endl;
}

int main(){
    int t;
    cin>>t;

    ll pow=1;
    while(true){
        pow*=2;
        pow2.push_back(pow);
        if(pow>1e9) break;
    }

    for(int i=1;i<=t;i++){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[60000];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t ;
    cin >> t;
    while(t --){
    int n;
    cin >> n ;
    for(int i = 0 ; i < n ; i ++ )
        cin >> a[i];
    long long ans = 0;
    for(int i = 0 ; i < n ; i ++ )
        for(int j = i + 1 ; j < n ; j ++ )
    {
        if(a[i] & a[j] >= a[i] ^ a[j] )ans++;

    }
        cout <<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[60];
int C(int x )
{
    int t = 0 ;
    while( x )
    {
        x >>= 1;
        t ++ ;

    }
    return t ;

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t ;
    cin >> t;
    while(t --){
            memset(a , 0 ,sizeof(a ) );

    int n;
    cin >> n ;
    int tmp;
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> tmp;
        a[ C(tmp) ] ++;
    }
    long long ans = 0;
    for(int i = 0 ; i < 60 ; i ++ )
    {

        ans += a[i] * ( a[i] -  1) / 2;

    }
        cout <<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[60];
int C(int x )
{
    int t = 0 ;
    while( x )
    {
        x >>= 1;
        t ++ ;

    }
    return t ;

}
int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    int t ;
    cin >> t;
    while(t --){

    memset(a , 0 ,sizeof(a ) );
    int n;
    cin >> n ;
    int tmp;
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> tmp;
        a[ C(tmp) ] ++;
    }
    int ans = 0;
    for(int i = 0 ; i < 60 ; i ++ )
    {
        if(a[i] > 0 )
        ans +=( a[i] * ( a[i] -  1) / 2 );

    }
        cout << ans <<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[60];
int C(int x )
{
    int t = 0 ;
    while( x )
    {
        x >>= 1;
        t ++ ;

    }
    return t ;

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t ;
    cin >> t;
    while(t --){
    memset(a , 0 ,sizeof(a ) );
    int n;
    cin >> n ;
    int tmp;
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> tmp;
        a[ C(tmp) ] ++;
    }
    long long ans = 0;
    for(int i = 0 ; i < 60 ; i ++ )
    {
        if(a[i] > 0 )
        ans += a[i] * ( a[i] -  1) / 2;

    }
        cout <<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[60];
int C(int x )
{
    int t = 0 ;
    while( x )
    {
        x >>= 1;
        t ++ ;

    }
    return t ;

}
int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    int t ;
    cin >> t;
    while(t --){
    memset(a , 0 ,sizeof(a ) );
    int n;
    cin >> n ;
    int tmp;
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> tmp;
        a[ C(tmp) ] ++;
    }
    int ans = 0;
    for(int i = 0 ; i < 60 ; i ++ )
    {
        if(a[i] > 0 )
        ans +=( a[i] * ( a[i] -  1) / 2 );

    }
        cout <<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[60000];
int C(int x )
{
    int t = 0 ;
    while( x )
    {
        x >>= 1;
        t ++ ;

    }
    return t ;

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t ;
    cin >> t;
    while(t --){
    int n;
    cin >> n ;
    int tmp;
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> tmp;
        a[i] = C(tmp);
    }
    long long ans = 0;
    for(int i = 0 ; i < n ; i ++ )
        for(int j = i + 1 ; j < n ; j ++ )
    {

        if(a[i] == a[j] )ans ++;

    }
        cout <<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[60];
ll C(int x )
{
    int t = 0 ;
    while( x )
    {
        x >>= 1;
        t ++ ;

    }
    return t ;

}
int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    int t ;
    cin >> t;
    while(t --){

    memset(a , 0 ,sizeof(a ) );
    int n;
    cin >> n ;
    int tmp;
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> tmp;
        a[ C(tmp) ] ++;
    }
    ll ans = 0;
    for(int i = 0 ; i < 60 ; i ++ )
    {
        if(a[i] > 0 )
        ans +=( a[i] * ( a[i] -  1) / 2 );

    }
        cout << ans <<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define task "..."
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 10;
const int MOD = 1e9 + 7;

template <class T> void read(T &x) {
    char ch; bool neg = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())
        neg = ch == '-';
    x = ch - '0';
    for (ch = getchar();  isdigit(ch); ch = getchar())
        (x *= 10) += ch - '0';
    x *= neg ? -1 : 1;
}

template <class T, class ...Args> void read(T &x, Args &...args) {
    read(x); read(args...);
}

template <class T> void write(T x) {
    if (x < 0) {
        putchar('-'); write(-x);
        return;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

template <class T> void writei(T x) {
    write(x); putchar(' ');
}

template <class T> void writeln(T x) {
    write(x); putchar('\n');
}

template <class T, class ...Args> void write(T x, Args &...args) {
    writei(x); writei(args...);
}

int a[N];

int t = 1;

void solve() {
    int n;
    read(n);
    for (int i = 1; i <= n; i++) {
        int tmp;
        read(tmp);
        for (int j = 30; j >= 0; j--) {
            if ((1 << j) & tmp) {
                a[i] = j;
                //cout << a[i] << endl;
                break;
            }
        }
    }
    sort(a + 1, a + 1 + n);
    int ans = 0;
    int cur = 1;
    for (int i = 2; i <= n; i++) {
        if (a[i] == a[i - 1]) {
            ans += i - cur;
        } else {
            cur = i;
        }
    }
    writeln(ans);
}

int main() {
    if (fopen(task ".inp","r")) {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    read(t);
    while (t--) {
        solve();
    }
}


#include <bits/stdc++.h>
#define task "..."
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 10;
const int MOD = 1e9 + 7;

template <class T> void read(T &x) {
    char ch; bool neg = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())
        neg = ch == '-';
    x = ch - '0';
    for (ch = getchar();  isdigit(ch); ch = getchar())
        (x *= 10) += ch - '0';
    x *= neg ? -1 : 1;
}

template <class T, class ...Args> void read(T &x, Args &...args) {
    read(x); read(args...);
}

template <class T> void write(T x) {
    if (x < 0) {
        putchar('-'); write(-x);
        return;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

template <class T> void writei(T x) {
    write(x); putchar(' ');
}

template <class T> void writeln(T x) {
    write(x); putchar('\n');
}

template <class T, class ...Args> void write(T x, Args &...args) {
    writei(x); writei(args...);
}

ll a[N];

int t = 1;

void solve() {
    int n;
    read(n);
    for (int i = 1; i <= n; i++) {
        int tmp;
        read(tmp);
        for (int j = 30; j >= 0; j--) {
            if ((1 << j) & tmp) {
                a[i] = j;
                //cout << a[i] << endl;
                break;
            }
        }
    }
    sort(a + 1, a + 1 + n);
    int ans = 0;
    int cur = 1;
    for (int i = 2; i <= n; i++) {
        if (a[i] == a[i - 1]) {
            ans += i - cur;
        } else {
            cur = i;
        }
    }
    writeln(ans);
}

int main() {
    if (fopen(task ".inp","r")) {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    read(t);
    while (t--) {
        solve();
    }
}


#include <bits/stdc++.h>
#define task "..."
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 0 + 10;
const int MOD = 1e9 + 7;

template <class T> void read(T &x) {
    char ch; bool neg = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())
        neg = ch == '-';
    x = ch - '0';
    for (ch = getchar();  isdigit(ch); ch = getchar())
        (x *= 10) += ch - '0';
    x *= neg ? -1 : 1;
}

template <class T, class ...Args> void read(T &x, Args &...args) {
    read(x); read(args...);
}

template <class T> void write(T x) {
    if (x < 0) {
        putchar('-'); write(-x);
        return;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

template <class T> void writei(T x) {
    write(x); putchar(' ');
}

template <class T> void writeln(T x) {
    write(x); putchar('\n');
}

template <class T, class ...Args> void write(T x, Args &...args) {
    writei(x); writei(args...);
}

int a[N];

int t = 1;

void solve() {
    int n;
    read(n);
    for (int i = 1; i <= n; i++) {
        int tmp;
        read(tmp);
        for (int j = 31; j >= 0; j--) {
            if ((1 << j) & tmp) {
                a[i] = j;
                //cout << a[i] << endl;
                break;
            }
        }
    }
    sort(a + 1, a + 1 + n);
    int ans = 0;
    int cur = 1;
    for (int i = 2; i <= n; i++) {
        if (a[i] == a[i - 1]) {
            ans += i - cur;
        } else {
            cur = i;
        }
    }
    writeln(ans);
}

int main() {
    if (fopen(task ".inp","r")) {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    read(t);
    while (t--) {
        solve();
    }
}


#include <bits/stdc++.h>
#define task "..."
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 10;
const int MOD = 1e9 + 7;

template <class T> void read(T &x) {
    char ch; bool neg = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())
        neg = ch == '-';
    x = ch - '0';
    for (ch = getchar();  isdigit(ch); ch = getchar())
        (x *= 10) += ch - '0';
    x *= neg ? -1 : 1;
}

template <class T, class ...Args> void read(T &x, Args &...args) {
    read(x); read(args...);
}

template <class T> void write(T x) {
    if (x < 0) {
        putchar('-'); write(-x);
        return;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

template <class T> void writei(T x) {
    write(x); putchar(' ');
}

template <class T> void writeln(T x) {
    write(x); putchar('\n');
}

template <class T, class ...Args> void write(T x, Args &...args) {
    writei(x); writei(args...);
}

ll a[N];

int t = 1;

void solve() {
    int n;
    read(n);
    for (int i = 1; i <= n; i++) {
        int tmp;
        read(tmp);
        for (int j = 30; j >= 0; j--) {
            if ((1 << j) & tmp) {
                a[i] = j;
                //cout << a[i] << endl;
                break;
            }
        }
    }
    sort(a + 1, a + 1 + n);
    int ans = 0;
    int cur = 1;
    for (int i = 2; i <= n; i++) {
        if (a[i] == a[i - 1]) {
            ans += ll(i - cur);
        } else {
            cur = i;
        }
    }
    writeln(ans);
}

int main() {
    if (fopen(task ".inp","r")) {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    read(t);
    while (t--) {
        solve();
    }
}


#include <bits/stdc++.h>
#define task "..."
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 10;
const int MOD = 1e9 + 7;

template <class T> void read(T &x) {
    char ch; bool neg = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())
        neg = ch == '-';
    x = ch - '0';
    for (ch = getchar();  isdigit(ch); ch = getchar())
        (x *= 10) += ch - '0';
    x *= neg ? -1 : 1;
}

template <class T, class ...Args> void read(T &x, Args &...args) {
    read(x); read(args...);
}

template <class T> void write(T x) {
    if (x < 0) {
        putchar('-'); write(-x);
        return;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

template <class T> void writei(T x) {
    write(x); putchar(' ');
}

template <class T> void writeln(T x) {
    write(x); putchar('\n');
}

template <class T, class ...Args> void write(T x, Args &...args) {
    writei(x); writei(args...);
}

int a[N];

int t = 1;

void solve() {
    int n;
    read(n);
    for (int i = 1; i <= n; i++) {
        int tmp;
        read(tmp);
        for (int j = 30; j >= 0; j--) {
            if ((1 << j) & tmp) {
                a[i] = j;
                //cout << a[i] << endl;
                break;
            }
        }
    }
    sort(a + 1, a + 1 + n);
    ll ans = 0;
    int cur = 1;
    for (int i = 2; i <= n; i++) {
        if (a[i] == a[i - 1]) {
            ans += i - cur;
        } else {
            cur = i;
        }
    }
    writeln(ans);
}

int main() {
    if (fopen(task ".inp","r")) {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    read(t);
    while (t--) {
        solve();
    }
}


#include <bits/stdc++.h>
#define task "..."
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 10;
const int MOD = 1e9 + 7;

template <class T> void read(T &x) {
    char ch; bool neg = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())
        neg = ch == '-';
    x = ch - '0';
    for (ch = getchar();  isdigit(ch); ch = getchar())
        (x *= 10) += ch - '0';
    x *= neg ? -1 : 1;
}

template <class T, class ...Args> void read(T &x, Args &...args) {
    read(x); read(args...);
}

template <class T> void write(T x) {
    if (x < 0) {
        putchar('-'); write(-x);
        return;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

template <class T> void writei(T x) {
    write(x); putchar(' ');
}

template <class T> void writeln(T x) {
    write(x); putchar('\n');
}

template <class T, class ...Args> void write(T x, Args &...args) {
    writei(x); writei(args...);
}

int a[N];

int t = 1;

void solve() {
    int n;
    read(n);
    for (int i = 1; i <= n; i++) {
        int tmp;
        read(tmp);
        for (int j = 31; j >= 0; j--) {
            if ((1 << j) & tmp) {
                a[i] = j;
                //cout << a[i] << endl;
                break;
            }
        }
    }
    sort(a + 1, a + 1 + n);
    int ans = 0;
    int cur = 1;
    for (int i = 2; i <= n; i++) {
        if (a[i] == a[i - 1]) {
            ans += i - cur;
        } else {
            cur = i;
        }
    }
    writeln(ans);
}

int main() {
    if (fopen(task ".inp","r")) {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    read(t);
    while (t--) {
        solve();
    }
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
       long int n;
        cin>>n;
       long int v[1000];
        vector< long int>m;
        vector<long int>r;
        for(long int i=0;i<n;i++)
        {
            cin>>v[i];
        }
        sort(v,v+n);

        for(long int i=0;i<n;i++)
        {
            for(long int j=i+1;j<n;j++)
            {
                if(v[i]<=v[j])
                {
                    m.push_back(v[i]&v[j]);
                    r.push_back(v[i]^v[j]);
                }
            }
        }

       long  int l=0;
        for(long int i=0,j=0;i<m.size();i++,j++)
        {
            if(m[i]>=r[j])
            {
                l++;
            }
        }
        cout<<l<<endl;
    }
}


#include<iostream>
#include<vector>
#include<algorithm>
#define NULL
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
       int n;
        cin>>n;
       int v[n];
        for(long int i=0;i<n;i++)
        {
            cin>>v[i];
        }
        sort(v,v+n);
       long  int l=0;

        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)

                if((v[i]&v[j])>=(v[i]^v[j]))
                {
                    l++;
                }

        }
        cout<<l<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int N = 10005;
int a , n;
char str[5];
char num[N];
void gao (int n , int b) {
    if (!n) return ;
    gao (n / b , b);
    if (n % b < 10) printf ("%d" , n % b);
    else printf ("%c" , n % b - 10 + 'A');
}
string s[] = {"I" , "V" , "X" , "L" , "C" , "D" , "M"};
int main () {
    #ifndef ONLINE_JUDGE
        freopen ("input.txt" , "r" , stdin);
        // freopen ("output.txt" , "w" , stdout);
    #endif
    scanf ("%d %s" , &a , str);
    scanf ("%s" , num);
    for (int i = 0 ; num[i] ; i ++) {
        n = n * a + (num[i] > '9' ? (num[i] - 'A' + 10) : (num[i] - '0'));
    }
    // cout << n << endl;
    if (str[0] == 'R') {
        int i = 0;
        string ans = "";
        while (n) {
            int p = n % 10;
            if (p == 1) ans = s[i] + ans;
            if (p == 2) ans = s[i] + s[i] + ans;
            if (p == 3) ans = s[i] + s[i] + s[i] + ans;
            if (p == 4) ans = s[i] + s[i + 1] + ans;
            if (p == 5) ans = s[i + 1] + ans;
            if (p == 6) ans = s[i + 1] + s[i] + ans;
            if (p == 7) ans = s[i + 1] + s[i] + s[i] + ans;
            if (p == 8) ans = s[i + 1] + s[i] + s[i] + s[i] + ans;
            if (p == 9) ans = s[i + 2] + s[i] + ans;
            i += 2;
            n /= 10;
        }
        cout << ans << endl;
    }
    else {
        int b;
        sscanf (str , "%d" , &b);
        gao (n , b);
        puts ("");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int N = 10005;
LL a , n;
char str[5];
char num[N];
void gao (LL n , int b) {
	if (!n) return ;
	gao (n / b , b);
	if (n % b < 10) printf ("%d" , n % b);
	else printf ("%c" , n % b - 10 + 'A');
}
string s[] = {"I" , "V" , "X" , "L" , "C" , "D" , "M"};
int main () {
	#ifndef ONLINE_JUDGE
        freopen ("input.txt" , "r" , stdin);
        // freopen ("output.txt" , "w" , stdout);
    #endif
    scanf ("%I64d %s" , &a , str);
    scanf ("%s" , num);
    for (int i = 0 ; num[i] ; i ++) {
    	n = n * a + (num[i] > '9' ? (num[i] - 'A' + 10) : (num[i] - '0'));
    }
    cout << n << endl;
    if (str[0] == 'R') {
    	int i = 0;
    	string ans = "";
    	while (n) {
    		int p = n % 10;
    		if (p == 1) ans = s[i] + ans;
    		if (p == 2) ans = s[i] + s[i] + ans;
    		if (p == 3) ans = s[i] + s[i] + s[i] + ans;
    		if (p == 4) ans = s[i] + s[i + 1] + ans;
    		if (p == 5) ans = s[i + 1] + ans;
    		if (p == 6) ans = s[i + 1] + s[i] + ans;
    		if (p == 7) ans = s[i + 1] + s[i] + s[i] + ans;
    		if (p == 8) ans = s[i + 1] + s[i] + s[i] + s[i] + ans;
    		if (p == 9) ans = s[i] + s[i + 2] + ans;
    		i += 2;
    		n /= 10;
    	}
    	cout << ans << endl;
    }
    else {
    	int b;
    	sscanf (str , "%d" , &b);
    	gao (n , b);
    	puts ("");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int N = 10005;
LL a , n;
char str[5];
char num[N];
void gao (LL n , int b) {
    if (!n) return ;
    gao (n / b , b);
    if (n % b < 10) printf ("%d" , n % b);
    else printf ("%c" , n % b - 10 + 'A');
}
string s[] = {"I" , "V" , "X" , "L" , "C" , "D" , "M"};
int main () {
    #ifndef ONLINE_JUDGE
        freopen ("input.txt" , "r" , stdin);
        // freopen ("output.txt" , "w" , stdout);
    #endif
    scanf ("%I64d %s" , &a , str);
    scanf ("%s" , num);
    for (int i = 0 ; num[i] ; i ++) {
        n = n * a + (num[i] > '9' ? (num[i] - 'A' + 10) : (num[i] - '0'));
    }
    // cout << n << endl;
    if (str[0] == 'R') {
        int i = 0;
        string ans = "";
        while (n) {
            int p = n % 10;
            if (p == 1) ans = s[i] + ans;
            if (p == 2) ans = s[i] + s[i] + ans;
            if (p == 3) ans = s[i] + s[i] + s[i] + ans;
            if (p == 4) ans = s[i] + s[i + 1] + ans;
            if (p == 5) ans = s[i + 1] + ans;
            if (p == 6) ans = s[i + 1] + s[i] + ans;
            if (p == 7) ans = s[i + 1] + s[i] + s[i] + ans;
            if (p == 8) ans = s[i + 1] + s[i] + s[i] + s[i] + ans;
            if (p == 9) ans = s[i] + s[i + 2] + ans;
            i += 2;
            n /= 10;
        }
        cout << ans << endl;
    }
    else {
        int b;
        sscanf (str , "%d" , &b);
        if (n == 0) printf ("0");
        gao (n , b);
        puts ("");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define scs(a) scanf("%s",a)
#define sc1(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define sc3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sc1l(a) scanf("%lld",&a)
#define sc2l(a,b) scanf("%lld %lld",&a,&b)
#define sc3l(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sc1d(a) scanf("%lf",&a)
#define sc2d(a,b) scanf("%lf %lf",&a,&b)
#define sc3d(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define pfs(a) printf("%s",a)
#define pf1(a) printf("%d",a)
#define pf2(a,b) printf("%d %d",a,b)
#define pf1l(a) printf("%lld",a)
#define pf2l(a,b) printf("%lld %lld",a,b)
#define pf1d(a) printf("%lf",a)
#define pf2d(a,b) printf("%lf %lf",a,b)

#define br printf("\n")
#define ok printf("ok\n");

#define pf printf
#define sc scanf
#define pb push_back
#define mp make_pair
#define F first
#define S second

#define pii pair<int,int>
#define pll pair<long long,long long>

#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))

#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)

const double PI=acos(-1.0);

long long  gcd(long long x,long long y){return !y?(long long)abs(x):gcd(y,x%y);}  //returns always positive gcd

long long lcm(long long a,long long b){return ((long long)abs((a/gcd(a,b))*b));} //returns always positive lcm

long long bigmod(long long b,long long p,long long m){              //for both positive and negative base
    if(!p)return 1%m;
    else if(p&1)return (((b%m)+m)%m*bigmod(b,p-1,m))%m;
    else {long long ret=bigmod(b,p/2,m);return (ret*ret)%m;}
}
// inverse will exist if and only if gcd(a,m) is 1
long long inverse(long long a,long long m){return bigmod(a,m-2,m);}  //if Mod is prime
long long extended(long long a,long long m){
    long long oldr=a,r=m,oldans=1,ans=0,quotient;
    while(r){
        quotient=oldr/r;
        oldr=oldr-r*quotient;
        swap(oldr,r);
        oldans=oldans-ans*quotient;
        swap(oldans,ans);
    }
    return ((oldans%m)+m)%m;
}

template<class t1,class t2>  bool checkBit(t1 &mask,t2 i){return mask&((t1)1<<i);}
template<class t1,class t2>  void setBit(t1 &mask,t2 i){mask=mask|((t1)1<<i);}
template<class t1,class t2>  void resetBit(t1 &mask,t2 i){mask=mask&~((t1)1<<i);}

typedef long long ll;
const long long inf=(ll)1e8;
const long long mod=(ll)1e9+7;

int a;
char b[5],c[1000+5];
long long t,x;

void convert(){
    x=0;
    for(int i=0;c[i];i++){
        x*=a;
        x+=isalpha(c[i])?c[i]-65+10:c[i]-48;
    }

    if(b[0]=='R')return;

    t=0;
    for(int i=0;b[i];i++){
        t*=10;
        t+=b[i]-48;
    }
}

string ans;
void c1(){
    while(x){
        int add=x%t;
        x/=t;
        if(add<10)ans+=add+48;
        else ans+=add-10+65;
    }
    reverse(ans.begin(),ans.end());
    if(!ans.size())ans+='0';
}
void c2(int z){
    while(z>=1000){
        ans+='M';
        z-=1000;
    }
    while(z>=900){
        ans+='C';
        ans+='M';
        z-=900;
    }
    while(z>=500){
        ans+='D';
        z-=500;
    }
    while(z>=400){
        ans+='C';
        ans+='D';
        z-=400;
    }
    while(z>=100){
        ans+='C';
        z-=100;
    }
    while(z>=90){
        ans+='X';
        ans+='C';
        z-=90;
    }
    while(z>=50){
        ans+='L';
        z-=50;
    }
    while(z>=40){
        ans+='X';
        ans+='L';
        z-=40;
    }
    while(z>=10){
        ans+='X';
        z-=10;
    }
    while(z>=9){
        ans+='I';
        ans+='X';
        z-=9;
    }
    while(z>=5){
        ans+='V';
        z-=5;
    }
    while(z>=4){
        ans+='I';
        ans+='V';
        z-=4;
    }
    while(z>=1){
        ans+='I';
        z-=1;
    }
}
int main(){
    scanf("%d %s %s",&a,b,c);
    convert();
    t?c1():c2(x);

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define scs(a) scanf("%s",a)
#define sc1(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define sc3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sc1l(a) scanf("%lld",&a)
#define sc2l(a,b) scanf("%lld %lld",&a,&b)
#define sc3l(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sc1d(a) scanf("%lf",&a)
#define sc2d(a,b) scanf("%lf %lf",&a,&b)
#define sc3d(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define pfs(a) printf("%s",a)
#define pf1(a) printf("%d",a)
#define pf2(a,b) printf("%d %d",a,b)
#define pf1l(a) printf("%lld",a)
#define pf2l(a,b) printf("%lld %lld",a,b)
#define pf1d(a) printf("%lf",a)
#define pf2d(a,b) printf("%lf %lf",a,b)

#define br printf("\n")
#define ok printf("ok\n");

#define pf printf
#define sc scanf
#define pb push_back
#define mp make_pair
#define F first
#define S second

#define pii pair<int,int>
#define pll pair<long long,long long>

#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))

#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)

const double PI=acos(-1.0);

long long  gcd(long long x,long long y){return !y?(long long)abs(x):gcd(y,x%y);}  //returns always positive gcd

long long lcm(long long a,long long b){return ((long long)abs((a/gcd(a,b))*b));} //returns always positive lcm

long long bigmod(long long b,long long p,long long m){              //for both positive and negative base
    if(!p)return 1%m;
    else if(p&1)return (((b%m)+m)%m*bigmod(b,p-1,m))%m;
    else {long long ret=bigmod(b,p/2,m);return (ret*ret)%m;}
}
// inverse will exist if and only if gcd(a,m) is 1
long long inverse(long long a,long long m){return bigmod(a,m-2,m);}  //if Mod is prime
long long extended(long long a,long long m){
    long long oldr=a,r=m,oldans=1,ans=0,quotient;
    while(r){
        quotient=oldr/r;
        oldr=oldr-r*quotient;
        swap(oldr,r);
        oldans=oldans-ans*quotient;
        swap(oldans,ans);
    }
    return ((oldans%m)+m)%m;
}

template<class t1,class t2>  bool checkBit(t1 &mask,t2 i){return mask&((t1)1<<i);}
template<class t1,class t2>  void setBit(t1 &mask,t2 i){mask=mask|((t1)1<<i);}
template<class t1,class t2>  void resetBit(t1 &mask,t2 i){mask=mask&~((t1)1<<i);}

typedef long long ll;
const long long inf=(ll)1e8;
const long long mod=(ll)1e9+7;

int a;
char b[5],c[1000+5];
long long t,x;

void convert(){
    x=0;
    for(int i=0;c[i];i++){
        x*=a;
        x+=isalpha(c[i])?c[i]-65+10:c[i]-48;
    }

    if(b[0]=='R')return;

    t=0;
    for(int i=0;b[i];i++){
        t*=10;
        t+=b[i]-48;
    }
}

string ans;
void c1(){
    while(x){
        int add=x%t;
        x/=t;
        if(add<10)ans+=add+48;
        else ans+=add-10+65;
    }
    reverse(ans.begin(),ans.end());
}
void c2(int z){
    while(z>=1000){
        ans+='M';
        z-=1000;
    }
    while(z>=900){
        ans+='C';
        ans+='M';
        z-=900;
    }
    while(z>=500){
        ans+='D';
        z-=500;
    }
    while(z>=400){
        ans+='C';
        ans+='D';
        z-=400;
    }
    while(z>=100){
        ans+='C';
        z-=100;
    }
    while(z>=90){
        ans+='X';
        ans+='C';
        z-=90;
    }
    while(z>=50){
        ans+='L';
        z-=50;
    }
    while(z>=40){
        ans+='X';
        ans+='L';
        z-=40;
    }
    while(z>=10){
        ans+='X';
        z-=10;
    }
    while(z>=9){
        ans+='I';
        ans+='X';
        z-=9;
    }
    while(z>=5){
        ans+='V';
        z-=5;
    }
    while(z>=4){
        ans+='I';
        ans+='V';
        z-=4;
    }
    while(z>=1){
        ans+='I';
        z-=1;
    }
}
int main(){
    scanf("%d %s %s",&a,b,c);
    convert();
    t?c1():c2(x);

    cout<<ans<<endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <ctype.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iostream>

using namespace std;

typedef pair<int,int> ii;

#define INF 0x3f3f3f3f
#define ll long long

char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

ll getval(char ch) {
	for (int i=0; i<40; i++)
		if (ch == values[i])
			return (ll)(i);
	return 0LL;
}

void converte(ll n, ll base) {
	string ans;

	while (n) {
		ans.push_back(values[n%base]);
		n /= base;
	}

	reverse(ans.begin(),ans.end());

	cout << ans << endl;
	return;
}

void converteR(ll n) {
	vector<int> num;

	while (n) {
		num.push_back(n%10);
		n /= 10;
	}

	for (int i=int(num.size())-1; i>=0; i--) {
		if (i == 3) {
			for (int j=0; j<num[i]; j++) cout << "M";
		}

		else if (i == 2) {
			if (num[i] == 4) cout << "CD";
			else if (num[i] == 9) cout << "CM";
			else {
				if (num[i]/5 == 1) cout << "D";
				for (int j=0; j<num[i]%5; j++) cout << "C";
			}
		}

		else if (i == 1) {
			if (num[i] == 4) cout << "XL";
			else if (num[i] == 9) cout << "XC";
			else {
				if (num[i]/5 == 1) cout << "L";
				for (int j=0; j<num[i]%5; j++) cout << "X";
			}
		}

		else {
			if (num[i] == 4) cout << "IV";
			else if (num[i] == 9) cout << "IX";
			else {
				if (num[i]/5 == 1) cout << "V";
				for (int j=0; j<num[i]%5; j++) cout << "I";
			}
		}
	}

	cout << endl;
	return;
}

int main() {
	ll A,B,n;
	string base,num;

	cin>>A>>base>>num;

	n = 0LL;
	for (int i=0; i<int(num.size()); i++) {
		n *= A;
		n += getval(num[i]);
	}

	if (base[0] == 'R') converteR(n);
	else {
		if (base.size() == 1) B = base[0]-'0';
		else B = (base[0]-'0')*10 + base[1]-'0';
		converte(n,B);
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <ctype.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iostream>

using namespace std;

typedef pair<int,int> ii;

#define INF 0x3f3f3f3f
#define ll long long

char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

ll getval(char ch) {
	for (int i=0; i<40; i++)
		if (ch == values[i])
			return (ll)(i);
}

void converte(ll n, ll base) {
	string ans;

	while (n) {
		ans.push_back(values[n%base]);
		n /= base;
	}

	reverse(ans.begin(),ans.end());

	cout << ans << endl;
	return;
}

void converteR(ll n) {
	vector<int> num;

	while (n) {
		num.push_back(n%10);
		n /= 10;
	}

	for (int i=num.size(); i>=0; i--) {
		if (i == 3) {
			for (int j=0; j<num[i]; j++) cout << "M";
		}

		else if (i == 2) {
			if (num[i] == 4) cout << "CD";
			else if (num[i] == 9) cout << "CM";
			else {
				if (num[i]/5 == 1) cout << "D";
				for (int j=0; j<num[i]%5; j++) cout << "C";
			}
		}

		else if (i == 1) {
			if (num[i] == 4) cout << "XL";
			else if (num[i] == 9) cout << "XC";
			else {
				if (num[i]/5 == 1) cout << "L";
				for (int j=0; j<num[i]%5; j++) cout << "X";
			}
		}

		else {
			if (num[i] == 4) cout << "IV";
			else if (num[i] == 9) cout << "IX";
			else {
				if (num[i]/5 == 1) cout << "V";
				for (int j=0; j<num[i]%5; j++) cout << "I";
			}
		}
	}

	cout << endl;
	return;
}

int main() {
	ll A,B,n;
	string base,num;

	cin>>A>>base>>num;

	n = 0LL;
	for (int i=0; i<num.size(); i++) {
		n *= A;
		n += getval(num[i]);
	}

	if (base[0] == 'R') converteR(n);
	else {
		if (base.size() == 1) B = base[0]-'0';
		else B = (base[0]-'0')*10 + base[1]-'0';
		converte(n,B);
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <ctype.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iostream>

using namespace std;

typedef pair<int,int> ii;

#define INF 0x3f3f3f3f
#define ll long long

char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

ll getval(char ch) {
	for (int i=0; i<40; i++)
		if (ch == values[i])
			return (ll)(i);
	return 0LL;
}

void converte(ll n, ll base) {
	string ans;

	while (n) {
		ans.push_back(values[n%base]);
		n /= base;
	}

	reverse(ans.begin(),ans.end());

	cout << ans << endl;
	return;
}

void converteR(int n) {
	vector<int> num;

	while (n > 0) {
		num.push_back(n%10);
		n /= 10;
	}

	for (int i=int(num.size()); i>=0; i--) {
		if (i == 3) {
			for (int j=0; j<num[i]; j++) cout << "M";
		}

		else if (i == 2) {
			if (num[i] == 4) cout << "CD";
			else if (num[i] == 9) cout << "CM";
			else {
				if (num[i] >= 5) cout << "D";
				num[i] -= 5;
				for (int j=0; j<num[i]; j++) cout << "C";
			}
		}

		else if (i == 1) {
			if (num[i] == 4) cout << "XL";
			else if (num[i] == 9) cout << "XC";
			else {
				if (num[i] >= 5) cout << "L";
				num[i] -= 5;
				for (int j=0; j<num[i]; j++) cout << "X";
			}
		}

		else {
			if (num[i] == 4) cout << "IV";
			else if (num[i] == 9) cout << "IX";
			else {
				if (num[i] >= 5) cout << "V";
				num[i] -= 5;
				for (int j=0; j<num[i]; j++) cout << "I";
			}
		}
	}

	cout << endl;
	return;
}

int main() {
	ll A,B,n;
	string base,num;

	cin>>A>>base>>num;

	n = 0LL;
	for (int i=0; i<int(num.size()); i++) {
		n *= A;
		n += getval(num[i]);
	}

	if (base[0] == 'R') converteR(int(n));
	else {
		if (base.size() == 1) B = base[0]-'0';
		else B = (base[0]-'0')*10 + base[1]-'0';
		converte(n,(ll)(B));
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <ctype.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iostream>

using namespace std;

typedef pair<int,int> ii;

#define INF 0x3f3f3f3f
#define ll long long

char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

ll getval(char ch) {
	for (int i=0; i<40; i++)
		if (ch == values[i])
			return (ll)(i);
	return 0LL;
}

void converte(ll n, ll base) {
	string ans;

	while (n) {
		ans.push_back(values[n%base]);
		n /= base;
	}

	if (ans.size() == 0) ans = "0";

	reverse(ans.begin(),ans.end());

	cout << ans << endl;
	return;
}

void converteR(ll n) {
	vector<int> num;

	while (n) {
		num.push_back(n%10);
		n /= 10;
	}

	for (int i=int(num.size())-1; i>=0; i--) {
		if (i == 3) {
			for (int j=0; j<num[i]; j++) cout << "M";
		}

		else if (i == 2) {
			if (num[i] == 4) cout << "CD";
			else if (num[i] == 9) cout << "CM";
			else {
				if (num[i]/5 == 1) cout << "D";
				for (int j=0; j<num[i]%5; j++) cout << "C";
			}
		}

		else if (i == 1) {
			if (num[i] == 4) cout << "XL";
			else if (num[i] == 9) cout << "XC";
			else {
				if (num[i]/5 == 1) cout << "L";
				for (int j=0; j<num[i]%5; j++) cout << "X";
			}
		}

		else {
			if (num[i] == 4) cout << "IV";
			else if (num[i] == 9) cout << "IX";
			else {
				if (num[i]/5 == 1) cout << "V";
				for (int j=0; j<num[i]%5; j++) cout << "I";
			}
		}
	}

	cout << endl;
	return;
}

int main() {
	ll A,B,n;
	string base,num;

	cin>>A>>base>>num;

	n = 0LL;
	for (int i=0; i<int(num.size()); i++) {
		n *= A;
		n += getval(num[i]);
	}

	if (base[0] == 'R') converteR(n);
	else {
		if (base.size() == 1) B = base[0]-'0';
		else B = (base[0]-'0')*10 + base[1]-'0';
		converte(n,B);
	}

	return 0;
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.14159265
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long

using namespace std;

int len,i,a,b;
char ch;
long long deg[10000];
string c;
long long num;

int T(char k)
{
    if (k>='0' && k<='9') return k-'0';else return k-'A'+10;
}

void solve(int X)
{
    int i;
    if (X==0) return;
    if (X>=1000)
    {
       for (i=1;i<=X/1000;i++)
       cout<<"M";
       solve(X%1000);
       return;
    }
    if (X>=900)
    {
        cout<<"CM";
        solve(X%900);
        return;
    }
    if (X>=500)
    {
       for (i=1;i<=X/500;i++)
       cout<<"D";
       solve(X%500);
       return;
    }
    if (X>=400)
    {
        cout<<"CD";
        solve(X%400);
        return;
    }
    if (X>=100)
    {
        for (i=1;i<=X/100;i++)
            cout<<"C";
        solve(X%100);
        return;
    }
    if (X>=90)
    {
        cout<<"XC";
        solve(X%90);
        return;
    }
    if (X>=50)
    {
        for (i=1;i<=X/50;i++)
            cout<<"L";
        solve(X%50);
        return;
    }
    if (X>=40)
    {
        cout<<"XL";
        solve(X%40);
        return;
    }
    if (X>=10)
    {
        for (i=1;i<=X/10;i++)
            cout<<"X";
        solve(X%10);
        return;
    }
    if (X>=9)
    {
        cout<<"IX";
        solve(X%9);
        return;
    }
    if (X>=5)
    {
        for (i=1;i<=X/5;i++)
            cout<<"V";
        solve(X%5);
        return;
    }
    if (X>=4)
    {
        cout<<"IV";
        solve(X%4);
        return;
    }
    if (X>=1)
    {
        for (i=1;i<=X;i++)
            cout<<"I";
        solve(0);
    }
}

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

int d[100];

main()
{
  //  freopen("text.in","r",stdin);
  //  freopen("text.out","w",stdout);

    scanf("%d %c",&a,&ch);
    if (ch=='R'){
        scanf("\n");
        cin>>c;
    }
    else
    {
        b=ch-'0';
        while (1)
        {
            scanf("%c",&ch);
            if (!(ch>='0' && ch<='9')) break;
            b=b*10+ch-'0';
        }
        cin>>c;
    }

    deg[0]=1;
    for (i=1;;i++)
    {
        deg[i]=(long long)deg[i-1]*a;
        if (deg[i]>10000000000000000LL) break;
    }

    for (i=0;i<c.S;i++)
    {
        num=(long long)num+(long long)T(c[i])*deg[c.S-i-1];
    }

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

    if (ch=='R'){
    solve(num);
    cout<<endl;
    return 0;
    }

    while (num>0)
    {
        d[++len]=num%b;
        num/=b;
    }

    for (i=len;i>=1;i--)
    cout<<P(d[i]);
    cout<<endl;

}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.14159265
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long

using namespace std;

int len,i,a,b;
char ch;
long long deg[1000];
string c;
long long num;

int T(char k)
{
    if (k>='0' && k<='9') return k-'0';else return k-'A'+10;
}

void solve(int X)
{
    int i;
    if (X==0) exit(0);
    if (X>=1000)
    {
       for (i=1;i<=X/1000;i++)
       cout<<"M";
       solve(X%1000);
       return;
    }
    if (X>=900)
    {
        cout<<"CM";
        solve(X%900);
        return;
    }
    if (X>=500)
    {
       for (i=1;i<=X/500;i++)
       cout<<"D";
       solve(X%500);
       return;
    }
    if (X>=400)
    {
        cout<<"CD";
        solve(X%400);
        return;
    }
    if (X>=100)
    {
        for (i=1;i<=X/100;i++)
            cout<<"C";
        solve(X%100);
        return;
    }
    if (X>=90)
    {
        cout<<"LC";
        solve(X%90);
        return;
    }
    if (X>=50)
    {
        for (i=1;i<=X/50;i++)
            cout<<"L";
        solve(X%50);
        return;
    }
    if (X>=40)
    {
        cout<<"XL";
        solve(X%40);
        return;
    }
    if (X>=10)
    {
        for (i=1;i<=X/10;i++)
            cout<<"X";
        solve(X%10);
        return;
    }
    if (X>=9)
    {
        cout<<"IX";
        solve(X%9);
        return;
    }
    if (X>=5)
    {
        for (i=1;i<=X/5;i++)
            cout<<"V";
        solve(X%5);
        return;
    }
    if (X>=4)
    {
        cout<<"IV";
        solve(X%4);
        return;
    }
    if (X>=1)
    {
        for (i=1;i<=X;i++)
            cout<<"I";
        solve(0);
    }
}

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

int d[100];

main()
{
  //  freopen("text.in","r",stdin);
  //  freopen("text.out","w",stdout);

    scanf("%d %c",&a,&ch);
    if (ch=='R'){
        scanf("\n");
        cin>>c;
    }
    else
    {
        b=ch-'0';
        while (1)
        {
            scanf("%c",&ch);
            if (!(ch>='0' && ch<='9')) break;
            b=b*10+ch-'0';
        }
        cin>>c;
    }

    deg[0]=1;
    for (i=1;;i++)
    {
        deg[i]=(long long)deg[i-1]*a;
        if (deg[i]>10000000000000000LL) break;
    }

    for (i=0;i<c.S;i++)
    {
        num=num+T(c[i])*deg[c.S-i-1];
    }

    if (ch=='R'){
    solve(num);
    return 0;
    }

    while (num>0)
    {
        d[++len]=num%b;
        num/=b;
    }

    for (i=len;i>=1;i--)
    cout<<P(d[i]);
    cout<<endl;

}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.14159265
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long

using namespace std;

int len,i,a,b;
char ch;
long long deg[10000];
string c;
long long num;

int T(char k)
{
    if (k>='0' && k<='9') return k-'0';else return k-'A'+10;
}

void solve(int X)
{
    int i;
    if (X==0) return;
    if (X>=1000)
    {
       for (i=1;i<=X/1000;i++)
       cout<<"M";
       solve(X%1000);
       return;
    }
    if (X>=900)
    {
        cout<<"CM";
        solve(X%900);
        return;
    }
    if (X>=500)
    {
       for (i=1;i<=X/500;i++)
       cout<<"D";
       solve(X%500);
       return;
    }
    if (X>=400)
    {
        cout<<"CD";
        solve(X%400);
        return;
    }
    if (X>=100)
    {
        for (i=1;i<=X/100;i++)
            cout<<"C";
        solve(X%100);
        return;
    }
    if (X>=90)
    {
        cout<<"XC";
        solve(X%90);
        return;
    }
    if (X>=50)
    {
        for (i=1;i<=X/50;i++)
            cout<<"L";
        solve(X%50);
        return;
    }
    if (X>=40)
    {
        cout<<"XL";
        solve(X%40);
        return;
    }
    if (X>=10)
    {
        for (i=1;i<=X/10;i++)
            cout<<"X";
        solve(X%10);
        return;
    }
    if (X>=9)
    {
        cout<<"IX";
        solve(X%9);
        return;
    }
    if (X>=5)
    {
        for (i=1;i<=X/5;i++)
            cout<<"V";
        solve(X%5);
        return;
    }
    if (X>=4)
    {
        cout<<"IV";
        solve(X%4);
        return;
    }
    if (X>=1)
    {
        for (i=1;i<=X;i++)
            cout<<"I";
        solve(0);
    }
}

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

int d[100];

main()
{
  //  freopen("text.in","r",stdin);
  //  freopen("text.out","w",stdout);

    scanf("%d %c",&a,&ch);
    if (ch=='R'){
        scanf("\n");
        cin>>c;
    }
    else
    {
        b=ch-'0';
        while (1)
        {
            scanf("%c",&ch);
            if (!(ch>='0' && ch<='9')) break;
            b=b*10+ch-'0';
        }
        cin>>c;
    }

    deg[0]=1;
    for (i=1;;i++)
    {
        deg[i]=(long long)deg[i-1]*a;
        if (deg[i]>10000000000000000LL) break;
    }

    for (i=0;i<c.S;i++)
    {
        num=(long long)num+(long long)T(c[i])*deg[c.S-i-1];
    }

    if (ch=='R'){
    solve(num);
    cout<<endl;
    return 0;
    }

    while (num>0)
    {
        d[++len]=num%b;
        num/=b;
    }

    for (i=len;i>=1;i--)
    cout<<P(d[i]);
    cout<<endl;

}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.14159265
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long

using namespace std;

int len,i,a,b;
char ch;
long long deg[10000];
string c;
long long num;

int T(char k)
{
    if (k>='0' && k<='9') return k-'0';else return k-'A'+10;
}

void solve(int X)
{
    int i;
    if (X==0) exit(0);
    if (X>=1000)
    {
       for (i=1;i<=X/1000;i++)
       cout<<"M";
       solve(X%1000);
       return;
    }
    if (X>=900)
    {
        cout<<"CM";
        solve(X%900);
        return;
    }
    if (X>=500)
    {
       for (i=1;i<=X/500;i++)
       cout<<"D";
       solve(X%500);
       return;
    }
    if (X>=400)
    {
        cout<<"CD";
        solve(X%400);
        return;
    }
    if (X>=100)
    {
        for (i=1;i<=X/100;i++)
            cout<<"C";
        solve(X%100);
        return;
    }
    if (X>=90)
    {
        cout<<"XC";
        solve(X%90);
        return;
    }
    if (X>=50)
    {
        for (i=1;i<=X/50;i++)
            cout<<"L";
        solve(X%50);
        return;
    }
    if (X>=40)
    {
        cout<<"XL";
        solve(X%40);
        return;
    }
    if (X>=10)
    {
        for (i=1;i<=X/10;i++)
            cout<<"X";
        solve(X%10);
        return;
    }
    if (X>=9)
    {
        cout<<"IX";
        solve(X%9);
        return;
    }
    if (X>=5)
    {
        for (i=1;i<=X/5;i++)
            cout<<"V";
        solve(X%5);
        return;
    }
    if (X>=4)
    {
        cout<<"IV";
        solve(X%4);
        return;
    }
    if (X>=1)
    {
        for (i=1;i<=X;i++)
            cout<<"I";
        solve(0);
    }
}

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

int d[100];

main()
{
  //  freopen("text.in","r",stdin);
  //  freopen("text.out","w",stdout);

    scanf("%d %c",&a,&ch);
    if (ch=='R'){
        scanf("\n");
        cin>>c;
    }
    else
    {
        b=ch-'0';
        while (1)
        {
            scanf("%c",&ch);
            if (!(ch>='0' && ch<='9')) break;
            b=b*10+ch-'0';
        }
        cin>>c;
    }

    deg[0]=1;
    for (i=1;;i++)
    {
        deg[i]=(long long)deg[i-1]*a;
        if (deg[i]>10000000000000000LL) break;
    }

    for (i=0;i<c.S;i++)
    {
        num=num+T(c[i])*deg[c.S-i-1];
    }

    if (ch=='R'){
    solve(num);
    return 0;
    }

    while (num>0)
    {
        d[++len]=num%b;
        num/=b;
    }

    for (i=len;i>=1;i--)
    cout<<P(d[i]);
    cout<<endl;

}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.14159265
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long

using namespace std;

int len,i,a,b;
char ch;
long long deg[10000];
string c;
long long num;

int T(char k)
{
    if (k>='0' && k<='9') return k-'0';else return k-'A'+10;
}

void solve(int X)
{
    int i;
    if (X==0) return;
    if (X>=1000)
    {
       for (i=1;i<=X/1000;i++)
       cout<<"M";
       solve(X%1000);
       return;
    }
    if (X>=900)
    {
        cout<<"CM";
        solve(X%900);
        return;
    }
    if (X>=500)
    {
       for (i=1;i<=X/500;i++)
       cout<<"D";
       solve(X%500);
       return;
    }
    if (X>=400)
    {
        cout<<"CD";
        solve(X%400);
        return;
    }
    if (X>=100)
    {
        for (i=1;i<=X/100;i++)
            cout<<"C";
        solve(X%100);
        return;
    }
    if (X>=90)
    {
        cout<<"XC";
        solve(X%90);
        return;
    }
    if (X>=50)
    {
        for (i=1;i<=X/50;i++)
            cout<<"L";
        solve(X%50);
        return;
    }
    if (X>=40)
    {
        cout<<"XL";
        solve(X%40);
        return;
    }
    if (X>=10)
    {
        for (i=1;i<=X/10;i++)
            cout<<"X";
        solve(X%10);
        return;
    }
    if (X>=9)
    {
        cout<<"IX";
        solve(X%9);
        return;
    }
    if (X>=5)
    {
        for (i=1;i<=X/5;i++)
            cout<<"V";
        solve(X%5);
        return;
    }
    if (X>=4)
    {
        cout<<"IV";
        solve(X%4);
        return;
    }
    if (X>=1)
    {
        for (i=1;i<=X;i++)
            cout<<"I";
        solve(0);
    }
}

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

int d[100];

main()
{
  //  freopen("text.in","r",stdin);
  //  freopen("text.out","w",stdout);

    scanf("%d %c",&a,&ch);
    if (ch=='R'){
        scanf("\n");
        cin>>c;
    }
    else
    {
        b=ch-'0';
        while (1)
        {
            scanf("%c",&ch);
            if (!(ch>='0' && ch<='9')) break;
            b=b*10+ch-'0';
        }
        cin>>c;
    }

    deg[0]=1;
    for (i=1;;i++)
    {
        deg[i]=(long long)deg[i-1]*a;
        if (deg[i]>10000000000000000LL) break;
    }

    for (i=0;i<c.S;i++)
    {
        num=(long long)num+(long long)T(c[i])*deg[c.S-i-1];
    }

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

    if (ch=='R'){
    solve(num);
    cout<<endl;
    return 0;
    }

    while (num>0)
    {
        d[++len]=num%b;
        num/=b;
    }

    for (i=len;i>=1;i--)
    cout<<P(d[i]);
    cout<<endl;

}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.14159265
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long

using namespace std;

int len,i,a,b;
char ch;
long long deg[1000];
string c;
long long num;

int T(char k)
{
    if (k>='0' && k<='9') return k-'0';else return k-'A'+10;
}

void solve(int X)
{
    int i;
    if (X==0) exit(0);
    if (X>=1000)
    {
       for (i=1;i<=X/1000;i++)
       cout<<"M";
       solve(X%1000);
       return;
    }
    if (X>=900)
    {
        cout<<"CM";
        solve(X%900);
        return;
    }
    if (X>=500)
    {
       for (i=1;i<=X/500;i++)
       cout<<"D";
       solve(X%500);
       return;
    }
    if (X>=400)
    {
        cout<<"CD";
        solve(X%400);
        return;
    }
    if (X>=100)
    {
        for (i=1;i<=X/100;i++)
            cout<<"C";
        solve(X%100);
        return;
    }
    if (X>=90)
    {
        cout<<"XC";
        solve(X%90);
        return;
    }
    if (X>=50)
    {
        for (i=1;i<=X/50;i++)
            cout<<"L";
        solve(X%50);
        return;
    }
    if (X>=40)
    {
        cout<<"XL";
        solve(X%40);
        return;
    }
    if (X>=10)
    {
        for (i=1;i<=X/10;i++)
            cout<<"X";
        solve(X%10);
        return;
    }
    if (X>=9)
    {
        cout<<"IX";
        solve(X%9);
        return;
    }
    if (X>=5)
    {
        for (i=1;i<=X/5;i++)
            cout<<"V";
        solve(X%5);
        return;
    }
    if (X>=4)
    {
        cout<<"IV";
        solve(X%4);
        return;
    }
    if (X>=1)
    {
        for (i=1;i<=X;i++)
            cout<<"I";
        solve(0);
    }
}

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

int d[100];

main()
{
  //  freopen("text.in","r",stdin);
  //  freopen("text.out","w",stdout);

    scanf("%d %c",&a,&ch);
    if (ch=='R'){
        scanf("\n");
        cin>>c;
    }
    else
    {
        b=ch-'0';
        while (1)
        {
            scanf("%c",&ch);
            if (!(ch>='0' && ch<='9')) break;
            b=b*10+ch-'0';
        }
        cin>>c;
    }

    deg[0]=1;
    for (i=1;;i++)
    {
        deg[i]=(long long)deg[i-1]*a;
        if (deg[i]>10000000000000000LL) break;
    }

    for (i=0;i<c.S;i++)
    {
        num=num+T(c[i])*deg[c.S-i-1];
    }

    if (ch=='R'){
    solve(num);
    return 0;
    }

    while (num>0)
    {
        d[++len]=num%b;
        num/=b;
    }

    for (i=len;i>=1;i--)
    cout<<P(d[i]);
    cout<<endl;

}


#include <iostream>
#include <map>
#define lli long long int

using namespace std;

int stringToInt(string s)
{
    int k=1,v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*(s[i]-'0'),k*=10;
    return v;
}

lli getVal(char c)
{
    if (c>='0' && c<='9') return c-'0';
    else return c-'A'+10;
}

lli toBaseTen(string s,int a)
{
    lli k=1, v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*getVal(s[i]),k*=a;
    return v;
}

char getChar(int k)
{
    if (k<10) return char(k+'0');
    else return char(k+'A'-10);
}

string toBaseN(lli a,int b)
{
    string s="";

    if (b==-1)
    {
        string t[6] = {"M","D","C","L","X","I"};
        int v[6] = {1000,500,100,50,10,1};

        map<int,string> m;
        m.insert(make_pair<int,string>(1,"I"));
        m.insert(make_pair<int,string>(2,"II"));
        m.insert(make_pair<int,string>(3,"III"));
        m.insert(make_pair<int,string>(4,"IV"));
        m.insert(make_pair<int,string>(5,"V"));
        m.insert(make_pair<int,string>(6,"VI"));
        m.insert(make_pair<int,string>(7,"VII"));
        m.insert(make_pair<int,string>(8,"VIII"));
        m.insert(make_pair<int,string>(9,"IX"));
        m.insert(make_pair<int,string>(10,"X"));
        m.insert(make_pair<int,string>(20,"XX"));
        m.insert(make_pair<int,string>(30,"XXX"));
        m.insert(make_pair<int,string>(40,"XL"));
        m.insert(make_pair<int,string>(50,"L"));
        m.insert(make_pair<int,string>(60,"LX"));
        m.insert(make_pair<int,string>(70,"LXX"));
        m.insert(make_pair<int,string>(80,"LXXX"));
        m.insert(make_pair<int,string>(90,"XC"));
        m.insert(make_pair<int,string>(100,"C"));
        m.insert(make_pair<int,string>(200,"CC"));
        m.insert(make_pair<int,string>(300,"CCC"));
        m.insert(make_pair<int,string>(400,"CCC"));
        m.insert(make_pair<int,string>(500,"CD"));
        m.insert(make_pair<int,string>(600,"D"));
        m.insert(make_pair<int,string>(700,"DC"));
        m.insert(make_pair<int,string>(800,"DCC"));
        m.insert(make_pair<int,string>(900,"DCCC"));
        m.insert(make_pair<int,string>(1000,"M"));
        m.insert(make_pair<int,string>(2000,"MM"));
        m.insert(make_pair<int,string>(3000,"MMM"));

        int r,i=0;
        while (a)
        {
            if (a>=v[i])
            {
                r=(a/v[i])*v[i];
                a-=r;

                s+=m[r];
            }
            i++;
        }
    }
    else
    {
        while (a)
        {
            s+=(getChar(a%b));
            a/=b;
        }
        string y = s;
        for (int i=y.size()-1,j=0 ; i>=0 ; i--,j++) s[j]=y[i];
    }
    if (s=="") s="0";

    return s;
}

int main()
{
    int a,b;
    string s;

    cin>>a>>s;
    if (s=="R") b=-1;
    else b = stringToInt(s);

    cin>>s;
    int i=0;
    while (s[i]=='0' && i<s.size()) i++;
    if (i==s.size()) s="0";
    else s=s.substr(i,s.size()-i);

    lli qa = toBaseTen(s,a);
    cout<<qa<<"\n";
    cout<<toBaseN(qa,b);

    return 0;
}


#include <iostream>
#include <map>
#define lli long long int

using namespace std;

int stringToInt(string s)
{
    int k=1,v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*(s[i]-'0'),k*=10;
    return v;
}

lli getVal(char c)
{
    if (c>='0' && c<='9') return c-'0';
    else return c-'A'+10;
}

lli toBaseTen(string s,int a)
{
    lli k=1, v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*getVal(s[i]),k*=a;
    return v;
}

char getChar(int k)
{
    if (k<10) return char(k+'0');
    else return char(k+'A'-10);
}

string toBaseN(lli a,int b)
{
    string s="";

    if (b==-1)
    {
        map<int,string> m;
        m.insert(make_pair<int,string>(1,"I"));
        m.insert(make_pair<int,string>(2,"II"));
        m.insert(make_pair<int,string>(3,"III"));
        m.insert(make_pair<int,string>(4,"IV"));
        m.insert(make_pair<int,string>(5,"V"));
        m.insert(make_pair<int,string>(6,"VI"));
        m.insert(make_pair<int,string>(7,"VII"));
        m.insert(make_pair<int,string>(8,"VIII"));
        m.insert(make_pair<int,string>(9,"IX"));
        m.insert(make_pair<int,string>(10,"X"));
        m.insert(make_pair<int,string>(20,"XX"));
        m.insert(make_pair<int,string>(30,"XXX"));
        m.insert(make_pair<int,string>(40,"XL"));
        m.insert(make_pair<int,string>(50,"L"));
        m.insert(make_pair<int,string>(60,"LX"));
        m.insert(make_pair<int,string>(70,"LXX"));
        m.insert(make_pair<int,string>(80,"LXXX"));
        m.insert(make_pair<int,string>(90,"XC"));
        m.insert(make_pair<int,string>(100,"C"));
        m.insert(make_pair<int,string>(200,"CC"));
        m.insert(make_pair<int,string>(300,"CCC"));
        m.insert(make_pair<int,string>(400,"CCC"));
        m.insert(make_pair<int,string>(500,"CD"));
        m.insert(make_pair<int,string>(600,"D"));
        m.insert(make_pair<int,string>(700,"DC"));
        m.insert(make_pair<int,string>(800,"DCC"));
        m.insert(make_pair<int,string>(900,"DCCC"));
        m.insert(make_pair<int,string>(1000,"M"));
        m.insert(make_pair<int,string>(2000,"MM"));
        m.insert(make_pair<int,string>(3000,"MMM"));

        int r,v;
        map<int,string>::iterator i=m.end(); i--;
        while (a)
        {
            v = i->first;
            if (a>=v)
            {
                r=(a/v)*v;
                a-=r;

                s+=(i->second);
            }
            i--;
        }
    }
    else
    {
        while (a)
        {
            s+=(getChar(a%b));
            a/=b;
        }
        string y = s;
        for (int i=y.size()-1,j=0 ; i>=0 ; i--,j++) s[j]=y[i];
    }
    if (s=="") s="0";

    return s;
}

int main()
{
    int a,b;
    string s;

    cin>>a>>s;
    if (s=="R") b=-1;
    else b = stringToInt(s);

    cin>>s;
    int i=0;
    while (s[i]=='0' && i<s.size()) i++;
    if (i==s.size()) s="0";
    else s=s.substr(i,s.size()-i);

    lli qa = toBaseTen(s,a);
    cout<<qa<<"\n";
    cout<<toBaseN(qa,b);

    return 0;
}


#include <iostream>
#include <map>
#define lli long long int

using namespace std;

int stringToInt(string s)
{
    int k=1,v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*(s[i]-'0'),k*=10;
    return v;
}

lli getVal(char c)
{
    if (c>='0' && c<='9') return c-'0';
    else return c-'A'+10;
}

lli toBaseTen(string s,int a)
{
    lli k=1, v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*getVal(s[i]),k*=a;
    return v;
}

char getChar(int k)
{
    if (k<10) return char(k+'0');
    else return char(k+'A'-10);
}

string toBaseN(lli a,int b)
{
    string s="";

    if (b==-1)
    {
        string t[6] = {"M","D","C","L","X","I"};
        int v[6] = {1000,500,100,50,10,1};

        map<int,string> m;
        m.insert(make_pair<int,string>(1,"I"));
        m.insert(make_pair<int,string>(2,"II"));
        m.insert(make_pair<int,string>(3,"III"));
        m.insert(make_pair<int,string>(4,"IV"));
        m.insert(make_pair<int,string>(5,"V"));
        m.insert(make_pair<int,string>(6,"VI"));
        m.insert(make_pair<int,string>(7,"VII"));
        m.insert(make_pair<int,string>(8,"VIII"));
        m.insert(make_pair<int,string>(9,"IX"));
        m.insert(make_pair<int,string>(10,"X"));
        m.insert(make_pair<int,string>(20,"XX"));
        m.insert(make_pair<int,string>(30,"XXX"));
        m.insert(make_pair<int,string>(40,"XL"));
        m.insert(make_pair<int,string>(50,"L"));
        m.insert(make_pair<int,string>(60,"LX"));
        m.insert(make_pair<int,string>(70,"LXX"));
        m.insert(make_pair<int,string>(80,"LXXX"));
        m.insert(make_pair<int,string>(90,"XC"));
        m.insert(make_pair<int,string>(100,"C"));
        m.insert(make_pair<int,string>(200,"CC"));
        m.insert(make_pair<int,string>(300,"CCC"));
        m.insert(make_pair<int,string>(400,"CCC"));
        m.insert(make_pair<int,string>(500,"CD"));
        m.insert(make_pair<int,string>(600,"D"));
        m.insert(make_pair<int,string>(700,"DC"));
        m.insert(make_pair<int,string>(800,"DCC"));
        m.insert(make_pair<int,string>(900,"DCCC"));
        m.insert(make_pair<int,string>(1000,"M"));
        m.insert(make_pair<int,string>(2000,"MM"));
        m.insert(make_pair<int,string>(3000,"MMM"));

        int r,i=0;
        while (a)
        {
            if (a>=v[i])
            {
                r=a/v[i];
                a-=r*v[i];

                s+=m[r];
            }
            i++;
        }
    }
    else
    {
        while (a)
        {
            s+=(getChar(a%b));
            a/=b;
        }
        string y = s;
        for (int i=y.size()-1,j=0 ; i>=0 ; i--,j++) s[j]=y[i];
    }
    if (s=="") s="0";

    return s;
}

int main()
{
    int a,b;
    string s;

    cin>>a>>s;
    if (s=="R") b=-1;
    else b = stringToInt(s);

    cin>>s;
    int i=0;
    while (s[i]=='0' && i<s.size()) i++;
    if (i==s.size()) s="0";
    else s=s.substr(i,s.size()-i);

    lli qa = toBaseTen(s,a);
    cout<<toBaseN(qa,b);

    return 0;
}


#include <iostream>
#include <map>
#define lli long long int

using namespace std;

int stringToInt(string s)
{
    int k=1,v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*(s[i]-'0'),k*=10;
    return v;
}

lli getVal(char c)
{
    if (c>='0' && c<='9') return c-'0';
    else return c-'A'+10;
}

lli toBaseTen(string s,int a)
{
    lli k=1, v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*getVal(s[i]),k*=a;
    return v;
}

char getChar(int k)
{
    if (k<10) return char(k+'0');
    else return char(k+'A'-10);
}

string toBaseN(lli a,int b)
{
    string s="";

    if (b==-1)
    {
        string t[6] = {"M","D","C","L","X","I"};
        int v[6] = {1000,500,100,50,10,1};

        map<int,string> m;
        m.insert(make_pair<int,string>(1,"I"));
        m.insert(make_pair<int,string>(2,"II"));
        m.insert(make_pair<int,string>(3,"III"));
        m.insert(make_pair<int,string>(4,"IV"));
        m.insert(make_pair<int,string>(5,"V"));
        m.insert(make_pair<int,string>(6,"VI"));
        m.insert(make_pair<int,string>(7,"VII"));
        m.insert(make_pair<int,string>(8,"VIII"));
        m.insert(make_pair<int,string>(9,"IX"));
        m.insert(make_pair<int,string>(10,"X"));
        m.insert(make_pair<int,string>(20,"XX"));
        m.insert(make_pair<int,string>(30,"XXX"));
        m.insert(make_pair<int,string>(40,"XL"));
        m.insert(make_pair<int,string>(50,"L"));
        m.insert(make_pair<int,string>(60,"LX"));
        m.insert(make_pair<int,string>(70,"LXX"));
        m.insert(make_pair<int,string>(80,"LXXX"));
        m.insert(make_pair<int,string>(90,"XC"));
        m.insert(make_pair<int,string>(100,"C"));
        m.insert(make_pair<int,string>(200,"CC"));
        m.insert(make_pair<int,string>(300,"CCC"));
        m.insert(make_pair<int,string>(400,"CCC"));
        m.insert(make_pair<int,string>(500,"CD"));
        m.insert(make_pair<int,string>(600,"D"));
        m.insert(make_pair<int,string>(700,"DC"));
        m.insert(make_pair<int,string>(800,"DCC"));
        m.insert(make_pair<int,string>(900,"DCCC"));
        m.insert(make_pair<int,string>(1000,"M"));
        m.insert(make_pair<int,string>(2000,"MM"));
        m.insert(make_pair<int,string>(3000,"MMM"));

        int r,i=0;
        while (a)
        {
            if (a>=v[i])
            {
                r=(a/v[i])*v[i];
                a-=r;

                s+=m[r];
            }
            i++;
        }
    }
    else
    {
        while (a)
        {
            s+=(getChar(a%b));
            a/=b;
        }
        string y = s;
        for (int i=y.size()-1,j=0 ; i>=0 ; i--,j++) s[j]=y[i];
    }
    if (s=="") s="0";

    return s;
}

int main()
{
    int a,b;
    string s;

    cin>>a>>s;
    if (s=="R") b=-1;
    else b = stringToInt(s);

    cin>>s;
    int i=0;
    while (s[i]=='0' && i<s.size()) i++;
    if (i==s.size()) s="0";
    else s=s.substr(i,s.size()-i);

    lli qa = toBaseTen(s,a);
    cout<<toBaseN(qa,b);

    return 0;
}


#include <iostream>
#include <map>
#define lli long long int

using namespace std;

int stringToInt(string s)
{
    int k=1,v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*(s[i]-'0'),k*=10;
    return v;
}

lli getVal(char c)
{
    if (c>='0' && c<='9') return c-'0';
    else return c-'A'+10;
}

lli toBaseTen(string s,int a)
{
    lli k=1, v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*getVal(s[i]),k*=a;
    return v;
}

char getChar(int k)
{
    if (k<10) return char(k+'0');
    else return char(k+'A'-10);
}

string toBaseN(lli a,int b)
{
    string s="";

    if (b==-1)
    {
        map<int,string> m;
        m.insert(make_pair<int,string>(1,"I"));
        m.insert(make_pair<int,string>(2,"II"));
        m.insert(make_pair<int,string>(3,"III"));
        m.insert(make_pair<int,string>(4,"IV"));
        m.insert(make_pair<int,string>(5,"V"));
        m.insert(make_pair<int,string>(6,"VI"));
        m.insert(make_pair<int,string>(7,"VII"));
        m.insert(make_pair<int,string>(8,"VIII"));
        m.insert(make_pair<int,string>(9,"IX"));
        m.insert(make_pair<int,string>(10,"X"));
        m.insert(make_pair<int,string>(20,"XX"));
        m.insert(make_pair<int,string>(30,"XXX"));
        m.insert(make_pair<int,string>(40,"XL"));
        m.insert(make_pair<int,string>(50,"L"));
        m.insert(make_pair<int,string>(60,"LX"));
        m.insert(make_pair<int,string>(70,"LXX"));
        m.insert(make_pair<int,string>(80,"LXXX"));
        m.insert(make_pair<int,string>(90,"XC"));
        m.insert(make_pair<int,string>(100,"C"));
        m.insert(make_pair<int,string>(200,"CC"));
        m.insert(make_pair<int,string>(300,"CCC"));
        m.insert(make_pair<int,string>(400,"CD"));
        m.insert(make_pair<int,string>(500,"D"));
        m.insert(make_pair<int,string>(600,"DC"));
        m.insert(make_pair<int,string>(700,"DCC"));
        m.insert(make_pair<int,string>(800,"DCCC"));
        m.insert(make_pair<int,string>(900,"CM"));
        m.insert(make_pair<int,string>(1000,"M"));
        m.insert(make_pair<int,string>(2000,"MM"));
        m.insert(make_pair<int,string>(3000,"MMM"));

        int r,v;
        map<int,string>::iterator i=m.end(); i--;
        while (a)
        {
            v = i->first;
            if (a>=v)
            {
                r=(a/v)*v;
                a-=r;

                s+=(i->second);
            }
            i--;
        }
    }
    else
    {
        while (a)
        {
            s+=(getChar(a%b));
            a/=b;
        }
        string y = s;
        for (int i=y.size()-1,j=0 ; i>=0 ; i--,j++) s[j]=y[i];
    }
    if (s=="") s="0";

    return s;
}

int main()
{
    int a,b;
    string s;

    cin>>a>>s;
    if (s=="R") b=-1;
    else b = stringToInt(s);

    cin>>s;
    int i=0;
    while (s[i]=='0' && i<s.size()) i++;
    if (i==s.size()) s="0";
    else s=s.substr(i,s.size()-i);

    lli qa = toBaseTen(s,a);
    cout<<toBaseN(qa,b);

    return 0;
}


#include <iostream>
#include <map>
#define lli long long int

using namespace std;

int stringToInt(string s)
{
    int k=1,v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*(s[i]-'0'),k*=10;
    return v;
}

lli getVal(char c)
{
    if (c>='0' && c<='9') return c-'0';
    else return c-'A'+10;
}

lli toBaseTen(string s,int a)
{
    lli k=1, v=0;
    for (int i=s.length()-1 ; i>=0 ; i--)
        v+=k*getVal(s[i]),k*=a;
    return v;
}

char getChar(int k)
{
    if (k<10) return char(k+'0');
    else return char(k+'A'-10);
}

string toBaseN(lli a,int b)
{
    string s="";

    if (b==-1)
    {
        map<int,string> m;
        m.insert(make_pair<int,string>(1,"I"));
        m.insert(make_pair<int,string>(2,"II"));
        m.insert(make_pair<int,string>(3,"III"));
        m.insert(make_pair<int,string>(4,"IV"));
        m.insert(make_pair<int,string>(5,"V"));
        m.insert(make_pair<int,string>(6,"VI"));
        m.insert(make_pair<int,string>(7,"VII"));
        m.insert(make_pair<int,string>(8,"VIII"));
        m.insert(make_pair<int,string>(9,"IX"));
        m.insert(make_pair<int,string>(10,"X"));
        m.insert(make_pair<int,string>(20,"XX"));
        m.insert(make_pair<int,string>(30,"XXX"));
        m.insert(make_pair<int,string>(40,"XL"));
        m.insert(make_pair<int,string>(50,"L"));
        m.insert(make_pair<int,string>(60,"LX"));
        m.insert(make_pair<int,string>(70,"LXX"));
        m.insert(make_pair<int,string>(80,"LXXX"));
        m.insert(make_pair<int,string>(90,"XC"));
        m.insert(make_pair<int,string>(100,"C"));
        m.insert(make_pair<int,string>(200,"CC"));
        m.insert(make_pair<int,string>(300,"CCC"));
        m.insert(make_pair<int,string>(400,"CCC"));
        m.insert(make_pair<int,string>(500,"CD"));
        m.insert(make_pair<int,string>(600,"D"));
        m.insert(make_pair<int,string>(700,"DC"));
        m.insert(make_pair<int,string>(800,"DCC"));
        m.insert(make_pair<int,string>(900,"DCCC"));
        m.insert(make_pair<int,string>(1000,"M"));
        m.insert(make_pair<int,string>(2000,"MM"));
        m.insert(make_pair<int,string>(3000,"MMM"));

        int r,v;
        map<int,string>::iterator i=m.end(); i--;
        while (a)
        {
            v = i->first;
            if (a>=v)
            {
                r=(a/v)*v;
                a-=r;

                s+=(i->second);
            }
            i--;
        }
    }
    else
    {
        while (a)
        {
            s+=(getChar(a%b));
            a/=b;
        }
        string y = s;
        for (int i=y.size()-1,j=0 ; i>=0 ; i--,j++) s[j]=y[i];
    }
    if (s=="") s="0";

    return s;
}

int main()
{
    int a,b;
    string s;

    cin>>a>>s;
    if (s=="R") b=-1;
    else b = stringToInt(s);

    cin>>s;
    int i=0;
    while (s[i]=='0' && i<s.size()) i++;
    if (i==s.size()) s="0";
    else s=s.substr(i,s.size()-i);

    lli qa = toBaseTen(s,a);
    cout<<toBaseN(qa,b);

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <ctype.h>
#include <algorithm>
#define pb push_back
#define lli long long int
#define MAX_LEN 1005
using namespace std;

vector <pair <int, string> > v;

int convert(char c) {
    if (isalpha(c)) {
        return 10 + c - 'A';
    } else {
        return c - '0';
    }
}

int main(void) {
    int a, b;
    lli num = 0;
    char aux[5];
    char s[MAX_LEN];
    string res = "";

    v.pb(make_pair(1000, "M"));
    v.pb(make_pair(900, "CM"));
    v.pb(make_pair(500, "D"));
    v.pb(make_pair(400, "CD"));
    v.pb(make_pair(100, "C"));
    v.pb(make_pair(90, "XC"));
    v.pb(make_pair(50, "L"));
    v.pb(make_pair(40, "XL"));
    v.pb(make_pair(10, "X"));
    v.pb(make_pair(9, "IX"));
    v.pb(make_pair(5, "V"));
    v.pb(make_pair(4, "IV"));
    v.pb(make_pair(1, "I"));

    scanf(" %d %s", &a, aux);
    scanf(" %s", s);
    for (int i = 0; s[i] != '\0'; i++) {
        num = a * num + convert(s[i]);
    }
    if (aux[0] == 'R') {
        while(num > 0) {
            for (int i = 0; i < (int)v.size(); i++) {
                if (num >= v[i].first) {
                    num -= v[i].first;
                    res += v[i].second;
                }
            }
        }
    } else {
        b = atoi(aux);
        while(num > 0) {
            int d = num % b;
            num /= b;
            char c;
            if (d <= 9) {
                c = d + '0';
            } else {
                c = d - 10 + 'A';
            }
            res += c;
        }
        reverse(res.begin(), res.end());
    }
    cout << res << endl;
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <ctype.h>
#include <algorithm>
#define pb push_back
#define lli long long int
#define MAX_LEN 1005
using namespace std;

vector <pair <int, string> > v;

int convert(char c) {
    if (isalpha(c)) {
        return 10 + c - 'A';
    } else {
        return c - '0';
    }
}

int main(void) {
    int a, b;
    lli num = 0;
    char aux[5];
    char s[MAX_LEN];
    string res = "";

    v.pb(make_pair(1000, "M"));
    v.pb(make_pair(900, "CM"));
    v.pb(make_pair(500, "D"));
    v.pb(make_pair(400, "CD"));
    v.pb(make_pair(100, "C"));
    v.pb(make_pair(90, "XC"));
    v.pb(make_pair(50, "L"));
    v.pb(make_pair(40, "XL"));
    v.pb(make_pair(10, "X"));
    v.pb(make_pair(9, "IX"));
    v.pb(make_pair(5, "V"));
    v.pb(make_pair(4, "IV"));
    v.pb(make_pair(1, "I"));

    scanf(" %d %s", &a, aux);
    scanf(" %s", s);
    for (int i = 0; s[i] != '\0'; i++) {
        num = a * num + convert(s[i]);
    }
    if (aux[0] == 'R') {
        while(num > 0) {
            for (int i = 0; i < (int)v.size(); i++) {
                if (num >= v[i].first) {
                    num -= v[i].first;
                    res += v[i].second;
                    break;
                }
            }
        }
    } else {
        b = atoi(aux);
        while(num > 0) {
            int d = num % b;
            num /= b;
            char c;
            if (d <= 9) {
                c = d + '0';
            } else {
                c = d - 10 + 'A';
            }
            res += c;
        }
        reverse(res.begin(), res.end());
    }
    cout << res << endl;
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <ctype.h>
#include <algorithm>
#define pb push_back
#define lli long long int
#define MAX_LEN 1005
using namespace std;

vector <pair <int, string> > v;

int convert(char c) {
    if (isalpha(c)) {
        return 10 + c - 'A';
    } else {
        return c - '0';
    }
}

int main(void) {
    int a, b;
    lli num = 0;
    char aux[5];
    char s[MAX_LEN];
    string res = "";

    v.pb(make_pair(1000, "M"));
    v.pb(make_pair(900, "CM"));
    v.pb(make_pair(500, "D"));
    v.pb(make_pair(400, "CD"));
    v.pb(make_pair(100, "C"));
    v.pb(make_pair(90, "XC"));
    v.pb(make_pair(50, "L"));
    v.pb(make_pair(40, "XL"));
    v.pb(make_pair(10, "X"));
    v.pb(make_pair(9, "IX"));
    v.pb(make_pair(5, "V"));
    v.pb(make_pair(4, "IV"));
    v.pb(make_pair(1, "I"));

    scanf(" %d %s", &a, aux);
    scanf(" %s", s);
    for (int i = 0; s[i] != '\0'; i++) {
        num = a * num + convert(s[i]);
    }
    if (aux[0] == 'R') {
        while(num > 0) {
            for (int i = 0; i < (int)v.size(); i++) {
                if (num >= v[i].first) {
                    num -= v[i].first;
                    res += v[i].second;
                    break;
                }
            }
        }
    } else {
        b = atoi(aux);
        while(num > 0) {
            int d = num % b;
            num /= b;
            char c;
            if (d <= 9) {
                c = d + '0';
            } else {
                c = d - 10 + 'A';
            }
            res += c;
        }
        if ((int)res.size() == 0) {
            res += '0';
        }
        reverse(res.begin(), res.end());
    }
    cout << res << endl;
    return 0;
}


//============================================================================
// Name        : cf@61-C.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define M 100005
using namespace std;
typedef long long LL;

int a, b;
char c[M];
char tmp[M];

int getNum(char c) {
	if ('0' <= c && c <= '9') {
		return c - '0';
	}
	else {
		return c - 'A' + 10;
	}
}

LL convince(char str[], int base) {
	int length = strlen(str);
	LL ret = 0;
	LL tmp = 1;
	for (int i = length - 1; i >= 0; i --) {
		ret += getNum(str[i]) * tmp;
		tmp *= base;
	}
	return ret;
}

void convince(LL num, int base) {
	int pos = 0;
	while (num) {
		int tt = num % base;
		if (tt > 9) {
			tt -= 10;
			tmp[pos] = tt + 'A';
		}
		else {
			tmp[pos] = tt + '0';
		}
		pos ++;
		num /= base;
	}
	for (int i = 0; i < pos; i ++) {
		c[pos - 1 - i] = tmp[i];
	}
	c[pos] = 0;
}

void printR(LL num) {
	while (num >= 1000) {
		printf ("M");
		num -= 1000;
	}

	if (num >= 100) {
		int t = num / 100;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("C");
			}
			break;
		case 4:
			printf ("CD");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("D");
			for (int i = 5; i < t; i ++) {
				printf ("C");
			}
			break;
		case 9:
			printf ("CM");
			break;
		}
		num -= t * 100;
	}

	if (num >= 10) {
		int t = num / 10;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("X");
			}
			break;
		case 4:
			printf ("XL");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("L");
			for (int i = 5; i < t; i ++) {
				printf ("X");
			}
			break;
		case 9:
			printf ("XC");
			break;
		}
		num -= t * 10;
	}

	if (num >= 1) {
		int t = num;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("I");
			}
			break;
		case 4:
			printf ("IV");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("V");
			for (int i = 5; i < t; i ++) {
				printf ("I");
			}
			break;
		case 9:
			printf ("IX");
			break;
		}
	}
	puts("");
}

int main() {
	scanf ("%d", &a);
	scanf ("%s", tmp);
	if (tmp[0] == 'R') {
		b = 1;
	}
	else {
		sscanf (tmp, "%d", &b);
	}
	scanf ("%s", c);

	LL base10 = convince(c, a);
//	cerr << base10 << endl;
	if (b == 1) {
		printR(base10);
	}
	else {
		if (base10) {
			convince(base10, b);
			printf ("%s\n", c);
		}
		else {
			puts("0");
		}
	}
	return 0;
}


//============================================================================
// Name        : cf@61-C.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define M 100005
using namespace std;
typedef long long LL;

int a, b;
char c[M];
char tmp[M];

int getNum(char c) {
	if ('0' <= c && c <= '9') {
		return c - '0';
	}
	else {
		return c - 'A' + 10;
	}
}

LL convince(char str[], int base) {
	int length = strlen(str);
	LL ret = 0;
	LL tmp = 1;
	for (int i = length - 1; i >= 0; i --) {
		ret += getNum(str[i]) * tmp;
		tmp *= base;
	}
	return ret;
}

void convince(LL num, int base) {
	int pos = 0;
	while (num) {
		int tt = num % base;
		if (tt > 9) {
			tt -= 10;
			tmp[pos] = tt + 'A';
		}
		else {
			tmp[pos] = tt + '0';
		}
		pos ++;
		num /= base;
	}
	for (int i = 0; i < pos; i ++) {
		c[pos - 1 - i] = tmp[i];
	}
	c[pos] = 0;
}

void printR(LL num) {
	while (num >= 1000) {
		printf ("M");
		num -= 1000;
	}

	if (num >= 100) {
		int t = num / 100;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("C");
			}
			break;
		case 4:
			printf ("CD");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("D");
			for (int i = 5; i < t; i ++) {
				printf ("C");
			}
			break;
		case 9:
			printf ("CM");
			break;
		}
		num -= t * 100;
	}

	if (num >= 10) {
		int t = num / 10;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("X");
			}
			break;
		case 4:
			printf ("XL");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("L");
			for (int i = 5; i < t; i ++) {
				printf ("X");
			}
			break;
		case 9:
			printf ("XC");
			break;
		}
		num -= t * 10;
	}

	if (num >= 1) {
		int t = num;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("I");
			}
			break;
		case 4:
			printf ("IV");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("V");
			for (int i = 5; i < t; i ++) {
				printf ("I");
			}
			break;
		case 9:
			printf ("IX");
			break;
		}
	}
	puts("");
}

int main() {
	scanf ("%d", &a);
	scanf ("%s", tmp);
	if (tmp[0] == 'R') {
		b = 1;
	}
	else {
		sscanf (tmp, "%d", &b);
	}
	scanf ("%s", c);

	LL base10 = convince(c, a);
//	cerr << base10 << endl;
	if (b == 1) {
		printR(base10);
	}
	else {
		convince(base10, b);
		printf ("%s\n", c);
	}
	return 0;
}


//============================================================================
// Name        : cf@61-C.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define M 1005
using namespace std;
typedef long long LL;

int a, b;
char c[M];
char tmp[M];

int getNum(char c) {
	if ('0' <= c && c <= '9') {
		return c - '0';
	}
	else {
		return c - 'A' + 10;
	}
}

LL convince(char str[], int base) {
	int length = strlen(str);
	LL ret = 0;
	LL tmp = 1;
	for (int i = length - 1; i >= 0; i --) {
		ret += getNum(str[i]) * tmp;
		tmp *= base;
	}
	return ret;
}

void convince(LL num, int base) {
	int pos = 0;
	while (num) {
		int tt = num % base;
		if (tt > 9) {
			tt -= 10;
			tmp[pos] = tt + 'A';
		}
		else {
			tmp[pos] = tt + '0';
		}
		pos ++;
		num /= base;
	}
	for (int i = 0; i < pos; i ++) {
		c[pos - 1 - i] = tmp[i];
	}
}

void printR(LL num) {
	while (num >= 1000) {
		printf ("M");
		num -= 1000;
	}

	if (num >= 100) {
		int t = num / 100;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("C");
			}
			break;
		case 4:
			printf ("CD");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("D");
			for (int i = 5; i < t; i ++) {
				printf ("C");
			}
			break;
		case 9:
			printf ("CM");
			break;
		}
		num -= t * 100;
	}

	if (num >= 10) {
		int t = num / 10;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("X");
			}
			break;
		case 4:
			printf ("XL");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("L");
			for (int i = 5; i < t; i ++) {
				printf ("X");
			}
			break;
		case 9:
			printf ("XC");
			break;
		}
		num -= t * 10;
	}

	if (num >= 1) {
		int t = num;
		switch(t) {
		case 1:
		case 2:
		case 3:
			for (int i = 0; i < t; i ++) {
				printf ("I");
			}
			break;
		case 4:
			printf ("IV");
			break;
		case 5:
		case 6:
		case 7:
		case 8:
			printf ("V");
			for (int i = 5; i < t; i ++) {
				printf ("I");
			}
			break;
		case 9:
			printf ("IX");
			break;
		}
	}
}

int main() {
	scanf ("%d", &a);
	scanf ("%s", tmp);
	if (tmp[0] == 'R') {
		b = 'R';
	}
	else {
		sscanf (tmp, "%d", &b);
	}
	scanf ("%s", c);

	LL base10 = convince(c, a);
//	cerr << base10 << endl;
	if (b == 'R') {
		printR(base10);
	}
	else {
		convince(base10, b);
		printf ("%s\n", c);
	}
	return 0;
}


#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 = end ; i >= 0 ; -- 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 <cstdlib>
#include <cstring>
#include <stdio.h>

using namespace std;

char data[ 1005 ];
int  answ[ 1001 ];
int  save[ 31 ];
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 < 30 ; ++ i )
			save[ i ] = 0;
		int le = strlen( data );
		for ( int i = 0 ; i < le ; ++ i ) {
			for ( int j = 0 ; j < 30 ; ++ 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 < 29 ; ++ j )
				if ( save[ j ] > 9 ) {
					save[ j+1 ] += save[ j ]/10;
					save[  j  ] %= 10;
				}
		}
		int end = 30;
		while ( !save[ end ] ) -- end;
		if ( bb[ 0 ] == 'R' ) {
			int value = 0;
			for ( int i = end ; i >= 0 ; -- 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 < 100 ; ++ 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 <cstdlib>
#include <cstring>
#include <stdio.h>

using namespace std;

char data[ 1005 ];
int  answ[ 1001 ];
int  save[ 31 ];
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 < 30 ; ++ i )
			save[ i ] = 0;
		int le = strlen( data );
		for ( int i = 0 ; i < le ; ++ i ) {
			for ( int j = 0 ; j < 30 ; ++ 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 < 29 ; ++ j )
				if ( save[ j ] > 9 ) {
					save[ j+1 ] += save[ j ]/10;
					save[  j  ] %= 10;
				}
		}
		int end = 30;
		while ( !save[ end ] ) -- end;
		if ( bb[ 0 ] == 'R' ) {
			int value = 0;
			for ( int i = end ; i >= 0 ; -- 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 < 100 ; ++ 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 > 0 ) -- 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 <cstdlib>
#include <cstring>

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[ i ] *= 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 <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[ i ] *= 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 <cstdlib>
#include <cstring>
#include <stdio.h>

using namespace std;

char data[ 1005 ];
int  answ[ 101 ];
int  save[ 31 ];
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 < 30 ; ++ i )
			save[ i ] = 0;
		int le = strlen( data );
		for ( int i = 0 ; i < le ; ++ i ) {
			for ( int j = 0 ; j < 30 ; ++ 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 < 29 ; ++ j )
				if ( save[ j ] > 9 ) {
					save[ j+1 ] += save[ j ]/10;
					save[  j  ] %= 10;
				}
		}
		int end = 30;
		while ( !save[ end ] ) -- end;
		if ( bb[ 0 ] == 'R' ) {
			int value = 0;
			for ( int i = end ; i >= 0 ; -- 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;
}