#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define all(x) begin(x),end(x)
#define sz(x) (int)(x).size()
typedef long long int ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
ll P[20][20];
ll find(ll n){
if(n==0)return 1;
vector<int>v;
while(n){
v.push_back(n%10);
n/=10;
}
reverse(all(v));
ll ans = 1;
vector<int>seen(10,0);
int choice = 10;
for(int i=0;i<sz(v);i++){
for(int j=1;j<v[i];j++){
if(!seen[j])ans += P[choice-1][sz(v)-i-1];
}
if(!seen[0]){
if(i>0 && v[i]>0)ans += P[choice-1][sz(v)-i-1];
}
seen[v[i]]++;
choice--;
if(seen[v[i]]>1)break;
if(i==sz(v)-1)ans++;
}
for(int i=1;i<sz(v);i++){
ans += P[10][i] - P[9][i-1];
}
return ans;
}
ll find2(ll n){
if(n==0)return 1;
vector<int>v;
while(n){
v.push_back(n%16);
n/=16;
}
reverse(all(v));
ll ans = 1;
vector<int>seen(16,0);
int choice = 16;
for(int i=0;i<sz(v);i++){
for(int j=1;j<v[i];j++){
if(!seen[j])ans += P[choice-1][sz(v)-i-1];
}
if(!seen[0]){
if(i>0 && v[i]>0)ans += P[choice-1][sz(v)-i-1];
}
seen[v[i]]++;
choice--;
if(seen[v[i]]>1)break;
if(i==sz(v)-1)ans++;
}
for(int i=1;i<sz(v);i++){
ans += P[16][i] - P[15][i-1];
}
return ans;
}
ll condec(string s0){
ll a = 0;
for(ll i=sz(s0)-1,j=1;;i--){
if(s0[i]>='a' && s0[i]<='f')a += j * (s0[i]-'a'+10);
else a += j * (s0[i]-'0');
j*=16;
if(i==0)break;
}
return a;
}
string conhex(ll n){
if(n==0)return "0";
string s;
while(n){
int r = n%16;
if(r<=9)s.push_back(r + '0');
else s.push_back(r-10+'a');
n/=16;
}
reverse(all(s));
return s;
}
//10P1 - 9P0 = 9
//+2
//10P1 + 10P2
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
P[0][0] = 1;
for(int i=0;i<=16;i++){
P[i][0] = 1;
ll f = 1;
for(int j=i;j>=1;j--){
f *= j;
P[i][i-j+1] = f;
}
}
int t;
cin>>t;
while(t--){
char c;
int mode;
cin>>c>>mode;
if(c == 'd'){
if(!mode){
ll a,b;
cin>>a>>b;
if(a==0)cout<<find(b)<<'\n';
else cout<<find(b)-find(a-1)<<'\n';
}else{
ll n;
cin>>n;
ll l = 0,r = 1e11;
while(r>=l){
ll mid = (l+r)/2;
//cout<<mid<<" "<<find(mid)<<'\n';
if(find(mid) >= n)r = mid-1;
else l = mid+1;
}
if(find(l) != n)cout<<"-"<<'\n';
else cout<<l<<'\n';
}
}else{
if(!mode){
string s0,s1;
cin>>s0>>s1;
ll a = condec(s0),b = condec(s1);
ll ans = 0;
if(a==0)ans = find2(b);
else ans = find2(b) - find2(a-1);
cout<<conhex(ans)<<'\n';
}else{
string s0;
cin>>s0;
ll n = condec(s0);
//cout<<s0<<" "<<n<<'\n';
ll l = 0,r = 1e17;
while(r>=l){
ll mid = (l+r)/2;
//cout<<mid<<" "<<find(mid)<<'\n';
if(find2(mid) >= n)r = mid-1;
else l = mid+1;
}
if(find2(l) != n)cout<<"-"<<'\n';
else cout<<conhex(l)<<'\n';
}
}
}
}
/*
6
h 1 147a
d 0 25 71
d 1 3587
d 0 26922 51887
d 1 711
d 0 3 5
h 0 7bf2defab442a0b1 f299a4cf1d4d9bed
d 0 6961 91018
d 1 4
d 1 876
h 1 12cc5d3370f99120
d 1 161315
h 0 25f 6959
*/