Problem:
 Score : 400 points    Problem Statement Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i  (1 \leq i \leq Q) , an integer T_i is provided, which means the following:    If T_i = 1 : reverse the string S .    If T_i = 2 : An integer F_i and a lowercase English letter C_i are additionally provided.   If F_i = 1 : Add C_i to the beginning of the string S .  If F_i = 2 : Add C_i to the end of the string S .     Help Takahashi by finding the final string that results from the procedure.      Constraints  1 \leq |S| \leq 10^5  S consists of lowercase English letters.  1 \leq Q \leq 2 \times 10^5  T_i = 1 or 2 .  F_i = 1 or 2 , if provided.  C_i is a lowercase English letter, if provided.         Input Input is given from Standard Input in the following format:  S  Q  Query_1  :  Query_Q   In the 3 -rd through the (Q+2) -th lines, Query_i is one of the following:  1   which means T_i = 1 , and:  2  F_i  C_i   which means T_i = 2 .      Output Print the resulting string.        Sample Input 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 cpa  There will be Q = 4 operations. Initially, S is a .    Operation 1 : Add p at the beginning of S . S becomes pa .    Operation 2 : Reverse S . S becomes ap .    Operation 3 : Add c at the end of S . S becomes apc .    Operation 4 : Reverse S . S becomes cpa .    Thus, the resulting string is cpa .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

Solution (Python):
import sys
input = sys.stdin.readline

from collections import deque


S = deque(input().strip())
q = int(input())

cnt = 0
for _ in range(q):
    query = input().strip()
    if query == "1":
        cnt += 1
    else:
        t, f, c = query.split()
        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)
ans = "".join(S)

print(ans if cnt % 2 == 0 else ans[::-1])

Solution (Python):
s=input()
q=int(input())
f=""
b=""
r=0
for i in range(q):
  query=input().split()
  if int(query[0])==1:
    r=1-r
  else:
    if (r^(int(query[1])-1))==0:
      f+=query[2]
    else:
      b+=query[2]
ret=f[::-1]+s+b
if r:
  ret=ret[::-1]
print(ret)

Solution (Python):
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))

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    
    s = str(input().rstrip())
    q =int(input())
    ten = 0
    r = []
    l = []
    la = l.append
    ra =r.append
    for _ in range(q):
        ls = tuple(map(str, sys.stdin.readline().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    la(ls[2])
                else:
                    ra(ls[2])
            else:
                if ten%2 ==1:
                    la(ls[2])
                else:
                    ra(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    s = str(input().rstrip())
    q =int(input())
    ten = 0
    r = []
    l = []
    la = l.append
    ra =r.append
    for _ in range(q):
        ls = tuple(map(str, sys.stdin.readline().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    la(ls[2])
                else:
                    ra(ls[2])
            else:
                if ten%2 ==1:
                    la(ls[2])
                else:
                    ra(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    s = str(input().rstrip())
    q =int(input())
    ten = 0
    r = []
    l = []
    la = l.append
    ra =r.append
    for _ in range(q):
        ls = tuple(map(str, sys.stdin.readline().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    la(ls[2])
                else:
                    ra(ls[2])
            else:
                if ten%2 ==1:
                    la(ls[2])
                else:
                    ra(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    s = input()
    q =int(input())
    ten = 0
    r = []
    l = []
    for _ in range(q):
        ls = tuple(map(str, sys.stdin.readline().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()

Solution (Python):
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()

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

def main():
    s = input()
    q = int(input())
    bottom = ''
    top = ''
    swapped = 0
    for _ in range(q):
        query = input()
        if len(query) == 1:
            swapped = 1 - swapped
            continue
        _, f, c = query.split()
        if swapped:
            if f == '1':
                bottom += c
            else:
                top += c
        else:
            if f == '1':
                top += c
            else:
                bottom += c

    if swapped:
        print(bottom[::-1] + s[::-1] + top)
    else:
        print(top[::-1]+s+bottom)


if __name__ == '__main__':
    main()

Solution (Python):
s=input()
q=int(input())
cnt=0
front=[]
rear=[]
for qq in range(q):
  l=list(input().split())
  if l[0]=='1':
    cnt+=1
  else:
    if (cnt+int(l[1]))%2==1:
      front.append(l[2])
    else:
      rear.append(l[2])
front=''.join(front[::-1])
rear=''.join(rear)
s=front+s+rear
print(s if cnt%2==0 else s[::-1])

Solution (Python):
import sys
input = sys.stdin.readline
import collections




def main():
    s = input().strip()
    q = int(input())
    flg = True
    deq = collections.deque(s)
    for i in range(q):
        t = input_list_str()
        if len(t) == 1:
            n = t[0]
            flg = False if flg is True else True
        else:
            n, f, c = t
            c = str(c).strip()
            if f == '1':
                if flg:
                    deq.appendleft(c)
                else:
                    deq.append(c)
            else:
                if flg:
                    deq.append(c)
                else:
                    deq.appendleft(c)
    if flg is False:
        deq.reverse()
    print(''.join(deq))


def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
q = int(input())
count = 0
a = []
b = []

for _ in range(q):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        count += 1
    else:
        if (int(Q[1])+count)%2 == 1:
            a.append(Q[2])
        else:
            b.append(Q[2])
    
        
a = "".join(a[::-1])
b = "".join(b)
s = a + s + b
if count%2 == 1 and count != 0:
    s = s[::-1]
print("".join(s))

Solution (Python):
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
q = INT()
p = [0]*q
flag = 0
c0 = c1 = ''

for i in range(q):
        p[i] = input()
        if p[i][0] == '1':
                flag = 1 - flag
        else:
                t, f, c = p[i].split()
                if (f == '1' and flag == 0) or (f == '2' and flag == 1):
                        c0 = c + c0
                else:
                        c1 = c1 + c

s = c0 + s + c1
print(s if flag == 0 else s[::-1])

Solution (Python):
s = ["", input()]
q = int(input())

for _ in range(q):
  que = input().split()
  if que[0] == "1":
    s = s[::-1]
  else:
    s[int(que[1])-1] += que[2]

print("".join(list(s[0])[::-1]) + s[1])

Solution (Python):
S = input()
Q = int(input())

prefix = ''
surfix = ''
cnt_reverse = 0

for i in range(Q):
    q = input()
    if q == '1':
        cnt_reverse += 1
    else:
        _, f, c = q.split()
        if (f=='1' and cnt_reverse%2==0):
            prefix = c + prefix
        elif (f=='2' and cnt_reverse%2==1):
            prefix = c + prefix
        elif (f=='1' and cnt_reverse%2==0):
            surfix = surfix + c
        else:
            surfix = surfix + c
if cnt_reverse % 2 == 0:
    S = prefix + S + surfix
else:
    S = surfix[::-1] + S[::-1] + prefix[::-1]
print(S)

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())

isR = -1
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        isR *= -1
    else:
        f = int(line[1])
        if isR + f == 0 or isR + f == 3:
            s.appendleft(line[2])
        else:
            s.append(line[2])
if isR == 1:
        s.reverse()
print("".join(s))

Solution (Python):
from collections import deque

S = list(input())
Q = int(input())

S = deque(S)
rev = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        rev *= -1
    else:
        T, F, C = query.split()
        if rev == 1:
            if F == "1":
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F == "1":
                S.append(C)
            else:
                S.appendleft(C)

if rev == -1:
    S = reversed(S)

print("".join(S))

Solution (Python):
S = input()
Q = int(input())
reverse = 0
left = ""
right = ""
for _ in range(Q):
    q = input().split()
    if q[0]=="1":
        reverse += 1
        left, right = right, left
    if q[0]=="2" and q[1]=="1":
        if reverse%2==0: left = q[2]+left
        else: left = left+q[2]
    if q[0]=="2" and q[1]=="2":
        if reverse%2==0: right = right+q[2]
        else: right = q[2]+right
if reverse%2==1:
    left = left[::-1]
    right = right[::-1]
    S = S[::-1]
print(left+S+right)

Solution (Python):
from collections import deque


S = deque(input())
Q = int(input())
is_reversed = False

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        is_reversed = not is_reversed
    else:
        F, C = int(Query[1]), Query[2]
        if is_reversed:
            F = 3 - F
        if F == 1:
                S.appendleft(C)
        else:
                S.append(C)

if is_reversed:
    S.reverse()
print("".join(S))

Solution (Python):
from collections import deque


S = deque(input())
Q = int(input())
reverse_count = 0

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        reverse_count += 1
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            if reverse_count % 2 == 0:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if reverse_count % 2 == 0:
                S.append(C)
            else:
                S.appendleft(C)

if reverse_count % 2 == 1:
    S.reverse()
print("".join(S))

Solution (Python):
from collections import deque
s = input()
q = int(input())
d = deque()
for i in s:
  d.append(i)
rv = 0
lst = [1, 0]
for i in range(q):
  z = input()
  if z[0] == '1':
    rv = lst[rv]
  elif z[2] == '1':
    if rv == 1:
      d.append(z[4])
    else:
      d.appendleft(z[4])
  else:
    if rv == 0:
      d.append(z[4])
    else:
      d.appendleft(z[4])
if rv == 1:
  print(''.join(d)[::-1])
else:
  print(''.join(d))

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S.appendleft(query[4])
            else:
                S.append(query[4])
        else:
            if reverse_index == -1:
                S.append(query[4])
            else:
                S.appendleft(query[4])

if reverse_index == 1:
    S.reverse()
    print(*S, sep='')
else:
    print(*S, sep='')

Solution (Python):
s = input()
q = int(input())
ans = ''
ans1 = ''
ans2 = ''
zengo = 1 
front = []
back = []
for i in range(q):
    a = list(input().split())
    if a[0] == '1':
        zengo *= -1
    else:
        if a[1] == '1':
            if zengo == 1:
                front.append(a[2])
            else:
                back.append(a[2])
        else:
            if zengo == 1:
                back.append(a[2])
            else:
                front.append(a[2])
front = list(reversed(front))
for i in range(len(front)):
    ans1 += front[i]
for i in range(len(back)):
    ans2 += back[i]
    
ans = ans1 + s + ans2
if zengo == -1:
    ans = ans[::-1]
print(ans)

Solution (Python):
import sys
from io import StringIO
import unittest
import os
from collections import deque


sys.setrecursionlimit(999999999)



def resolve(test_def_name=""):
    que = deque()
    que.extend(list(input()))
    q = int(input())
    query_s = [list(input().split()) for i in range(q)]

    isReverse = False
    for query in query_s:
        if query[0] == "1":
            isReverse = True if not isReverse else False
        else:
            
            if (isReverse is False and query[1] == "1") or (isReverse and query[1] == "2"):
                que.appendleft(query[2])
            
            else:
                que.append(query[2])

    ans = ""
    if isReverse:
        ans = "".join(list(reversed(que)))
    else:
        ans = "".join(que)

    print(ans.replace("\r", ""))









class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

Solution (Python):
S = list(input())
Q = int(input())
QL = []

for _ in range(Q):
    QL.append(input())

inversed = False
invc = 0

prefix = []
suffix = []

for query in reversed(QL):
    
    if query == '1':
        
        inversed = not inversed
        invc += 1
    
    else:
        
        
        __, f, c = query.split()

        if f == '1':
            if inversed:
                suffix.append(c)
            else:
                prefix.append(c)
        else:
            if not inversed:
                suffix.append(c)
            else:
                prefix.append(c)




if inversed:
    S = list(reversed(S))

print("".join(prefix) + "".join(S) + "".join(list(reversed(suffix))))

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
query = [list(input().replace(' ','')) for _ in range(q)]

r = False
for i in range(q):
    if r:
        if len(query[i]) == 1:
            r = False
        elif query[i][1] == '1':
            s.append(query[i][2])
        else:
            s.appendleft(query[i][2])
    else:
        if len(query[i]) == 1:
            r = True
        elif query[i][1] == '1':
            s.appendleft(query[i][2])
        else:
            s.append(query[i][2])

if r:
    s = reversed(s)
    t = ''.join(s)
    print(t)
else:
    t = ''.join(s)
    print(t)

Solution (Python):
S = input()
Q = int(input())

rev = False
l, r = "", ""

for _ in range(Q):
    query = input()
    if query[0] == "1":
        rev = not rev
    else:
        T, F, C = query.split()
        if F == "1":
            if rev:
                r += C
            else:
                l = C + l
        else:
            if rev:
                l = C + l
            else:
                r += C

res = l + S + r
if rev:
    res = res[::-1]

print(res)

Solution (Python):
import collections
S = collections.deque(list(input()))
Q=int(input())

q=[0]*Q
turn=0
houkou=['']*2
for i in range(Q):
    q[i]=input()

    if(q[i][0]=='1'):
        turn=not turn

    else:
        if(turn==0):
            if(q[i][2]=='1'):
                S.appendleft(q[i][4])

            else:
                S.append(q[i][4])
        else:
            if(q[i][2]=='1'):
                S.append(q[i][4])

            else:
                S.appendleft(q[i][4])



if(turn==0):
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Solution (Python):

"""
Created on Sun Jun 28 12:53:41 2020

@author: reira
"""

S = input()
Q = int(input())
flag = 0
l = ""
r = ""
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag = (flag+1)%2
    else:
        if (int(q[1]) + flag)%2:
            l += q[2]
        else:
            r += q[2]
if flag:
    print(r[::-1]+S[::-1]+l)
else:
    print(l[::-1]+S+r)

Solution (Python):


import sys
input=sys.stdin.readline

def main():
    S=input().strip("\n")
    Q=int(input())
    sign=True
    head=[]
    tail=[]
    for i in range(Q):
        q=tuple(input().split())
        if q[0]=="1":
            sign^=True
        else:
            if (q[1]=="1" and sign==True) or (q[1]=="2" and sign==False):
                head.append(q[2])
            else:
                tail.append(q[2])
    if sign==True:
        print("".join(head[::-1])+S+"".join(tail))
    else:
        print("".join(tail[::-1])+S[::-1]+"".join(head))
    
if __name__=="__main__":
    main()

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
cur = 0
for i in range(q):
    ls = list(map(str, input().split()))
    if len(ls) == 1:
        cur += 1
    else:
        if ls[1] == '1':
            if cur%2 == 0:
                s.appendleft(ls[2])
            else:
                s.append(ls[2])
        else:
            if cur%2 == 0:
                s.append(ls[2])
            else:
                s.appendleft(ls[2])
if cur%2 == 0:
    print(*s, sep='')
else:
    print(*list(s)[::-1], sep='')

Solution (Python):
S=input()
Q=int(input())
reverse=False
front=""
rear=""
for _ in range(Q):
    inp=input()
    if inp[0]=="1":
        reverse= not reverse
    elif inp[0]=="2":
        if (inp[2]=="1" and not reverse) or (inp[2]=="2" and reverse):
            front=inp[4]+front
        else:
            rear=rear+inp[4]
ans=front+S+rear
if reverse:
    ans=ans[::-1]
print(ans)

Solution (Python):
s = input()
q = int(input())
 
head = []
tail = []
flip = 0
 
for i in range(q):
    query = input().split()
    if query[0] == "1":
        flip ^=1
    else:
        f = int(query[1]) - 1
        c = query[2]
        if (f ^ flip) == 0:
            head.append(c)
        else:
            tail.append(c)
 
if flip:
    head, tail = tail, head
    s = s[::-1]
 
head = "".join(head)
tail = "".join(tail)
 
print(head[::-1] + s + tail)

Solution (Python):
from collections import deque
from itertools import islice

S = input()

result = deque(S)


def input_str(line):
    r = line.split()
    if len(r) == 1:
        return int(r[0]), None, None
    else:
        return int(r[0]), int(r[1]), r[2]


reverse_flag = False

for _ in range(int(input())):
    Q, F, C = input_str(input())
    if Q == 1:
        reverse_flag ^= True
    if Q == 2:
        if reverse_flag:
            if F == 1:
                result.append(C)
            else:
                result.appendleft(C)
        else:
            if F == 1:
                result.appendleft(C)
            else:
                result.append(C)

if reverse_flag:
    result.reverse()
print(''.join(result))

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
reverse = 0
Tlist = []
for i in range(Q):
    Tlist.append(list(input().split()))
    if Tlist[i][0] == str(2):
        if (int(Tlist[i][1])+reverse) % 2 == 0:
            S.append(Tlist[i][2])
        else:
            S.appendleft(Tlist[i][2])
    else:
        if reverse == 1:
            reverse = 0
        else:
            reverse = 1
if reverse == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Solution (Python):
from collections import deque

def reversedeq(deq):
    newdeq = deque()
    for _ in range(len(deq)):
        c = deq.pop()
        newdeq.append(c)
    
    return newdeq


S = deque(list(input()))
Q = int(input())
reverse_state = False

for _ in range(Q):
    Query = [x for x in input().split()]
    if len(Query) == 1:
        reverse_state = reverse_state ^ 1
    else:
        if reverse_state:
            if Query[1] == '1':
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
        else:
            if Query[1] == '1':
                S.appendleft(Query[2])
            else:
                S.append(Query[2])

if reverse_state:
    S = reversedeq(S)

print("".join(S))

Solution (Python):
S=input()
q=int(input())
s=[]
r=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0 or int(qv[1])^t==2:
            r.append(qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    ss=s.copy()
    s=[]
    for j in range(len(ss)):
        s.append(ss[-1-j])
    ans=s
    ans.extend(r)
else:
    rr=r.copy()
    r=[]
    for j in range(len(rr)):
        r.append(rr[-1-j])
    ans=r
    ans.extend(s)
answer=''.join(ans)
print(answer)

Solution (Python):
from collections import deque
s = input()
Q = int(input())
que = deque(s)
cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == '1':
        cnt += 1
    else:
        reverse = cnt % 2
        if q[1] == '1':
            if reverse == 0:
                s = que.appendleft(q[2])
            else:
                s = que.append(q[2])
        else:
            if reverse == 0:
                s = que.append(q[2])
            else:
                s = que.appendleft(q[2])

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

Solution (Python):
from collections import deque
S = input()
Q = int(input())
t = []
f = []
c = []
for i in range(Q):
    in_ = list(input().split())
    if in_[0] == "1":
        t.append(1)
        f.append(0)
        c.append(0)
    else:
        t.append(2)
        f.append(int(in_[1]))
        c.append(in_[2])
acc = [0 for _ in range(Q)]
if t[-1] == 1:  acc[-1] = 1
for i in reversed(range(Q - 1)):
    if t[i] == 1:
        acc[i] = acc[i + 1] + 1
    else:
        acc[i] = acc[i + 1]
        
if acc[0] % 2 == 0:
    ans = deque(S)
else:
    ans = deque(S[::-1])
    
for i in range(Q):
    if t[i] == 2:
        if (f[i] + acc[i]) % 2 == 1:
            ans.appendleft(c[i])
        else:
            ans.append(c[i])
print("".join(ans))

Solution (Python):
s = input()

x = ""
y = ""

Q = int(input())

rev = 0
for i in range(Q):
    q = list(input().split())
    
    if q[0] == "2":
        q[1] = int(q[1])
        q[1] -= 1 
        
        if rev:
            q[1] = 1 - q[1]
        
        if q[1] == 0:
            x += q[2]
        else:
            y += q[2]
    else:
        rev = 1 - rev
        
x = x[::-1]
ans = x + s + y

if rev:
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque

s = input()
Q = int(input())

l = deque()
l.append(s)

rev = True
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        rev = not rev
    else:
        if rev:
            if q[1] == "1":
                l.appendleft(q[2])
            else:
                l.append(q[2])
        else:
            if q[1] == "1":
                l.append(q[2])
            else:
                l.appendleft(q[2])

ans = ""    
for i in l:
    ans += i

if not rev:
    print(ans[::-1])
else:
    print(ans)

Solution (Python):
from collections import deque
s=input()
q=int(input())
n=0
que=deque(s)
for i in range(q):
    l=input()
    if l[0]=='1':
        n+=1
    else:
        if (l[2]=='1' and n%2==0) or (l[2]=='2' and n%2==1):
            que.appendleft(l[4])
        else:
            que.append(l[4])

ans=''.join(que)
if n%2==0:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
import itertools
import functools
import math
from collections import Counter
from itertools import combinations

def CHK():
    S=list(input())
    Q=int(input())
    H=""
    B=""
    rflg = 0
    for i in range(Q):
        Que = input()
        if Que == '1':
            if rflg == 0:
                rflg = 1
            else:
                rflg = 0
            
        else:
            T,F,C=map(str,Que.split())
            if F == '1' and rflg == 0 or (F != '1' and rflg == 1):
                
                H = C + H
            else:
                
                B = B + C

    S = list(H) + S + list(B)
    if rflg == 1:
        S = list(reversed(S))
    print("".join(S))

CHK()

Solution (Python):
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))

Solution (Python):
S = list(input())
Q = int(input())
A = []
a = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    a += 1
  else:
    if (q[1] == '1' and a % 2 == 0) or (q[1] == '2' and a % 2 == 1):
      A.append(q[2])
    else:
      S.append(q[2])
A = A[::-1]
S = A + S
if a % 2 == 1:
  S = S[::-1]
ans = ''.join(S)
print(ans)

Solution (Python):
from collections import deque
S = deque(list(input().strip()))
Q = int(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))

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
s = input()
q = I()
rev = False
ans = deque((s))
for i in range(q):
    A = input().split()
    if A[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        if A[1] == '1':
            if rev:
                ans.append(A[2])
            else:
                ans.appendleft(A[2])
        else:
            if rev:
                ans.appendleft(A[2])
            else:
                ans.append(A[2])
if rev:
    ans.reverse()
    print(*ans,sep='')
else:
    print(*ans,sep='')