Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###


def get_sieve_of_eratosthenes(n):
    prime = [1, 2]
    limit = int(n**0.5)
    data = [i + 1 for i in range(2, n, 2)]
    while True:
        p = data[0]
        if limit <= p:
            return prime + data
        prime.append(p)
        data = [e for e in data if e % p != 0]

def get_gcd(a, b):
    if b > a:
        a, b = b, a
    r = a%b
    if r == 0:
        return b
    else:
        return get_gcd(b, r)

A, B = map(int, input().split())
gcd = get_gcd(A, B)
if gcd == 1:
  print("1")
else:
  primes = get_sieve_of_eratosthenes(gcd)
  divisors = [p for p in primes if gcd%p==0]
  print(len(divisors))

### FIXED CODE ###

def get_gcd(a, b):
    if b > a:
        a, b = b, a
    r = a%b
    if r == 0:
        return b
    else:
        return get_gcd(b, r)


def prime_factorize(n):
    answers = []
    while n % 2 == 0:
        answers.append(2)
        n //= 2
    f = 3
    while f**2 <= n:  # instead of f <= n**0.5
        if n % f == 0:
            answers.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        answers.append(n)
    return answers

A, B = map(int, input().split())
gcd = get_gcd(A, B)
factors = prime_factorize(gcd)
print(len(set(factors+[1])))
### BUGGY CODE ###

import collections

N = int(input())
D = list(map(int, input().split()))
M = int(input())
T = list(map(int, input().split()))

D.sort()
T2 = collections.Counter(T)

for i in T2.keys():
    if T2[i] > D.count(i):
        print("NO")
        exit()

print("YES")


### FIXED CODE ###

import collections
import bisect

N = int(input())
D = list(map(int, input().split()))
M = int(input())
T = list(map(int, input().split()))

D.sort()
T2 = collections.Counter(T)

for i in T2.keys():
    if T2[i] > bisect.bisect_right(D, i) - bisect.bisect_left(D, i):
        print("NO")
        exit()

print("YES")
### BUGGY CODE ###

n, q = map(int, input().split())
s = input()

cnt = [0]*(n-1)

for i in range(n-1):
    if s[i:i+2] == "AC":
        cnt[i] = 1

for j in range(q):
    l, r = map(int, input().split())
    print(sum(cnt[l-1:r-1]))

### FIXED CODE ###

n, q = map(int, input().split())
s = input()

cnt = [0]*(n)

plus = 0
for i in range(n-1):
    if s[i:i+2] == "AC":
        plus += 1
    cnt[i+1] = plus

for j in range(q):
    l, r = map(int, input().split())
    print(cnt[r-1]-cnt[l-1])
### BUGGY CODE ###

def main():
    N, D, A = map(int, input().split())

    XHN = []
    for _ in range(N):
        X, H = map(int, input().split())
        XHN.append([X, H, X + 2 * D])

    XHN.sort()

    ans = 0
    for i in range(N):
        while 0 < XHN[i][1]:
            X, H, bomb_p = XHN[i]
            attack_count = H // -A * -1
            ans += attack_count
            for j in range(i, N):
                if XHN[j][0] <= bomb_p:
                    XHN[j][1] -= attack_count * A
                else:
                    break

    print(ans)


main()


### FIXED CODE ###

from collections import deque


def main():
    N, D, A = map(int, input().split())

    XHN = []
    for _ in range(N):
        X, H = map(int, input().split())
        XHN.append([X, H, X + 2 * D])

    XHN.sort()

    q = deque()
    acc_damege = 0
    ans = 0
    for i in range(N):
        if 0 < XHN[i][1]:
            X, H, bomb_p = XHN[i]

            while q:
                acc_x, acc_d = q[0]
                if acc_x < X:
                    acc_damege -= acc_d
                    del q[0]
                else:
                    break

            H -= acc_damege

            if 0 < H:
                attack_count = H // -A * -1
                damege = A * attack_count
                ans += attack_count
                XHN[i][1] -= damege

                acc_damege += damege
                q.append((bomb_p, damege))

    print(ans)


main()
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))

total = 1
flag = 9
for i in range(N):
  total *= A[i]
  if total > 10**18:
    flag = 0
    break
  elif min(A) == 0:
    flag = 1
    break
if flag == 0:
  print(-1)
elif flag == 1:
  print(0)
else:
  print(total)
    

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))

A.sort()
B = list(set(A))

total = 1
flag = 9
for i in range(len(B)):
  total *= B[i]**A.count(B[i])
  if total > 10**18:
    flag = 0
    break
  elif min(A) == 0:
    flag = 1
    break
  
if flag == 0:
  print(-1)
elif flag == 1:
  print(0)
else:
  print(total)
### BUGGY CODE ###

from collections import Counter
N=int(input())
A=sorted(list(map(int,input().split())))
c=Counter(A)
ans=0
for i in range(N):
    if c[A[i]]>1:
        continue
    for j in range(N):
        if A[i]/2<A[j]:
            ans+=1
            break
        if A[i]%A[j]==0:
            break
    else:
        ans+=1
print(ans)

### FIXED CODE ###

from collections import Counter
N=int(input())
A=sorted(list(map(int,input().split())))
c=Counter(A)
if 1 in A:
    if c[1]==1:
        print(1)
    else:
        print(0)
    exit()
n=max(A)
ans=[0]*(n+1)
for k,v in c.items():
    if v==1:
        ans[k]=1
for i in c.keys():
    t=i*2
    while t<=n:
        ans[t]=0
        t+=i
print(sum(ans))
### BUGGY CODE ###

from collections import deque
 
H, W = [int(i) for i in input().split()]
S = [list(input()) for _ in range(H)]
 
white = sum(s.count(".") for s in S)
 
Q = deque([(0, 0, 1)])
while Q:
    h, w, count = Q.popleft()
    if (h, w) == (H - 1, W - 1):
        print(white - count)
        quit()
 
    S[h][w] = "_"
 
    for nh, nw in [(h, w + 1), (h, w - 1), (h + 1, w), (h - 1, w)]:
        if not (0 <= nh < H and 0 <= nw < W):
            continue
        if S[nh][nw] == ".":
            Q.append((nh, nw, count + 1))
print(-1)

### FIXED CODE ###

from collections import deque

H, W = [int(i) for i in input().split()]
S = [list(input()) for _ in range(H)]

white = sum(s.count(".") for s in S)

S[0][0] = "_"
Q = deque([(0, 0, 1)])
while Q:
    h, w, count = Q.popleft()
    if (h, w) == (H - 1, W - 1):
        print(white - count)
        quit()

    for nh, nw in [(h, w + 1), (h, w - 1), (h + 1, w), (h - 1, w)]:
        if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == ".":
            S[nh][nw] = "_"
            Q.append((nh, nw, count + 1))
print(-1)
### BUGGY CODE ###

from heapq import heappush, heappop
import sys
readline = open(0).readline
xs, ys, xt, yt = map(int, readline().split())
N = int(readline())
C = [list(map(int, readline().split())) for i in range(N)]
C.append((xt, yt, 0))

que = []
dist = [10**18]*(N+1)
rest = set(range(N+1))
for i in range(N+1):
    x, y, r = C[i]
    d0 = (xs - x)**2 + (ys - y)**2
    r0 = (d0)**.5 - r if d0 > r**2 else 0
    dist[i] = r0
    heappush(que, (r0, i))

remove = rest.remove
while que:
    c, i = heappop(que)
    if i not in rest:
        continue
    if i == N:
        break
    xi, yi, ri = C[i]
    remove(i)
    d = dist[i]
    for j in rest:
        x, y, r = C[j]
        d0 = (xi - x)**2 + (yi - y)**2
        r0 = (d0)**.5 - ri - r if d0 > (ri + r)**2 else 0
        if d + r0 < dist[j]:
            dist[j] = v = d + r0
            heappush(que, (v, j))
print("%.016f" % dist[N])

### FIXED CODE ###

from heapq import heappush, heappop
from math import sqrt
xs, ys, xt, yt = map(int, raw_input().split())
n = input()
ps = [map(int, raw_input().split()) for i in xrange(n)]
cant = [0]*n
for i in xrange(n):
    x0, y0, r0 = ps[i]
    for j in xrange(n):
        x1, y1, r1 = ps[j]
        if i == j or r0 > r1: continue
        if (x0 - x1)**2 + (y0 - y1)**2 <= (r1 - r0)**2:
            cant[i] = 1
            break

EPS = 1e-10
que = []
dist = [10**18+7]*n
def calc(x0, y0, r0, x1, y1, r1):
    v = (x0 - x1)**2 + (y0 - y1)**2
    if v <= (r0+r1)**2:
        return 0.
    return sqrt(v) - r0 - r1
for i in xrange(n):
    if cant[i]: continue
    x, y, r = ps[i]
    d = dist[i] = calc(xs, ys, 0, x, y, r)
    heappush(que, (d, i))
while que:
    d, v = heappop(que)
    if dist[v] < d:
        continue
    x0, y0, r0 = ps[v]
    for t in xrange(n):
        if v == t or cant[t]: continue
        x1, y1, r1 = ps[t]
        nd = calc(x0, y0, r0, x1, y1, r1)
        if d+nd < dist[t]:
            dist[t] = d+nd
            heappush(que, (d+nd, t))
ans = sqrt((xs - xt)**2 + (ys - yt)**2)
for i in xrange(n):
    if cant[i]: continue
    x, y, r = ps[i]
    d = calc(xt, yt, 0, x, y, r) + dist[i]
    ans = min(ans, d)
print "%.010f" % ans
### BUGGY CODE ###

print(sum(range(1, int(input()))))

### FIXED CODE ###

n = int(input())

print(n*(n-1) // 2)
### BUGGY CODE ###

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


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            s = s[::-1]
        else:
            if query[1] == "1":
                s = query[2] + s
            else:
                s += query[2]

print(s)


### FIXED CODE ###

from collections import deque

s = input()
q = int(input())
d = deque(s)
reverseFlag = False


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            reverseFlag = not reverseFlag
        else:
            if query[1] == "1":
                if reverseFlag:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
            else:
                if reverseFlag:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])

ans = "".join(d)
if reverseFlag:
    ans = ans[::-1]

print(ans)
### BUGGY CODE ###

n = int(input())

sum = 0

for i in range(n) :
    sum += i

print(sum)

### FIXED CODE ###

n = int(input())

print(n * (n - 1) // 2)
### BUGGY CODE ###

n=int(input())
s=["".join(sorted(input())) for _ in range(n)]
c=0
for i in range(n):
  for j in range(i+1,n):
    if s[i]==s[j]:
      c+=1
print(c)

### FIXED CODE ###

from collections import defaultdict
n=int(input())
s=["".join(sorted(input())) for _ in range(n)]

d = defaultdict(int)
for a in s:
  d[a]+=1
c=0
for a in d.values():
  if a>1:
    c+=a*(a-1)//2
print(c)
### BUGGY CODE ###

import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
negative_A = - np.array(A)

ki_idx = [i for i in range(N) if i % 2 == 1]

A_sum = sum(A)

ans = []
for idx, dam in enumerate(A):
    ans.append(str(A_sum - 2*sum(A[ki_idx])))
    
    A = np.roll(A, -1)
    
print(" ".join(ans))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))

irekae_sum = 0
for idx, a in enumerate(A):
    if idx % 2 == 0:    
        irekae_sum += a
    else:
        irekae_sum -= a
        
ans = []
for i in range(N):
    ans.append(str(irekae_sum))
    irekae_sum = - irekae_sum + 2*A[i]
            
print(" ".join(ans))
### BUGGY CODE ###

def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)


N = int(input())
A = list(map(int, input().split()))

A.sort()
ans = 1

for i in range(N):
    B = A[:]
    B.pop(i)
    tmp=B[0];
    for j in B:
        tmp=gcd(tmp,j)

    ans = max(ans, tmp)

print(ans)

### FIXED CODE ###

def gcd(a, b):
    if b == 0: return a
    return gcd(b, a % b)


N = int(input())
A = list(map(int, input().split()))

L = [0] * (N + 1)
L[0] = 0
R = [0] * (N + 1)
R[0] = 0

for i in range(N):
    L[i + 1] = gcd(L[i], A[i])
    R[i + 1] = gcd(R[i], A[-i - 1])

ans = 0
for i in range(N):
    ans = max(ans, gcd(L[i], R[N - i - 1]))
print(ans)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = 1
for a in A:
    ans *= a
if ans > 10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

import sys

N = int(input())
A = list(map(int, input().split()))

if 0 in A:
    print(0)
    sys.exit();

ans = 1
for a in sorted(A, reverse=True):
    ans *= a
    if ans > 10**18:
        print(-1)
        break
else:
    print(ans)
### BUGGY CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
mi = 0
zero = 0
for a in A:
  if a<0:
    mi+=1
  elif a==0:
    zero+=1
  else:
    break

plu = N-mi-zero
A_mi = A[0:mi:]
A_mi_rev = A_mi[::-1]
A_plu = A[mi+zero::]
A_plu_rev = A_plu[::-1]
num_mi = plu*mi
num_zero = (plu+mi)*zero+(zero*(zero-1))//2
r = max(A[-1]**2+10,A[0]**2+10)
l = A[0]*A[-1]-10
while l+1!=r:
  m = (l+r)//2
  num = 0
  if m>0:
    num+=num_mi+num_zero
    if num>=K:
      r=m
      continue
    cou=0
    for i in range(0,plu):
      while True:
        if cou>=plu-i-1:
          num+=plu-i-1
          break
        x = A_plu_rev[i]*A_plu[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break
    if num>=K:
      r=m
      continue
    cou = 0
    for i in range(0,mi):
      while True:
        if cou>=mi-i-1:
          num+=mi-i-1
          break
        x = A_mi[i]*A_mi_rev[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break

    if num>=K:
      r=m
    else:
      l=m
    
  elif m==0:
    num+=num_mi
    if num>=K:
      r=m
    else:
      l=m
  else:
    cou = 0
    for a in A_mi_rev:
      while True:
        if cou==plu:
          num+=cou
          break
        x = a*A_plu_rev[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break
    if num>=K:
      r = m
    else:
      l = m
      
print(l)

### FIXED CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
mi = 0
zero = 0
for a in A:
  if a<0:
    mi+=1
  elif a==0:
    zero+=1
  else:
    break

plu = N-mi-zero
A_mi = A[0:mi:]
A_mi_rev = A_mi[::-1]
A_plu = A[mi+zero::]
A_plu_rev = A_plu[::-1]
num_mi = plu*mi
num_zero = (plu+mi)*zero+(zero*(zero-1))//2
r = max(A[-1]**2+10,A[0]**2+10)
l = A[0]*A[-1]-10
while l+1!=r:
  m = (l+r)//2
  num = 0
  if m>0:
    num+=num_mi+num_zero
    if num>=K:
      r=m
      continue
    cou=0
    for i in range(0,plu):
      while True:
        if cou>=plu-i-1:
          num+=plu-i-1
          break
        x = A_plu_rev[i]*A_plu[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break
    if num>=K:
      r=m
      continue
    cou = 0
    for i in range(0,mi):
      while True:
        if cou>=mi-i-1:
          num+=mi-i-1
          break
        x = A_mi[i]*A_mi_rev[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break

    if num>=K:
      r=m
    else:
      l=m
    
  elif m==0:
    num+=num_mi
    if num>=K:
      r=m
    else:
      l=m
  else:
    cou = 0
    for a in A_mi_rev:
      while True:
        if cou==plu:
          num+=cou
          break
        x = a*A_plu_rev[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break
    if num>=K:
      r = m
    else:
      l = m
      
print(l)
### BUGGY CODE ###

s = input()
n = int(input())
for i in range(n):
    a = input().split()
    if len(a) == 1:
        s = s[::-1]
    else:
        if int(a[1]) == 1:
            s  = a[2]+s
        else:
            s = s + a[2]
print(s)

### FIXED CODE ###





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)
### BUGGY CODE ###

def Replacing():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    bc = [tuple(map(int,input().split())) for i in range(Q)]
    for i,j in bc:
        res = 0
        for k in range(len(A)):
            if A[k] == i:
                A[k] = j
            res += A[k]
        print(res)
        
Replacing()
        

### FIXED CODE ###

def Replacing():
    N = int(input())
    A = list(map(int,input().split()))
    lis = [0 for i in range(10 ** 5)]
    res = 0
    for i in A:
        lis[i-1] += 1
        res += i
    
    Q = int(input())
    for i in range(Q):
        b, c = map(int,input().split())
        num = lis[b-1]
        res += (c - b) * num
        lis[b-1] = 0
        lis[c-1] += num
        print(res)
        
Replacing()
### BUGGY CODE ###

n = int(input())
a_list = [int(input()) for _ in range(n)]
max_num = max(a_list)
for i in range(len(a_list)):
    if a_list[i] != max_num:
        print(max_num)
    else:
        print(max(a_list[:i]+a_list[i+1:]))

### FIXED CODE ###

import heapq
n = int(input())
a_list = [int(input()) for _ in range(n)]
a_list_heap = list(map(lambda x:(-1)*x,a_list))
heapq.heapify(a_list_heap)
max_num = a_list_heap[0]*(-1)

for i in range(len(a_list)):
    if a_list[i] != max_num:
        print(max_num)
    else:
        tmp = heapq.heappop(a_list_heap)
        print(a_list_heap[0]*(-1))
        heapq.heappush(a_list_heap,tmp)
### BUGGY CODE ###

import fractions
n = int(input())
a = list(map(int, input().split()))
t = []

for i in range(n):
    x = 0
    for j in range(n):
        if j == i:
            pass
        else:
            x = fractions.gcd(x, a[j])
    t.append(x)

print(max(t))

### FIXED CODE ###

from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
result = []

left = [0] * (n+1)
right = [0] * (n+1)
for i in range(n):
    left[i+1] = gcd(left[i], a[i])
for j in range(n-1, -1, -1):
    right[j] = gcd(right[j+1], a[j])
for i in range(n):
    result.append(gcd(left[i], right[i+1]))

print(max(result))
### BUGGY CODE ###

def main():
    s = input()
    n = len(s)
    k = int(input())
    # dp[左端][左端からの右端の距離][使った回数]
    dp = [[[1]*(k+1) for _ in [0]*(n-i)] for i in range(n)]

    for i in range(n-1):
        if s[i] == s[i+1]:
            dp[i][1][0] = 2
    if k > 0:
        for cnt in range(1, k+1):
            for i in range(n-1):
                dp[i][1][cnt] = 2

    for cnt in range(k):
        for d in range(2, n):
            for left in range(n-d):
                right = left+d
                plus = (s[left] == s[right])*2
                dp[left][d][cnt] = max(
                    dp[left][d][cnt], dp[left][d-1][cnt],
                    dp[left+1][d-1][cnt], dp[left+1][d-2][cnt]+plus)
                dp[left][d][cnt+1] = max(
                    max(dp[left][d][cnt:cnt+2]), dp[left+1][d-2][cnt]+2)
    for d in range(2, n):
        for left in range(n-d):
            right = left+d
            plus = (s[left] == s[right])*2
            dp[left][d][k] = max(
                dp[left][d][k], dp[left][d-1][k], dp[left+1][d-1][k], dp[left+1][d-2][k]+plus)
    print(dp[0][-1][-1])


main()

### FIXED CODE ###

def main():
    s = input()
    n = len(s)
    k = int(input())
    # dp[使った回数][左端からの距離][左端]
    dp = [[[1]*(n-i) for i in range(n)] for _ in range(k+1)]

    for i in range(n-1):
        if s[i] == s[i+1]:
            dp[0][1][i] = 2
    if k > 0:
        for cnt in range(1, k+1):
            for i in range(n-1):
                dp[cnt][1][i] = 2

    for cnt in range(k):
        for d in range(2, n):
            for left in range(n-d):
                right = left+d
                plus = (s[left] == s[right])*2
                dp[cnt][d][left] = max(
                    dp[cnt][d][left], dp[cnt][d-1][left],
                    dp[cnt][d-1][left+1], dp[cnt][d-2][left+1]+plus)
                dp[cnt+1][d][left] = max(
                    dp[cnt+1][d][left], dp[cnt][d][left], dp[cnt][d-2][left+1]+2)
    for d in range(2, n):
        for left in range(n-d):
            right = left+d
            plus = (s[left] == s[right])*2
            dp[k][d][left] = max(
                dp[k][d][left], dp[k][d-1][left], dp[k][d-1][left+1], dp[k][d-2][left+1]+plus)
    print(dp[-1][-1][0])


main()
### BUGGY CODE ###

def main():
    n = int(input())
    a = list(map(int, input().split()))
    mod = 10**9+7
    max_len = len(format(max(a), "b"))
    b = [0 for _ in range(max_len)]

    for i in range(n):
        count = 0
        while a[i] > 0:
            b[count] += a[i]%2
            a[i] //= 2
            count += 1

    result = 0
    for i in range(max_len):
        r = b[i]*(n-b[i])%mod
        for j in range(i):
            r = r*2 %mod
        result += r
    print(result%mod)

if __name__=="__main__":
    main()

### FIXED CODE ###

import numpy as np
mod = 10**9+7
n = int(input())
a = np.fromstring(input(), dtype = np.int64, sep = ' ')

result = 0
for i in range(60):
    count_one = int((a&1).sum())
    result += count_one*(n-count_one)*(2**i)
    a >>= 1
print(result%mod)
### BUGGY CODE ###

N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for i in range(M)]

temp = []
for i in range(M):
    temp += [BC[i][1]]*BC[i][0]
    
A += temp

A.sort(reverse = True)

print(sum(A[:N]))

### FIXED CODE ###

from operator import itemgetter

N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = []
for i in range(M):
    B,C = map(int,input().split())
    BC.append([B,C])

BC.sort(key = itemgetter(1),reverse=True)

change = []
for b,c in BC:
    change += [c] * b
    if len(change) > N:
        break
A += change
A.sort(reverse=True)
answer = sum(A[:N])
print(answer)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)
INF = 1<<32


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    
    ans = set(list(range(L[0], R[0]+1)))
    for i in range(1, M):
        ans = ans & set(list(range(L[i],R[i]+1)))

    print(len(ans))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    lm = max(L)
    rm = min(R)
    print(max(0,rm-lm+1))

    return


# Generated by 1.1.4 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()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]" 
    R = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

S = input()
Q = int(input())
s = 0
for i in range(Q):
    q = list(input().split())
    s += int(q[0])
    if q[0]=='2':
        s_i = s + int(q[1]) 
        if s_i%2 == 0:
            S += q[2]
        else:
            S = q[2] + S
if s%2 == 0:
    print(S)
else:
    print(S[::-1])

### FIXED CODE ###

S = input()
Q = int(input())
s = 0
s1 = []
s2 = []
for i in range(Q):
    q = list(input().split())
    s += int(q[0])
    if q[0]=='2':
        s_i = s + int(q[1]) 
        if s_i%2 == 0:
            s1.append(q[2])
        else:
            s2.append(q[2])
s1 = ''.join(s1)
s2 = ''.join(s2)
s2 = s2[::-1]
S = s2 + S + s1
if s%2 == 0:
    print(S)
else:
    print(S[::-1])