qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from collections import deque

s = input()
q = int(input())
r = 1
top = deque()
top.append(s)
for _ in range(q):
    inp = input()
    if inp[0] == "1":
        r *= -1
    else:
        o, f, c = inp.split()
        if f == "1" and r == -1 or f == "2" and r == 1:
            top.append(c)
        else:
            top.appendleft(c)
ans = "".join(top)
ans = ans if r == 1 else s[::-1]
print(ans)


Fixed code:
from collections import deque

s = input()
q = int(input())
r = 1
top = deque()
top.append(s)
for _ in range(q):
    inp = input()
    if inp[0] == "1":
        r *= -1
    else:
        _, f, c = inp.split()
        if f == "1" and r == -1 or f == "2" and r == 1:
            top.append(c)
        else:
            top.appendleft(c)
ans = "".join(top)
print(ans if r == 1 else ans[::-1])
Buggy code:
from collections import deque
S = input()
Q = int(input())
R = deque(S)
rev = 0

for i in range(Q):
    T = input().split()
    if T[0] == '1':
        rev += 1
    else:
        if T[1] == '1':
            if rev % 2 == 0:
                R.append(T[2])
            else:
                R.appendleft(T[2])
        else:
            if rev % 2 == 0:
                R.appendleft(T[2])
            else:
                R.append(T[2])
ans = ''.join(R)
if rev % 2 ==0:
    print(ans)
else:
    print(ans[::1])

Fixed code:
from collections import deque
S = input()
Q = int(input())
R = deque(S)
rev = 0

for i in range(Q):
    T = input().split()
    if T[0] == '1':
        rev += 1
    else:
        if T[1] == '1':
            if rev % 2 == 0:
                R.appendleft(T[2])
            else:
                R.append(T[2])
        else:
            if rev % 2 == 0:
                R.append(T[2])
            else:
                R.appendleft(T[2])
ans = ''.join(R)
if rev % 2 ==0:
    print(ans)
else:
    print(ans[::-1])
Buggy code:
def main():
    from collections import deque
    S = deque(input().split())
    Q = int(input())

    q_list = [input().split() for _ in range(Q)]

    flag = True
    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1':
            flag = not flag
        else:
            if (f == '1') ^ flag:
                S.appendleft(c)
            else:
                S.append(c)
        # print(q)
        # print("".join(S))
    print("".join(S))


if __name__ == '__main__':
    main()


Fixed code:
def main():
    from collections import deque
    S = deque(input())
    Q = int(input())

    q_list = [input().split() for _ in range(Q)]

    flag = False
    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1':
            flag = not flag
        else:
            if (f == '1') ^ flag:
                S.appendleft(c)
            else:
                S.append(c)
    prS(S, flag)
    # prS(S, flag)


def prS(S, flag):
    if flag:
        S.reverse()
    print("".join(S))
    if flag:
        S.reverse()


if __name__ == '__main__':
    main()
Buggy code:
from collections import deque
S = input()
ans = deque(S)
Q = int(input())
rev = True
for i in range(Q):
    a_list = input().split()
    if a_list[0] == "1":
        ans.reverse()
        if rev == True:
            rev = False
        else:
            rev = True
    elif (a_list[1] == "1" and rev == True) or (a_list[1] == "2" and rev == False):
        ans.appendleft(a_list[2])
    else:
        ans.append(a_list[2])
    if rev:
        ans.reverse()
ans = "".join(ans)
print(ans)

Fixed code:
from collections import deque
S = input()
ans = deque(S)
Q = int(input())
rev = False
for i in range(Q):
    a_list = input().split()
    if a_list[0] == "1":
        if rev == True:
            rev = False
        else:
            rev = True
    elif (a_list[1] == "1" and rev == False) or (a_list[1] == "2" and rev == True):
        ans.appendleft(a_list[2])
    else:
        ans.append(a_list[2])
if rev:
    ans.reverse()
ans = "".join(ans)
print(ans)
Buggy code:
from collections import deque
S=input()
s=deque(S)
Q=int(input())
Que=[list(map(str, input().split())) for _ in range(Q)]
#print(Que)
for i in range(Q):
    if Que[i][0]=="1":
        s=s[::-1]
    elif Que[i][0]=="2":
        if Que[i][1]=="2":
            s.append(Que[i][2])
        elif Que[i][1]=="1":
            s.appendleft(Que[i][2])
ans="".join(s)
print(ans)


Fixed code:
from collections import deque
S=input()
s=deque(S)
Q=int(input())
Que=[list(map(str, input().split())) for _ in range(Q)]
#print(Que)
a=0
for i in range(Q):
    if Que[i][0]=="1":
        a+=1
    elif Que[i][0]=="2":
        if a%2==0:
            if Que[i][1]=="2":
                s.append(Que[i][2])
            elif Que[i][1]=="1":
                s.appendleft(Que[i][2])
        else:
            if Que[i][1]=="1":
                s.append(Que[i][2])
            elif Que[i][1]=="2":
                s.appendleft(Que[i][2])
s=list(s)
if a%2==1:
    s=s[::-1]
ans="".join(s)
print(ans)
Buggy code:
from collections import deque

S = deque(list(input()))

Q = int(input())

cnt = 0
for i in range(Q):
    l = list(map(str,input().split()))
    if int(l[0]) == 1:
        cnt += 1
    else:
        if int(l[1]) == 1:
            if cnt % 2 == 1:
                S.append(l[2])
            else:
                S.appendleft(l[2])
        else:
            if cnt % 2 == 1:
                S.appendleft(l[2])
            else:
                S.append(l[2])

print(''.join(S))

Fixed code:
from collections import deque

S = deque(list(input()))

Q = int(input())

cnt = 0
for i in range(Q):
    l = list(map(str,input().split()))
    if int(l[0]) == 1:
        cnt += 1
    else:
        if int(l[1]) == 1:
            if cnt % 2 == 1:
                S.append(l[2])
            else:
                S.appendleft(l[2])
        else:
            if cnt % 2 == 1:
                S.appendleft(l[2])
            else:
                S.append(l[2])

if cnt % 2 == 1:
    S = list(S)[::-1]
print(''.join(S))
Buggy code:
from collections import deque

S = str(input())
Q = int(input())
Query = [list(map(str, input().split())) for _ in range(Q)]

sArry = deque(S.split())
rFlag = False
for i in range(Q):
    if Query[i][0] == '1':
        rFlag = (rFlag == False)
        
    else:
        if rFlag:
            sArry.reverse()
        
        if Query[i][1] == '1':
            sArry.appendleft(Query[i][2])
        else:
            sArry.append(Query[i][2])
#    print("  ", i, Query[i], "".join(sArry))


print("".join(sArry))

Fixed code:
from collections import deque

S = str(input())
Q = int(input())
Query = [list(map(str, input().split())) for _ in range(Q)]

sArry = deque(S)
rFlag = False
for i in range(Q):
    if Query[i][0] == '1':
        rFlag = (rFlag == False)
        
    else:
        if rFlag:
            if Query[i][1] == '1':
                sArry.append(Query[i][2])
            else:
                sArry.appendleft(Query[i][2])
        else:
            if Query[i][1] == '1':
                sArry.appendleft(Query[i][2])
            else:
                sArry.append(Query[i][2])
            
#    print("  ", i, rFlag, "".join(sArry), Query[i])



if rFlag:
    print("".join(reversed(sArry)))
else:
    print("".join(sArry))
Buggy code:
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

s = input()
n = len(s)
q = int(input())
front = ""
back = ""
sgn = 0
for _ in range(q):
	query = input().split()
	if query[0] == "1":
		sgn ^= 1
	else:
		f, c = int(query[1])-1, query[2]
		if f ^ sgn == 0:
			front += c
		else:
			back += c
if not sgn:
	print(front[::-1] + s + back)
else:
	print(back[::-1] + s[::-1] + front)

Fixed code:
import sys
def input():
	return sys.stdin.readline()[:-1]

s = input()
#n = len(s)
q = int(input())
front = ""
back = ""
sgn = 0
for _ in range(q):
	query = input().split()
	if query[0] == "1":
		sgn ^= 1
	else:
		f, c = int(query[1])-1, query[2]
		if f ^ sgn == 0:
			front += c
		else:
			back += c
if not sgn:
	print(front[::-1] + s + back)
else:
	print(back[::-1] + s[::-1] + front)
Buggy code:
def main():
    import sys
    input = sys.stdin.readline
    s = input()
    q =int(input())
    ten = 0
    r = []
    l = []
    for _ in range(q):
        ls = tuple(map(str, input().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
            else:
                if ten%2 ==1:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Fixed code:
def main():
    s = input()
    q =int(input())
    ten = 0
    r = []
    l = []
    for _ in range(q):
        ls = tuple(map(str, input().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
            else:
                if ten%2 ==1:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()
Buggy code:
S = input()
Q = int(input())
re = False
front = []
back = []

for i in range(Q):
    t = input()
    
    if len(t) == 1:
        re = not re
        
    else:
        q, f, c = t.split()
        if (re == True and int(f) == 2) or (re == False and int(f) == 1):
            front.append(c)
        elif (re == True and int(f) == 1) or (re == False and int(f) == 2):
            back.append(c)

if re == False:
    front.reverse()
    F = "".join(front)
    B = "".join(back)
    print(F + S + B)
else:
    back.reverse()
    S.reverse()
    F = "".join(front)
    B = "".join(back)
    print(B + S + F)

Fixed code:
S = input()
Q = int(input())
re = False
front = []
back = []

for i in range(Q):
    t = input()
    
    if len(t) == 1:
        re = not re
        
    else:
        q, f, c = t.split()
        if (re == True and int(f) == 2) or (re == False and int(f) == 1):
            front.append(c)
        elif (re == True and int(f) == 1) or (re == False and int(f) == 2):
            back.append(c)

if re == False:
    front.reverse()
    F = "".join(front)
    B = "".join(back)
    print(F + S + B)
else:
    back.reverse()
    S = S[::-1]
    F = "".join(front)
    B = "".join(back)
    print(B + S + F)
Buggy code:
import sys
input = sys.stdin.readline

s = input().split()[0]
q = int(input())

left, right = [], []
state = 1
for _ in range(q):
    q_i = input().split()
    if len(q_i) == 1:
        state = 2
    else:
        _, f, c = q_i
        f = int(f)
        if (f+state)%2 == 0:
            left += [c]
        else:
            right += [c]
if state == 1:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s + ''.join(left))

Fixed code:
import sys
input = sys.stdin.readline

s = input().split()[0]
q = int(input())

left, right = [], []
state = 1
for _ in range(q):
    q_i = input().split()
    if len(q_i) == 1:
        state = (state)%2 + 1
    else:
        _, f, c = q_i
        f = int(f)
        if (f+state)%2 == 0:
            left += [c]
        else:
            right += [c]
if state == 1:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s[::-1] + ''.join(left))
Buggy code:
from collections import deque

s = deque(input())
q = int(input())
flag = 1
for _ in range(q):
    tfc = input().split()
    print(tfc)
    if tfc[0] == "1":
        flag *= -1

    elif tfc[0] == "2":
        if (tfc[1] == "1" and flag == 1) or (tfc[1] == "2" and flag == -1):
            s.appendleft(tfc[2])
        elif (tfc[1] == "1" and flag == -1) or (tfc[1] == "2" and flag == 1):
            s.append(tfc[2])
else:

    if flag == -1:
        s = list(s)[::-1]
        
print("".join(s))

Fixed code:
from collections import deque

s = deque(input())
q = int(input())

flag = 1

for _ in range(q):
    tfc = input().split()
    if tfc[0] == "1":
        flag *= -1

    elif tfc[0] == "2":
        if (tfc[1] == "1" and flag == 1) or (tfc[1] == "2" and flag == -1):
            s.appendleft(tfc[2])
        elif (tfc[1] == "1" and flag == -1) or (tfc[1] == "2" and flag == 1):
            s.append(tfc[2])
else:

    if flag == -1:
        s = list(s)[::-1]

print("".join(s))
Buggy code:
def main():
    c = 0
    s = input()
    f = ''
    b = ''
    for _ in range(int(input())):
        q = input().split()
        if q[0] == '1':
            c += 1
        else:
            if (q[1] == '1' and c%2 == 0) or (q[1]=='2' and c%2==1):
                f = q[2] + f
            else:
                b += q[2]
    if c % 2:
        print(f+s+b)
    else:
        print(b[::-1]+s[::-1]+f[::-1])

if __name__ == '__main__':
    main()


Fixed code:
def main():
    c = 0
    s = input()
    f = ''
    b = ''
    for _ in range(int(input())):
        q = input().split()
        if q[0] == '1':
            c += 1
        else:
            if (q[1] == '1' and c%2 == 0) or (q[1]=='2' and c%2==1):
                f = q[2] + f
            else:
                b += q[2]
    if c % 2 == 0:
        print(f+s+b)
    else:
        print(b[::-1]+s[::-1]+f[::-1])

if __name__ == '__main__':
    main()
Buggy code:
from collections import deque

S = input()
B = deque()
res = 0
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    A = list(map(str, input().split()))
    if int(A[0]) == 1:
        if res == 0:
            res = 1
        else:
            res = 0
    else:
        if int(A[1]) == 1:
            if res == 0:
                B.appendleft(A[2])
            else:
                B.append(A[2])
        else:
            if res == 0:
                B.append(A[2])
            else:
                B.appendleft(A[2])
if res == 0:
    print(''.join(B))
else:
    B = B[::-1]
    print(''.join(B))

Fixed code:
from collections import deque

S = input()
B = deque()
res = 0
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    A = list(map(str, input().split()))
    if int(A[0]) == 1:
        if res == 0:
            res = 1
        else:
            res = 0
    else:
        if int(A[1]) == 1:
            if res == 0:
                B.appendleft(A[2])
            else:
                B.append(A[2])
        else:
            if res == 0:
                B.append(A[2])
            else:
                B.appendleft(A[2])
if res == 0:
    print(''.join(B))
else:
    B.reverse()
    print(''.join(B))
Buggy code:
def main():
    s = input()
    n = int(input())
    pre = ''
    suf = ''
    r = 1
    for _ in range(n):
        q = input().split()
        if len(q) == 1:
            r *= -1
            continue
        if r * int(q[1]) in (1, -2):
            pre = q[2] + pre
        else:
            suf += q[2]
    ans = pre + s + suf
    if r == -1:
        ans = s[::-1]
    print(ans)


if __name__ == '__main__':
    main()


Fixed code:
def main():
    s = input()
    n = int(input())
    pre = ''
    suf = ''
    r = 1
    for _ in range(n):
        q = input().split()
        if len(q) == 1:
            r *= -1
            continue
        if r * int(q[1]) in (1, -2):
            pre = q[2] + pre
        else:
            suf += q[2]
    ans = pre + s + suf
    if r == -1:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    main()
Buggy code:
S=input()
Q=int(input())
query=[]
for _ in range(Q):
    query.append(input().split())

flag = True

for i in query:
    if i[0]=="1":
        flag = not flag
#        S=S[::-1]
    else:
        if flag:
            if i[1]=="1":
                S=i[2]+S
            else:
                S=S+i[2]
        else:
            if i[1]=="1":
                S=S+i[2][::-1]
            else:
                S=i[2][::-1]+S
if flag:
    print(S)
else:
    print(S[::-1]

Fixed code:
S=input()
Q=int(input())
query=[]
for _ in range(Q):
    query.append(input().split())

flag = True
t=""
u=""

for i in query:
    if i[0]=="1":
        u,t=t,u
        flag = not flag
#        S=S[::-1]
    else:
        if i[1]=="1":
            t+=i[2]
        else:
            u+=i[2]
if flag:
    print(t[::-1]+S+u)
else:
    print(t[::-1]+S[::-1]+u)
Buggy code:
S = input()
Q = int(input())

def add_car(S,s,is_left,is_rev):
    if is_rev:
        is_left = not(is_left)
    if is_left:
        S = ''.join([s,S])
    else:
        S = ''.join([S,s])
    return S

is_rev = False
for q in range(Q):
    x = input().split(' ')
    if len(x) == 1:
        is_rev = not is_rev
    else:
        S = add_car(S,x[2],x[1]=='1',is_rev)

if is_rev:
    S = list(reversed(S))
print(S)

Fixed code:
S = input()
S_h = ''
S_t = ''
Q = int(input())

def add_car(S_h,S_t,s,is_left,is_rev):
    if is_rev:
        is_left = not(is_left)
    if is_left:
        S_h = ''.join([S_h,s])
    else:
        S_t = ''.join([S_t,s])
    return S_h,S_t

is_rev = False
for q in range(Q):
    x = input().split(' ')
    if len(x) == 1:
        is_rev = not is_rev
    else:
        S_h,S_t = add_car(S_h,S_t,x[2],x[1]=='1',is_rev)

if is_rev:
    S_t = ''.join(list(reversed(S_t)))
    S = ''.join(list(reversed(S)))
    print(''.join([S_t,S,S_h]))
else:
    S_h = ''.join(list(reversed(S_h)))
    print(''.join([S_h,S,S_t]))
Buggy code:
s=input()
q=int(input())
que=[list(map(str,input().split())) for i in range(q)]
count=0
for i in que:
  if i[0]=="1":
    count+=1
ans=[[],[]]
    
for i in que:
  if i[0]=="1":
    count-=1
  else:
    if i[1]=="1":
      ans[count%2].append(i[2])
    else:
      ans[(count+1)%2].append(i[2])
print("".join(ans[0])+s+"".join(ans[1]))

Fixed code:
s=input()
q=int(input())
que=[list(map(str,input().split())) for i in range(q)]
count=0
for i in que:
  if i[0]=="1":
    count+=1
ans=[[],[]]
ak=count
for i in que:
  if i[0]=="1":
    count-=1
  else:
    if i[1]=="1":
      ans[count%2].append(i[2])
    else:
      ans[(count+1)%2].append(i[2])
if ak%2==0:
  print("".join(reversed(ans[0]))+s+"".join(ans[1]))
else:
  print("".join(reversed(ans[0]))+"".join(reversed(list(s)))+"".join(ans[1]))
Buggy code:
s=list(input())
top=bool()
t=[]
u=[]
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      t.insert(0,q[2])
    else:
      u.append(q[2])
s=t+s+u
if top:
  print(*list(reversed(s)),sep='')
else:
  print(s)


Fixed code:
s=list(input())
top=bool()
t=[]
u=[]
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      t.append(q[2])
    else:
      u.append(q[2])
s=list(reversed(t))+s+u
if top:
  print(*list(reversed(s)),sep='')
else:
  print(*s,sep='')
Buggy code:
from collections import deque
s = deque(input())
q = int(input())

cnt = 0
for _ in range(q):
    i = input().split()
    t = int(i[0])

    if t == 1:
        cnt += 1
    else:
        f = int(i[1])
        c = i[2]
        if (cnt % 2 == 0 and f == 1) or (cnt % 2 == 1 and f == 2):
            s.appendleft(i[2])
        else:
            s.append(i[2])
print(''.join(s) if cnt % 2 == 0 else ''.join(s[::-1]))

Fixed code:
from collections import deque
s = deque(input())
q = int(input())

cnt = 0
for _ in range(q):
    i = input().split()
    t = int(i[0])

    if t == 1:
        cnt += 1
    else:
        f = int(i[1])
        c = i[2]
        if (cnt % 2 == 0 and f == 1) or (cnt % 2 == 1 and f == 2):
            s.appendleft(i[2])
        else:
            s.append(i[2])

if cnt % 2 == 1:
    s.reverse()
print(''.join(s))
Buggy code:
s=input()
q=int(input())
que=[]
count=0
left=[]
right=[]
for i in range(q):
  qu=list(map(str,input().split()))
  if qu[0]=="1":
    count+=1
  que.append(qu)
han=0
for i in range(q):
  if que[i][0]=="1":
    han+=1
  else:
    if que[i][1]=="1":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
    elif que[i][1]=="2":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
ls="".join(left[::-1])
lr="".join(right)
print(ls+s+lr)

Fixed code:
s=input()
q=int(input())
que=[]
count=0
left=[]
right=[]
for i in range(q):
  qu=list(map(str,input().split()))
  if qu[0]=="1":
    count+=1
  que.append(qu)
han=0
for i in range(q):
  if que[i][0]=="1":
    han+=1
  else:
    if que[i][1]=="1":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
    elif que[i][1]=="2":  
      if (count-han)%2==0:
        right.append(que[i][2])
      else:
        left.append(que[i][2])
ls="".join(left[::-1])
rs="".join(right)
if count%2==0:
  print(ls+s+rs)
else:
  print(ls+s[::-1]+rs)
Buggy code:
import copy
import itertools

s = input()
q = int(input())
#s= list(input())

ct = 0
ls = []
rs = []
for i in range(q):
    qtmp = input().split()
    "qtmp = [2,1,'c']
    t = int(qtmp[0])
    if(t == 1):
        ct += 1
        ct = ct % 2
    else:
        f = int(qtmp[1])
        if(ct == f):
            rs.append( qtmp[2])
        else:
            ls.append( qtmp[2])
if(ct == 1):
    tmp = rs
    rs = ls
    ls = tmp
for i in range(len(ls)):
    print(ls[len(ls)-i-1], end='')
print(s, end='')
for i in range(len(rs)):
    print(rs[i], end='')
print("")

Fixed code:
import copy
import itertools

s = input()
q = int(input())
#s= list(input())

ct = 0
ls = []
rs = []
for i in range(q):
    qtmp = input().split()
    #qtmp = [2,1,'c']
    t = int(qtmp[0])
    if(t == 1):
        ct += 1
        ct = ct % 2
    else:
        f = int(qtmp[1])
        if(ct+1 == f):
            ls.append( qtmp[2])
        else:
            rs.append( qtmp[2])
if(ct == 1):
    tmp = rs
    rs = ls
    ls = tmp
    s = s[::-1]
for i in range(len(ls)):
    print(ls[len(ls)-i-1], end='')
print(s, end='')
for i in range(len(rs)):
    print(rs[i], end='')
print("")
Buggy code:
#import time
S = input()
Q = int(input())
f = True
for i in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        f = not f
    else:
        if (query[1] == '1' and f) or (query[1] == '0' and not f):
            S = query[2]+S
        else:
            S = S+query[2]

print(S)

# start = time.time()
# elapsed_time = time.time() - start
# print("elapsed_time:{0}".format(elapsed_time) + "[sec]")


Fixed code:
#import time
S = input()
Q = int(input())
f = True
head = []
tail = []
for i in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        f = not f
    else:
        if (query[1] == '1' and f) or (query[1] == '2' and not f):
            head.append(query[2])
        else:
            tail.append(query[2])

S = ''.join(head)[::-1]+S+''.join(tail)
if not f:
    S = S[::-1]
print(S)

# start = time.time()
# elapsed_time = time.time() - start
# print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
Buggy code:
s = input()
q = int(input())
t = []
 
def F(f,fr,ba,c):
  if f == '1':
     return c + fr, ba
  if f == '2':
    return fr, ba + c
 
def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

fr = ''
ba = ''
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr,ba = F(t[1], fr, ba,t[2])
 
    
if r == '1': 
   print(fr+s+ba)
else:
  print(fr+s+ba[::-1])

Fixed code:

s = input()
q = int(input())
t = []
 
def F(f,fr,ba,c):
  if f == '1':
     return c + fr, ba
  if f == '2':
    return fr,ba + c

def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

ans = s
fr = ''
ba = ''
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr, ba = F(t[1],fr,ba,t[2])
 
    
if r == '1': 
   print(fr + ans + ba)
else:
  print(ba[::-1] + ans[::-1]+ fr[::-1])