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-----
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-----
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-----
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.

Starter code:

Solutions:
t = int(input())

for i in range(t):
    n = int(input())
    dir = []
    
    for j in range(n):
        dir.append(input().strip().split())
  
    for j in range(n-1):
        if dir[j+1][0] == 'Right':
            dir[j][0] = 'Left'
        else:
            dir[j][0] = 'Right'

    dir[n-1][0] = 'Begin'

    for j in reversed(dir):
        print(' '.join(j))


        


# -*- coding: utf-8 -*-
"""
Created on Sun Dec 23 22:17:11 2012

@author: anant
"""

T = int(input())
for i in range(0,T):
    l = []
    l2 = []
    n = int(input())
    for j in range(0,n):
      l.append(input())
    length = len(l)
    word = l[length-1].split();
    direction = word[0]
    city = l[length-1][l[length-1].find("on")+3:]
    temp = "Begin on "+city
    l2.append(temp)
    for i in range(2,length+1):
      
      word = l[length-i].split();
      if direction == "Right":
        direction = "Left"
      else:
        direction = "Right"
      city = l[length-i][l[length-i].find("on")+3:]
      temp = direction + " on "+city
      l2.append(temp)
      
      direction = word[0]
     # l2.append(direction + "on" + l[length-i][])
     
    for sentence in l2:
      print(sentence)
    print("")
      
      
      
    


a=int(eval(input()))
for i in range(0,a):
    n=eval(input())
    k=n
    word=[]
    for j in range(0,n):
        t=input()
        word.append(t)
        l=[]
    flag=0
    for j in range(k-1,-1,-1):
        l=word[j].split()
        
        if(j==k-1):
            print("Begin", end=' ')
        elif(right==0):
            print("Right", end=' ')
        elif(right==1):    
            print("Left", end=' ')
        
        if(l[0]=='Left'):
            right=0
        elif(l[0]=='Right'):
            right=1
        for index in range (1,len(l)-1):
            print(l[index], end=' ')
        print(l[index+1])
    print() 
      #  print l
#print word[0].split()
        

a=int(eval(input()))
for i in range(0,a):
    n=eval(input())
    k=n
    word=[]
    for j in range(0,n):
        t=input()
        word.append(t)
        l=[]
    flag=0
    for j in range(k-1,-1,-1):
        l=word[j].split()
        
        if(j==k-1):
            print("Begin", end=' ')
        elif(right==0):
            print("Right", end=' ')
        elif(right==1):    
            print("Left", end=' ')
        
        if(l[0]=='Left'):
            right=0
        elif(l[0]=='Right'):
            right=1
        for index in range (1,len(l)-1):
            print(l[index], end=' ')
        print(l[index+1])
    print() 
      #  print l
#print word[0].split()
        

# Reverse Directions problem - Dec Cook off on Code Chef
import sys

class Directions:
	
	ReversedDirections=[]
	ReversedDirLines=[]

	def __init__(self,dirLines,directions):
		self.DirLines=dirLines
		self.Directions=directions
		
def FormReversedDirLines(d):
	i=-1
	j=0
	ReversedDirLines=[]
	while i>=(-1*len(d.DirLines)):
		direction=d.ReversedDirections[j]
		t=' '.join(d.DirLines[i].split()[1:])
		ReversedDirLines.append(direction+' '+t)
		i=i-1
		j=j+1
	return ReversedDirLines


def CorrectDirections(directions):
	correctedDirs=[directions[0]]
	for d in directions[1:]:
		if d=="Right":
			correctedDirs.append("Left")
		else:
			correctedDirs.append("Right")
	return correctedDirs

def Reverse(l):
	newL=[]
	i=-1
	while i>=(-1*len(l)):
		newL.append(l[i])
		i=i-1
	return newL

def RightCircularShift(l):
	newL=l[1:]
	newL.append(l[0])
	return newL


def ReverseDirections(Directions):
	rShiftDirs=RightCircularShift(Directions)
	reverse=Reverse(rShiftDirs)
	reverse=CorrectDirections(reverse)
	return reverse

def WriteOutput(fPath,output):
	f=open(fPath,"w")
	for d in output:
		f.writelines(d.ReversedDirLines)
		f.write('\n')
	f.close()

def WriteOutputToConsole(output):
	for d in output:
		for l in d.ReversedDirLines:
			print(l)
		print('')

def GetDirectionsFromLines(lines):
	directions=[]
	for l in lines:
		directions.append(l.split()[0])
	return directions

def ReadInputFromConsole():
	lines=[]
	k=input()
	while k!='':
		lines.append(k)
		k=input()
	return ParseInput(lines)

def ParseInput(lines):
	nCases=int(lines[0])
	parsedLines=[]
	prev=0
	i=0
	while i<nCases:
		numLineIndex=prev+1
		nDirLines=int(lines[numLineIndex])
		parsedLines.append(lines[numLineIndex+1:numLineIndex+1+nDirLines])
		i=i+1
		prev=prev+1+nDirLines
	inputDirs=[]
	for p in parsedLines:
		dirLines=p
		directions=GetDirectionsFromLines(p)
		inputDirs.append(Directions(dirLines,directions))
	return inputDirs

def ReadInput(fPath):
	f=open(fPath,"r")
	lines=f.readlines()
	f.close()
	return ParseInput(lines)		

def __starting_point():
	# TestCases
	#print ReverseDirections(["Begin", "Right", "Right", "Left"])
	#print ReverseDirections(["Begin","Left","Left","Right","Right","Right"])
	directions=ParseInput(sys.stdin.readlines())
	for d in directions:
		d.ReversedDirections=ReverseDirections(d.Directions)
		d.ReversedDirLines=FormReversedDirLines(d)

	WriteOutputToConsole(directions)
	

__starting_point()

tc = int(input())
while tc:
	n = int(input())

	direction = []
	road = []

	for i in range(0,n):
		line = input()
		direction.append(line[0])

		if line[0] == 'B' or line[0] == 'R':
			road.append(line[9:])
		else:
			road.append(line[8:])

	print('Begin on ' + road[n-1])
	for i in range(0,n-1):
		if(direction[n-i-1] == 'R'):
			op = 'Left on '
		else:
			op = 'Right on '
		print(op + road[n-i-2])
	print()

	tc -= 1

t=eval(input())
for q in range(t):
	n=eval(input())
	a=[]
	for i in range(n):
		a.append(input().split())
		s=''
		for x in range(2,len(a[i])):
			s+=' '+a[i][x]
		a[i][2]=s
	ans='Begin on'+a[n-1][2]+'\n'

	i=n-2
	while i>-1:
		if a[i+1][0]=='Left':
			ans+='Right on'
		else:
			ans+='Left on'
		ans+=a[i][2]+'\n'
		i-=1
	print(ans)



import sys

t=int(sys.stdin.readline());
while t>0:
	directions=[];
	n=int(sys.stdin.readline());
	for i in range(0,n):
		line=sys.stdin.readline();
		directions.append(line);
	x=directions[len(directions)-1].split()[2:]
	print("Begin on", end=' ')
	for item in x:
		print(item, end=' ')
	print()
		
	for i in range(len(directions)-2,-1,-1):
		first=directions[i+1].split()[0]
		x=directions[i].split()[2:]
		if first=="Left":
			first="Right"
		elif first=="Right":
			first="Left"
		print(first+" on", end=' ')
		for item in x:
			print(item, end=' ')
		print()
	if t!=1:	
		print()
	t -= 1;


n=int(input())
while n>0:
    t=int(input())
    i=t
    a=[]
    while t>0:
        r=input().split()
        a.append(r)
        t-=1
    p=''
    while i>0:
        r=a[i-1]
        j=r[0]
        if t==0:
            r[0]='Begin'
            t=1        
        elif p=='Right':
            r[0]='Left'
        elif p=='Left':
            r[0]='Right'
        r=' '.join(r)
        print(r)
        i-=1
        p=j
    print('\n')
    n-=1


t = eval(input())
for i in range(0, t):
	n = eval(input())
	directions = []
	for j in range(0, n):
		x = input()
		directions.append(x)
	for j in range(n-1, -1, -1):
		output = ""
		if j == (n-1):
			output = "Begin "
		else:
			if(directions[j+1].find("Left") == -1):
				output = "Left "
			else:
				output = "Right "
		output = output + directions[j][directions[j].find("on"):]
		print(output)


def __starting_point():
    ntc=int(input())
    c=0
    res=[]
    while c<ntc:
        ro=[]
        di=[]
        n=int(input())
        c1=0
        res.append([])
        res[c].append("")
        while c1<n:
            var=input()
            a=var.index("on")
            ro.append(var[a+3:])
            a=var.index(" ")
            di.append(var[:a])
            c1+=1
        c1=n-2
        res[c][0]="Begin on " +ro[n-1]
        while c1>=0:
             if di[c1+1]=="Left":
                 res[c].append("Right")
             elif di[c1+1]=="Right":
                 res[c].append("Left")
             res[c][n-1-c1]=res[c][n-1-c1]+" "+"on"+" "+ro[c1]
             c1-=1
             
        c+=1
    c=0    
    while c<ntc:
        l=len(res[c])
        c1=0
        while(c1<l):
            print(res[c][c1])
            c1+=1
        c+=1
        
    
    

             
             
            
            
            
            
        

__starting_point()

t=int(input())
listd=[]
listr=[]
while(t):
    n=int(input())
    x=n
    while(n):
        a,b=input().split(' on ')
        listd.append(a)
        listr.append(b)
        n=n-1
    print('Begin on '+listr.pop())
    listd.pop(0)
    x=x-1
    while(x):
        temp=listd.pop()
        if(temp=='Left'):
            print('Right on '+listr.pop())
        else:
            print('Left on '+listr.pop())
        x=x-1
    print('')
    t=t-1

cases = int(input())
for case in range (cases):
    N = int(input())
    A = []
    B = []
    for n in range (N):
        x,y = input().split(" on ")
        A.append(x)
        B.append(y)
    x = n
    y = n-1
    print(A[0],'on',B[n])
    for y in range (N-1,0,-1):
        if (A[x] == "Right"):
            print("Left on",B[y-1])
        else:
            print("Right on",B[y-1])
        x-=1
    if (case != cases-1):
        print("")


T=int(input())

for _ in range(T):
    N=int(input())
    road=[]
    for _ in range(N):
        t=input()
        if t[0]=='B':
            road.append(('B',t[5:]))
        elif t[0]=='R':
            road.append(('R',t[5:]))
        else:
            road.append(('L',t[4:]))
    for x in range(N):
        Tprev, temp=road.pop()
        if x == 0:
            print('Begin' + temp)
        else:
            if prev == 'L':
                print('Right' + temp)
            else:
                print('Left' + temp)
        prev=Tprev
    print()


import sys
t = int( input() )
while t > 0:
	n = int( input() )
	ins = []
	while n > 0:
		s = input()
		ins.append(s)
		n -= 1
	idx = len(ins) - 2
	if ins[idx + 1].startswith('Left'):
		print('Begin' + ins[idx + 1][4:])
	else:
		print('Begin' + ins[idx + 1][5:])
	
	while idx >= 0:
		if ins[idx + 1].startswith('Left'):
			if ins[idx].startswith('Left'):
				print('Right' + ins[idx][4:])
			else:
				print('Right' + ins[idx][5:])
		else:
			if ins[idx].startswith('Left'):
				print('Left' + ins[idx][4:])
			else:
				print('Left' + ins[idx][5:])
		idx -= 1
	print('')
	t -= 1


x=int(input())
for i in  range(0,x):
	y=int(input())
	list=[]
	for p in range(0,y):
		m=input()
		list.append(m)
	listans=[]
	s=list[y-1]
	s=s.split(" ")
	s[0]="Begin"
	q=""
	flag=0
	for r in s:
		if flag==1:
			q=q+" "
		else:
			flag=1
		q=q+r
		
#	print q

	listans.append(q)
	listk=[]
	for p in range(0,y-1):
		q=""
		flag=0
		s=list[p]
		r=list[p+1]
		r=r.split(" ")
		s=s.split(" ")
		if r[0]=="Right":
			s[0]="Left"
		else:
			s[0]="Right"
		for r in s:
			if flag==1:
				q=q+" "
			else:
				flag=1
			q=q+r
		#print q
		listk.append(q)
	for z in range(0,len(listk)):
		listans.append(listk[len(listk)-1-z])
	for z in listans:
		print(z)



import sys

line = sys.stdin.readline()

t = int(line)

for i in range(0,t):
	line = sys.stdin.readline();
	n = int(line);
	r = []
	lr = []
	line = sys.stdin.readline();
	line = line.strip()
	#print("line " , line);
	r.append(line[9:])
	lr.append(-1)
	for j in range(1,n):
		line = sys.stdin.readline()
		line = line.strip()
		if (line[0:7] == "Left on"):
			lr.append(0)
			r.append(line[8:])
			#print(lr)
			#print(r)
		else:
			lr.append(1)
			r.append(line[9:])
			#print(lr)
			#print(r)
	print("Begin on " + r[n-1])
	j = n - 1;
	while j > 0:
		if (lr[j]):
			print("Left on " + r[j-1])
		else:
			print("Right on " + r[j-1])
		j = j - 1
	print("")


def opp(a):
    if a=="Right":
        return "Left"
    else:
        return "Right"
def main():
    t=int(input())
    while(t):
        t-=1
        n=int(input())
        a=[]
        for i in range(n):
            a.append(list(_ for _ in input().split(' on ')))
        a.reverse()
        print("Begin on", a[0][1])
        for i in range(1,n):
            print(opp(a[i-1][0]),"on",a[i][1])
        print("")
main()

import sys

t = int(sys.stdin.readline())

for test in range(t):
    n = int(sys.stdin.readline())
    lines = [sys.stdin.readline().strip() for i in range(n)]
    turns = [x.split()[0] for x in lines][1:][::-1]
    turns = ['Right' if t == 'Left' else 'Left' for t in turns]
    turns = ['Begin'] + turns
    lines = lines[::-1]
    for i in range(n):
        print(turns[i], ' '.join(lines[i].split()[1:]))
        
    print()

        


t=eval(input())
for x in range (0,t):
	n=eval(input())
	a=[]
	for i in range (0,n):
		a.append(input())
	c=a[n-1][0]
	a[n-1]=a[n-1].replace("Left","Begin")
	a[n-1]=a[n-1].replace("Right","Begin")
	print(a[n-1])
	for i in range (1,n-1):
		if(c=='R'):
			c=a[n-1-i][0]
			a[n-1-i]=a[n-1-i].replace("Right","Left")
		else:
			c=a[n-1-i][0]
			a[n-1-i]=a[n-1-i].replace("Left","Right")
		print(a[n-1-i])
	if(c=='L'):
		a[0]=a[0].replace("Begin","Right")
	else:
		a[0]=a[0].replace("Begin","Left")
	print(a[0])
		


import math
import sys
def comp(x):
	if(x=="Left"):return "Right"
	elif(x=="Right"):return "Left"
	return x
y=[""]*45
test=int(eval(input()))
for i in range(test):
	n=int(eval(input()))
	for j in range(n):
		y[j]=input()
	prev="Begin"
	for j in range(n):
		x=y[n-j-1].split()
		print(comp(prev), end=' ')
		for f in x[1:]:
			print(f, end=' ')
		print() 
		prev=x[0]
		

#!/usr/bin/python

def main():
	t = int(input())
	while t:
		n= int(input())
		lst = []
		for i in range(0,n):
			temp = input()
			temp = temp.split()
			lst.append(temp)
		print("Begin on", end=' ')
		l = len(lst)
		for i in range(0, len(lst[l-1]) - 3):
			print(lst[l-1][i+2], end=' ')
		print(lst[l-1][len(lst[l-1]) - 1])

		for i in range(1,l):
			ind = l - i - 1
			if lst[ind+1][0] == "Left":
				print("Right on", end=' ')
			else :
				print("Left on", end=' ')
			for j in range(0,len(lst[ind])-3):
				print(lst[ind][j+2], end=' ')
			print(lst[ind][len(lst[ind]) - 1])

		 
		t = t - 1


def __starting_point():
  main()

__starting_point()


T = int(input())
for t in range(T):
	N = int(input())
	p = []
	q = []
	for n in range(N):
		x = input().split()
		if x[0] == 'Right':
			p.append('Left')
		elif x[0] == 'Left':
			p.append('Right')
		q.append(' '.join(x[2:]))
	p.append('Begin')
	for n in range(N):
		print(p[-1-n], 'on', q[-1-n])
	print()

T = int(input())
for i in range(T):
    N = int(input())
    strs = []
    for j in range(N):
        strs.append(input())
    strs = list(reversed(strs))
    prev = None
    for index, direction in enumerate(strs):
        if index == 0:
            s = direction.split()
            print('Begin ' + ' '. join(s[1:]))
            prev = direction
            continue
        if prev.startswith('Left'):
            print('Right ' + ' '. join(direction.split()[1:]))
        else:
            print('Left ' + ' '. join(direction.split()[1:]))
        prev = direction