peterverebics/CodeNet_Python_118 huggingface dataset:
n, a, b = map(int, input().split())
ret = 0
ret += a*n//(a+b)
rem = n%(a+b)
ret += min(rem, a)
print(ret)
n, a, b = map(int, input().split())
 
if a == 0:
  print(0)
elif a + b == 0:
  print(0)
else:
  q1, mod1 = divmod(n, (a + b))
  q2, mod2 = divmod(mod1, a)
  ans = (q1 + q2) * a + mod2
  print(ans)
n, a, b = map(int, input().split())

if n%(a+b) == 0:
  print(n//(a+b)*a)
else:
  tmp = n%(a+b)
  if tmp <= a:
    print(n//(a+b)*a + tmp)
  else:
    print(n//(a+b)*a)
N, A, B = map(int, input().split())
loop = N // (A + B)
amari = N % (A + B)
if amari >= A:
    blue_amari = amari - A
else:
    blue_amari = 0
blue = B * loop + blue_amari
print(blue)
def main():
  n,a,b = (int(x) for x in input().split())

  num_x = 0
  num_a = int(n / (a + b))
  num_b = int(n % (a + b))
  
  if(num_b >= a):
    nun_x = a
  else:
    num_x = num_b

  print(num_a * a + num_x)
  
if __name__ == '__main__':
  main()
N,A,B = map(int,input().split())
b = N%(A+B)+(N//(A+B))*A
print(b)
n, a, b = map(int,input().split())

c = n // (a + b)
if c == 0:
    d = a + b
else:
    d = n - (c * (a+b))

if d > a:
    ans = (c*a) + a
else:
    ans = (c*a) + d
    
print(ans)
N,A,B = map(int, input().split())

a = N // (A+B)

if A == 0:
  ans = 0
elif B == 0:
  ans = N
elif a > 1:
  ans = A*a + A
else:
  ans = A + (N-(A+B))

print(ans)
n,a,b = map(int,input().split())
ans = 0
q = n // (a+b)
ans += q*a
q = n - (a+b)*q
if q - a >= 0:
    ans += a

print(ans)
N,A,B = map(int,input().split())

ans = A + (N - (A + B))
if A == 0:
    print('0')

elif B == 0:
    print(N)
    
else:
    print(ans)
n,a,b = map(int,input().split())
print(n%(a+b)+n//(a+b)*a)
n,a,b=map(int,input().split())
print((n//(a+b))*(n-b))
n, a, b = [int(x) for x in input().split()]

if a == 0 and b == 0:
    print("0")
elif a >= n:
    print(n)
else:
    x = n // (a + b)
    if x == 0:
        print(a)
    else:
        if (n - ((a + b) * x)) >= a:
            print(a * x + a)
        else:
            print((a + b) * x)
N,A,B = map(int,input().split())
C = A+B
D = C//N
ans = 0
ans+=D*A
ans+=min(N-D*C,A)
print(ans)
N, A, B = map(int, input().split())

if A == 0:
	print(0)
else:
	print(A * N // (A + B) + min(N % (A + B), A))
n, a, b = map(int, input().split())
c = a + b
if (n % c >= a):
  m = a
else:
  m = n % c
print(round(n / c * a + m))
N, A, B = map(int, input().split())
res = ((N // (A + B)) * A) + (N % (A + B))
print(res)
N,A,B = map(int,input().split())

a = N // (A+B)
b = N % (A+B)
print(A*a + b)
n,a,b = map(int,input().split())
c = a + b
ans = n/(a+b)*b + min(n%c,b)

print(ans)
import math

str = input()

nums = [int(x) for x in str.split(' ')]

ct = nums[0] / (nums[1] + nums[2])
res = nums[0] % (nums[1] + nums[2])

if res > nums[1]:
  res = nums[1]

blue_ball = nums[1] * ct + res

print(math.floor(blue_ball))
N,A,B = (int(x) for x in input().split())
c = N // (A + B)
p = N - c * (A + B)
a = c * A
a += a if p > B else p
print(a)
n,a,b = map(int,input().split())
l = []

def append():
    for i in range(a):
        l.append("b")
        if len(l)==n:
            return
    for k in range(b):
        l.append("r")
        if len(l)==n:
            return

append()
if len(l)<n:
    append()


#print(l)
print(l.count('b'))
n, a, b = map(int,input().split())
x = n//(a+b)
y = n%(a+b)
print(x*a+y)
ball = input()
N, A, B = ball.split()
N = int(N)
A = int(A)
B = int(B)
if 1<=N<=(10**18):
    if A >= 0 and B >= 0:
        if 0 < (A+B) <= (10**18):
            if A == 0:
                print(A)
            else:
                print(N-B)
import math
List = [int(i) for i in input().split()]
print(math.floor(List[0] / (List[1] + List[2])) * List[1] + List[0] % (List[1]+List[2]))
N, A, B = [int(x) for x in input().split()]
ans = 0
kake = 0
AAA = 10 ** 18

if A + B == N:
    ans = A
elif N == 0:
    ans = 0
elif A == 0:
    ans = 0
elif B == 0:
    ans = N
elif A + B > AAA:
    ans = 0
elif N > AAA:
    ans = 0
else:
    kake = int(N // (A + B))
    mod = int(N % (A + B))
    if mod <= A:
        print(kake*A + mod)
    else:
        print(kake*A + A)
N,A,B = map(int, input().split())
S = 0
T = 0

for i in range(N):
    i = i + A
    T = A
    if(i > N):
        T = T + (i-N)
    if(i <= N):
        i = i + B
    else :
        break
    
print(T)
n,a,b = map(int,input().split())
if a == 0:
    print(0)
if n-b >= 0:
    print(n-b)
else:
    print(a)
n,a,b = map(int,input().split())
c = a+b
if n%c <= a:
    ans = a * (n//c) + n%c
else:
    ans = a *(n//c)
print(ans)
N, A, B = map(int, input().split())

if A + B == 0:
    print(0)
elif A+B >= N:
    print(A)
else:
    print((N // (A+B))*A + N % (A+B))
n, a, b = map(int,input().split())
k=0
l=0

if a <= 0 and b <= 0:
    print("0")
    exit()

if a >= n:
    k = a - (a - n)
elif a < n:
    k = a * (n // (a+b))
    l = n % (a + b)

print(k + l)
N,A,B=map(int,input().split())
a=(N//(A+B))*A
b=N%(A+B)

if b<=A:
  print(a+b)
else:
  print(a)
from collections import deque

s = input()
q = int(input())
revflag = 1
d = deque(s)
for i in range(q):
	tfc = list(map(str, input().split()))
	if tfc[0] == '1':
		revflag *= -1
	elif tfc[1] == '1':
		if revflag > 0:
			d.appendleft(tfc[2])
		else:
			d.append(tfc[2])
	else:
		if revflag > 0:
			d.append(tfc[2])
		else:
			d.appendleft(tfc[2])
if revflag > 0:
	print(''.join(d))
else:
	print(''.join(reversed(d)))
S = input()
Q = int(input())

prefix = ''
suffix = ''
mode = 0
for _ in range(Q) :
  Query = list(input().split())
  if Query[0] == '1' :
    mode ^= 1
  else :
    _, F, C = Query
    F = int(F) - 1
    
    if mode != F :
      suffix += C
    else :
      prefix += C
      

if mode == 0 :
  print(prefix[::-1] + S + suffix)
else :
  print(suffix[::-1] + S[::-1] + prefix)
s = input()
q = int(input())
jun = 1
f = "";t = ""
for _ in range(q):
  
  tmp = input()
  if len(tmp)==1:
    jun = 1 if jun==0 else 0 
  else:
    _,ff,c = tmp.split()
    if jun+int(ff) ==2:
      f = c+f
    else:
      t += c
if jun==1:
  print(f+s+t)
else:
  print((f+s+t)[::-1])
S = input()
Q = int(input())

jun = True
head = ""

for _ in range(Q):
    T = input().split()
    if T[0] == "1":
        jun = not jun
    
    else:

        if (T[1] == "1" and jun) or (T[1] == "2" and not jun):
            head += T[2]
            continue

        else:
            S += T[2]

if jun:
    print(head[::-1] + S)
else:
    print(S[::-1] + head)
S=input()
p=1
head=""
tail=""
Q=int(input())
for _ in range(Q):
  q=input()
  if q=='1':
    p^=1
  else:
    a,f,c=q.split()
    f=int(f)%2
    if p==0:
      f=(f+1)%2
    if f:
      head+=c
    else:
      tail+=c
if p:
  S=head[::-1]+S+tail
else:
  S=tail[::-1]+S[::-1]+head
print(S)
from collections import deque
 
S = input()
Q = int(input())
S = deque(S)


flg_reverse = False
for q in range(Q):
  _ = input()
  if _[0] == '1':
    flg_reverse = flg_reverse ^ True
  else:
    _T, _F, _C = _.split()
    if (_F == '1') ^ flg_reverse:
      S.appendleft(_C)
    else:
      S.append(_C)        

S = list(S)
# reverseの部分を対応
if flg_reverse:
  S.reverse()

print(''.join(S))
s = input()
q = int(input())
r, st1, st2 = 0, "", ""
for _ in range(q):
  query = input()
  if query[0] == "1":
    r += 1
  else:
    t, f, c = query.split()
    if (r + int(f)) % 2:
      st1 += c
    else:
      st2 += c

string = "".join(reversed(list(st1))) + s + st2
if r % 2 == 0:
  print(string)
else:
  print("".join(reversed(list(string))))
from collections import deque
Q=deque(list(input()))
rev=0

for _ in range(int(input())):
  query=input()
  if query[0]=='1':
    rev^=1
  else:
    q,f,c=query.split()
    if (f=='1')^(rev):
      Q.appendleft(c)
    else:
      Q.append(c)

if rev:
  Q=reversed(Q)
print(''.join(Q))
s = input()
q = int(input())

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

print(t[::-1] + s)
from collections import deque
S=list(input())
Q=int(input())
ls=[list(input().split()) for _ in range(Q)]
D=deque()
for i in range(len(S)):
  D.append(S[i])  
a=1
for i in range(Q):
  if ls[i][0]=="1":
    a*=(-1)
  else:
    if (ls[i][1]=="1" and a==1) or (ls[i][1]=="2" and a==-1):
      D.appendleft(ls[i][2])
    else:
      D.append(ls[i][2])
if a==1:
  while D:
    print(D.popleft(), end="")
else:    
  while D:
    print(D.pop(), end="")
from collections import deque

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

ANS = deque(S)

rev = 0

for _ in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        rev = rev ^ 1
    else:
        f, c = int(query[1]), query[2]
        if (f - 1) ^ rev:
            ANS.append(c)
        else:
            ANS.appendleft(c)

if rev:
    ANS.reverse()

print(''.join(ANS))
s = input()
q = int(input())
f = []
b = []
h = 0

for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        f, b = b, f
        h = 1 - h
    elif len(a) >= 3:
        if a[1] == "1":
            f.append(a[2])
        elif a[1] == "2":
            b.append(a[2])

if h == 0:
    print("".join(f[::-1]) + s + "".join(b))
else:
    print("".join(f[::-1]) + s[::-1] + "".join(b))
S = list(input())
q = int(input())
procedure = [list(input().split()) for i in range(q)]

cnt = 0
S2 = []

for i in range(q):
    if procedure[i][0] == '1':
        cnt += 1
    if procedure[i][0] == '2':
        if (procedure[i][1] == '1' and cnt%2 == 0) or (procedure[i][1] == '2' and cnt%2 == 1):
            S2.append(procedure[i][2])
        else:
            S.append(procedure[i][2])

S2.reverse()
S = S2 + S
if cnt%2 == 1:
    S.reverse()

print(''.join(S))
s=input()
n=len("s")
q=int(input())
l=[]
start=""
end=""
m=0
for i in range(q):
    l.append(input())
    if l[i]=="1":
        m += 1

    else:
        due,f,c=l[i].split()
        if (f=="1" and m % 2==0) or (f !="1" and m % 2==1) :
            start += c
        else:
            end += c
if m % 2 ==0:

    start=start[::-1]
    s = start + s + end

else:
    s = s + end
    s = s[::-1]
    s = s + start

print(s)
from collections import deque

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

rev = True

for i in range(q):
    q = list(input().split())
    if(int(q[0]) == 1):
        rev = rev ^ True
    else:
        n = int(q[1])
        if((n == 1 and rev) or (n == 2 and not rev)):
            s.appendleft(q[2])
        else:
            s.append(q[2])


txt = ''
if rev:
    txt = ''.join(s)
else:
    while not(not s):
        txt += s.pop()

print(txt)
from collections import deque

S = input()
dq = deque(S)

Q = int(input())
inverse = False
for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        inverse = not inverse
    else:
        tmp = (int(query[1]) - 1) ^ inverse
        if tmp: dq.append(query[2])
        else: dq.appendleft(query[2])

if inverse: dq.reverse()
for c in dq:
    print(c, end='')
print('')
from collections import deque
S = deque(list(input()))
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 1 :
      S.appendleft(b[1])
    else:
      S.append(b[1])
D = list(S)
print ("".join(D) if f % 2 == 0 else "".join(D[::-1]))
I = input
s = I()
q = int(I())
a = [], []
i = 0
for _ in range(q):
  x = I()
  if x[0] == '1':
    i ^= 1
  else:
    f = int(x[2]) - 1
    a[i ^ f].append(x[4])
print(''.join(reversed(a[i])) + (s[::-1] if i else s) + ''.join(a[i^1]))
from collections import deque
S = input()
S = deque(S)

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

print(''.join(reversed(S)) if flipped else ''.join(S))
from collections import deque
s = input()
q = int(input())
Q = [list(input().split()) for _ in range(q)]

ans = deque(s)
ok = True
for v in Q:
    if v[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if v[1] == "1":
            if ok: ans.appendleft(v[2])
            else: ans.append(v[2])
        else:
            if ok: ans.append(v[2])
            else: ans.appendleft(v[2])
ans = list(ans)
if not ok: ans = ans[::-1]
print("".join(ans))
s  = input()
q  = int(input())
inv = 1
sfb = [0,"",""]
for i in range(q):
    ls = list(input().split())
    if ls[0] == "1":
        inv *=-1
    else:
        f,c = ls[1:]
        if f == "1":
            sfb[inv] +=c
        else:
            sfb[-inv] +=c

ans = sfb[1][::-1] + s + sfb[-1]
print(ans[::inv])
s=input()
n=int(input())
f=0
l=""
r=""
for i in range(n):
	q=input().split()
	if q[0]=="1":
		f^=1
	else:
		if (f==0 and q[1]=="1") or (f==1 and q[1]=="2"):
			l=l+q[2]
		else:
			r=r+q[2]

if f==0:
	ans=l[::-1]+s+r
else:
	ans=r[::-1]+s[::-1]+l

print(ans)
from collections import deque

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

rev = False
for i in range(q):
    query = input().split()
    if query[0] == "1":
        rev ^= True
    else:
        if (query[1] == "1" and not rev) or (query[1] == "2" and rev):
            s.appendleft(query[2])
        else:
            s.append(query[2])

if rev:
    print("".join(list(s)[::-1]))
else:
    print("".join(list(s)))
from collections import deque

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

asc = True
for _ in range(q):
    qs = input()
    if qs[0]=="1":
        asc = not asc
        continue

    _,f,c = qs.split()
    if f=="1":
        if asc:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if asc:
            s.append(c)
        else:
            s.appendleft(c)

if not asc:
    s.reverse()
print("".join(s))
s = input()
q = int(input())
l = []
r = []
c = 0
for _ in range(q):
  t = input().split()
  if t[0] == '1':
    c += 1
    a = l
    l = r
    r = a
  else:
    if t[1] == '1':
      l.append(t[2])
    else:
      r.append(t[2])
l = "".join(l[::-1])
r = "".join(r)
if c%2 ==0:
  print(l + s + r)
else:
  print(l + s[::-1] + r)
from collections import deque

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

inv = 0
for i in range(Q):
  query = input().split()
  if query == ['1']:
    inv = (inv + 1)%2
  else:
    f, c = query[1], query[2]
    if f == '1':
      if inv:
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if inv:
        S.appendleft(c)
      else:
        S.append(c)

S = list(S)
if inv:
  print(''.join(S[::-1]))
else:
  print(''.join(S))
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


S = input()
Q = int(input())
q = deque(S)
flipped = False
for _ in range(Q):
    q_t, *q_d = input().split()
    if q_t == "1":
        flipped = not flipped
    else:
        F, C = q_d
        if (not flipped and F == "2") or (flipped and F == "1"):
            q.append(C)
        else:
            q.appendleft(C)

ans = list(q)
if flipped:
    print("".join(ans[::-1]))
else:
    print("".join(ans))
s=input()
n=int(input())
head=[]
end=[]
rev = False
for i in range(n):
    q = list(map(str,input().split()))
    if q[0]=='1':
        head,end = end, head
        rev = not rev
    elif q[0]=='2':
        if q[1]=='1':
            head.append(q[2])
        else:
            end.append(q[2])

head.reverse()
if rev:
    ans = list(s)
    ans.reverse()
else:
    ans = list(s)
print("".join(head+ans+end))
from collections import deque

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

deq = deque([s])
flag = 1
for i in range(q):
    query = input().split()
    if query[0]==str(1):
        flag*=-1 
    if query[0]==str(2):
        x = 1 if query[1]==str(1) else -1
        if flag*x==1:
            deq.appendleft(query[2])
        else:
            deq.append(query[2])

if flag==-1:
    print(''.join(deq)[::-1])
else:
    print(''.join(deq))
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)
S = input()
Q = int(input())

dir_changed = False
hd, ta = [], []

for _ in range(Q):
  A = list(input().split())
  if A == ["1"]:
    dir_changed = not dir_changed
    hd, ta = ta, hd
    continue
  F, C = int(A[1]), A[2]
  #if dir_changed:
  #  F = 3-F
  if F == 1:
    hd.append(C)
  else:
    ta.append(C)
    
if dir_changed:
  S = S[::-1]
print("".join(hd[::-1]) + S + "".join(ta))
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=deque(S)
reverse=False
for i,q in enumerate(query):
  if len(q)==1:
    reverse=not reverse
  else:
    _,f,c=q
    if (f=='2') ^ reverse:
      ans.append(c)
    else:
      ans.appendleft(c)
if reverse:
  print(*reversed(ans),sep='')
else:
  print(*ans,sep='')
from collections import deque

d = deque(list(input()))
flag = 1
for _ in range(int(input())):
    a = input()
    if a[0] == '1':
        flag *= -1
    elif a[2] == '1' and flag == 1:
        d.appendleft(a[4])
    elif a[2] == '1' and flag == -1:
        d.append(a[4])
    elif a[2] == '2' and flag == 1:
        d.append(a[4])
    else:
        d.appendleft(a[4])
if flag == -1:
    d.reverse()
print(*d, sep='')
from collections import deque
s = deque(i for i in input())
n = int(input())
l = [[i for i in input().split()] for _ in range(n)]

front = True

for i in l:
    if i[0] == '1':
        front = not front
    else:
        if front ^ (int(i[1]) == 1):
            s.append(i[2])
        else:
            s.appendleft(i[2])
            
ans = ''.join(s)

if front:
    print(ans)
else:
    ans = ans[::-1]
    print(ans)
from collections import deque

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

inv_flag = False
for _ in range(Q):
    query = list(input().split())
    if int(query[0]) == 1:
        inv_flag = not inv_flag
    else:
        if (int(query[1]) - 1) ^ inv_flag:
            S.append(query[2])
        else:
            S.appendleft(query[2])
if inv_flag:
    print(''.join(reversed(S)))
else:
    print(''.join(S))
from collections import deque
s = deque(input())
q = int(input())

flag = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": flag = 1 - flag
  else:
    if query[1] == "1":
      if flag == 0: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if flag == 0: s.append(query[2])
      else: s.appendleft(query[2])

if flag == 1: s.reverse()
print(*s, sep="")
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
Q = ir()
rev = 0
top = ''
tail = ''
# xor
for _ in range(Q):
    x = sr().split()
    if x[0] == '1':
        rev ^= 1
    else:
        _, f, c = x
        if (int(f)^rev) & 1:
            top += c
        else:
            tail += c

answer = top[::-1] + S + tail
if rev&1:
    answer = answer[::-1]
print(answer)
# 40
s = input()
q = int(input())
h, t = [], []
p = 0
for i in range(q):
	c = input().split()
	if c[0] == '1':
		h, t = t, h
		p = 1 - p
	else:
		if c[1] == '1':
			h.append(c[2])
		else:
			t.append(c[2])
if p == 1:
	s = s[::-1]
print("".join(h[::-1]) + s + "".join(t))
from collections import deque
import sys
input = sys.stdin.readline

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

t = 0
for _ in range(q):
  query = list(input().split())
  query[0] = int(query[0])
  if query[0] == 1: t = 1 - (t)
  else:
    if t == 0:
      if int(query[1]) == 1: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if int(query[1]) == 1: s.append(query[2])
      else: s.appendleft(query[2])
if t == 0: print(*s, sep = '')
else: print(*reversed(s), sep = '')
from collections import deque

string = input()
x = deque()
for s in string:
    x.append(s)

head_left = True
B = int(input())
for i in range(B):
    cmd = input().split()
    if len(cmd) == 1 and cmd[0] == "1":
        head_left = not head_left
    else:
        _, n, c = cmd
        if (n == "1" and head_left) or (n == "2" and not head_left):
            x.appendleft(c)
        else:
            x.append(c)

if head_left:
    print("".join(list(x)))
else:
    print("".join(reversed(list(x))))
# -*- coding: utf-8 -*-
S = input()
Q = int(input())

flip = 0
ad1 = []
ad2 = []
for i in range(Q):
  st = input()
  if(st[0] == '1'):
    flip += 1
  else:
    if((st[2] == '1' and flip % 2 == 0) or (st[2] == '2' and flip % 2 == 1)):
      #nS = st[4] + S
      ad1.append(st[4])
    else:
      #nS = S + st[4]
      ad2.append(st[4])

ft = ''.join(ad1)
ft = ft[::-1]
ed = ''.join(ad2)

S = ft + S + ed

if(flip % 2 != 0):
  S = S[::-1]
print(S)
from collections import deque
def main():
  d=deque(list(input()))
  Q=int(input())
  rev=0
  for i in range(Q):
    q=list(map(str, input().split()))
    if q[0]=='1':
      rev+=1
    else:
      if (int(q[1])+rev)%2==1:
        d.appendleft(q[2])
      else:
        d.append(q[2])
  if rev%2==0:
    print(''.join(list(d)))
  else:
    d.reverse()
    print(''.join(list(d)))
main()
from collections import deque
d = deque()
d.extend(list(input()))
q = int(input())
z = 0
F = ""
R = ""
for i in range(q):
    a = tuple(input().split())
    if a[0] == "1":
        z += 1
        z %= 2
    else:
        if (int(a[1]) + z) % 2 == 0:
            d.append(a[2])
        else:
            d.appendleft(a[2])

ans = "".join(d)
if z % 2 == 0:
    print(ans)
else:
    print(ans[::-1])
s = input()
Q = int(input())

rev_count = 0
sbf=''
saf=''
for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            saf = saf + que[4]
        else:
            sbf = que[4] + sbf

print(sbf+s+saf if rev_count %2 == 0 else (sbf+s+saf)[-1::-1])
s = input()
p = int(input())

dir = 0
mae = []
usi = []

for _ in range(p):
  Q = input().split()
  if Q[0] == "1":
    dir = not dir
  else:
    if (int(Q[1]) - 1) == dir:
      mae.append(Q[2])
    else:
      usi.append(Q[2])
      
ans = "".join(mae[::-1]) + s + "".join(usi)
      
if not dir:
  print(ans)
else:
  print(ans[::-1])
s=input()
n=int(input())
r=-1
f=[]
b=[]
for i in range(n):
    q=input().split()
    if q[0]=='1':
        r*=-1
    else:
        t=int(q[1])
        if t*r==-1 or t*r==2 :
            f.append(q[2])
        else:
            b.append(q[2])
f=''.join(f)
b=''.join(b)
s=f[::-1]+s+b
if r==1:
    s=s[::-1]
print(s)
#!/usr/bin/env python3
s, _, *q = open(0)
g = ""
s = s.strip()
for q in q:
  q = q.strip().split()
  if q[0] == "1":
    g, s = s, g
  else:
    if q[1] == "1":
      g += q[2]
    else:
      s += q[2]
print(g[::-1] + s)
import sys
S = input()
Q = int(input())
pin = 0
left = ''
right = ''
for i in range(Q):
  t = list(sys.stdin.readline().split())
  if int(t[0]) == 1:
    pin += 1
  else:
    if int(t[1]) % 2 == pin % 2 :
      right = right + t[2]
    else:
      left = t[2] + left
S = left + S + right
if pin % 2 == 1:
  S = S[::-1]
print(S)
s,_,*q=open(0)
a=['','']
p=1
for q in q:
  p^=q[0]<'2'
  a[(q<'2 2')^p]+=q[-2]*(q[0]>'1')
print(a[p^1][::-1]+s[:-1][::p or-1]+a[p])
S = input()
Q = int(input())
T1 = 0
S_left = ''
S_right = ''
for _ in range(Q):
  a = list(map(str,input().split()))
  if a[0] == '1':
    T1 += 1
  elif a[0] == '2':
    if ((a[1]=='1')and(T1%2==0)) or ((a[1]=='2')and(T1%2!=0)):
      S_left = a[2]+S_left
    else:
      S_right = S_right+a[2]
S = S_left+S+S_right
if T1 % 2 == 0:
  print(S)
else:
  print(S[::-1])