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



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Accepted

Solution code:
s = input()
q = int(input())
cnt = 0
front = []
back = []
for i in range(q):
    t = input()
    if t[0] == "1":
        cnt += 1
        cnt %= 2
    else:
        _, f, c = t.split()
        f = int(f)
        x = f+cnt
        if x%2 == 1:
            front.append(c)
        else:
            back.append(c)

a = "".join(front)
a = a[::-1]
b = "".join(back)
ans = "".join([a,s,b])
if cnt%2 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
s = deque([i for i in s])
q = int(input())
r = False
for _ in range(q):
    qu = input()
    if qu == "1":
        r = not r
    else:
        _, f, c = [i for i in qu.split()]
        if (f != "1") == r:
            s.appendleft(c)
        else:
            s.append(c)

s = "".join(s)
if r:
    s = reversed(s)
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

S = deque(input().rstrip())
Q = int(input())
rev = False
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        rev = rev ^ True
    else:
        F, C = q[1:]
        if (F == '1') ^ rev:
            S.appendleft(C)
        else:
            S.append(C)
S = list(S)
if rev:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
r=0
m=[]
u=[]
for i in range(q):
    p=list(map(str,input().split()))
    if int(p[0])==1:
        r+=1
    else:
        if (int(p[1])==1 and r%2==0) or (int(p[1])==2 and r%2==1):
            m.append(p[2])
        else:
            u.append(p[2])
if len(m)>0:
    m.reverse()
ans=m+s+u
if r%2==1:
    ans.reverse()
print(''.join(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
from collections import deque

state = 0
s = deque(input().rstrip())
q = int(input())
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        state ^= 1
    else:
        f = int(query[1]); c = query[2]
        if state == 0:
            if f == 1:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == 1:
                s.append(c)
            else:
                s.appendleft(c)

if state == 0:
    ans = "".join([item for item in s])
else:
    s.reverse()
    ans = "".join([item for item in s])

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
rev = False
for i in range(Q):
  q = input()
  if len(q) == 1:
    rev = False if rev else True
  else:
    q, f, c = q.split()
    if rev:
      if f == "1":
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if f == "1":
        S.appendleft(c)
      else:
        S.append(c)
print("".join(reversed(S)) if rev else "".join(S))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
F,B = "",""
k = 1
for i in range(Q):
    q = list(input().split())
    if int(q[0])==1:k = abs(k-1)
    else:
        if int(q[1])==1:
            if k:F+=q[2]
            else:B+=q[2]
        else:
            if k:B+=q[2]
            else:F+=q[2]
if k:print(F[::-1]+S+B)
else:print(B[::-1]+S[::-1]+F)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
S = deque(S)

Q = int(input())
flipped = False
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        flipped = not flipped
    elif (q[1] == '1') == flipped:
        S.append(q[2])
    else:
        S.appendleft(q[2])

print(''.join(reversed(S)) if flipped else ''.join(S))

Submission status: Accepted

Solution code:
import sys

s = input()
q = int(input())
rev = 0
append_front_rev = ''
append_rear = ''

for line in sys.stdin:
    q = line.strip()
    if q == '1':
        rev ^= 1
    else:
        _, f, c = q.split()
        if f == '1':
            if rev == 0:
                append_front_rev += c
            else:
                append_rear += c
        else:
            if rev == 0:
                append_rear += c
            else:
                append_front_rev += c

s = append_front_rev[::-1] + s + append_rear
if rev:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    flip = False
    head = ''
    tail = ''
    for _ in range(Q):
        q = input()
        if q == '1':
            flip = not flip
            head, tail = tail, head
        else:
            _, f, c = q.split()
            if f == '1':
                head += c
            else:
                tail += c
    if flip:
        S = S[::-1]
    return head[::-1] + S + tail
   

print(main())

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
s = deque(input().rstrip())
q = int(input())
flg = 0
for _ in range(q):
  t = input()
  if t[0] == "1":
    flg = 1-flg
  else:
    _,x,y = t.split()
    if x == "1":
      if flg:
        s.append(y)
      else:
        s.appendleft(y)
    else:
      if flg:
        s.appendleft(y)
      else:
        s.append(y)
if flg:
  u = list(s)[::-1]
  print(*u,sep="")
else:
  print(*s,sep="")

Submission status: Accepted

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

jun = 1
front = []
back = []

for _ in range(Q):
    inp = input().split()
    if len(inp) == 1:
        jun = 1 - jun
    else:
        _, F, C = inp
        if jun and F == "1":
            front.append(C)
        elif jun and F == "2":
            back.append(C)
        elif not jun and F == "1":
            back.append(C)
        else:
            front.append(C)

front = front[::-1]
ans = "".join(front) + S + "".join(back)
if not jun:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

s = input()
q = ni()
f = 0
top = ''
bot = ''
for _ in range(q):
    w = input().split()
    if w[0] == '1':
        f ^= 1
        top,bot = bot,top
    elif w[1] == '1':
        top += w[2]
    else:
        bot += w[2]
print(top[::-1] + s[::1-f*2] + bot)

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
S = input()
for i in S:
    d.append(i)
Q = int(input())
f = True
for i in range(Q):
    t = input()
    if t == "1":
        f = bool(not f)
    else:
        _, F, C = (a for a in t.split())
        if f:
            if F == "1":
                d.appendleft(C)
            else:
                d.append(C)
        else:
            if F == "1":
                d.append(C)
            else:
                d.appendleft(C)
if not f:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

s = S()
s = deque(s)
n = I()
flg = 0
for _ in range(n):
    L = LS()
    if L[0] == '1':
        flg ^= 1
    else:
        if flg ^ (L[1] == '1'):
            s.appendleft(L[2])
        else:
            s.append(L[2])


if flg:
    s.reverse()
ans = ''.join(s)
print(ans)

Submission status: Accepted

Solution 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)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
t=0
zengo=["",""]
for i in range(q):
    l=input().split()
    if len(l)==1:
        t^=1
    else:
        f,c=int(l[1])-1,l[2]
        zengo[f^t]+=c
if t:
    print(zengo[1][::-1]+s[::-1]+zengo[0])
else:
    print(zengo[0][::-1]+s+zengo[1])

Submission status: Accepted

Solution code:
rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())

from collections import deque

A = deque(rr())
flipped = 0

Q = rri()
for _ in xrange(Q):
    query = rr().split()
    if query[0] == '1':
        flipped ^= 1
    else:
        f, c = query[1:]
        f = int(f)
        if f == 1:
            if flipped == 0:
                A.appendleft(c)
            else:
                A.append(c)
        else:
            if flipped == 0:
                A.append(c)
            else:
                A.appendleft(c)
    #print A, flipped

if flipped == 0:
    print "".join(A)
else:
    print "".join(reversed(A))

Submission status: Accepted

Solution code:
import os
import sys
from collections import deque

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

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
Q = int(sys.stdin.buffer.readline())
queries = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(Q)]

S = deque(list(S))
even = True
for query in queries:
    if query[0] == '1':
        even = not even
    else:
        _, f, c = query.split()
        if f == '1':
            if even:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if even:
                S.append(c)
            else:
                S.appendleft(c)

if not even:
    S.reverse()
ans = ''.join(S)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque

def solve():
    input = sys.stdin.readline
    S = list(input().strip("\n"))
    q = deque()
    for i, s in enumerate(S): q.append(s)
    state = 0 #0の時初期の順番
    Q = int(input())
    for _ in range(Q):
        query = list(input().strip("\n").split())
        if query[0] == "1": state ^= 1
        else:
            f = int(query[1]) % 2
            c = query[2]
            if state ^ f == 1: q.appendleft(c)
            else: q.append(c)
    Ans = ""
    if state == 0:
        while q: Ans += q.popleft()
    else:
        while q: Ans += q.pop()
    print(Ans)
    
    

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

s = ns()
head = []
tail = []
rev = False
q = ni()
for i in range(q):
    cs = stdin.readline().rstrip().split()
    if cs[0] == "1":
        rev ^= True
        head, tail = tail, head
    else:
        if cs[1] == "1":
            head.append(cs[2])
        else:
            tail.append(cs[2])
print("".join(head[::-1]) + (s[::-1] if rev else s) + "".join(tail))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#h,w,k = [int(i) for i in read().split()]


s = input()
u = ""
t = ""
q = int(input())
num = 0

for _ in range(q):
    a = readline().split()
    if len(a)==1:
        u,t = t,u    
        num ^= 1
    else:
        f = int(a[1])
        c = a[2]
        if f==1:
            t += c
        else:
            u += c

if num:
    print(t[::-1]+s[::-1]+u)
else:
    print(t[::-1]+s+u)

Submission status: Runtime Error

Solution code:
S = int(input())
Q = int(input())
rev = 1

for i in range(Q):
  line = input()
  if line == '1':
    rev *= -1
  else:
    a, F, C = input().split()
    if rev * int(F) in [1, -2]:
      S = C + S
    else:
      S = S + C

if rev == -1:
  S = S[::-1]
  
print(S)

Submission status: Runtime Error

Solution code:
import sys
S = input()
Q = int(input())
key = 1

for i in sys.stdin:
    if i[0] == '0':
        break

    if i[0] == '1':
        key = -key
    
    if key == 1:
        if i[2] == '1':
            S = i[4] + S
        else:
            S = S + i[4]

    else:
        if i[2] == '2':
            S = i[4] + S
        else:
            S = S + i[4]


print(S[::key])

Submission status: Runtime Error

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

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

is_inv = False

for q in query:
    if t[0] == '1':
        is_inv = ~is_inv
    else:
        if t[1] == '1':
            if is_inv:
                S = S + t[2]
            else:
                S = t[2] + S
        else:
            if is_inv:
                S = t[2] + S
            else:
                S = S + t[2]

if is_inv:
    print(''.join(reversed(list(S))))
else:
    print(S)

Submission status: Runtime Error

Solution code:
    def main():
    s = input()
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[2] + s
                elif query[1] == "2":
                    s = s + query[2]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[2]
                elif query[1] == "2":
                    s = query[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
Query = list(input().split() for _ in range(Q))

count = 0
L, R = "", ""

for i in range(Q):
  if Query[i][0] == "1": count += 1
  else:
    if Query[i][1] == "1":
      if count % 2 == 0: L = Query[i][2] + L
      else: R += Query[i][2]
    else:
      if count % 2 == 0: R += Query[i][2]
      else: L = Query[i][2] + L
  print(L + S + R)

if count % 2 == 0: print(L + S + R)
else: print(R[::-1] + S[::-1] + L[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque

z=list(input())
Q=deque()
Q.append((z))
q=int(input())

cnt=0

for i in range(q):
  x=input()
  if x[0]=='1':
    cnt+=1
    cnt%=2
    
  elif x[0]=='2':
    if x[2]=='1':
        if cnt%2==0:
            Q.appendleft(x[4])
        else:
            Q.append(x[4])

    else:
        if cnt%2==0:
            Q.append(x[4])
        else:
            Q.appendleft(x[4])
      
if cnt%2==1:
  Q.reverse()


print(''.join(Q))

Submission status: Runtime Error

Solution code:
from collections import deque

z=list(input())
Q=deque()
Q.append(z)
q=int(input())

cnt=0

for i in range(q):
  x=input()
  if x[0]=='1':
    cnt+=1
    cnt%=2
    
  elif x[0]=='2':
    if x[2]=='1':
        if cnt%2==0:
            Q.appendleft(x[4])
        else:
            Q.append(x[4])

    else:
        if cnt%2==0:
            Q.append(x[4])
        else:
            Q.appendleft(x[4])
      
if cnt%2==1:
  Q.reverse()


print(''.join(Q))

Submission status: Runtime Error

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
s = input()
q = I()
straight = True
ans = deque(s)
for i in range(q):
    A = input().split()
    if A[0] == '1':
        straight = not straight
    else:
        if straight:
            if A[1] == '1':
                ans.append(A[2])
            else:
                ans.appendleft(A[2])
        else:
            if A[1] == '1':
                ans.appendleft(A[2])
            else:
                ans.append(A[2])
if straight:
    print(*ans,sep='')
else:
    print(*an[::-1],sep='')

Submission status: Runtime Error

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

c1 = 0
for i in range(Q):
    q = input()
    T = int(q[0])
    if T == 1:
        c1 = (c1 + 1) % 2
    else: # T == 2
        F, C = int(q[1]), q[2]
        if F + c1 == 1:
            S = C + S
        else: # F + c2 == 2
            S = S + C

print(S)

Submission status: Runtime Error

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

c1 = 0
c2 = 0
query = []
for i in range(Q):
    q = [x for x in input().split()]
    T = int(q[0])
    if T == 1:
        query.append([T])
        c1 += 1
    else: # T == 2
        F, C = int(q[1]), q[2]
        query.append([T, F, C])
c2 = Q - c1

for i in range(c2 + c1 // 2):
    T1 = query[i][0]
    if T1 == 1:
        T2 = query[i + 1][0]
        if T2 == 1:
            query = query[:i] + query[i + 2:]
        if T2 == 2:
            F, C = query[i + 1][1], query[i + 1][2]
            if F == 1:
                query[i] = [2, 2, C]
                query[i + 1] = [1]
            else: # F == 2
                query[i] = [2, 1, C]
                query[i + 1] = [1]

M = len(query)
for i in range(M):
    T = query[i][0]
    if T == 1:
        L = len(S)
        S = S[L - 1::-1]
    else: # T == 2
        F, C = query[i][1], query[i][2]
        if F == 1:
            S = C + S
        else: # F == 2
            S = S + C

print(S)

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
a=[list(map(str,input().split())) for i in range(q)]

ans=[]
ans.append(s)

for i in range(q):
  if len(a[i])==1:
    ans.insert(0,ans[-1])
    ans.append(ans[1])
    print(ans)
    ans.pop(1)
    ans.pop(-2)

  else:
    if a[i][1]=='1':
      ans.insert(0,a[i][2])
    else:
      ans.append(a[i][2])

print(''.join(ans))

Submission status: Runtime Error

Solution code:
S = list(input())

Q = int(input())

for i in range(Q):
  T = input().split()
  t = int(T[0])
  print(S)
  if t == 1:
    S = S[::-1]
  elif t == 2:
    f = int(T[1])
    c = T[2]
    if f == 1:
      S.insert(0,c)
    elif f == 2:
      S.append(c)

SS = ''.join(S)

print(SS)

Submission status: Runtime Error

Solution code:
from collections import deque
S = input()
Q = int(input())
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:
                S.insert(0, T[2])
            else:
                S.append(T[2])
        else:
            if rev % 2 == 0:
                S.append(T[2])
            else:
                S.insert(0,T[2])
ans = ''.join(S)
if rev % 2 ==0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque
S = input()
Q = int(input())
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:
                S.appendleft(T[2])
            else:
                S.append(T[2])
        else:
            if rev % 2 == 0:
                S.append(T[2])
            else:
                S.appendleft(T[2])
ans = ''.join(S)
if rev % 2 ==0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Runtime Error

Solution code:
def t_1(s):
	return	s[::-1]
    
def t_2(s,f,c,flag):
  if f == 1:
    if flag==True:
    	return c+s
  	elif flag== False:
    	return s+c
  
  if f == 2:
    if flag == True:
    	return s+c
  	elif flag == False:
    	return c+s


S = input()
Q = int(input())
"""flag = True 正しい、false 逆方向"""
flag = True

for i in range(Q):
  inputs = list(input().split())
  
  if int(inputs[0]) == 1:
    flag = not(flag)
  elif int(inputs[0]) == 2:
    S = t_2(S,int(inputs[1]),inputs[2],flag)

if flag:
  print(S)
elif not(flag):
  print(t_1(S))

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
h=0
m=''
u=''
for _  in range(q):
  d=input()
  if '2' in d:
    a,b,c=map(str,d.split())
    if b=='1':
      if h%2==0:
        u+=c
      else:
        m+=c
        
    else:
      if h%2==0:
        m+=c
      else:
        u+=c
        
  else:
    h+=1
    
M=list(m)
G=list(reversed(M))
s=G+s+u
if h%2==0:
  print(s)
else:
  f=list(s)
  g=list(reversed(f))
  print(''.join(g))

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
front = True

for i in range(Q):
    t, f, c = map(str, input().split())
    t = int(t)
    f = int(f)
    if t == 1:
        if front:
            front = False
        else:
            front = True
    else:
        if f == 1:
            if front:
                S = c + S
            else:
                S = S + c
        else:
            if front:
                S = S + c
            else:
                S = c + S

if not front:
    S = S[::-1]

print(S)

Submission status: Runtime Error

Solution code:
from numba import njit

S = input()
Q = int(input())
front = True

@njit
for i in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        if front:
            front = False
        else:
            front = True
    else:
        if q[1] == "1":
            if front:
                S = q[2] + S
            else:
                S = S + q[2]
        else:
            if front:
                S = S + q[2]
            else:
                S = q[2] + S

if not front:
    S = S[::-1]

print(S)

Submission status: Runtime Error

Solution code:
str1 = input()
num = int(input())
for i in range(num):
    flag1 = input().split()
    if flag1[0] == 1:
        str1 = str1[::-1]
    else:
        if int(flag1[1]) == 2:
            str1 = str1 + flag1[2]
        else:
            str1 = flag1[2] + str1
print(str1)

Submission status: Runtime Error

Solution code:
str1 = input()
num = int(input())
i = 0
while i < num:
    flag1 = input()
    if len(flag1) == 1:
        str1 = str1[::-1]
    else:
        trash, flag2, char = map(str, flag1.split())
        if int(flag2) == 2:
            str1 = str1 + char
        else:
            str1 = char + str1
print(str1)

Submission status: Runtime Error

Solution code:
str1 = input()
num = int(input())
for i in range(num):
  f1 = input()
  if len(f1) == 1:
    str1 = str1[::-1]
   else:
    trash, f2, ch = map(str, flag.split())
    if int(f2) ==2:
      str1 = str1 + char
    else :
      str1 = char + str1
print(str1)

Submission status: Runtime Error

Solution code:
s = list(input())
q = int(input())
rev = False
for i in range(q):
    print(input().split())
    if len(input()) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        if input()[2] == "1":
            a = input()[5]
            if rev:
                s.append(a)
            else:
                s.insert(0, a)
        else:
            a = input()[5]
            if rev:
                s.insert(0, a)
            else:
                s.append(a)
if rev:
    s.reverse()
print("".join(s))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
for i in range(q):
  a = input()
  if " " in a:
    l = list(a.split())
    if l[1] == "1":
      s = l[2] + s
    else:
      s += l[2]
  else:
    s.reverse()
print(s)

Submission status: Runtime Error

Solution code:
S = str(input())
N = int(input())
memo = 0
for i in range(N):
    CL = list(map(str,input().split()))
    if int(CL[0]) == 1:
        if len(C) != 1:
            if memo == 0:
                memo += 1
            else:
                memo -= 1
    elif int(CL[1]) == 1:
        if memo == 0:
            S = CL[2] + S
        else:
            S += CL[2]
    else:
        if memo == 0:
            S += CL[2]
        else:
            S = CL[2] + S
if memo == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Runtime Error

Solution code:
s = str(input())
Q = int(input())
front = 1
for i in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        front *= -1
    else:
        if q[1] == "1":
            if front == 1
                s = q[2] + s
            else:
                s = s + q[2]             
        else:
            if front == 1
                s = s + q[2]
            else:
                s = q[2] + s

if front == -1:
    s = s[::-1]
print(s)

Submission status: Runtime Error

Solution code:
s = input()
Q = int(input())
for i in range(Q):
    q = list(map(str, input().split())
    if q[0] == "1":
        if len(s) > 1:
            s = s[-1] + s[1:-1] + s[0]
    else:
        if q[1] == "1":
            s = q[2] + s 
        else:
            s = s + q[2]
print(s)

Submission status: Runtime Error

Solution code:
S = input()
N = int(input())
line = [''] * (N * 2)
rev = False
pos = N
line[pos] = S
leftPos = pos - 1
rightPos = pos + 1

for i in range(N):
    A = input().split()
    if len(A) == 1:
        rev = not rev
    else:
        if A[1] == '1':
            if rev == False:
                pos = leftPos
                leftPos -= 1
            else:
                pos = rightPos
                rightPos += 1
        else:
            if rev == False:
                pos = rightPos
                rightPos += 1
            else:
                pos = leftPos
                leftPos -= 1
        line[pos] = A[2]

ans = line[leftPos+1:rightPos]
ans = "".join(ans)
if rev == False:
    print(ans)
else:
    print(ans[::-1])

Submission status: Runtime Error

Solution code:
raise RuntimeError

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(list(input()))

q = int(input())

reverse = 0

for i in range(q):
  query = list(input().split())
  if query[0] == '1':
    reverse ^= 1
    continue
  else:
    if query[1] == '1':
      if reverse:
        s.append(query[2])
      else:
        s.appendleft(query[2])
    else:
      if reverse:
        s.appendleft(query[2])
      else:
        s.append(query[2])
if reverse:
  s = s[::-1]
print(''.join(s))

Submission status: Runtime Error

Solution code:
s = list(input())

q = int(input())

reverse = 0

for i in range(q):
  query = list(input().split())
  if query[0] == '1':
    reverse ^= 1
    continue
  else:
    if query[1] == '1':
      if reverse:
        s.append(query[2])
      else:
        s.appendleft(query[2])
    else:
      if reverse:
        s.appendleft(query[2])
      else:
        s.append(query[2])
if reverse:
  s = s[::-1]
print(''.join(s))

Submission status: Runtime Error

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

s = list(input().strip())
q=int(input())
for i in range(q):
    L = list(input().rstrip().split())    
    if L[0]=='1':
        s.reverse()
    else:
        a,b,c = L
        if int(b)==2:
            s.append(c)
        else:
            s.insert(0,c)
            s = "".join(s)
print(s)

Submission status: Runtime Error

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

s = list(input().strip())
q=int(input())
for i in range(q):
    L = list(input().rstrip().split())    
    if L[0]=='1':
        s.reverse
    else:
        a,b,c = L
        if int(b)==2:
            s.append(c)
        else:
            s.insert(0,c)
            s = "".join(s)
print(s)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

input = sys.stdin.readline

S = deque(input()[:-1])
reverse_flg = 0
Q = int(input())
qs = [input().split() for _ in range(Q)]
for q in qs:
    if q[0] == '1':
        reverse_flg = reverse_flg ^ 1
    else:
        flg = (int(q[1]) + reverse_flg) % 2
        if flg == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])

if reverse_flg:
    S = S[::-1]
print(''.join(S))

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
a = [input() for i in range(n)]
t=True
for i in range(n):
    for i in range(n):
        q = input()
        if q[0] == "1":
            t=False
        else:
            t, f, c = map(str, q.split())
            if (f == "1" and t==True) or (f=="2" and t==False):
                s = c+s
            else:
                s =s+c
if t==False:
    s=s[::-1]

print(s)

Submission status: Runtime Error

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

s, q = input()[:-1], int(input())
for _ in range(q):
	o = input()[:-1].split()
    if len(o) == 1: s = s[::-1]
    else:
      if o[1] == '1': s = o[2] + s
      else: s += o[2]
print(s)

Submission status: Runtime Error

Solution 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]))