CodeEff/ECCO huggingface dataset:
# Original solution
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    a, *ret = sys.stdin.readline().rstrip()
    ret.insert(0, a)
    Q = int(sys.stdin.readline().rstrip())

    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            ret.reverse()
        elif line[0] == '2' and line[1] == '1':
            ret.insert(0, line[2])
        else:
            ret.append(line[2])

    print(("".join(ret)))


if __name__ == '__main__':
    main()


# Optimized solution
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    ret = sys.stdin.readline().rstrip()
    Q = int(sys.stdin.readline().rstrip())
    m = ""
    u = ""
    r = 0
    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            m, u = u, m
            r ^= 1
        elif line[0] == '2' and line[1] == '1':
            m += line[2]
        else:
            u += line[2]

    if r == 0:
        print((m[::-1] + ret[::1] + u))
    else:
        print((m[::-1] + ret[::-1] + u))


if __name__ == '__main__':
    main()
# Original solution
from collections import deque
S = deque(list(input().strip()))
Q = int(eval(input()))
cnt = 0
for _ in range(Q):
    q = list(input().split())
    if int(q[0])==1:
        cnt += 1
    else:
        if cnt%2==0:
            if int(q[1])==1:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if int(q[1])==1:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if cnt%2==0:
    print(("".join(S)))
else:
    x=""
    for i in range(len(S)-1,-1,-1):
        x += S[i]
    print(x)

# Optimized solution
from collections import deque
S = deque(list(input().strip()))
Q = int(eval(input()))
cnt = 0
for _ in range(Q):
    q = list(input().split())
    if int(q[0])==1:
        cnt += 1
    else:
        if cnt%2==0:
            if int(q[1])==1:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if int(q[1])==1:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if cnt%2==0:
    print(("".join(S)))
else:
    S.reverse()
    print(("".join(S)))
# Original solution
from collections import deque
S = deque(list(input().strip()))
Q = int(eval(input()))
cnt = 0
for i in range(Q):
    q = list(input().split())
    if len(q)==1:
        cnt += 1
    else:
        F = int(q[1])
        C = q[2]
        if cnt%2==0:
            if F==1:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F==1:
                S.append(C)
            else:
                S.appendleft(C)
if cnt%2==0:
    print(("".join(S)))
else:
    S = list(S)[::-1]
    print(("".join(S)))

# Optimized solution
from collections import deque
S = deque(list(input().strip()))
Q = int(eval(input()))
P = 0
for _ in range(Q):
    q = list(input().split())
    if q[0]=="1":
        P += 1
        P = P%2
    else:
        c = q[2]
        if q[1]=="1":
            if P==0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if P==0:
                S.append(c)
            else:
                S.appendleft(c)
if P==1:
    S = list(S)
    S = S[::-1]
print(("".join(S)))
# Original solution
from collections import deque
S = deque(list(input().strip()))
Q = int(eval(input()))
P = 0
for _ in range(Q):
    q = list(input().split())
    if q[0]=="1":
        P += 1
        P = P%2
    else:
        c = q[2]
        if q[1]=="1":
            if P==0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if P==0:
                S.append(c)
            else:
                S.appendleft(c)
if P==1:
    S = list(S)
    S = S[::-1]
print(("".join(S)))

# Optimized solution
S = input().strip()
Q = int(eval(input()))
ind = 1
x = []
y = []
for i in range(Q):
    q = list(input().split())
    if len(q)==1:
        ind = ind*(-1)
    else:
        if q[1]=="1":
            if ind>0:
                x.append(q[2])
            else:
                y.append(q[2])
        else:
            if ind>0:
                y.append(q[2])
            else:
                x.append(q[2])
if ind>0:
    x = x[::-1]
    S = "".join(x)+S+"".join(y)
else:
    S = S[::-1]
    y = y[::-1]
    S = "".join(y)+S+"".join(x)
print(S)
# Original solution
import sys
input = lambda: sys.stdin.readline().rstrip() 
from collections import deque

def resolve():
    S = input()
    Q = int(input())
    Query = [input().split() for _ in range(Q)]

    S_deque = deque()
    for s in S:
        S_deque.append(s)

    for q in Query:
        if q[0]=='1':
            S_deque.reverse()
        elif q[0]=='2':
            if q[1]=='1':
                S_deque.appendleft(q[2])
            elif q[1]=='2':
                S_deque.append(q[2])

    print(*S_deque, sep='')

if __name__ == '__main__':
    resolve()


# Optimized solution
import sys
input = lambda: sys.stdin.readline().rstrip() 
from collections import deque

def resolve():
    S = input()
    Q = int(input())
    Query = [input().split() for _ in range(Q)]

    S_deque = deque()
    for s in S:
        S_deque.append(s)
 
    is_reversed = False
    for q in Query:
        if q[0]=='1':
            is_reversed = not is_reversed
        elif q[0]=='2':
            if (not is_reversed and q[1]=='1') or (is_reversed and q[1]=='2'):
                S_deque.appendleft(q[2])
            else:
                S_deque.append(q[2])

    if is_reversed:
        S_deque.reverse()

    print(*S_deque, sep='')

if __name__ == '__main__':
    resolve()
# Original solution
from collections import deque


s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(eval(input()))
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        if f:
            f = False
        else:
            f = True
    else:
        f_i = int(q[1])
        if f_i == 1:
            if f:
                ans.appendleft(q[2])
            else:
                ans.append(q[2])
        else:
            if f:
                ans.append(q[2])
            else:
                ans.appendleft(q[2])
ans = list(ans)
if f:
    print(("".join(ans)))
else:
    print(("".join(ans[::-1])))


# Optimized solution
from collections import deque
import sys
input = sys.stdin.readline
s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(eval(input()))
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        f = not f
    else:
        f_i = int(q[1])
        if (f_i == 1) ^ f:
            ans.append(q[2])
        else:
            ans.appendleft(q[2])
ans = "".join(list(ans))
if not f:
    ans = ans[::-1]
print(ans)
# Original solution
from sys import stdin
from collections import deque

# not AC
# def get_result(data):
#     S, Q = data[0][0], int(data[1][0])
#     for i in range(Q):
#         if data[i+2][0] == '1':
#             S = S[::-1]
#         else:
#             F, C = data[i+2][1], data[i+2][2]
#             if F == '1':
#                 S = C + S
#             else:
#                 S = S + C
#     return S


def get_result(data):
    S, Q = data[0][0], int(data[1][0])
    # 文字列と反転状況を別で判断する
    ans = deque(S)
    cnt = 0
    for i in range(Q):
        if data[i+2][0] == '1':
            cnt += 1
        else:
            F, C = data[i+2][1], data[i+2][2]
            if F == '1':
                if cnt % 2 == 0:
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if cnt % 2 == 0:
                    ans.append(C)
                else:
                    ans.appendleft(C)

    if cnt % 2 == 1:
        ans = ''.join(reversed(ans))
    else:
        ans = ''.join(ans)

    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)


# Optimized solution
from sys import stdin
from collections import deque

# not AC
# def get_result(data):
#     S, Q = data[0][0], int(data[1][0])
#     for i in range(Q):
#         if data[i+2][0] == '1':
#             S = S[::-1]
#         else:
#             F, C = data[i+2][1], data[i+2][2]
#             if F == '1':
#                 S = C + S
#             else:
#                 S = S + C
#     return S


def get_result(data):
    S, Q = data[0][0], int(data[1][0])
    # 文字列と反転状況を別で判断する
    ans = deque(S)
    cnt = 0
    for i in range(Q):
        if data[i+2][0] == '1':
            cnt += 1
        else:
            F, C = data[i+2][1], data[i+2][2]
            if F == '1':
                if cnt % 2 == 0:
                    # 前に追加
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if cnt % 2 == 0:
                    ans.append(C)
                else:
                    ans.appendleft(C)

    ans = ''.join(ans)
    if cnt % 2 == 1:
        ans = ans[::-1]

    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)
# Original solution

import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def S(): return input().rstrip()
def LIR(n): return [S().split() for i in range(n)]

def main():
    s = S()
    Q = I()
    Qu = LIR(Q)
    mode = 0; left = ''; right =  ''
    for que in Qu:
        if que[0] == '1': mode = (mode+1)%2
        else:
            if mode:
                if que[1] == '1': s = ''.join([s, que[2]])
                else: s = ''.join([que[2], s])
            else:
                if que[1] == '1': s = ''.join([que[2], s])
                else: s = ''.join([s, que[2]])
    if mode: print((s[::-1]))
    else: print(s)

if __name__ == '__main__':
    main()


# Optimized solution
import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict, deque
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def S(): return input().rstrip()
def LIR(n): return [S().split() for i in range(n)]

def main():
    s = S(); Q = I()
    mode = 2
    d = deque(s)

    for i in range(Q):
        query = S()
        if query[0] == "1": mode = 3 - mode
        else:
            t, f, c = list(map(str, query.split()))
            if (int(f) + mode)% 2  == 0: d.append(c)
            else: d.appendleft(c)
    if mode == 2: print((''.join(d)))
    else: print((''.join(list(d)[::-1])))

if __name__ == '__main__':
    main()
# Original solution
import sys
from collections import deque

s = list(sys.stdin.readline().rstrip())
q = int(sys.stdin.readline())
s = deque(s)

for _ in range(q):
    query = sys.stdin.readline().split()
    if len(query) == 1:
        s.reverse()
    elif query[1] == '1':
        s.appendleft(query[2])
    else:
        s.append(query[2])

print((''.join(s)))


# Optimized solution
import sys
from collections import deque

s = list(sys.stdin.readline().rstrip())
q = int(sys.stdin.readline())
head = 0
s = deque(s)

for _ in range(q):
    query = sys.stdin.readline().split()
    if len(query) == 1:
        head = 1 if head == 0 else 0
    elif (query[1] == '1' and head == 1) or (query[1] == '2' and head == 0):
        s.append(query[2])
    else:
        s.appendleft(query[2])

if head == 1:
    s.reverse()

print((''.join(s)))
# Original solution
s,_,*q=open(0)
a=['','']
p=1
for q in q:
  p^=q[0]<'2'
  a[(q<'2 2')^p]+=q[-2]*(q[0]>'1')
print((a[p^1][::-1]+s[:-1][::p or-1]+a[p]))

# Optimized solution
s,_,*q=open(0)
a=['','']
p=1
for q in q:
  p^=q<'2'
  a[(q<'2 2')^p]+=q[-2]*(q>'2')
print((a[p^1][::-1]+s[:-1][::p or-1]+a[p]))
# Original solution
s,_,*q=open(0)
a=s[:-1]
p=1
for q in q:
  try:
    q,f,c=q.split()
    if(f<'2')^p:a+=c
    else:a=c+a
  except:p^=1
print((a[::p or-1]))

# Optimized solution
s,_,*q=open(0)
a='%s'
p=1
for q in q:
  try:
    q,f,c=q.split()
    if(f<'2')^p:a+=c
    else:a=c+a
  except:p^=1
print(((a%s[:-1])[::p or-1]))
# Original solution
from collections import deque
s = deque(list(input()))
q = int(input())
f = 0
for _ in range(q):
  l = input()
  if l == "1":
    f ^= 1
  else:
    a, b, c = l.split()
    if f ^ (b=="1"):
      s.appendleft(c)
    else:
      s.append(c)
if f:
  ans = list(s)[::-1]
else:
  ans = list(s)
print(*ans, sep="")


# Optimized solution
from collections import deque
def main():
  s = deque(list(input()))
  q = int(input())
  f = 0
  for _ in range(q):
    l = input()
    if l == "1":
      f ^= 1
    else:
      a, b, c = l.split()
      if f ^ (b=="1"):
        s.appendleft(c)
      else:
        s.append(c)
  if f:
    ans = list(s)[::-1]
  else:
    ans = list(s)
  print(*ans, sep="")
if __name__ == "__main__":
  main()
# Original solution
s = list(input())
n = int(input())
re = 1

for i in range(n):
    q = input().split()
    if q[0] == '1':
        re = re * -1
    else:
        if re == 1:
            if q[1] == '1':
                s.insert(0, q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == '1':
                s.append(q[2])
            else:
                s.insert(0, q[2])

print(''.join(s)) if re == 1 else print(''.join(s[::-1]))


# Optimized solution
s = input()
n = int(input())
re = 1
head = ''
tail = ''

for i in range(n):
    q = input().split()
    if q[0] == '1':
        re = re * -1
    else:
        if re == 1:
            if q[1] == '1':
                head = q[2] + head
            else:
                tail = tail + q[2]
        else:
            if q[1] == '1':
                tail = tail + q[2]
            else:
                head = q[2] + head

print(head+s+tail) if re == 1 else print((head+s+tail)[::-1])
# Original solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools

s = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())

for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        s = ''.join(list(reversed(s)))
    else:
        if q[1] == "1": s = q[2] + s
        if q[1] == "2": s = s + q[2]

print (s)


# Optimized solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools

s = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())

flag = 0
for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        flag = (flag + 1) % 2
    else:
        if (q[1] == "1" and flag == 0) or (q[1] == "2" and flag == 1):
            s = q[2] + s
        else:
            s = s + q[2]

if flag == 1: s = ''.join(list(reversed(s)))
print (s)
# Original solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools

s = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())

flag = 0
prefix = []
suffix = []

for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        flag = (flag + 1) % 2
    else:
        if (q[1] == "1" and flag == 0) or (q[1] == "2" and flag == 1):
            prefix.insert(0,q[2])
        else:
            suffix.append(q[2])

s = ''.join(prefix) + s + ''.join(suffix)

if flag == 1: s = ''.join(list(reversed(s)))
print (s)


# Optimized solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

s = deque(stdin.readline().rstrip())
n = int(stdin.readline().rstrip())

flag = 0

for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

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

if flag == 1:
    s = ''.join(s)[::-1]
else:
    s = ''.join(s)
print (s)
# Original solution
from collections import deque

s = deque(list(map(str, input().rstrip())))
q = int(eval(input()))

for _ in range(q):
    li = list(map(str, input().split()))
    if li[0] == "1":
        s.reverse()
    elif li[1] == "1":
        s.appendleft(li[2])
    else:
        s.append(li[2])

print(("".join(s)))

# Optimized solution
from collections import deque

s = deque(list(map(str, input().rstrip())))
q = int(eval(input()))

state = 1
for _ in range(q):
    li = list(map(str, input().split()))
    if li[0] == "1":
        state *= -1
    elif (li[1] == "1" and state == 1) or (li[1] == "2" and state == -1):
        s.appendleft(li[2])
    else:
        s.append(li[2])

if state == -1:
    s.reverse()

print(("".join(s)))
# Original solution
#!/usr/bin/env python3
import sys

input = sys.stdin.readline


def ST():
    return input().rstrip()


def I():
    return int(eval(input()))


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


def LI():
    return list(MI())


S = ST()
Q = I()

for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        S = S[::-1]
    else:
        if query[1] == "1":
            S = query[2] + S
        else:
            S = S + query[2]

print(S)


# Optimized solution
#!/usr/bin/env python3
import sys

input = sys.stdin.readline


def ST():
    return input().rstrip()


def I():
    return int(eval(input()))


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


def LI():
    return list(MI())


S = ST()
Q = I()

switch = True
prefix = ""
suffix = ""
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        if switch:
            switch = False
        else:
            switch = True
    else:
        if switch:
            if query[1] == "1":
                prefix = query[2] + prefix
            else:
                suffix = suffix + query[2]
        else:
            if query[1] == "1":
                suffix = suffix + query[2]
            else:
                prefix = query[2] + prefix

ans = prefix + S + suffix
if switch:
    print(ans)
else:
    print((ans[::-1]))
# Original solution
S = list(input().rstrip())
Q = int(input().rstrip())


q = []
for n in range(Q):
  q.append(list(input().rstrip().split()))

#print(q)
def rev(olist):
  nlist  = olist[::-1]
  return nlist

def addhead(olist,ele):
  nlist = olist.insert(0,ele)
  return nlist
  
def addtail(olist,ele):
  nlist = olist.append(ele)
  return nlist

def rev2(flg):
  if flg == 0:
    flg = 1
  else:
    flg = 0
  return flg

def add(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head.insert(0,ele)
  elif j == '1' and flg == 1:
    tail.append(ele)
  elif j == '2' and flg == 0:
    tail.append(ele)
  elif j == '2' and flg == 1:
    head.insert(0,ele)

flg = 0
head = []
tail = []

for n in range(Q):
  
  if q[n][0] == '1':
    flg = rev2(flg)
  
  else:
    add(S,q[n][2],q[n][1],flg,head,tail)

ans = head + S + tail

if flg == 1:
  ans = rev(ans)
  
print((''.join(ans)))



# Optimized solution
S = input().rstrip()
Q = int(input().rstrip())


q = []
for n in range(Q):
  q.append(list(input().rstrip().split()))

#print(q)
def rev(olist):
  nlist  = olist[::-1]
  return nlist

def addhead(olist,ele):
  nlist = olist.insert(0,ele)
  return nlist
  
def addtail(olist,ele):
  nlist = olist.append(ele)
  return nlist

def rev2(flg):
  if flg == 0:
    flg = 1
  else:
    flg = 0
  return flg

def add(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head.insert(0,ele)
  elif j == '1' and flg == 1:
    tail.append(ele)
  elif j == '2' and flg == 0:
    tail.append(ele)
  elif j == '2' and flg == 1:
    head.insert(0,ele)
  
def add2(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head = ele + head
  elif j == '1' and flg == 1:
    tail = tail + ele
  elif j == '2' and flg == 0:
    tail = tail + ele
  elif j == '2' and flg == 1:
    head = ele + head
  return head,tail
flg = 0
#head = []
#tail = []
head=''
tail = ''
for n in range(Q):
  
  if q[n][0] == '1':
    flg = rev2(flg)
  
  else:
    head,tail = add2(S,q[n][2],q[n][1],flg,head,tail)


ans = head + S + tail

if flg == 1:
  ans1 = rev(list(ans))
  print((''.join(ans1)))
else:
  print(ans)
# Original solution
S = input().rstrip()
Q = int(input().rstrip())
a = -1
for _ in range(Q):
    stdin = input().rstrip()
    if int(stdin[0]) == 1:
        a = ~a
    else:
        T,F,C = stdin.split()
        if int(F) == 2+a:
            S = C + S
        else:
            S = S + C
if a == 0:
    S = S[::-1]
print(S)

# Optimized solution
S = input().rstrip()
Q = int(input().rstrip())
a = -1
before =[]
after =[]
for _ in range(Q):
    stdin = input().rstrip()
    if int(stdin[0]) == 1:
        a = ~a
    else:
        T,F,C = stdin.split()
        if int(F) == 2+a:
            before.append(C)
        else:
            after.append(C)
S = ''.join(before)[::-1] + S + ''.join(after)
if a == 0:
    S = S[::-1]
print(S)
# Original solution
word = input().rstrip()
n = int(eval(input()))
hanten = 0
lhanten = ""
r = ""
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        hanten = (hanten + 1) % 2
        # word = word[::-1]
        # print(word)
    else:
        if (a[1] == "2" and hanten == 0) or (a[1] == "1" and hanten == 1):
            r += a[2]
            # print("rtuika")
        else:
            lhanten += a[2]
            # print("ltuika")

if hanten == 0:
    print((lhanten[::-1] + word + r))
else:
    print((r[::-1] + word[::-1] + lhanten))

# Optimized solution
word = input().rstrip()
n = int(eval(input()))
hanten = 0
lhantenlist = []
rlist = []
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        hanten = (hanten + 1) % 2
        # word = word[::-1]
        #print(word)
    else:
        if (a[1] == "2" and hanten == 0) or (a[1] == "1" and hanten == 1):
            rlist.append(a[2])
            #print("rtuika: ",rlist)
        else:
            lhantenlist.append(a[2])
            #print("ltuika: ",lhantenlist)

lhanten="".join(lhantenlist)
r="".join(rlist)

if hanten == 0:
    anslist=[lhanten[::-1],word,r]
    ans="".join(anslist)
else:
    anslist = [r[::-1], word[::-1], lhanten]
    ans="".join(anslist)
print(ans)