#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 unsigned long long int ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
int dp[11][1024]; //if I have i digits left and mask denotes the digit used, how many assignments?
int hp[17][65536];
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));
n = sz(v);
ll ans = 1;
int mask = 0;
for(int i=1;i<n;i++)ans += P[10][i] - P[9][i-1];
for(int i=0;i<n;i++){
if(!i){
if(v[i]>1){
ll c = dp[v[i]-1][mask];
ans += (c-1) * P[10-i-1][n-i-1];
}
}else if(v[i]){
ll c = dp[v[i]-1][mask];
ans += c * P[10-i-1][n-i-1];
}
if(mask & (1<<v[i]))break;
mask |= (1<<v[i]);
if(i==n-1)ans++;
}
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));
n = sz(v);
ll ans = 1;
int mask = 0;
for(int i=1;i<n;i++)ans += P[16][i] - P[15][i-1];
for(int i=0;i<n;i++){
if(!i){
if(v[i]>1){
ll c = hp[v[i]-1][mask];
ans += (c-1) * P[16-i-1][n-i-1];
}
}else if(v[i]){
ll c = hp[v[i]-1][mask];
ans += c * P[16-i-1][n-i-1];
}
if(mask & (1<<v[i]))break;
mask |= (1<<v[i]);
if(i==n-1)ans++;
}
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;
}
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;
}
}
//dp[i][len][mask] = if mask is used, my number has len digits and the next digit is <= i how many ways to make the number?
for(int mask=0;mask<(1<<10);mask++){
int run = 0;
for(int j=0;j<10;j++){
if(!(mask&(1<<j)))run++;
dp[j][mask] = run;
}
}
for(int mask=0;mask<(1<<16);mask++){
int run = 0;
for(int j=0;j<16;j++){
if(!(mask&(1<<j)))run++;
hp[j][mask] = run;
}
}
//for(int i=0;i<=100;i++)cout<<i<<" "<<find(i)<<'\n';
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';
}
}
}
}