#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
int n,m,num[25],len;//memest会死哎,但是flg=1的状态唯一应该不用记录都是对的
ull f[20][1<<17][2],K;
ull dfs(int cnt,int S,int flg){
if(cnt<0)return 1;
if(!flg&&f[cnt][S][K==10]!=-1)return f[cnt][S][K==10];
int up=flg?num[cnt]:K-1;ull res=0;
for(int i=0;i<=up;++i)if(((S>>i)&1)==0)res+=dfs(cnt-1,(S==0&&i==0)?0:(S|(1<<i)),flg&(i==up));
if(!flg)f[cnt][S][K==10]=res;
return res;
}
ull solve(ull x){
ull tmp=x,len=0;
while(tmp)num[len++]=tmp%K,tmp/=K;
return dfs(len-1,0,1);
}
ull read(){
string s;cin>>s;
ull x=0;
for(int i=0;i<s.size();++i){
x=x*K;
if(s[i]>='0'&&s[i]<='9')x+=s[i]-'0';
else x+=s[i]-'a'+10;
}return x;
}
int out[55];
void Print(ull x){
// string a="";
if(!x){
cout<<"0\n";return ;
}
if(K==10){
cout<<x<<"\n"; return ;
}
int c=0;
while(x){
out[++c]=x%K;x/=K;
}
reverse(out+1,out+c+1);
for(int i=1;i<=c;++i){
if(out[i]<10)cout<<out[i];
else {
char x='a'+out[i]-10;
cout<<x;
}
}
cout<<"\n";
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
memset(f,-1,sizeof f);
ull T;cin>>T;
while(T--){
char op;cin>>op;
if(op=='d')K=10;
else K=16;
ull opt;cin>>opt;
if(opt){
ull k=read();
ull l=0,r=0;--r;
// cout<<r<<'\n';
if(solve(r)<k){
cout<<"-\n";continue;
}
while(l<r){
ull mid=l+r>>1;
// cout<<l<<" "<<r<<"\n";
if(solve(mid)<k)l=mid+1;
else r=mid;
}
Print(l);
}
else {
ull l=read(),r=read();
// cout<<l<<" "<<r<<"\n";
ull res=solve(r)-(l?solve(l-1):0);
Print(res);
}
}
}
/*
6
d 0 10 20
h 0 10 1f
d 1 10
h 1 f
d 1 1000000000
h 1 ffffffffffffffff
*/\