Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
Ss = S
for i in range(Q) :
    Query = input()
    q = Query.split(" ")
    if int(q[0]) == 1 :
        Ss = Ss[::-1]
    else:
        if int(q[1]) == 1 :
            Ss = str(q[2]) + Ss
        else:
            Ss = Ss + str(q[2])
print(Ss)

Fixed submission:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
Ss = S
q = []
for _ in range(Q):
    q.append(list(map(str,input().rsplit())))
for i in range(Q) :
    if int(q[i][0]) == 1 :
        Ss = Ss[::-1]
    else:
        if int(q[i][1]) == 1 :
            Ss = str(q[i][2]) + Ss
        else:
            Ss = Ss + str(q[i][2])
print(Ss)

Fixed submission:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Buggy python submission (verdict: Runtime Error):
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    while q > 0:
        t = list(input().strip().split())
        if t[0] == 1:
            re = not re
        else:
            if re:
                if t[1] == 2:
                    l = t[2] + l
                else:
                    r = r + t[2]
            else:
                if t[1] == 2:
                    l = t[2] + l
                else:
                    r = r + t[2]
        q -= 1
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

Fixed submission:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Buggy python submission (verdict: Time Limit Exceeded):
def string_formation():
    s = str(input().strip())
    q = int(input())
    flag = 0
    for _ in range(q):
        line = input()
        if len(line) == 1:
            flag = (flag + 1) % 2
        else:
            if (line[2] == '1' and flag == 1) \
            or (line[2] == '2' and flag == 0):
                s += line[-1]
            else:
                s = line[-1] + s
    if flag: return ''.join(reversed(s))

    return s
print(string_formation())

Fixed submission:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Buggy python submission (verdict: Time Limit Exceeded):
def string_formation():
    s = str(input().strip())
    q = int(input())
    for _ in range(q):
        line = input().strip().split()
        op = int(line[0])
        if op == 1:
            s = ''.join(reversed(s))
        elif op == 2:
            pos = int(line[1])
            if pos == 2:
                s += line[2]
            elif pos == 1:
                s = line[2] + s
    return s

print(string_formation())

Fixed submission:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
Q=int(input())
case=0
for i in range(Q):
    temp=input().split()
    if len(temp)==1:
        case=1-case	
    else:
        if int(temp[1])==1:
            if case==0:
                S=temp[2]+S
            else:
                S=S+temp[2]

        else:
            if case==1:
                S=temp[2]+S
            else:
                S=S+temp[2]
if case==1:
    S=S[::-1]

print(S)



Fixed submission:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
Q=int(input())
for i in range(Q):
    temp=input().split()
    if len(temp)==1:
        S=S[::-1]
    else:
        if int(temp[1])==1:
            S="".join([temp[2],S])

        if int(temp[1])==2:
            S="".join([S,temp[2]])
			
print(S)


Fixed submission:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)