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:
s = input()
a = "*"
n = int(input())
kaitenn = 1

for i in range(n):
    q = input()
    if q[0] == "1":
        kaitenn *= -1
        a = a[::-1]
    else:
        if q[2] == "1":
            a = q[4] + a
        else:
            a = a + q[4]
            
print(a[:a.index("*")]+s[::kaitenn]+a[a.index("*")+1:])

Submission status: Time Limit Exceeded

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

for i in range(q):
    buf = input().split()
    if(len(buf) == 1):
        s = s[::-1]
    else:
        if buf[1] == '1':
            s = deque(s)
            s.appendleft(buf[2])
            s = list(s)
        else:
            s.append(buf[2])

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
 
alpha_map = {chr(i):i for i in range(97,123)}
alpha_map_inv = {i:chr(i) for i in range(97,123)}
a = [0] * len(S)
for i, s in enumerate(S):
  a[i] = alpha_map[s]

reversed = False

for _ in range(Q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    if reversed:
      reversed = False
    else:
      reversed = True
  else:
    if (query[1] == '1')&(not reversed):
      a.insert(0,alpha_map[query[2]])
    elif (query[1] == '2')&(reversed):
      a.insert(0,alpha_map[query[2]])
    else:
      a.append(alpha_map[query[2]])
ss = [''] * len(a)
for i, aa in enumerate(a):
  ss[i] = alpha_map_inv[aa]
if reversed:
  ss.reverse()
print(''.join(ss))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
is_r = False
f_list = []
b_list = []

for i in range(q):
    q_i = input().split(' ')
    t_i = q_i[0]
    if t_i == '1':
        is_r = not is_r
    else:
        f_i = q_i[1]
        if (f_i == '1' and not is_r) or (f_i == '2' and is_r):
            f_list.insert(0, q_i[2])
        else:
            b_list.append(q_i[2])

f_list.append(s)
f_list.extend(b_list)
s = ''.join(f_list) if not is_r else ''.join(f_list)[::-1]
print(s)

Submission status: Time Limit Exceeded

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

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
while idx < q:
    if t[idx] == 1:
        flip = True
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            s = s[::-1]
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            s = ch + s
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            s = s + ch
    idx += 1

print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  input_data = input().split()
  if input_data[0] == '1':
    S = S[::-1]
  else:
    F = input_data[1]
    C = input_data[2]
    if F == '1':
      S = C + S
    else:
      S = S + C
print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    listA=input().split()
    if listA[0]=='1':
        S = S[::-1]
    else:
        if listA[1]=='1':
            S = str(listA[2])+S
        else:
            S = S+str(listA[2])

print(S)

Submission status: Time Limit Exceeded

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

for i in range(0, Q):
    op = [x for x in input().split()]
    if op[0] == '1':
        S = S[::-1]
    else:
        if op[1] == '1':
            S = op[2] + S
        else:
            S = S + op[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
import math
s = input()
Q = int(input())
for i in range(Q):
  q = input().split(' ')
  T = int(q[0])
  if T == 1:
    s = ''.join(list(reversed(s)))
  elif T == 2:
    F = int(q[1])
    C = q[2]
    
    if F == 1:
      s = C + s
    elif F == 2:
      s = s + C

print(str(s))

Submission status: Time Limit Exceeded

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

rev = 0
front = ''
back = ''

for i in range(Q):
    query = input()
    ti = list(query)[0]
    if ti == '1':
        rev = (rev + 1) % 2
    else:
        ti, fi, ci = query.split()
        if (int(fi) + rev) == 2:
            back += ci
        else:
            front = ci + front

s = front + s + back

if rev == 1:
    s = list(s)
    s.reverse()
    s = ''.join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
# 2020/03/07
# AtCoder Beginner Contest 158 - D

# Input
s = input()
q = int(input())
tcnt = 0
for i in range(q):
    l = list(input().split())
        
    if l[0] == "1":
        tcnt = tcnt + 1
    else:
        if tcnt % 2 == 0:
            if l[1] == "1":
                s = l[2] + s
            else:
                s = s + l[2]
        else:
            if l[1] == "1":
                s = s + l[2]
            else:
                s = l[2] + s

# Case odd: Reverse
if tcnt % 2 == 1:
    ans = ''.join(list(reversed(s)))
else:
    ans = s

# Output
print(ans)

Submission status: Time Limit Exceeded

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

for query in QUERY:
    #print(query)
    if query[0] == "1":#反転
        S = S[::-1]
    else:#=="2"
        if query[1] == "1":
            S = query[2] + S
        else:#=="2"
            S += query[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

T_list = []
for q in range(Q):
    T = stdin.readline()[:-1]

    if T == '1':
        S = S[::-1]

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

        if f == '1':
            tmp_s = [c, S]
            S = ''.join(tmp_s)
        else:
            tmp_s = [S, c]
            S = ''.join(tmp_s)

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()

flag = 0
cnt = 0
n = int(input())

for i in range(n):
    que = input()
    if(len(que) == 1):
        cnt += 1
    else:
        que = que.split()
        if(que[1]=="1" and cnt%2 == 0):
            s = que[2] + s
        if(que[1]=="1" and cnt%2 == 1):
            s = s + que[2]
        if(que[1]=="2" and cnt%2 == 0):
            s = s +que[2] 
        if(que[1]=="2" and cnt%2 == 1):
            s = que[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
S =input()
Q = int(input())
T = [0]*Q
for i in range(0,Q):
  T[i] = list(map(str, input().split()))

l = []
for i in range(0,Q):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0


  
  
nS = ""
for i in range(0,Q):
  if(T[i][0] == "2" and T[i][1] == "1"):
      S = T[i][2]+S
  elif(T[i][0] == "2" and T[i][1] =="2"):
      S = S+T[i][2]
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = []

for _ in range(Q):
    Query += [input()]

reversed = False
for i, q in enumerate(Query):
    if q == "1":
        reversed = not reversed
        # S = S[::-1]
    else:
        Ti, Fi, Ci = list(q.split())
        # print(f"Fi:{Fi} S:{S} reversed:{reversed} Ci:{Ci}")
        if Fi == "1":
            if not reversed:
                S = Ci + S
            else:
                S = S + Ci
        elif Fi == "2":
            # print(f"S:{S} reversed:{reversed} Ci:{Ci}")
            if not reversed:
                S = S + Ci
            else:
                S = Ci + S
    # print(f"i:{i} S:{S}")

if reversed:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
info=[]
rev=[] #i回までに何回反転があるか
num=0
for i in range(q):
    info.append(list(input().split()))
moji=0 #文字の追加数
for i in range(q):
    if info[i][0]=="1":
        num+=1
    else:
        if info[i][1]=="2":
            rev.append([info[i][2],2,num]) #末尾
        else:
            rev.append([info[i][2],1,num])
        moji+=1
if num%2==1:
    s.reverse()
#print(rev)
#print(num)
for i in range(moji):
    now=rev[i][1]
    if (num-rev[i][2])%2==1: #これが奇数だと２なら先頭になる
        if now==2:
            now=1
        else:
            now=2
    if now==2:
        s.append(rev[i][0])
    else:
        s.insert(0,rev[i][0])
print("".join(s))

Submission status: Time Limit Exceeded

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

def sol():
    s=input().rstrip()
    q=int(input())
    x=0
    for i in range(q):
        l=tuple([i for i in input().split()])
        if l[0]=="1":
            x=(x+1)%2
        elif l[1]=="1":
            if x==0: s=l[2]+s
            else: s=s+l[2]
        elif l[1]=="2":
            if x==0: s=s+l[2]
            else: s=l[2]+s
    print(s if x==0 else s[::-1])

if __name__=="__main__":
    sol()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
i = 0
while(q>i):
    i += 1
    query = input()
    if query[0] == '1':
        s = s[::-1]
    else:
        if query[2] == '1':
            # 先頭
            s = query[4] + s
        else:
            # 末尾
            s += query[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
count_1=0
l=[]
num =int(input())


for x in range(num):
    i=input().rstrip().split()
    if i[0]=="1":
        count_1+=1
        
    elif i[1]=="1" and count_1%2==0:
        s=i[2]+s
   
    elif i[1]=="1" and count_1%2==1:
        s=s+i[2]

    elif i[1]=="2" and count_1%2==0:
        s=s+i[2]

    elif i[1]=="2" and count_1%2==1:
        s=i[2]+s

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
query = [input().split() for _ in range(Q)]

for i in query:
    if "1"==i[0]:
        S=S[::-1]


    else:
        if i[1]=="1":
            S=S[::-1]
            S+=i[2]
            S=S[::-1]

        else:
            S+=i[2]

print(S)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

rev = 0
for i in range(Q):
    if q[i][0] == "1":
        rev += 1
    else:
        if (q[i][1] == "1" and rev%2 == 0) or (q[i][1] == "2" and rev%2 == 1):
            S = q[i][2] + S
        else:
            S = S + q[i][2]

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

Submission status: Time Limit Exceeded

Solution code:
S = input() 
Q = int(input())
def trans(f):
    if f == 1:
        f = 0
    elif f == 0:
        f = 1
    return f

def pros(S, f, r, add):
    if f == 0:
        if r == "1":
            S = add + S
        elif r == "2":
            S = S + add
    elif f == 1:
        if r == "2":
            S = add + S
        elif r == "1":
            S = S + add
    return S

flag = 0
cnt = 0
for i in range(Q):
    lists = input().split()
    if lists[0] == "1":
        flag = trans(flag)
        cnt += 1
    elif lists[0] == "2":
        S = pros(S, flag, lists[1], lists[2])

if cnt % 2 == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    QQ = input().split()
    if QQ[0] == '1':
        S.reverse()
    else:
        if QQ[1] == '1':
            S.insert(0, QQ[2])
        else:
            S.append(QQ[2])
print(''.join(S))

Submission status: Time Limit Exceeded

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

ans = s

flag = 0

for i in range(q):
    p = input()

    if len(p)==1:
        flag = 1-flag
    if len(p)==5:
        if(p[2]=='1'):
            if flag ==1:
                ans = ans + p[4]
            else:
                ans = p[4] + ans
        else:
            if flag ==1:
                ans = p[4] + ans
            else:
                ans = ans + p[4]
    #print(ans)
    #print(f'flag{flag}')

if flag==0:
    print(ans)
else:
    num = len(ans)
    for i in range(num):
        print(ans[num-1-i],end="")
    print()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys


def solve(S, Q, Qvery):
    fw = True
    ret = list(S)
    # ret.append(S)
    for qvery in Qvery:
        if qvery[0] == 1:
            # fw = not fw
            if fw:
                fw = False
            else:
                fw = True
        else:
            if qvery[1] == 1 and fw or qvery[1] == 2 and not fw:  # 先頭に追加
                ret.insert(0, qvery[2])
                # S = qvery[2] + S
            else:  # 末尾に追加
                ret.append(qvery[2])
                # S = S + qvery[2]

    if fw:
        print(''.join(ret))
    else:
        rev_ret = ret[::-1]
        print(''.join(rev_ret))


# 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 word
    tokens = iterate_tokens()
    S = str(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    Qvery = []
    for i in range(Q):
        T = int(next(tokens))
        if T == 1:
            Qvery.append([1])
        elif T == 2:
            Qvery.append([2, int(next(tokens)), str(next(tokens))])
    solve(S, Q, Qvery)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    s = input()
    q = int(input())
    ans = deque()
    swap = False
    for i in range(len(s)):
        ans.append(s[i])

    for i in range(q):
        #print(ans, swap)
        inp = input()
        if len(inp) == 1:
            hoge = int(inp)
            if swap:
                swap = False
            else:
                swap = True
        else:
            # print(tuple(inp))
            fi, ci = tuple(inp)[2], tuple(inp)[4]
                       
            if int(fi) == 1:
                if not swap:
                    ans.appendleft(ci)
                else:
                    ans.append(ci)
            if int(fi) == 2:
                if not swap:
                    ans.append(ci)
                else:
                    ans.appendleft(ci)
    s_ans = str()
    if swap:
        for i in range(len(ans)-1,-1, -1):
            s_ans += ans[i]
    else:
        for i in range(len(ans)):
            s_ans += ans[i]
    
    print(s_ans)
                
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

S=list(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:
                SS=[q[2]]
                SS.extend(S)
                S=SS
            else:
                S.append(q[2])
        else:
            if f==0:
                S.append(q[2])
            else:
                SS=[q[2]]
                SS.extend(S)
                S=SS
if f==0:
    print("".join(S))
else:
    print("".join(S[::-1]))

Submission status: Time Limit Exceeded

Solution code:
"""
D - String Formation
"""
s = input()
q = int(input())
string = []
for i in range(q):
    string.append(input())

for emir in string:
    if emir[0] == '1':
        s = s[::-1]
    if emir[0] == '2':
        if emir[2] == '1':
            s = emir[4] + s
        elif emir[2] == '2':
            s += emir[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    a = list(input().strip().split())
    if len(a) == 1:
        s = ''.join(list(reversed(s)))
    else:
        if a[1] == '2':
            s = s + a[2]
        else:
            s = a[2] + s
print(s)

Submission status: Time Limit Exceeded

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

Qu =[]
for i in range(Q):
    Qu.append(list(map(str,input().split())))
#print(Qu)

for i in range(Q):
    if Qu[i][0] == '1':
        S = S[::-1]
    elif Qu[i][0] == '2':
        if Qu[i][1] == '1':
            S.insert(0,Qu[i][2])
        else:
            S.append(Qu[i][2])
            
ans = ''.join(S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math
S = str(input())
Q = int(input())
for i in range(Q):
  l =input().split()
  if l[0]=='1':
    S = ''.join(list(reversed(S)))
  elif l[1] == '1':
    S = l[2]+S
  else:
    S = S+l[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
re = 1

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

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

Submission status: Time Limit Exceeded

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

import collections
s = collections.deque(s)
L = []
for i in range(n):
    L = (list(map(str,input())))
    #print(L)
    if len(L) >1:
        if int(L[2]) == 1:
            #print(s)
            s.appendleft(L[4])
        elif int(L[2]) == 2:
            s.append(L[4])
    
    elif len(L) ==1:
        s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
N = int(input())


order = []
state = True

for n in range(N):
    order.append(input().split())


for n in range(N):
    if order[n][0] == '2':
        if (order[n][1] == '1') == state:
            S.insert(0,order[n][2])
        else:
            S.append(order[n][2])
    elif order[n][0] == '1':
        if state:
            state = False
        else:
            state = True


if state:
    out = ''.join(S)
else:
    out = ''.join(S[::-1])

print(out)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
S_list=deque()
for k in range(len(S)):
    S_list.append(S[k])
Q=int(input())
kouho=[input().split() for i in range(Q)]
"""for i in range(Q):
    kari=input().split()
    kouho.append(kari)"""
for j in kouho:
    if j[0]=="1":
        S_list.reverse()
    else:
        if j[1]=="1":
            S_list.appendleft(j[2])
        else:
            S_list.append(j[2])
print(*S_list,sep="")

Submission status: Time Limit Exceeded

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

for i in range(q):
    if len(Q[i]) == 1:
        S.reverse()
    elif Q[i][1] == "1":
        S = [Q[i][2]] + S
    else:
        S = S + [Q[i][2]]

for i in range(len(S)):
    print(S[i],end="")
print()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(0,q):
    k=input()
    if k[0]=="1":
        s = s[::-1]
    elif k[2]=="1":
        s=k[4]+s
    else:
        s=s+k[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:

#import numpy as np
s = input()
n = int(input())
#qt = np.ones((n),dtype=np.int32)
#qf = np.zeros((n),dtype=np.int64)
#qc = [''] * n

mode = True
#mode = True : f1->s=c+s,f2->s=s+c
#mode = False : f1->s=s+c,f2->s=c+s

for i in range(n):
    qs = input()
    if len(qs) == 1:
        mode = not mode
        #s = s[::-1]
        #pass
    else:
        #qt[i] = 2
        #qf[i] = int(qs[1:-2])
        #qc[i] = qs[-1]
        if mode:
            if int(qs[2]) == 1:
                s = qs[4] + s
            else:
                s = s + qs[4]
        else:
            if int(qs[2]) == 1:
                s = s + qs[4]
            else:
                s = qs[4] + s
        
#print(mode)
if not mode:
    s = s[::-1]



#for i in range(n):
#    print(qt[i],qf[i],qc[i])

print(s)

Submission status: Time Limit Exceeded

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

tmp = ''
rev = False
for _ in range(q):
    query = input().split()

    if query[0] == '1' and not(rev):
        rev = True
    elif query[0] == '1' and rev:
        rev = False

    elif query[0] == '2' and query[1] == '1' and not(rev):
        s = ''.join([query[2], s])

    elif query[0] == '2' and query[1] == '1' and rev:
        s = ''.join([s, query[2]])

    elif query[0] == '2' and query[1] == '2' and not(rev):
        s = ''.join([s, query[2]])

    elif query[0] == '2' and query[1] == '2' and rev:
        s = ''.join([query[2], s])

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
  t = input()
  if t[0] == '1':
    s = s[::-1]
  else:
    _, f, c = map(str, t.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())

Query = [input() for i in range(Q)]

for q in Query:
    query = q.split(' ')
    if q == '1':
        S = S[::-1]
    elif query[0]=='2':
        if query[1]=='1':
            S = query[2] + S
        elif query[1]=='2':
            S = S + query[2]
        
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=input()
q=int(input())
query=[]
for i in range(q):
  a=list(map(str,input().split()))
  if len(a)==3:
    a[0]=int(a[0])
    a[1]=int(a[1])
  else:
    a[0]=int(a[0])
  query.append(a)
S=deque(list(s))
for j in range(q):
  if query[j][0]==1:
    S.reverse()
  else:
    if query[j][1]==1:
      S.appendleft(query[j][2])
    else:
      S.append(query[j][2])
T=list(S)
t=''.join(T)
print(t)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  inp = input()
  if len(inp) == 1:
    num_rev += 1
  else:
    T, F, C = inp.split()
    if ((num_rev % 2 == 0) and int(F) == 1) or ((num_rev % 2 == 1) and int(F) == 2):
      S = C + S
    else:
      S = S + C

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
 
for i in range(q):      #一行ずつ読み込んでその都度処理していく。
    li = input().split()
    if li[0] == "1":
        s = s[::-1]
    else:
        if li[1] == "1":
            s = li[2] + s
 
        else:
            s = s + li[2]
 
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
which=0#0 normal #1 reverse
check=[list(map(str,input().split())) for i in range(Q)]
#print(check)
for i in range(Q):
    if check[i][0]=="1":
        which+=1
        which%=2
        continue
    if check[i][0]!="1":
        if which==0:
            if check[i][1]=="1":
                S.insert(0,check[i][2])
            if check[i][1]=="2":
                S.append(check[i][2])
        if which==1:
            if check[i][1]=="1":
                S.append(check[i][2])
            if check[i][1]=="2":
                S.insert(0,check[i][2])
if which==1:
    print("".join(S[len(S)-i-1] for i in range(len(S))))
if which==0:
    print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = 0
mae = ""
usiro = ""

for i in range(q):
    a = input()
    
    if a[0] == '1':
        rev += 1
    else:
        if (int(a[2]) + rev) % 2 == 1:
            mae += a[4]
        else:
            usiro += a[4]

mae = list(reversed(mae))
mae2 = "".join(mae)
s = mae2 + s + usiro
if rev%2 == 0:
    print(s)
else:
    s = list(reversed(s))
    ans = "".join(s)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(list(str(input())))
Q = int(input())
flag = False

for i in range(Q):
    Query =  [str(p) for p in input().split()]
    if(int(Query[0]) == 1):
        flag = not(flag)
    else:
        if(int(Query[1]) == 1):
            if(flag):
                S.reverse()
                flag = not(flag)
            S.appendleft(Query[2])
        else:
            if(flag):
                S.reverse()
                flag = not(flag)
            S.append(Query[2])

if(flag):
    S.reverse()

print("".join(list(S)))

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import deque
    S = input()
    Sd = deque(list(S))
    Q = int(input())
    Query = [input().split() for i in range(Q)]
    for q in Query:
        if q[0] == "1":
            Sd.reverse()
        elif q[0] == "2":
            if q[1] == "1":
                Sd.appendleft(q[2])
            elif q[1] == "2":
                Sd.append(q[2])

    print("".join(Sd))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

def abc158d(s_init, n, query):
    ans = s_init
    for i in range(n):
        tgt = query[i]
        if tgt[0] == "1":
            ans = ans[::-1]
        elif tgt[0] == "2":
            if tgt[1] == "1":
                ans = tgt[2] + ans
            elif tgt[1] == "2":
                ans = ans + tgt[2]
            else:
                print("invalid query!!:" + str(tgt[0]) + str(tgt[1]))
        else:
            print("invalid query!!:" + str(tgt[0]))

    return ans

###
# main
if(__name__ == '__main__'):
    # input
    s_init = input()
    n = int(input())
    query = []
    for i in range(n):
        query.append(input().split(" "))
    
    accomplishment = abc158d(s_init, n, query)
    
    # output
    print(accomplishment)
# else:
    # do nothing

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
sousa = []
for i in range(Q):
  sousa.append(input().split())

for j in range(Q):
  if sousa[j][0] == '1':
    S.reverse()
  else:
    if sousa[j][1] == '1':
      S.appendleft(sousa[j][2])
    else:
      S.append(sousa[j][2])
Ans = ''.join(list(S))
print(Ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

fc = []
for i in range(q):
    t, *f = map(str, input().split())
    if t == "1":
        s.reverse()
    else:
        if f[0] == "1":
            s.appendleft(f[1])
        else:
            s.append(f[1])

print("".join(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 = "".join(query[2] + s)
        else:
            s = "".join(s + query[2])
    print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
queries = []
for _ in range(0, q):
    query = input()
    if len(query) == 1:
        queries.append([int(query)])
    else:
        query = query.split(' ')
        query = [int(query[0]), int(query[1]), query[2]]
        queries.append(query)

for i in range(0, q):
    if queries[i][0] == 1:
        s = s[::-1]
    else:
        if queries[i][1] == 1:
            s = queries[i][2] + s
        else:
            s = s + queries[i][2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

s=input()
q=int(input())
for i in range(q):
    Q = input().split()
    if Q[0]=="1" and len(Q)==1:
        s=''.join(list(reversed(s)))
    elif 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())
S = list(S)

A = []
B = []
count = 0

for i in range(Q):
    k = input()
    if k == "1":
        count += 1
    else:
        if k[2] == "1":
            if count % 2 == 0:
                A = A + [k[4]]
            else:
                B = B + [k[4]]
        else:
            if count % 2 == 1:
                A = A + [k[4]]
            else:
                B = B + [k[4]]

if count % 2 == 0:
    A.reverse()
    S = A + S + B
else:
    B.reverse()
    S.reverse()
    S = B + S + A

print(*S, sep="")