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:
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


def resolve():
    S = [x for x in sys.stdin.readline().split()][0]  # 文字列 一つ
    Q = [int(x) for x in sys.stdin.readline().split()][0]  # int 一つ
    # N, D = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    # h_list = [int(x) for x in sys.stdin.readline().split()]  # 複数int

    # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)]  # 文字列grid
    # v_list = [int(sys.stdin.readline().split()[0]) for _ in range(N)]
    # grid = [[int(x) for x in sys.stdin.readline().split()]
    #         for _ in range(N)]  # int grid

    logger.debug('{}'.format([]))

    reverse = False

    for _ in range(Q):
        q = input().strip()
        if q == '1':
            reverse = not reverse
        else:
            t, f, c = q.split(' ')
            if (f == '1' and not reverse) or (f == '2' and reverse):
                S = ''.join([c, S])
            else:
                S = ''.join([S, c])

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


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest


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

    def test_入力例_1(self):
        input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(input, output)

Submission status: Time Limit Exceeded

Solution code:



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

order = 1

ans = S
for i in range(Q):
    tfc = list(map(str, input().split()))

    if tfc[0] == "1":
        order *= -1
    else:
        if order == 1:
            if tfc[1] == "1":
                ans = tfc[2] + ans
            else:
                ans = ans + tfc[2]

        else:
            if tfc[1] == "2":
                ans = tfc[2] + ans
            else:
                ans = ans + tfc[2]


if order == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

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

s = input()
q = int(input())
query = ["" for _ in range(q)]

for i in range(q):
    query = input()
    if query == "1":
        s = "".join(list(reversed(s)))
        #print(s)
    else:
        q2 = query.split()
        if q2[1] == "1":
            s = q2[2]+s
        else:
            s = s + q2[2]
        #print(s)

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
p = 1
pre = ''
post = ''
for i in range(q):
    x = input().split()
    if len(x) == 1:
        p *= -1
    else:
        f = int(x[1])
        c = x[2]
        if (p > 0 and f == 1) or (p < 0 and f == 2):
            pre += c
        else:
            post += c
if p < 0:
    print(post[::-1] + s[::-1] + pre)
else:
    print(pre[::-1] + s + post)

Submission status: Time Limit Exceeded

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

for ele in list_of_q:
    if (ele[0] == "1"):
        s = s[::-1]
    elif (ele[0] == "2"):
        if (ele[1] == "1"):
            s = ele[-1] + s
        else:
            s = s + ele[-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
  q = input().split()
  
  if int(q[0])==1:
    S = S[::-1]
  else:
    if int(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):
    tfc = input()
    if len(tfc) == 1:
        s = s[::-1]
    else:
        t, f, c = tfc.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())

for i in range(Q):
    tmp = input().split()
    if tmp[0] == "1":
        S = S[::-1]
    elif tmp[0] == "2":
        if tmp[1] == "1":
            S = tmp[2] + S
        elif tmp[1] == "2":
            S = S + tmp[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
s=list(s)
t=int(input())
for i in range(t):
    arr=input().split()
    if len(arr)==1:
        s=s[::-1]
    else:
        if arr[1]=="1":
            
            s=[arr[2]]+s 
        
        else:
            s=s+[arr[2]]
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s= input()
q= int(input())
for i in range(q):
    p = input()
    if p[0]=='1':s = s[::-1]
    elif p[0]=='2':
        _,f,c= p.split()
        if f=='1':s = c + s
        elif f=='2':s = s + c
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]
        continue
    if query[1] == '1':
        s = query[2] + s
        continue
    s = s + query[2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
hanten=0
mae=""
ato=""

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

answer=""
if hanten==0:
    lem=len(mae)
    for i in range(lem):
        answer+=mae[-i-1]
    answer+=s
    answer+=ato
else:
    lem=len(ato)
    les=len(s)
    for i in range(lem):
        answer+=ato[-i-1]
    for i in range(les):
        answer+=s[les-i-1]
    answer+=mae
    
print(answer)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
  query=input()
  if len(query)==1:
    s=s[::-1]
  else:
    t,f,c=map(str,query.split())
    if f=="1":
      s=c+s
    else:
      s=s+c
print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]
flag=0
#print(query)
s=S
s2=""
for i in range(Q):
    if len(query[i])==1:
        flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            if query[i][1]=="1":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
        else:
            if query[i][1]=="2":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
if flag==0:
    print(s)
else:
    for i in range(len(s)):
        s2="".join(())
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = [input() for i in range(q)]
rvs = 1
for i in range(q):
    if query[i] == "1":
        rvs = rvs * (-1)
    else:
        if query[i][2:3] == "1":
            if rvs == 1:
                s = query[i][4:5] + s
            else:
                s = s + query[i][4:5]
        else:
            if rvs == -1:
                s = query[i][4:5] + s
            else:
                s = s + query[i][4:5]
if rvs == -1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

pre = ""
suf = ""

rev = 0
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        rev = (rev + 1) % 2
    else:
        _, f, c = q
        if f == '1':
            if rev == 0:
                pre += c
            else:
                suf += c
        else:
            if rev == 0:
                suf += c
            else:
                pre += c


ans = pre[::-1] + S + suf
if rev == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    q = input()
    if len(q)>1:
        _, f, c = q.split()
        f = int(f)
        if f==1:
            S = c+S
        else:
            S = S+c
    else:
        S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

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

    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
q=int(input())
now = 0
mae,usiro="",""
for _ in range(q):
  lis = list(map(str,input().split()))
  if int(lis[0]) == 1:
    now = (now+1)%2
  else:
    if (int(lis[1])+now)%2 == 1:
      mae = lis[2]+mae
    else:
      usiro= usiro +lis[2]
s = mae+s+usiro
if now ==0:
  for i in range(len(s)):
    print(s[i],end="")
else:
  for i in range(len(s)):
    print(s[-i-1],end="")

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
reverse = False
for i in range(q):
    inp = list(input().split())
    if len(inp) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    t, f, c = inp[0], int(inp[1]), inp[2]
    if not reverse:
        if f == 1:
            s = c + s
        else:
            s = s + c
    else:
        if f == 2:
            s = c + s
        else:
            s = s + c
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()

for _ in range(int(input())):
    a = input().split()

    if a[0] == '1':
        s = s[::-1]
    else:
        if a[1] == '1':
            s = a[2] + s
        else:
            s = s + a[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    t = input()
    if len(t) == 1:
        s = s[::-1]
    else:
        _, a, b = (str(i) for i in t.split())
        if a == "1":
            s = b + s
        elif a == "2":
            s = s + b

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

s = s_in()
q = n_in()

d = True

pre = ''
suf = ''

for _ in range(q):
    query = s_in()
    if len(query) == 1:
        d = not d
        pass
    else:
        _, f, c = query.split()
        if d:
            if f == '1':
                pre = c + pre
            else:
                suf = suf + c
        else:
            if f == '1':
                suf = suf + c
            else:
                pre = c + pre


if d:
    print(pre+s+suf)
else:
    print(suf[::-1]+s[::-1]+pre[::-1])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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


def rev(xs, dummy):
    return xs[::-1]


def head(xs, c):
    return c + xs


def tail(xs, c):
    return xs + c


query = {}
for i in range(Q):
    line = input()
    if line[0] == "1":
        query[i] = {"fun": rev, "c": None}
    else:
        if line[2] == "1":
            query[i] = {"fun": head, "c": line[4]}
        else:
            query[i] = {"fun": tail, "c": line[4]}

for i in range(Q):
    S = query[i]["fun"](S, query[i]["c"])

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
QUERY = []
for i in range(Q):
    # q = list(map(int, input().split()))
    q = input().split()
    if q[0] == '1':
        QUERY.append([int(q[0])])
    else:
        QUERY.append([int(q[0]), int(q[1]), q[2]])

for q in QUERY:
    if q[0] == 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())

qs = []
for i in range(q):
    qs.append(input().split(' '))

for i in range(q):

    if qs[i][0] == '1':
        s = s[::-1]
    else:
        if qs[i][1] == '1':
            s = qs[i][2] + s
        else:
            s = s + qs[i][2]
            
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input().split()

    if q[0] == "1":
        s = s[::-1]
    elif q[0]=="2":
        tmp = q[2]
        if q[1] == "1":
            tmp += s
            s = tmp
        elif q[1] =="2":
            s += tmp
print(s)

Submission status: Time Limit Exceeded

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

is_reversed = False

for i in range(Q):
    query = raw_input().split()
    if query[0] == "1":
        is_reversed = not is_reversed
    elif query[0] == "2":
        F = int(query[1])
        C = query[2]
        if F == 1:
            if not is_reversed:
                S = C + S
            else:
                S = S + C
        elif F == 2:
            if not is_reversed:
                S = S + C
            else:
                S = C + S


if not is_reversed:
    ans =  S
else:
    ans = S[::-1]
print ans

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = list(input())
q = int(input())
a = []
for i in range(q):
    a.append([str(i) for i in input().split()])

for i in range(q):
    if a[i][0] == '1':
        s.reverse()
    if a[i][0] == '2':
        if a[i][1] == '1':
            s.insert(0, a[i][2])
        if a[i][1] == '2':
            s.append(a[i][2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = S
for i in range(Q):
    query = input().split()
    if (query[0] == "1"):
        ans = ans[::-1]
    if (query[0] == "2"):
        if (query[1] == "1"):
            ans = query[2] + ans
        if (query[1] == "2"):
            ans = ans + query[2]

print(ans)

Submission status: Time Limit Exceeded

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

# hd = []
# bc = []
# cnt = 0

for i in range(Q):
    q = list(input().split())

    if q[0]=="1":
        S.reverse()
    else:
        f = int(q[1])
        c = list(q[2])

        if f%2==0:
            S = S+c
        else:
            S = c+S
    # print(S)


    # if q[0]=="1":
    #     hd,bc = bc,hd
    #     cnt += 1
    
    # elif q[0]=="2":
    #     f = int(q[1])
    #     c = q[2]

    #     if f%2==0:
    #         bc.append(c)
    #     else:
    #         hd.append(c)
    # print(hd,bc)

# ans = hd + S + bc

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(q):
    A = list(input().split())
    if A[0] == '1':
        s = s[::-1]
    else:
        if A[1] == '1':
            s = A[2] + s
        else:
            s = s + A[2]
print(s)

Submission status: Time Limit Exceeded

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

def solve(S, query):
    for q in query:
        command = q.split()
        T = int(command[0])
        if T == 1:
            S = S[::-1]
        elif T == 2:
            F = int(command[1])
            C = command[2]
            if F == 1:
                S = C + S
            elif F == 2:
                S += C
    return S


def main():
    S = input()
    Q = int(input())
    query = [input() for n in range(Q)]
    print(solve(S, query))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
reverseFlag = False

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

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

Submission status: Time Limit Exceeded

Solution code:
# from fractions import gcd
# mod = 10 ** 9 + 7
# N = int(input())
# a = list(map(int,input().split()))
# a,b,c = map(int,input().split())
# ans = [0] * N
# math.ceilで切り上げ
# dp = [[0] * 4 for i in range(3)] #2次元配列初期化
# dp = [[[0] * 2 for i in range(3)] for j in range(5)]
# (ord('A'))でASCII出力 Aは65
import math
import statistics
import string


def intinput():
    return int(input())
def listintinput():
    return list(map(int, input().split()))
def splitintinput():
    return map(int, input().split())
def factorialsurplus(x, y, z):  # xからyまでの階乗をzで割ったあまりを求める
    ret = 1
    for i in range(x, y + 1):
        ret = (ret * i) % z
    return (ret)
def isprime(x):
    if x == 1:
        return False
    if x == 2:
        return True
    sq = int(math.sqrt(x))
    for i in range(2,sq+1):
        if x % i == 0:
            return False
    return True
def hcfnaive(a,b):#最大公約数 math.gcdが使えない時の手段として入れとく。pythonが更新されたらいらなくなるはず
    if(b==0):
        return a
    else:
        return hcfnaive(b,a%b)

s = input()
Q = intinput()

for i in range(Q):
    a = input()
    if a[0] == "1":
        s = s[::-1]
    else:
        b = a.split()
        if b[1] == "1":
            s = b[2] + s
        else:
            s = s + b[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

reverse = 0
pre = ''
after = ''
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == '1':
        reverse = 1 - reverse
    else:
        T, F, C = q.split()
        if (F == '1') ^ (reverse == 1):
            pre += C
        else:
            after += C
pre = pre[::-1]
S = pre + S + after
if reverse:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
direction = 1
mae = ""
usiro = ""
for i in range(q):
    # print(i)
    qi = input()
    if qi == "1":
        direction *= -1
        # print(123, direction)
    else:
        tfq = list(qi.split())
        # print(tfq)
        t = tfq[0]
        f = tfq[1]
        q = tfq[2]

        if f == "1":
            if direction == 1:
                mae += q
            else:
                usiro += q
        else:
            if direction == 1:
                usiro += q
            else:
                mae += q
    # print(i, direction)

if direction == -1:
    s = usiro[::-1] + s[::-1] + mae
else:
    s = mae[::-1] + s + usiro
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
#n = int(input())
#a, b = map(int, input().split())
s = input()
#x = list(map(int, input().split()))
q = int(input())
l = []
for i in range(len(s)):
    l.append(s[i])

rev = 1
for i in range(q):
    que = list(map(str, input().split()))
    if que[0] == "1":
        rev *= -1

    else:
        if rev == 1:
            if que[1] == "1":
                l.insert(0, que[2])
            else:
                l.append(que[2])

        else:
            if que[1] == "1":
                l.append(que[2])
            else:
                l.insert(0, que[2])

if rev == 1:
    for i in range(len(l)):
        print(l[i], end="")
else:
    for i in range(len(l)):
        print(l[len(l) - i - 1], end="")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = list(S)
for i in range(Q):
    if query[i][0] == '1':
        ans = ans[::-1]
        continue
    elif query[i][0] == '2':
        f = query[i][1]
        c = query[i][2]
        if f == '1':
            ans = [c] + ans
        else:
            ans = ans + [c]
print(''.join(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rotate = 1
for i in range(Q):
    n = [str(i) for i in input().split()]
    if n[0] == "1":
        rotate *= -1
    else:
        if n[1] == "1":
            if rotate == 1:
                S = n[2] + S
            else:
                S = S + n[2]
        else:
            if rotate == 1:
                S = S + n[2]
            else:
                S = n[2] + S
if rotate == 1:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for _ in range(Q):
    target = input()
    if target == '1':
        S=S[::-1]
    else:
        a,b,c=target.split()
        if b=='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):
  tfc = input().split()
  if tfc[0] == '1':
    s = s[::-1]
  else:
    if tfc[1] == '1':
      s = tfc[2] + s
    else:
      s = s + tfc[2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for q in query:
    if len(q) == 1:
        S = S[::-1]
    else:
        if int(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())
reverse = False
for _ in range(Q):
    tmp = input().split()
    q = int(tmp[0])
    if q == 1:
        reverse = not reverse
    elif q == 2:
        f = int(tmp[1])
        if f == 1:
            if reverse:
                S = S + tmp[2]
            else:
                S = tmp[2] + S
        elif f == 2:
            if reverse:
                S = tmp[2] + S
            else:
                S = S + tmp[2]
if reverse:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
mode = 0
for i in range(q):
  read = list(map(str,input().split()))
  if read[0]=='1':
    mode = (mode+1)%2
  else:
    if (read[1]=='1' and mode == 0) or (read[1]=='2' and mode == 1):
      s = [read[2]]+ s
    else:
      s = s + [read[2]]
if mode == 0:
  print(''.join(s))
else:
  print(''.join(s[::-1]))

Submission status: Time Limit Exceeded

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

# 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=next(tokens)
    Q=int(next(tokens))
    queries = []
    invert = False
    for i in range(Q):
        t = int(next(tokens))
        if t == 1:
            queries.append((t,))
        else:
            queries.append((t,int(next(tokens)),next(tokens)))
    for query in queries:
        if query[0] == 1:
            invert = not invert
        else:
            if query[1] == 1:
                if invert:
                    S = S+query[2]
                else:
                    S = query[2] + S
            else:
                if not invert:
                    S = S+query[2]
                else:
                    S = query[2] + S
    print(S if not invert else ''.join(list(reversed(S))))
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
mode=0
for i in range(Q):
  query=list(input().split())
  if query[0]=='1':
    mode=(mode+1)%2
  if query[0]=='2':
    if query[1]=='1':
      if mode==0:
        S=query[2]+S
      else:
        S=S+query[2]
    else:
      if mode==0:
        S=S+query[2]
      else:
        S=query[2]+S
if mode==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)]

reverse=False
for q in query:
    if q[0]=="1":
        reverse = not reverse
    else:
        if q[1]=="1":
            if reverse:
                S=S+q[2]
            else:
                S=q[2]+S
        else:
            if reverse:
                S=q[2]+S
            else:
                S=S+q[2]
if reverse:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
s1="";s2=""
adds=[s1,s2]
sta=0
for i in range(q):
    a=list(input())#;print(a)
    if a[0]=="1":
        sta^=1
    else:
        if a[2]=="1":
            if not sta:
                s1=a[4]+s1
            else:
                s2+=a[4]
        if a[2]=="2":
            if sta:
                s1=a[4]+s1
            else:
                s2+=a[4]                
ans=s1+s+s2
#print(s1,s,s2)
if sta:
    print(ans[::-1])
else:print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    l = input()
    if l == "1":
        S = S[::-1]
    elif len(l) > 1:
        t, f, c = l.split()
        if f == "1":
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

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

for query in Q:
    if query[0] == '1':
        s = s[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            s = query[2] + s
        else:
            s = s + query[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
input=sys.stdin.readline
moji=deque([])
s=input().rstrip()
for i in range(len(s)):
	moji.append(ord(s[i]))
q=int(input())
rev=0
for _ in range(q):
	qw=input().split()
	if qw[0]=="1":
		rev+=1
	else:
		f=qw[1]
		c=qw[2]
		if f=="1":
			if rev%2==0:
				moji.appendleft(ord(c))
			else:
				moji.append(ord(c))
		else:
			if rev%2==0:
				moji.append(ord(c))
			else:
				moji.appendleft(ord(c))
ans=""
for i in range(len(moji)):
	ans+=chr(moji[i])
if rev%2==0:
	print(ans)
else:
	print(ans[::-1])

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    IN = list(map(str,input().split()))
    if IN[0] == "1":
        S = S[::-1]
        continue
    _,F,C = IN
    if F == "1":
        S = C + S
    else:
        S += C

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
muki = 1
one = ''
two = ''

for i in range(Q):
    I = input().split()
    if I[0] == '1':
        muki *= -1
    elif I[0] == '2':
        F = int(I[1])
        C = I[2]
        if F==1:
            if muki == 1:
                t = [one, C ]
                one = ''.join(t)
            else:
                t = [two, C ]
                two = ''.join(t)
        elif F==2:
            if muki == 1:
                t = [two, C ]
                two = ''.join(t)
            else:
                t = [one, C ]
                one = ''.join(t)
if muki == 1:
    ANS = [one[::-1],S,two]
    print(''.join(ANS))
else:
    ANS = [two[::-1],S[::-1],one]
    print(''.join(ANS))