Chef recently printed directions from his home to a hot new restaurant across the town, but forgot to print the directions to get back home. Help Chef to transform the directions to get home from the restaurant.
A set of directions consists of several instructions. The first instruction is of the form "Begin on XXX", indicating the street that the route begins on. Each subsequent instruction is of the form "Left on XXX" or "Right on XXX", indicating a turn onto the specified road.
When reversing directions, all left turns become right turns and vice versa, and the order of roads and turns is reversed. See the sample input for examples.

------ Input ------ 

Input will begin with an integer T, the number of test cases that follow. Each test case begins with an integer N, the number of instructions in the route. N lines follow, each with exactly one instruction in the format described above.

------ Output ------ 

For each test case, print the directions of the reversed route, one instruction per line. Print a blank line after each test case.

------ Constraints ------ 

$1 ≤ T ≤ 15$
$2 ≤ N ≤ 40$
$Each line in the input will contain at most 50 characters, will contain only alphanumeric characters and spaces and will not contain consecutive spaces nor trailing spaces. By alphanumeric characters we mean digits and letters of the English alphabet (lowercase and uppercase).$

----- Sample Input 1 ------ 
2
4
Begin on Road A
Right on Road B
Right on Road C
Left on Road D
6
Begin on Old Madras Road
Left on Domlur Flyover
Left on 100 Feet Road
Right on Sarjapur Road
Right on Hosur Road
Right on Ganapathi Temple Road
----- Sample Output 1 ------ 
Begin on Road D
Right on Road C
Left on Road B
Left on Road A

Begin on Ganapathi Temple Road
Left on Hosur Road
Left on Sarjapur Road
Left on 100 Feet Road
Right on Domlur Flyover
Right on Old Madras Road
----- explanation 1 ------ 
In the first test case, the destination lies on Road D, hence the reversed route begins on Road D. The final turn in the original route is turning left from Road C onto Road D. The reverse of this, turning right from Road D onto Road C, is the first turn in the reversed route.

Source: codechef | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Implementation


Solutions:
for i in range(int(input())):
	n = int(input())
	b = []
	for i in range(n):
		a = list(input().split())
		b.append(a)
	for i in range(n - 1):
		if b[i + 1][0] == 'Right':
			b[i][0] = 'Left'
		else:
			b[i][0] = 'Right'
	b[n - 1][0] = 'Begin'
	final = b[::-1]
	for i in final:
		print(*i)


t = int(input())
for z in range(t):
	n = int(input())
	a = [0] * n
	for i in range(0, n):
		a[i] = input()
	r = []
	d = []
	for i in a:
		r.append(i[i.index('on') + 3:])
		d.append(i[:i.index('on') - 1])
	print('Begin on', r[n - 1])
	for i in range(1, n):
		m = ''
		if d[n - i] == 'Right':
			m = 'Left'
		elif d[n - i] == 'Left':
			m = 'Right'
		print(m, 'on', r[n - i - 1])


import copy
import re
t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for j in range(n):
		l.append(input())
	s = copy.deepcopy(l)
	s[-1] = re.sub('Begin|Left|Right', 'Begin', s[-1])
	for j in range(n - 1):
		if re.search('Left', l[j + 1]):
			s[j] = re.sub('Begin|Left|Right', 'Right', s[j])
		elif re.search('Right', l[j + 1]):
			s[j] = re.sub('Begin|Left|Right', 'Left', s[j])
	s.reverse()
	for x in s:
		print(x)


listInput = lambda : list(map(int, input().split(' ')))
mapInput = lambda : map(int, input().split(' '))
for _ in range(int(input())):
	n = int(input())
	L = []
	for _ in range(n):
		L.append(list(input().split()))
	L = L[::-1]
	for i in range(n - 1, 0, -1):
		if L[i - 1][0] == 'Left':
			L[i][0] = 'Right'
		elif L[i - 1][0] == 'Right':
			L[i][0] = 'Left'
	L[0][0] = 'Begin'
	for each in L:
		print(' '.join(each))


x = int(input())
for i in range(x):
	y = int(input())
	dir = []
	block = []
	dir2 = []
	for i in range(y):
		x = input().split()
		dir.append(x[0])
		block.append(x[2:])
	dir.reverse()
	block.reverse()
	dir2.append('Begin')
	for i in range(len(dir) - 1):
		if dir[i] == 'Right':
			dir2.append('Left')
		else:
			dir2.append('Right')
	for i in range(y):
		print(str(dir2[i]) + ' on ' + ' '.join([str(elem) for elem in block[i]]))


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	for j in range(1, n + 1):
		if j == 1:
			p = list(l[-1].split(' '))
			p[0] = 'Begin'
			print(' '.join(p))
		if j != 1:
			p = list(l.pop().split(' '))
			q = list(l[-1].split(' '))
			if p[0] == 'Right':
				q[0] = 'Left'
				print(' '.join(q))
			else:
				q[0] = 'Right'
				print(' '.join(q))


for _ in range(int(input())):
	n = int(input())
	d = [0] * n
	p = [0] * n
	nd = []
	for i in range(n):
		(d[i], p[i]) = input().split(' on ')
	for i in range(1, n):
		if d[i] == 'Left':
			nd.append('Right')
		else:
			nd.append('Left')
	nd.append('Begin')
	for i in range(n):
		print(nd[n - 1 - i] + ' on ' + p[n - 1 - i])


for _ in range(int(input())):
	N = int(input())
	directions = []
	streetnames = []
	for i in range(N):
		x = input().split()
		streetnames.append(' '.join(x[2:]))
		if x[0] != 'Begin':
			directions.append(x[0])
	directions = directions[::-1]
	directions = ['Left' if i == 'Right' else 'Right' for i in directions]
	streetnames = streetnames[::-1]
	for i in range(N):
		if i == 0:
			print('{0} on {1}'.format('Begin', streetnames[i]))
			continue
		print('{0} on {1}'.format(directions[i - 1], streetnames[i]))


for i in range(int(input())):
	N = int(input())
	A = []
	B = []
	for j in range(N):
		x = input().split(' on ')
		A.append(x[0])
		B.append(x[1])
	newa = []
	for j in range(1, N):
		if A[j] == 'Right':
			newa.append('Left')
		else:
			newa.append('Right')
	newa.append('Begin')
	for j in range(N - 1, -1, -1):
		print(newa[j] + ' on ' + B[j])


for _ in range(int(input())):
	x = int(input())
	l = []
	for j in range(x):
		l.append(input().split())
	for a in range(x - 1):
		if l[a + 1][0] == 'Right':
			l[a][0] = 'Left'
		else:
			l[a][0] = 'Right'
	l[-1][0] = 'Begin'
	for a in range(x - 1, -1, -1):
		print(' '.join(l[a]))


for t in range(int(input())):
	n = int(input())
	a = [''] * n
	for i in range(n):
		a[i] += input()
	for i in range(n - 1):
		if a[i + 1].startswith('Left'):
			a[i] = a[i].replace('Begin', 'Right')
			a[i] = a[i].replace('Left', 'Right')
		else:
			a[i] = a[i].replace('Begin', 'Left')
			a[i] = a[i].replace('Right', 'Left')
	a[-1] = a[-1].replace('Right', 'Begin')
	a[-1] = a[-1].replace('Left', 'Begin')
	for i in range(n):
		print(a[n - 1 - i])


n = int(input())
while n > 0:
	direction = []
	rest = []
	a = int(input())
	for i in range(a):
		s = input()
		ind = s.find(' ')
		direction.append(s[:ind])
		rest.append(s[ind:])
	print('Begin' + rest[a - 1])
	for i in range(a - 1, 0, -1):
		if direction[i] == 'Left':
			print('Right' + rest[i - 1])
		else:
			print('Left' + rest[i - 1])
	n -= 1


t = int(input())
while t > 0:
	n = int(input())
	lst = []
	for i in range(n):
		(direction, road) = map(str, input().split(' on '))
		lst.append([direction, road])
	for i in range(len(lst) - 1, -1, -1):
		if i == len(lst) - 1:
			print('Begin on ' + lst[i][1])
		elif lst[i + 1][0] == 'Right':
			print('Left on ' + lst[i][1])
		else:
			print('Right on ' + lst[i][1])
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	place = []
	direction = []
	for i in range(n):
		a = input().split(' on ')
		direction.append(a[0])
		place.append(a[1])
	print(f'Begin on {place[-1]}')
	i = n - 1
	while i:
		current_direction = 'Right' if direction[i] == 'Left' else 'Left'
		print(f'{current_direction} on {place[i - 1]}')
		i -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = []
	list2 = []
	for i in range(n):
		s = input()
		if s[4] == ' ':
			list1.append(s[4:])
		else:
			list1.append(s[5:])
		list2.append(s[0])
	print('Begin' + list1[n - 1])
	for i in reversed(range(n - 1)):
		if list2[i + 1][0] == list2[i][0]:
			if list2[i][0] == 'R':
				print('Left' + list1[i])
			elif list2[i][0] == 'L':
				print('Right' + list1[i])
		elif list2[i][0] == 'B':
			if list2[i + 1][0] == 'R':
				print('Left' + list1[i])
			elif list2[i + 1][0] == 'L':
				print('Right' + list1[i])
		elif list2[i][0] == 'R':
			print('Right' + list1[i])
		elif list2[i][0] == 'L':
			print('Left' + list1[i])


g = int(input())
po = []
for kku in range(0, g):
	kl = int(input())
	tr = []
	for hh in range(0, kl):
		ppe = str(input())
		tr.append(ppe)
	po.append(tr)
for nbv in po:
	gte = list(nbv)
	gte.reverse()
	final = []
	ujk = ''
	for mmk in gte:
		opl = mmk.split(' ')
		if ujk == '':
			ujk = opl[0]
			opl.pop(0)
			opl.insert(0, 'Begin')
			lpy = ' '.join(opl)
			final.append(lpy)
		elif ujk == 'Left':
			ujk = opl[0]
			opl.pop(0)
			opl.insert(0, 'Right')
			lptr = ' '.join(opl)
			final.append(lptr)
		else:
			ujk = opl[0]
			opl.pop(0)
			opl.insert(0, 'Left')
			otre = ' '.join(opl)
			final.append(otre)
	for mkre in final:
		print(mkre)


for _ in range(int(input())):
	N = int(input())
	D = []
	R = []
	for _ in range(N):
		S = input()
		if S.startswith('Left'):
			D.append('Right on')
			R.append(S[8:])
		elif S.startswith('Right'):
			D.append('Left on')
			R.append(S[9:])
		else:
			R.append(S[9:])
	D.append('Begin on')
	for (d, r) in zip(reversed(D), reversed(R)):
		print(d, r)


for i in range(int(input())):
	n = int(input())
	a = []
	b = []
	for j in range(n):
		x = input().split(' on ')
		a.append(x[0])
		b.append(x[1])
	newa = []
	for j in range(1, n):
		if a[j] == 'Right':
			newa.append('Left')
		else:
			newa.append('Right')
	newa.append('Begin')
	for j in range(n - 1, -1, -1):
		print(newa[j] + ' on ' + b[j])


for _ in range(int(input())):
	n = int(input())
	l = []
	for __ in range(n):
		s = input()
		s = s.split(' on ')
		l.append(s)
	for i in range(int(n / 2)):
		temp = l[i][1]
		l[i][1] = l[n - i - 1][1]
		l[n - i - 1][1] = temp
	for i in range(1, n):
		if l[i][0] == 'Left':
			l[i][0] = 'Right'
		elif l[i][0] == 'Right':
			l[i][0] = 'Left'
	for i in range(1, int((n + 1) / 2)):
		temp = l[i][0]
		l[i][0] = l[n - i][0]
		l[n - i][0] = temp
	for i in l:
		s = ''
		s += i[0] + ' on ' + i[1]
		print(s)


t = int(input(''))
for _ in range(t):
	n = int(input(''))
	a = []
	for i in range(n):
		st = input().split()
		a.append(st)
	for j in range(n):
		if j == 0:
			print('Begin on', end=' ')
			for x in a[n - j - 1][2:]:
				print(x, end=' ')
			print()
		elif a[n - j][0] == 'Left':
			print('Right on', end=' ')
			for x in a[n - j - 1][2:]:
				print(x, end=' ')
			print()
		elif a[n - j][0] == 'Right':
			print('Left on', end=' ')
			for x in a[n - j - 1][2:]:
				print(x, end=' ')
			print()


for i in range(int(input())):
	k = int(input())
	l1 = []
	for i in range(k):
		l = list(map(str, input().split(' ')))
		l1.append(l)
	c = len(l1) - 1
	l2 = []
	b = l1[c][1:]
	b.insert(0, 'Begin')
	d = ' '.join(b)
	l2.append(d)
	for i in range(c):
		if l1[c][0] == 'Right':
			a = l1[c - 1][1:]
			a.insert(0, 'Left')
			e = ' '.join(a)
			l2.append(e)
		elif l1[c][0] == 'Left':
			a = l1[c - 1][1:]
			a.insert(0, 'Right')
			e = ' '.join(a)
			l2.append(e)
		c -= 1
	for i in l2:
		print(i)


for _ in range(int(input())):
	n = int(input())
	lst = []
	for _ in range(n):
		lst.insert(0, input().split())
	d = {'Left': 'Right', 'Right': 'Left'}
	for i in range(n - 1, 0, -1):
		lst[i][0] = d[lst[i - 1][0]]
	lst[0][0] = 'Begin'
	for i in range(n):
		print(' '.join(lst[i]))


for t in range(int(input())):
	n = int(input())
	a = list()
	for i in range(n):
		b = list(map(str, input().split()))
		a.append((b[0], b[2:]))
	print('Begin on', *a[n - 1][1])
	for i in range(n - 2, -1, -1):
		c = ''
		d = a[i + 1][0]
		if d == 'Left':
			c = 'Right'
		else:
			c = 'Left'
		print(c, 'on', *a[i][1])


n = int(input())
for i in range(0, n):
	a = input()
	list1 = []
	for j in range(0, int(a)):
		list1.append(input().split())
	word = list1[0][0]
	for k in range(0, int(a)):
		if k == int(a) - 1:
			list1[k][0] = word
		else:
			list1[k][0] = list1[k + 1][0]
			if list1[k][0] == 'Right':
				list1[k][0] = 'Left'
			else:
				list1[k][0] = 'Right'
	list1 = list1[::-1]
	for b in list1:
		for c in b:
			print(c, end=' ')
		print('\n')


for _ in range(int(input())):
	n = int(input())
	s1 = []
	for i in range(n):
		s1.append(input())
	if s1[n - 1][0] == 'R':
		print('Begin ' + s1[i][6:])
		prev = 'R'
	elif s1[n - 1][0] == 'L':
		print('Begin ' + s1[i][5:])
		prev = 'L'
	for i in range(n - 2, -1, -1):
		if prev == 'R' and s1[i][0] == 'L':
			print('Left ' + s1[i][5:])
			prev = 'L'
		elif prev == 'R' and s1[i][0] != 'L':
			print('Left ' + s1[i][6:])
			prev = 'R'
		elif prev == 'L' and s1[i][0] == 'L':
			print('Right ' + s1[i][5:])
			prev = 'L'
		else:
			print('Right ' + s1[i][6:])
			prev = 'R'


for _ in range(int(input())):
	n = int(input())
	s = []
	for i in range(n):
		s.append(list(map(str, input().split(' on '))))
	print('Begin on {}'.format(s[-1][-1]))
	for i in range(n - 1, 0, -1):
		if s[i][0] == 'Left':
			print('Right on {}'.format(s[i - 1][-1]))
		else:
			print('Left on {}'.format(s[i - 1][-1]))


for t in range(int(input())):
	dict = []
	answer = ''
	steps = int(input())
	for n in range(steps):
		dict.append(input().split())
	print('Begin on', end='')
	for i in range(2, len(dict[steps - 1])):
		print('', dict[steps - 1][i], end='')
	print('')
	for i in range(steps - 2, -1, -1):
		if dict[i + 1][0] == 'Left':
			answer = 'Right'
		else:
			answer = 'Left'
		print(answer, 'on', end='')
		for j in range(2, len(dict[i])):
			print('', dict[i][j], end='')
		print('')


t = int(input())
while t != 0:
	d = int(input())
	l = []
	c = []
	for i in range(d):
		l.append(input())
		if i != 0:
			c.append(l[i][0])
	l.reverse()
	c.reverse()
	for i in range(len(l)):
		l[i] = l[i].split()
		if i == 0:
			l[i][0] = 'Begin'
		elif c[i - 1] == 'L':
			l[i][0] = 'Right'
		else:
			l[i][0] = 'Left'
		l[i] = ' '.join(l[i])
	for i in l:
		print(i)
	t -= 1


for _ in range(int(input())):
	(direction, locs) = ([], [])
	n = int(input())
	for _ in range(n):
		inpt = input().split()
		direction.append(inpt[0])
		locs.append(' '.join(inpt[2:]))
	direction.reverse()
	direction = ['Begin'] + direction
	for i in range(n):
		if direction[i] == 'Begin':
			pass
		elif direction[i] == 'Right':
			direction[i] = 'Left'
		else:
			direction[i] = 'Right'
	locs.reverse()
	for i in range(n):
		print(direction[i] + ' on ' + locs[i])


for _ in range(int(input())):
	l = []
	for i in range(int(input())):
		s = input()
		l.append(s)
	d = l[1:]
	d = d[::-1]
	d.insert(0, l[0])
	c = l[::-1]
	c[0] = c[0].replace(c[0][:c[0].index(' ')], d[0][:d[0].index(' ')])
	print(c[0])
	for i in range(1, len(c)):
		c[i] = c[i].replace(c[i][:c[i].index(' ')], d[i][:d[i].index(' ')])
		c[i] = c[i].replace('Right', '@')
		c[i] = c[i].replace('Left', 'Right')
		c[i] = c[i].replace('@', 'Left')
		print(c[i])


for _ in range(int(input())):
	n = int(input())
	ar = []
	for i in range(n):
		ar.append(input())
	d = {'Left': 'Right', 'Right': 'Left'}
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			st = 'Begin '
		else:
			st = d[ar[i + 1].split()[0]] + ' '
		st += ' '.join(ar[i].split()[1:])
		print(st)


def cnv(a):
	if a == 'Right':
		return 'Left'
	if a == 'Left':
		return 'Right'
	return a
for _ in range(int(input())):
	m = int(input())
	l = []
	for i in range(m):
		l.append(input().split())
	k = list(l)
	k = k[1:] + k[:1]
	n = []
	z = []
	for i in range(m):
		n.append(cnv(k[i][0]))
		z.append([cnv(k[i][0])] + l[i][1:])
	z = z[::-1]
	for i in range(m):
		print(' '.join(z[i]))


n = int(input())
for i in range(n):
	l = int(input())
	s = []
	for j in range(l):
		s.append(input())
	for k in range(l - 1, -1, -1):
		if k == l - 1:
			print(f"Begin {' '.join(s[k].split(' ')[1:])}")
		else:
			if s[k + 1].split(' ')[0] == 'Right':
				z = 'Left'
			else:
				z = 'Right'
			print(f"{z} {' '.join(s[k].split(' ')[1:])}")


for i in range(int(input())):
	n = int(input())
	a = []
	s = []
	for i in range(n):
		a.append(input())
	while a:
		t = a.pop()
		if len(a) == n - 1:
			if t[0] == 'L':
				s.append('Right')
			else:
				s.append('Left')
			t = t.replace('Left', 'Begin').replace('Right', 'Begin')
		else:
			d = s.pop()
			if t[0] == 'L':
				s.append('Right')
			else:
				s.append('Left')
			t = t.replace('Left', d).replace('Right', d).replace('Begin', d)
		print(t)


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for j in range(n):
		x = [str(X) for X in input().split()]
		a.append(x)
	b = []
	for k in range(0, n - 1):
		if a[k + 1][0] == 'Right':
			a[k][0] = 'Left'
			b.append(a[k])
		elif a[k + 1][0] == 'Left':
			a[k][0] = 'Right'
			b.append(a[k])
	a[n - 1][0] = 'Begin'
	print(*a[n - 1])
	for l in range(n - 1):
		print(*b[n - 2 - l])


def opposite(x):
	if x == 'Right':
		return 'Left'
	else:
		return 'Right'
for _ in range(int(input())):
	n = int(input())
	routes = [input() for __ in range(n)]
	routes = routes[::-1]
	print('Begin' + routes[0][routes[0].index(' '):])
	for i in range(1, n):
		print(opposite(routes[i - 1][:routes[i - 1].index(' ')]) + routes[i][routes[i].index(' '):])


for _ in range(int(input())):
	n = int(input())
	lst = []
	l = []
	for i in range(n):
		st = input()
		lst.append(st)
		if st[0] == 'L':
			l.append('Right')
		elif st[0] == 'R':
			l.append('Left')
	l.append('Begin')
	st = lst[-1]
	if st[0] == 'L':
		st = st.replace('Left', 'Begin')
		lst[-1] = st
	else:
		st = st.replace('Right', 'Begin')
		lst[-1] = st
	print(lst[-1])
	for i in range(n - 2, -1, -1):
		st = lst[i]
		if st[0] == 'L':
			st = st.replace('Left', l[i])
			lst[i] = st
		elif st[0] == 'R':
			st = st.replace('Right', l[i])
			lst[i] = st
		else:
			st = st.replace('Begin', l[i])
			lst[i] = st
		print(lst[i])


t = int(input())
for i in range(t):
	n = int(input())
	list_a = []
	for i in range(n):
		s = input().split()
		list_a.append(s)
	list_a = list_a[::-1]
	new_list = []
	for i in range(n - 1):
		if i == 0:
			new_list.append(list_a[0][0])
			list_a[0][0] = 'Begin'
		else:
			new_list.append(list_a[i][0])
	for i in range(len(new_list)):
		if new_list[i] == 'Left':
			new_list[i] = 'Right'
		elif new_list[i] == 'Right':
			new_list[i] = 'Left'
	for i in range(1, n):
		list_a[i][0] = new_list[i - 1]
	for char in list_a:
		print(' '.join(char))


for _ in range(int(input())):
	n = int(input())
	direction = []
	place = []
	for i in range(n):
		s = input()
		place.append(s)
	for i in place[::-1]:
		if direction == []:
			direction.append(i.replace('Right', 'Begin') if i[0] == 'R' else i.replace('Left', 'Begin'))
		elif i[0] == 'R':
			direction.append(i.replace('Right', direc))
		elif i[0] == 'L':
			direction.append(i.replace('Left', direc))
		else:
			direction.append(i.replace('Begin', direc))
		direc = 'Left' if i[0] == 'R' else 'Right'
	print(*direction, sep='\n')


t = input()
for c in range(0, int(t)):
	n = input()
	direct = []
	place = []
	for x in range(0, int(n)):
		s = input().split()
		direct.append(s[0])
		place.append(s[2:])
	out = ''
	for y in range(int(n) - 1, -1, -1):
		out = ''
		if y == int(n) - 1:
			out = out + 'Begin on '
		elif direct[y + 1] == 'Right':
			out = out + 'Left on '
		else:
			out = out + 'Right on '
		temp = place[y]
		for z in range(0, len(temp)):
			out = out + temp[z] + ' '
		print(out)


for tc in range(int(input())):
	n = int(input())
	roads = []
	turns = []
	for i in range(n):
		dire = input()
		if dire[0] == 'L':
			r = dire[8:]
			roads.append(r)
			turns.append('Right on')
		elif dire[0] == 'R':
			r = dire[9:]
			roads.append(r)
			turns.append('Left on')
		else:
			t = dire[:8]
			r = dire[9:]
			roads.append(r)
			turns.append(t)
	n = len(roads)
	p = n
	while p:
		t = turns[p % n]
		r = roads[(p - 1) % n]
		print(t, r)
		p -= 1


d = {'Left': 'Right', 'Right': 'Left'}
for _ in range(int(input())):
	n = int(input())
	road = list()
	for i in range(n):
		s = list(map(str, input().split(' ')))
		road.append(s)
	k = road[n - 1][0]
	print('Begin', *road[n - 1][1:])
	for i in range(n - 2, -1, -1):
		print(d[k], *road[i][1:])
		k = road[i][0]


import copy
for i in range(int(input())):
	n = int(input())
	L = []
	for i in range(n):
		inst = list(map(str, input().split()))
		L.append(inst)
	L.reverse()
	import copy
	a = copy.deepcopy(L)
	for i in range(len(L)):
		if i == 0:
			L[i][0] = 'Begin'
		elif a[i - 1][0] == 'Right':
			L[i][0] = 'Left'
		elif a[i - 1][0] == 'Left':
			L[i][0] = 'Right'
	for i in L:
		print(*i)


t = int(input())

def change(di):
	if di == 'Left':
		di = 'Right'
	else:
		di = 'Left'
	return di
for _ in range(t):
	n = int(input())
	xpath = []
	xx = []
	for i in range(n):
		ax = input().split()
		xpath.append(ax[1:])
		xx.append(ax[0])
	path = []
	x = []
	for i in range(n - 1, -1, -1):
		path.append(xpath[i])
		x.append(xx[i])
	for i in range(n):
		pt = path[i]
		if i == 0:
			fi = ['Begin'] + pt
		else:
			di = x[i - 1]
			di = change(di)
			fi = [di] + pt
		print(*fi)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def outp(n):
	sys.stdout.write(str(n) + '\n')

def outlt(lst):
	sys.stdout.write(' '.join(map(str, lst)) + '\n')

def outplt(lst):
	sys.stdout.write('\n'.join(map(str, lst)))

def outpltlt(lst):
	sys.stdout.write('\n'.join(map(str, (' '.join(map(str, a)) for a in lst))))
code = {'Left': 'Right', 'Right': 'Left'}
ans = []
for _ in range(inp()):
	N = inp()
	A = [insr() for x in range(N)]
	old = A[-1].split()
	ans.append('Begin ' + ' '.join(old[1:]))
	for a in A[-2:-N - 1:-1]:
		new = a.split()
		ans.append(code[old[0]] + ' ' + ' '.join(new[1:]))
		old = new
outplt(ans)


t = int(input())
while t:
	n = int(input())
	l = []
	for i in range(n):
		a = list(input().split())
		l.append(a)
	l.append(['Begin'])
	l.reverse()
	for i in range(n):
		k = ' '.join(l[i + 1][1:])
		if l[i][0] == 'Begin':
			print('Begin ' + k)
		elif l[i][0] == 'Left':
			print('Right ' + k)
		elif l[i][0] == 'Right':
			print('Left ' + k)
	t -= 1


def change(st):
	if st == 'Begin':
		return st
	elif st == 'Right':
		return 'Left'
	return 'Right'
for i in range(int(input())):
	no = int(input())
	root = []
	add = []
	for j in range(no):
		st = input().split(' on ')
		root.append(st[0])
		add.append(st[1])
	root = root[1:] + root[:1]
	for i in range(no):
		print(change(root.pop()) + ' on ' + add.pop())


get_int = lambda : int(input())
get_mint = lambda : list(map(int, input().split()))
for _ in range(get_int()):
	N = get_int()
	(directions, roads) = ([], [])
	for _ in range(N):
		(d, r) = input().split(' on ')
		if d == 'Left':
			directions.append('Right on')
		elif d == 'Right':
			directions.append('Left on')
		roads.append(r)
	directions.append('Begin on')
	for _ in range(N):
		print(directions.pop(), roads.pop())


T = int(input())
for x in range(T):
	n = int(input())
	a = []
	b = []
	nd = []
	for i in range(n):
		s = input().split(' on ')
		a.append(s[0])
		b.append(s[1])
	for i in range(1, n):
		if a[i] == 'Left':
			nd.append('Right')
		else:
			nd.append('Left')
	nd.append('Begin')
	for i in range(n):
		print(nd[n - 1 - i] + ' on ' + b[n - 1 - i])


for i in range(int(input())):
	n = int(input())
	st1 = ''
	list2 = []
	list1 = []
	for x in range(n):
		st = list(map(str, input().split()))
		if st[0] == 'Left':
			list1.insert(0, 'Right')
		elif st[0] == 'Right':
			list1.insert(0, 'Left')
		for z in st[1:]:
			st1 += z
			st1 += ' '
		length = len(st1)
		list2.insert(0, st1[:length - 1])
		st1 = ''
	list1.insert(0, 'Begin')
	j = 0
	while j < len(list2):
		print(list1[j] + ' ' + list2[j])
		j += 1


for _ in range(int(input())):
	n = int(input())
	di_li = []
	words = []
	for _ in range(n):
		li = list(input().split())
		dire = li[0]
		words.append(' '.join(li[1:]))
		if dire == 'Left':
			di_li.insert(1, 'Right')
		elif dire == 'Right':
			di_li.insert(1, 'Left')
		else:
			di_li.insert(0, dire)
	together = list(zip(di_li, words[::-1]))
	for i in together:
		print(i[0] + ' ' + i[1])


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input().split(' ', 2))
	res = 'Begin'
	for i in range(n - 1, -1, -1):
		res = res + ' ' + l[i][1] + ' ' + l[i][2]
		print(res)
		res = 'Right' if l[i][0] == 'Left' else 'Left'


t = int(input())
for i in range(t):
	n = int(input())
	ins = []
	nams = []
	for i in range(n):
		(a, b) = input().split(' on ')
		ins.append(a)
		nams.append(b)
	nams.reverse()
	ins.reverse()
	print('Begin on', nams[0])
	ins.pop(n - 1)
	nams.pop(0)
	for i in range(n - 1):
		if ins[i] == 'Right':
			print('Left on', nams[i])
		else:
			print('Right on', nams[i])


T = int(input())
for tests in range(T):
	n = int(input())
	arr = []
	direction = []
	rest = []
	for _ in range(n):
		arr = input()
		ind = arr.find(' ')
		direction.append(arr[:ind])
		rest.append(arr[ind:])
	print('Begin' + rest[n - 1])
	for index in range(n - 1, 0, -1):
		if direction[index] == 'Left':
			print('Right' + rest[index - 1])
		else:
			print('Left' + rest[index - 1])


for _ in range(int(input())):
	xx = []
	x = []
	for __ in range(int(input())):
		x.append(list(map(str, input().split())))
	print(x[0][0], x[0][1], ' '.join(x[-1][2:]))
	for (i, j) in zip(x[::-1], x[::-1][1:]):
		print('Left' if i[0] == 'Right' else 'Right', 'on', ' '.join(j[2:]))


for _ in range(int(input())):
	dirct = []
	n = int(input())
	for i in range(n):
		dirct.append(input())
	for j in range(n - 1):
		if 'Right' in dirct[j + 1]:
			if j == 0:
				dirct[j] = dirct[j].replace('Begin', 'Left')
			else:
				dirct[j] = dirct[j].replace('Right', 'Left')
		elif j == 0:
			dirct[j] = dirct[j].replace('Begin', 'Right')
		else:
			dirct[j] = dirct[j].replace('Left', 'Right')
	if 'Right' in dirct[-1]:
		dirct[-1] = dirct[-1].replace('Right', 'Begin')
	else:
		dirct[-1] = dirct[-1].replace('Left', 'Begin')
	for k in dirct[::-1]:
		print(k)


for a in range(int(input())):
	N = int(input())
	l = []
	for i in range(N):
		s = str(input())
		l.append(s)
	for j in range(1, N + 1):
		if j == 1:
			output = list(l[-1].split(' '))
			output[0] = 'Begin'
			print(' '.join(output))
		if j != 1:
			output = list(l.pop().split(' '))
			output1 = list(l[-1].split(' '))
			if output[0] == 'Right':
				output1[0] = 'Left'
				print(' '.join(output1))
			else:
				output1[0] = 'Right'
				print(' '.join(output1))


tests = int(input())
for i in range(tests):
	length = int(input())
	dir_arr = []
	for j in range(length):
		dir_arr.append(input().split())
	append = 'Begin'
	for j in range(length - 1, -1, -1):
		temp = dir_arr[j][0]
		dir_arr[j][0] = append
		print(' '.join(dir_arr[j]))
		if temp == 'Right':
			append = 'Left'
		else:
			append = 'Right'


for _ in range(int(input())):
	n = int(input())
	x = [input() for _ in range(n)]
	last = ''
	for i in range(n - 1, -1, -1):
		a = x[i]
		j = list(map(str, a.split()))
		if last == '':
			last = j[0]
			j[0] = 'Begin'
		elif last == 'Right':
			last = j[0]
			j[0] = 'Left'
		else:
			last = j[0]
			j[0] = 'Right'
		print(' '.join(j))


t = int(input())
for i in range(t):
	n = int(input())
	k = []
	for j in range(n):
		s = input()
		l = list(s.split(' '))
		k.append(l)
	t = []
	for j in k:
		t.append(j[0])
	val = t.pop(0)
	t.append(val)
	nt = []
	for j in t:
		if j == 'Right':
			nt.append('Left')
		elif j == 'Left':
			nt.append('Right')
		else:
			nt.append(j)
	for j in range(len(nt)):
		k[j][0] = nt[j]
	for j in k[::-1]:
		print(' '.join(j))


for i in range(int(input())):
	m = int(input())
	List = []
	for i in range(m):
		List.append(input().split())
	towars_res = []
	for i in List:
		towars_res.append(i[0])
	towars_home = []
	for i in towars_res:
		if i == 'Right':
			towars_home.append('Left')
		elif i == 'Left':
			towars_home.append('Right')
	towars_home.append('Begin')
	towars_home = towars_home[::-1]
	reversed_list = List[::-1]
	new_List = []
	for i in reversed_list:
		new_List.append(i[1:])
	for i in range(m):
		s = ' '.join(new_List[i])
		s = towars_home[i] + ' ' + s
		print(s)


for _ in range(int(input())):
	n = int(input())
	direction = []
	place = []
	output = []
	for _ in range(n):
		inst = str(input())
		temp = inst.find(' on ')
		direction.append(inst[:temp])
		place.append(inst[temp:])
	print('Begin' + place[-1])
	for i in range(n - 1, 0, -1):
		if direction[i] == 'Left':
			print('Right' + place[i - 1])
		else:
			print('Left' + place[i - 1])


from collections import *
tc = int(input())
for _ in range(1, 1 + tc):
	n = int(input())
	dir = deque()
	area = deque()
	for i in range(n):
		temp = input().split()
		area.append(temp[1:])
		if temp[0] == 'Right':
			dir.append('Left')
		if temp[0] == 'Left':
			dir.append('Right')
	dir.append('Begin')
	while len(dir) > 0:
		s = ' '.join(area.pop())
		print(f'{dir.pop()} {s}')