#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <string>

using namespace std;

char s[100];
long long N;
int L;

string wal(int L, long long N){
	if (L==1) return string(1,(char)('0'+N));
	long long H = 1LL << (L-1);
	switch ((L-2)%8){
		case 0: return (N<H)? (wal(L-1,N)+"0") : (wal(L-1,N-H)+"1");
		case 1: return (N<H)? ("0"+wal(L-1,N)) : ("1"+wal(L-1,N-H));
		case 2: return (N<H)? (wal(L-1,N)+"1") : (wal(L-1,N-H)+"0");
		case 3: return (N<H)? ("1"+wal(L-1,N)) : ("0"+wal(L-1,N-H));
		case 4: return (N<H)? (wal(L-1,N)+"0") : (wal(L-1,H-(N-H)-1)+"1");
		case 5: return (N<H)? ("0"+wal(L-1,N)) : ("1"+wal(L-1,H-(N-H)-1));
		case 6: return (N<H)? (wal(L-1,N)+"1") : (wal(L-1,H-(N-H)-1)+"0");
		case 7: return (N<H)? ("1"+wal(L-1,N)) : ("0"+wal(L-1,H-(N-H)-1));
	}
	return "error";
}

long long seq(char *s, int i, int j){
	if (i==j) return s[i]-'0';
	long long N = 1LL << (j-i), ret = 0, add = 0;
	switch ((j-i-1)%8){
		case 0 : return (s[j]=='0')? seq(s,i,j-1) : (N + seq(s,i,j-1));
		case 1 : return (s[i]=='0')? seq(s,i+1,j) : (N + seq(s,i+1,j));
		case 2 : return (s[j]=='1')? seq(s,i,j-1) : (N + seq(s,i,j-1));
		case 3 : return (s[i]=='1')? seq(s,i+1,j) : (N + seq(s,i+1,j));
		case 4 : ret = seq(s,i,j-1); if (s[j]=='1') ret = N-ret-1 + N; break;
		case 5 : ret = seq(s,i+1,j); if (s[i]=='1') ret = N-ret-1 + N; break;
		case 6 : ret = seq(s,i,j-1); if (s[j]=='0') ret = N-ret-1 + N; break;
		case 7 : ret = seq(s,i+1,j); if (s[i]=='0') ret = N-ret-1 + N; break;
	}
	return ret;
}

int main(){
	while (scanf("%s",s)!=EOF){
		if (s[0]=='W'){
//			scanf("%d %lld",&L,&N);
			cin>>L>>N;
			printf("%s\n",wal(L, N-1).c_str());
		} else {
			scanf("%s",s);
			cout<<seq(s,0,strlen(s)-1)+1<<endl;
		}
	}
}
