can anyone tell whats wrong with this python code?

```
t=input()
a=[]
for i in range(t):
n=input()
a.append([])
for j in range(n):
a[i].append(raw_input().split(" on "))
for i in a:
for j in range(len(i)-1):
if i[j+1][0]=="Right":
i[j][0]="Left"
if i[j+1][0]=="Left":
i[j][0]="Right"
i[len(i)-1][0]="Begin"
for k in range(len(i)-1,0,-1):
print i[k][0],"on",i[k][1]
if i!=a[len(a)-1]:
print i[0][0],"on",i[0][1],"\n"
else:
print i[0][0],"on",i[0][1]
```