#include<stdio.h>
#include<string.h>
#include<iostream>
#include<sstream>
#include<vector>
using namespace std;
int from(int r,int n)
{
	int res=0;
	int a=1;
	do
	{
		res+=a*(n%10);
		a*=r;
	}while (n/=10);
	return res;
}
vector<int> to(int r,int n)
{
	vector<int> help;
	do
	{
		help.push_back(n%r);
	}while (n/=r);
	vector<int> res;res.push_back(0);
	for (int i=help.size()-1;i>=0;i--) res.push_back(help[i]);
	return res;
}
vector<int> make(vector<int> temp,int r)
{
	bool sp=0;
	for (int i=temp.size()-1;i>=1;i--)
	{
		sp=1-sp;
		if (temp[i]>=0) continue;
		temp[i]-=r;
		if (!sp) temp[i-1]++; else temp[i-1]--;
		
	}
	return temp;
}

int main()
{
	string z;vector<int> q,a;
	cin>>z;
	while (z!="end")
	{
		string help="";
		bool qqq=0;int n,r;
		if (z[0]=='t') qqq=1; else qqq=0;
		if (qqq) help=z.substr(3); else help=z.substr(5);
		istringstream iss(help);iss>>r;r=-r;
		scanf("%d",&n);
		if (qqq==0){printf("%d\n",from(r,n));}else
		{
			q=to(r,n);
			a=make(q,r);bool w=1;
			for (int i=0;i<a.size();i++) {  if (w && a[i]==0) continue; else w=0; printf("%d",a[i]);}printf("\n");
		}
		cin>>z;
	}
	
	return 0;
}




