# Michael Chen
# Canyon Crest Academy
# Senior Division
# 11th grade
# Standard Input
l = input().split(", ")
#set up board : 2d list
board = [[0 for i in range(8)] for j in range(8)] # list comprehension
for i in range(len(l)):
dec = int(l[i], 16)
b8 = str(oct(dec))[2:] # base 8 representation
for j in range(len(b8)):
board[i][j] = int(b8[j])
for _ in range(5): # five test cases
l = input().split(", ")
# x and y coordinates of our current position
x = int(l[0])-1 # 0 based index
y = int(l[1])-1
dirs = {'R':180, 'L':0, 'B':90, 'A':270} # Used to get the angle of the direction we came from
angle = (dirs[l[2]] - board[x][y]*45)+180 # Angle if we consider facing right to be 0 degrees
angle %= 360
k = int(l[3])
tmpx = [0,1,1,1,0,-1,-1,-1]
tmpy = [1,1,0,-1,-1,-1,0,1]
chx = {} # How our x coord changes with a given angle
chy = {} # How our y coord changes with a given angle
ind = 0
for i in range(0,360,45):
chx[i] = tmpx[ind]
chy[i] = tmpy[ind]
ind += 1
for _ in range(k): # Move k times
nx = x + chx[angle] # Get next x coord
ny = y + chy[angle] # Get next y coord
nx %= 8 # In case we "fall off" the board
ny %= 8
x = nx
y = ny
turn = board[x][y]*45 # How much we turn
#How our angle changes based off of turn
angle -= turn
angle += 180
angle %= 360
print("{}, {}".format(x+1,y+1)) # We used 0 based indexing while problem uses 1 based so add 1