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: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    S_reverse = S[::-1]

    for i in range(Q):
        q = list(input().split(' '))
        if len(q) > 1:
            if q[1] == '1':
                S = q[2] + S
                S_reverse = S_reverse + q[2] 
            elif q[1] == '2':
                S = S + q[2]
                S_reverse = q[2] + S_reverse
        elif q[0] == '1':
            temp = S_reverse
            S_reverse = S
            S = temp

    print(S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
hanten = False
query = []
for i in range(q):
    query.append(list(input().split()))

for i in range(q):
    if hanten == False:
        if len(query[i]) == 1:
            hanten = True
        else:
            if int(query[i][1]) == 1:
                #先頭にquery[i][2]を追加
                s = query[i][2] + s
            else:
                #末尾にquery[i][2]を追加
                s = s + query[i][2]
        #print(s)
    else:
        if len(query[i]) == 1:
            hanten = False
        else:
            if int(query[i][1]) == 1:
                #末尾にquery[i][2]を追加
                s = s + query[i][2]
            else:
                #先頭にquery[i][2]を追加
                s = query[i][2] + s
        #print(s)
                
if hanten == False:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    a = input()
    if a == '1':
        S = S[::-1]
    else:
        a = [x for x in a]
        if a[2] == '1':
            S = a[4] + S
        else:
            S = S + a[4]

print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

queryList = deque()

for n in range(Q):
    query = input().split(" ")
    queryList.append(query)

for query in queryList:
    if len(query) == 1:
        S.reverse()
    else:
        if query[1] == "1":
            S.appendleft(query[2])
        else:
            S.append(query[2])

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
s = input()
q = int(input())
f = 0
for _ in range(q):
    li = list(input().split())
    if len(li) == 1:
        f = ~f
    else:
        if li[1] == '1':
            if f == 0:
                s = li[2] + s
            else:
                s = s + li[2]
        else:
            if f == 0:
                s = s + li[2]
            else:
                s = li[2] + s
if f == -1:
    s = s[::-1]
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())

t = 0
for i in range(n):
  a = input().split()
  if int(a[0]) == 1:
    #s = s[::-1]
    s = ''.join(list(reversed(s)))
  elif int(a[1]) == 1:
    s = a[2] + s
  elif int(a[1]) == 2:
    s = s + a[2]
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for _ in range(q):
  Q = [x for x in input().split()]
  if Q[0] == '1':
    s.reverse()
  else:
    if Q[1] == '1':
      s.appendleft(Q[2])
    else:
      s.append(Q[2])

print('' . join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = [l for l in input()]
Q = int(input())
d = deque(S)

for i in range(Q):
    query = input()
    if query[0] == '1':
        d.reverse()
    else :
        t, f, c = query.split()
        if f == '1':
            d.appendleft(c)
        else :
            d.append(c)

print(''.join(d))

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
    Querys=input().split(' ')
    if Querys[0] =='1':
        S = S[::-1]
    else:
        if Querys[1] =='1':
            S = Querys[2] + S
        else:
            S += Querys[2]
print(S)

Submission status: Time Limit Exceeded

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


def main():
    s = "".join(input().split())
    q = int(input())
    queries = [list(input().split()) for _ in range(q)]
    for query in queries:
        if query[0] == "1":
            s = s[::-1]
            continue
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[2]
    print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
cnt = 0
h = []
t = []
for i in range(Q):
  s = input().split()
  if s[0] == "1":
    cnt+=1
  elif s[0] == "2":
    if s[1] == "1":
      if cnt%2 == 0:        
        S.insert(0,s[2])
      else:
        S.append(s[2])
    elif s[1] == "2":
      if cnt%2==1:
        S.insert(0,s[2])       
      else:
        S.append(s[2])

if cnt%2 == 1:
  S = list(reversed(S))
  
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input() 
Q = int(input())
process_list = [input().split() for i in range(Q)]  #[ [2, 1, p], [1], [2, 2, c], [1] ]

for lists in process_list:
    if lists[0] == "1":
        lists[0] = int(lists[0])
    if lists[0] == "2":
        lists[0] = int(lists[0])
        lists[1] = int(lists[1])

for lists in process_list:
    if lists[0] == 1:
        S = S[::-1]
    elif lists[0] == 2:
        if lists[1] == 1:
            S = lists[2] + S
        elif lists[1] == 2:
            S = S + lists[2]

print(S)

Submission status: Time Limit Exceeded

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

hanten = 0

for i in range(Q):
    q = input().split()
    
    if q[0] == "1":
        hanten += 1
    else:
        if hanten % 2 == 0:
            if q[1] == "1":
                S = q[2] + S
            else:
                S = S + q[2]
        else:
            if q[1] == "1":
                S = S + q[2]
            else:
                S = q[2] + S

if hanten % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
##
s = input()
q = int(input())
for i in range(q):
    x = input().split()
    if x[0] == "1":
        s = s[::-1]
    else:
        if x[1] == "1":
            s = x[2] + s
        else:
            s = s + x[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
def solve(S, Q, query):
    st = S
    for qq in query:
        if qq[0] == "1":
            st = st[::-1]
        else :
            if qq[1] == "1": st = qq[2] + st
            elif qq[1] == "2": st = st + qq[2]

    print(st)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            # for word in line.split():
            yield line
    tokens = iterate_tokens()
    S = str(next(tokens)).replace('\n', '')  # type: str
    Q = str(next(tokens)).replace('\n', '')  # type: str
    query = []
    for x in tokens:
        sp = x.split()
        # print(sp)
        if sp[0] == "1": query.append(sp[0])
        else :
            query.append((sp[0], sp[1], sp[2]))
    # print(query)

    solve(S, Q, query)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
L = [list(input().split()) for i in range(Q)]

ans = S
for l in L:
    if len(l) == 1:
        ans = ''.join(list(reversed(ans)))
    elif l[1] == '1':
        ans = l[2] + ans
    else:
        ans = ans + l[2]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    rvCt = 0
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            rvCt += 1
        else:
            if rvCt % 2 == 1:
                S = S[::-1]
            rvCt = 0
            if query[1] == '1':
                S = query[2] + S
            else:
                S = S + query[2]

    if rvCt % 2 == 1:
        S = S[::-1]
    
    print(S)

main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
moj = str(input())
moj_list =list(moj)
kai = int(input())
han = 0
for i in range(kai):
    kyu = []
    kyu = list(input().split(" "))
    if kyu[0] == "1":
        if han == 0:
            han = 1
        elif han == 1:
            han = 0
    elif kyu[0] == "2":
        if han ==0:
            if kyu[1] == "1":
                moj = kyu[2] + moj
            elif kyu[1] == "2":
                moj = moj + kyu[2]
        if han ==1:
            if kyu[1] == "1":
                moj = moj + kyu[2]
            elif kyu[1] == "2":
                moj = kyu[2] + moj        
if han == 1:
    moj = moj[::-1]
    
print(moj)

Submission status: Time Limit Exceeded

Solution code:
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()
    else:
        if query[1] == '1':
            s.appendleft(query[2])
        else:
            s.append(query[2])

print(''.join(s))

Submission status: Time Limit Exceeded

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

import collections

s = collections.deque(input().strip())
num = int(input())
query = [0] * num
for i in range(num):
    query[i] = input().strip()

for q in query:
    if q[0] == '1':
        s.reverse()
    else:
        _, f, c = q.split()
        if f == '1':
            s.appendleft(c)
        else:
            s.append(c)
print(''.join(list(s)))

Submission status: Time Limit Exceeded

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

qurey = [input().split() for i in range(Q)]

for i in range(Q):
	if qurey[i][0] == "1":
		S = S[::-1]
	else:
		if qurey[i][1] == "1":
			S = qurey[i][2] + S
		else:
			S = S + qurey[i][2]
print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        S = S[::-1]
    else:
        f_i = int(query[1])
        c_i = query[2]
        if f_i == 1:
            S = c_i + S
        else:
            S = S + c_i
print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import re
import math
import itertools
from copy import deepcopy
import fractions
import random
from functools import lru_cache
from heapq import heappop,heappush
import time
import sys
input=readline = sys.stdin.readline
sys.setrecursionlimit(2000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def fac_list(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s

#main
s=input()
Q=int(input())
l=""
r=""
count=0
for a in range(Q):
    tmp=input().split()
    if tmp[0]=="1":
        count+=1
    else:
        if tmp[1]=="1":
            if(count%2==0):l=tmp[2]+""+l
            else:r+=tmp[2]
        else:
            if(count%2==0):r+=tmp[2]
            else:l=tmp[2]+""+l
s=l+s+r
s=s.replace("\n","")
if(count%2==1):
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        S.reverse()
    else:
        if q[1] == "1":
            S.insert(0,q[2])
        else:
            S.append(q[2])
for s in S:
    print(s,end='')

Submission status: Time Limit Exceeded

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

def make_reverse(S):

    if len(S) == 1:
        return S

    reversedS = ""
    for i in range(len(S)-1, -1, -1):
        reversedS += S[i]
    else: return reversedS


def main(S, Q):
    
    for i in range(Q):

        query = input().split()
        if int(query[0]) == 1:
            S = make_reverse(S)
        else:
            if int(query[1]) == 1:
                S = query[2] + S
            else:
                S = S + query[2]
    
    else: return S


if __name__ == "__main__":

    answer = main(S, Q)
    print(answer)

Submission status: Time Limit Exceeded

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

for i in range(q):
  query = input()
  if query[0] == '1':
    s = s[::-1]
  else:
    t, f, c = query.split()
    if f == '1':
      s = c + s
    else:
      s = s + c

print(s)

Submission status: Time Limit Exceeded

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

roll = False

for i in range(q) :
    inp = input()
    if inp[:1] == '1' :
        roll = not roll
        #print(roll)
    else :
        t, f, c = inp.split()
        #print(t, f, c)
        if (f == '1' and (not roll)) or (f == '2' and roll) :
            s = "{}{}".format(c, s)
        else :
            s = "{}{}".format(s, c)

if roll :
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

ans = S
ansf = []
ansb = []
T = [0,0,""]
for i in range(Q):
  TS = input()
  if TS == "1":
    revcount += 1
  else:
    T = TS.rstrip().split(" ")
    if (int(T[1]) == 1 and revcount % 2 == 0) or (int(T[1]) == 2 and revcount % 2 == 1):
      ansf.insert(0,T[2])
    else:
      ansb.append(T[2])
      
ansff = "".join(ansf)
ansbb = "".join(ansb)

ans2 = ansff + ans + ansbb

if revcount % 2 == 1:
  print(ans2[::-1])
else:
  print(ans2)

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
Q = int(input())
for q in range(Q):
  Query = str(input())
  if list(Query)[0] == '1':
    S = S[::-1]
  else:
    if list(Query)[2] == '1':
      S = list(Query)[4:] + S
    else:
      S = S + list(Query)[4:]
print(''.join(S))

Submission status: Time Limit Exceeded

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

S=input()[:-1]
Q=int(input()[:-1])
f=0

for _ in range(Q):
    q=input().split()
    if int(q[0])==1:
        f = (f+1)%2
    if int(q[0])==2:
        if int(q[1])==1:
            if f==0:
                S=q[2]+S
            else:
                S=S+q[2]
        else:
            if f==0:
                S=S+q[2]
            else:
                S=q[2]+S
if f==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import math
import sys 
from collections import deque
s = deque(list(input()))
q = int(input())
for i in range(q):
    a = list(input().split())
    if a[0] == '1':
        s = deque(list(s)[::-1])
    else:
        if a[1] == '1':
            s.appendleft(a[2])
        else:
            s.append(a[2])
print(''.join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    s = input().split()
    if len(s) == 1:
        S = S[::-1]
    else:
        if s[1] == "1":
            S = s[2] + S
        else:
            S = S + s[2]
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = []
for i in range(Q):
    q = input().split(" ")
    q = [int(a) if i == 0 or i == 1 else a for i, a in enumerate(q)]
    if len(q) == 1:
        S = S[::-1]
    else:
        if q[1] == 1:
            S = q[2] + S
        else:
            S = S + q[2]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
S = input()
Q = int(input())
pin = 0
for i in range(Q):
  t = list(sys.stdin.readline().split())
  if int(t[0]) == 1:
    if pin == 1:
      pin = 0
    else:
      pin = 1
  else:
    if int(t[1]) % 2 == pin :
      S = S + t[2]
    else:
      S = t[2] + S
if pin == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
s = input()
n = int(input())
for i in range(n):
    a = input().split()
    # print(a)
    if len(a) == 1:
        s = s[::-1]
    elif a[1] == "1":
        s = a[2] + s
        # print(a[2])
    else:
        s = s + a[2]
    # print(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
	s = list(str(input()))
	q = int(input())
	qu = [list(map(str,input().split())) for x in range(q)]

	for i in range(q):
		if qu[i][0] != '1':
			if qu[i][1] == '1':
				s.insert(0,qu[i][2])
			else:
				s.append(qu[i][2])
		else:
			s.reverse()

	print(''.join(map(str,s)))

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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

for q in range(Q):
    T = input()
    if T == '1':
        S = S[::-1]

    else:
        t, f, c = T.split()

        if f == '1':
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
	S = input()
	Q = int(input())

	for i in range(Q):
		I = input()
		if len(I) == 1:
			S = S[::-1]
		else:
			T, F, C = I.split()
			f = int(F)
			if f == 1:
				S = C + S
			else:
				S = S + C

	print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
    e=list(map(str,input().split()))
    q=int(e[0])
    if len(e)!=1:
        f=int(e[1])
        c=e[2]
    if q==2:
        if f==1:
            s=c+s
        else:
            s=s+c
    else:
        s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = input()
	S = S[:-1]
	Q = inputInt()

	save = []

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					S = inpt[2] + S
				else:
					S = S + inpt[2]
			else:
				if flg == True:
					S = S + inpt[2]
				else:
					S = inpt[2] + S

		if len(S) > 50000:
			save.append(S[1:-1])
			#print(save)
			S = S[0] + S[-1]
			#print(S)

	if flg == True:
		if len(save) > 0:
			tmp = "".join(save)
			ind = len(S) // 2
			if len(S) % 2 != 0:
				ind += 1
			print(S[:ind] + tmp + S[ind:])
		else:
			print(S)
	else:
		if len(save) > 0:
			print(save)
			print(S)
			S = S[::-1]
			tmp = "".join(save)
			tmp = tmp[::-1]
			ind = len(S) // 2
			if len(S) % 2 != 0:
				ind += 1
			print(S[:ind] + tmp + S[ind:])
		else:
			print(S[::-1])


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def main():
    a = input()
    d = deque(a)
    n = int(input())
    for i in range(n):
        j = input().split()
        if len(j) == 3 and j[1] == "1":
            d.appendleft(j[2])
        elif len(j) == 3 and j[1] == "2":
            d.append(j[2])
        else:
            d.reverse()
    ans = ""
    for j in d:
        ans = ans + j
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        S = S[::-1]
    elif a == "2":
        c, d = b
        if c == "1":
            S.insert(0, d)
        elif c == "2":
            S.append(d)
print(*S, sep="")

Submission status: Time Limit Exceeded

Solution code:
s=input()
from sys import stdin
input = stdin.readline
q=int(input())
cnt=0
qq=[tuple(input().split()) for _ in range(q)]
for j in qq:
    if  len(j)!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
queri=[]
rv=0

if rv==1: s=s[::-1]
forward=""
back=""
for i in range(q):
    queri=list(map(lambda x:x,input().split()))
    if queri[0]=="2":
        rvt=(rv+int(queri[1]))%2
        if rvt==1: forward+=queri[2]
        else: back+=queri[2]

    else: rv=(rv+1)%2

ans=forward[::-1]+s+back
print(ans if rv==0 else ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
Q = int(input())
num_rev = 0
for i in range(Q):
  a = list(map(str, input().split()))
  if len(a) == 1:
    num_rev += 1
  else:
    if num_rev % 2 == 0:
      if a[1] == "1":
        S.insert(0, a[2])
      else:
        S.append(a[2])
    else:
      if a[1] == "1":
        S.append(a[2])
      else:
        S.insert(0, a[2])
if num_rev % 2 == 1:
  S = list(reversed(S))
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = [input().split() for i in range(q)]

for i in query:
  if i[0] == '2':
    if i[1] == '1':
      s = '{}{}'.format(i[2], s)
    else:
      s = '{}{}'.format(s, i[2])
  else:
    s = ''.join(reversed(s))
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())

q=int(input())
for i in range(q):
    a=input().strip()
    a=list(a)
    #print(a)
    if int(a[0])==1:
        #print("DFGHJ")
        s=s[::-1]
    if int(a[0])==2:
        #print(a[2])
        s=list(s)
        if int(a[2])==1:
            s.insert(0,str(a[4]))
        else:
            s.append(str(a[4]))
        s=''.join(s)
        #print(s)        
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    q = input()
    if q!='1':
        tmp = []
        if q[2]=='1':
            tmp.append(q[4])
            tmp.append(S)
        else:
            tmp.append(S)
            tmp.append(q[4])
        S = ''.join(tmp)
    else:
        S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

R=0

for i in range(Q):
    temp=list(input().split())
    if int(temp[0])==1:
        R+=1
    elif int(temp[0])==2:
        F=int(temp[1])
        c=temp[2]
        if F==1:
            if R%2==0:
                S=c+S
            else:
                S=S+c
        elif F==2:
            if R%2==0:
                S=S+c
            else:
                S=c+S

if R%2==1:
    ans=S[-1::-1]
    print(ans)
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
for i in range(Q):
    tmp = list(input())
    if len(tmp)==1:
        s = s[::-1]
    else:
        f,c = tmp[2], tmp[4]
        if f=='1':
            s = '{}{}'.format(c,s)
        elif f=='2':
            s = '{}{}'.format(s,c)
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
z = 0
F = ""
R = ""
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        z += 1
    else:
        if (int(a[1]) + z) % 2 == 0:
            R += a[2]
        else:
            F = a[2] + F
ans = F + s + R
if z%2==0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q_num = int(input())
for i in range(q_num):
    q = list(input().split())
    if q[0] == '1':
        s = s[::-1]
    else:
        if q[1] == '1':
            s = q[2] + s 
        else:
            s += q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

s = input()
s = deque(s)
q = INT()


L = [S_LIST() for i in range(q)]
for i in range(q):
    if L[i][0] == "1":
        s.reverse()
    else:
        query, sent = int(L[i][1]), str(L[i][2])
        if query == 1:
            s.appendleft(sent)
        else:
            s.append(sent)
print("".join(list(s)))