import java.util.*;
import java.io.*;

public class Walaweh {
	String wal(int L, long N){
		if (L==1) return ""+N;
		long H = 1L << (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 seq(String S, int i, int j){
		if (i==j) return S.charAt(i)-'0';
		long N = 1L << (j-i), ret = 0, add = 0;
		switch ((j-i-1)%8){
			case 0 : return (S.charAt(j)=='0')? seq(S,i,j-1) : (N + seq(S,i,j-1));
			case 1 : return (S.charAt(i)=='0')? seq(S,i+1,j) : (N + seq(S,i+1,j));
			case 2 : return (S.charAt(j)=='1')? seq(S,i,j-1) : (N + seq(S,i,j-1));
			case 3 : return (S.charAt(i)=='1')? seq(S,i+1,j) : (N + seq(S,i+1,j));
			case 4 : ret = seq(S,i,j-1); if (S.charAt(j)=='1') ret = N-ret-1 + N; break;
			case 5 : ret = seq(S,i+1,j); if (S.charAt(i)=='1') ret = N-ret-1 + N; break;
			case 6 : ret = seq(S,i,j-1); if (S.charAt(j)=='0') ret = N-ret-1 + N; break;
			case 7 : ret = seq(S,i+1,j); if (S.charAt(i)=='0') ret = N-ret-1 + N; break;
		}
		return ret;
	}

	void solve() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		for (String line; (line = br.readLine())!=null; ){
			String[] s = line.split(" ");
			if (s[0].charAt(0)=='W'){
				int L = Integer.parseInt(s[1]);
				long N = Long.parseLong(s[2]);
				bw.write(wal(L, N-1) + "\n");
			} else {
				bw.write((seq(s[1],0,s[1].length()-1)+1) + "\n");
			}
		}
		br.close();
		bw.close();
	}

	public static void main(String[] args) throws Exception {
		new Walaweh().solve();
	}
}
