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

# -*- coding: utf-8 -*-

#整数値入力 1文字の入力
def input_one_number():
    return int(input())

#整数値龍力　複数の入力
def input_multiple_number():
    return map(int, input().split())

#整数値龍力　複数の入力(配列)
def input_multiple_number_as_list():
    return list(map(int, input().split()))



#複数の最小公倍数
def calc_lcm_list(l):
    lcm = l[0]
    for i in range(1, N):
        lcm = lcm * l[i] // fractions.gcd(lcm, l[i])
    return lcm


import fractions
N = input_one_number()
lstA = input_multiple_number_as_list()

gcd = calc_lcm_list(lstA)

ans = 0
for a in lstA:
    ans += (gcd // a)

print(ans % 1000000007)


### FIXED CODE ###

# -*- coding: utf-8 -*-

#整数値入力 1文字の入力
def input_one_number():
    return int(input())

#整数値龍力　複数の入力
def input_multiple_number():
    return map(int, input().split())

#整数値龍力　複数の入力(配列)
def input_multiple_number_as_list():
    return list(map(int, input().split()))

def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
def lcm(a,b):
    return a*b//gcd(a,b)

def calc_gcd_list(l):
    gcd = l[0]
    for i in range(1, N):
        gcd = gcd(gcd, l[i])
    return gcd

def calc_lcm_list(l):
    lcm = 1
    for i in l:
        lcm = lcm * i // gcd(lcm, i)
    return lcm


N = input_one_number()
lstA = input_multiple_number_as_list()
lcm = calc_lcm_list(lstA)
pow = 10**9 +7
print(sum(lcm//a for a in lstA)%pow)
### BUGGY CODE ###

import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

value = 0
# k+1個取って，ret-1個戻す
for k in reversed(range(K)):
    for ret in range(K-k): #戻す個数はret個
        if (ret < k+1):
            #print('{}個取って{}戻す'.format(k+1,ret))
            for i in range(k+1+1): # 左右の分け方
                tmp = []
                V_tmp = copy.copy(V)
                #print('元々：',V_tmp)
                tmp.extend(V_tmp[:i]) # 左端
                #print('左端：',V_tmp[:i])
                V_tmp = V_tmp[i:]
                #print('左端取った：',V_tmp)
                if (k+1-i) > 0:
                    tmp.extend(V_tmp[-(k+1-i):]) #右端
                    #print('右端：',V_tmp[-(k+1-i):])
                # V_tmp = V_tmp[:-(k+1-i)]
                # print('右端取った：',V_tmp)
                if V_tmp == []:
                    break
                tmp = list(filter(lambda x: x not in sorted(tmp)[:ret],tmp))
                #print('手元の宝石：',tmp)
                value = max(value,sum(tmp))
            #print()

print(value)


### FIXED CODE ###

import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

result = 0
# i個取る
for i in range(min(N,K)+1):
    #print('i:',i)
    for sp in range(i+1): # 左右の分け方
        #print('right',sp)
        #print('left',i-sp)
        tmp = []
        V_tmp = copy.copy(V)
        #print('元々：',V_tmp)
        tmp.extend(V_tmp[:sp]) # 左端追加
        #print('left',V_tmp[:sp])
        tmp.extend(V_tmp[N-(i-sp):]) #右端追加
        #print('right',V_tmp[N-(i-sp):])
        #print(tmp)
        tmp = sorted(tmp)
        value = sum(tmp)
        # ret個戻す
        for ret in range(0, min(K-i,i)): #残り操作回数か取った石の個数iの小さい方
            if ret < i:
                # マイナスの価値は戻す
                if tmp[ret] < 0:
                    value -= tmp[ret]
                    #print('捨てた宝石：',tmp[ret])
            else:
                break
        result = max(result,value)
            #print()

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

def warshall_floyd(graph):
    num_v = len(graph)

    for k in range(num_v):
        for i in range(num_v):
            for j in range(num_v):
                if graph[i][j] > graph[i][k] + graph[k][j]:
                    graph[i][j] = graph[i][k] + graph[k][j]
    return graph

N = int(input())
graph = [[float("inf") for _ in range(N)] for _ in range(N)]
for i in range(N):
    a_list = list(map(int, input().split()))
    for j in range(N):
        if i == j:
            continue
        graph[i][j] = a_list[j]
import copy
a_list = copy.deepcopy(graph)
w_graph = warshall_floyd(graph)
result = 0
for i in range(N):
    for j in range(i+1, N):
        if i == j:
            continue
        if w_graph[i][j] != a_list[i][j]:
            print(-1)
            exit()
        d = []
        for k in range(N):
            d.append(w_graph[i][k] + w_graph[j][k])
        if min(d) > w_graph[i][j]:
            result += w_graph[i][j]
print(result)


### FIXED CODE ###

def warshall_floyd(graph):
    num_v = len(graph)

    for k in range(num_v):
        for i in range(num_v):
            for j in range(num_v):
                if graph[i][j] > graph[i][k] + graph[k][j]:
                    graph[i][j] = graph[i][k] + graph[k][j]
    return graph

N = int(input())
graph = [[float("inf") for _ in range(N)] for _ in range(N)]
for i in range(N):
    a_list = list(map(int, input().split()))
    for j in range(N):
        if i == j:
            continue
        graph[i][j] = a_list[j]
import copy
a_list = copy.deepcopy(graph)
w_graph = warshall_floyd(graph)
result = 0
for i in range(N):
    for j in range(i+1, N):
        if i == j:
            continue
        if w_graph[i][j] != a_list[i][j]:
            print(-1)
            exit()
        d = []
        for k in range(N):
            d.append(w_graph[i][k] + w_graph[j][k])
        if min(d) > w_graph[i][j]:
            result += w_graph[i][j]
print(result)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
diff = [(i+1)-A[i] for i in range(N)]
plus = [A[i]+(i+1) for i in range(N)]
ans = 0
for i in range(N-1):
    pnum = plus[i]
    ans += diff[i+1:].count(pnum)
print(ans)

### FIXED CODE ###

from collections import defaultdict
import bisect
N = int(input())
A = list(map(int,input().split()))
plus = [A[i]+(i+1) for i in range(N)]
diff = [-A[i]+(i+1) for i in range(N)]

plusc = defaultdict(list)
for i in range(N):
    plusc[plus[i]].append(i)
ans = 0
for i in range(1,N):
    ans += bisect.bisect_left(plusc[diff[i]],i)
print(ans)
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a.reverse()
maxa = a[0]
for i in range(m):
    a[0] = a[0] / 2
    a.sort()
    a.reverse()
ans = 0
#print(a)
for i in range(n):
    ans += int(a[i])
print(ans)

### FIXED CODE ###

from heapq import heapify, heappop, heappush

n, m = map(int, input().split())
a = list(map(int, input().split()))

for i in range(n):
    a[i] *= -1
heapify(a)
#print(a)
for i in range(m):
    maxa = heappop(a)
    heappush(a, -(-maxa//2))
#print(a)
print(-(sum(a)))
### BUGGY CODE ###

from collections import defaultdict
 
N,W = map(int, input().split())
WV = []
for i in range(N):
    WV.append(list(map(int, input().split())))
 
dp = defaultdict(int)
 
dp[0] = 0
 
for w,v in WV:
    for nw, nv in list(dp.items()):
        if nw+w <= W:
            dp[nw+w] = max(dp[nw+w], nv+v)
            
print(max(dp.values()))

### FIXED CODE ###

import numpy as np
N,W = map(int,input().split())
l = []
for i in range(N):
  y,x =  map(int,input().split())
  l.append((x,y))
V = sum([x[0] for x in l])
dp = np.full((N+1,V+1), np.inf)
dp[0][0]=0
for i in range(N):
  v = l[i][0]
  w = l[i][1]
  dp[i+1]=dp[i]
  dp[i+1][v:] = np.minimum(dp[i][v:],dp[i][:V-v+1]+w)
i = V
while True:
    if dp[N][i] <= W:
      print(i)
      break
    i -= 1
### BUGGY CODE ###

def main():
  import sys
  input = sys.stdin.readline
  
  N,D,A = map(int,input().split())
  XH = [tuple(map(int,input().split())) for _ in range(N)]
  XH.sort(key=lambda x:x[0])
  #print(XH)

  B = []
  ans = 0
  for x,h in XH:
    BB = B[:]
    for s,t,c in BB:
      #print(s,t,c,x)
      if s <= x <= t:
        h -= A*c
      elif t < x:
        B.remove((s,t,c))
    #print(h)
    if h > 0:
      B.append((x,x+2*D,(h+A-1)//A))
      ans += (h+A-1)//A
    #print(ans,x,x+2*D,(h+A-1)//A,B)
  print(ans)

if __name__ == '__main__':
  main()

### FIXED CODE ###

def main():
  import sys
  from heapq import heappush, heappop, heapify
  input = sys.stdin.readline
  
  N,D,A = map(int,input().split())
  hq = []
  for i in range(N):
    x,h = map(int,input().split())
    hq.append((x,0,h))
  heapify(hq)

  ans = 0
  b = 0
  while(len(hq) > 0):
    x,a,h = heappop(hq)
    if a == 0:
      if b < h:
        cnt = ((h-b)+A-1)//A
        ans += cnt
        b += A*cnt
        heappush(hq, (x+2*D,1,A*cnt))
    else:
      b -= h
  
  print(ans)

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

import collections

N = int(input())
S = [str(input()) for _ in range(N)]

c = collections.Counter(S)

ans = [i[0] for i in c.items() if i[1] >= c.most_common(1)[0][1]]
ans.sort()

for a in ans:
    print(a)


### FIXED CODE ###

N = int(input())
S = [str(input()) for _ in range(N)]

S.sort()

ans = []
most = 0
tmpstr = S[0]
tmpcnt = 0


for s in S:
    if tmpstr == s:
        tmpcnt += 1
    else:
        if tmpcnt > most:
            ans.clear()
            ans.append(tmpstr)
            most = tmpcnt

        elif tmpcnt == most:
            ans.append(tmpstr)

        tmpcnt = 1
        tmpstr = s

if tmpcnt > most:
    ans.clear()
    ans.append(tmpstr)
    most = tmpcnt
elif tmpcnt == most:
    ans.append(tmpstr)

for a in ans:
    print(a)
### BUGGY CODE ###

from itertools import accumulate
n = int(input())
A = [0]+list(accumulate(map(int,input().split())))
ans = 0
for i in range(1,n+1):
    for j in range(n+1-i):
        if A[j+i]-A[j] == 0:
            ans += 1
print(ans)

### FIXED CODE ###

from collections import Counter
n = int(input())
A = [0]
for i in map(int,input().split()):
    A.append(A[-1]+i)
ans = 0
B = Counter(A)
for i in B.values():
    if i >= 2:
        ans += int(i*(i-1)/2)
print(ans)
### BUGGY CODE ###

n = int(raw_input())
A = map(int, raw_input().split())
q = int(raw_input())
M = map(int, raw_input().split())

def solve(i,m):
    if m == 0:
        return True
    if i >= n:
        return False
    res = solve(i+1, m) or solve(i+1, m-A[i])
    return res

def main():
    for m in M:
        res = solve(0,m)
        if res:
            print 'yes'
        else:
            print 'no'

main()
    

### FIXED CODE ###

def solve(i, m):
  if dp[i][m] != -1:
    return dp[i][m]
 
  if m == 0:
    dp[i][m] = 1
  elif i >= n:
    dp[i][m] = 0
  elif solve(i+1, m):
    dp[i][m] = 1
  elif solve(i+1, m-A[i]):
    dp[i][m] = 1
  else:
    dp[i][m] = 0
 
  return dp[i][m]
 
 
 
n = input()
A = map(int, raw_input().split())
p = input()
m = map(int, raw_input().split())
 
dp = [[-1 for col in range(2000)] for row in range(n+1)]
 
for i in range(p):
  if solve(0, m[i]):
    print 'yes'
  else:
    print 'no'
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N=int(input())
mod=10**9+7

S=[tuple(map(int,input().split())) for i in range(N)]

from collections import Counter
from fractions import Fraction
C=Counter()
PLUS=0
for a,b in S:
    if a==0 and b==0:
        PLUS+=1
    elif b!=0:
        C[Fraction(a,b)]+=1
    else:
        C[float("inf")]+=1
    
ANS=1

for x in list(C.keys()):
    if x!=float("inf") and x!=Fraction(0,1):
        n=x.numerator
        d=x.denominator

        ANS=ANS*(pow(2,C[x],mod)+pow(2,C[Fraction(-d,n)],mod)-1)%mod

        C[x]=0
        C[Fraction(-d,n)]=0
        
    else:
        ANS=ANS*(pow(2,C[float("inf")],mod)+pow(2,C[Fraction(0,1)],mod)-1)%mod

        C[float("inf")]=0
        C[Fraction(0,1)]=0

print((ANS-1+PLUS)%mod)
    


### FIXED CODE ###

import sys
input = sys.stdin.readline
from collections import Counter
from fractions import Fraction
from math import gcd

N=int(input())
mod=10**9+7

S=[tuple(map(int,input().split())) for i in range(N)]

def main():
    C=Counter()
    PLUS=0
    for a,b in S:
        if a==0 and b==0:
            PLUS+=1
        elif a==0:
            C[(0,1)]+=1
        elif b!=0 and a>0:
            C[(a//gcd(a,b),b//gcd(a,b))]+=1
        elif b!=0 and a<0:
            C[(-a//gcd(a,b),-b//gcd(a,b))]+=1
            
        else:
            C[float("inf")]+=1
        
    ANS=1

    #print(C)

    for x in list(C.keys()):
        if x!=float("inf") and x!=(0,1):

            a,b=x

            if b>0:
                ANS=ANS*(pow(2,C[x],mod)+pow(2,C[(b,-a)],mod)-1)%mod
                C[(b,-a)]=0
            else:
                ANS=ANS*(pow(2,C[x],mod)+pow(2,C[(-b,a)],mod)-1)%mod
                C[(-b,a)]=0
                
            C[x]=0

            
        else:
            ANS=ANS*(pow(2,C[float("inf")],mod)+pow(2,C[(0,1)],mod)-1)%mod

            C[float("inf")]=0
            C[(0,1)]=0

    print((ANS-1+PLUS)%mod)

main()
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
b = []
for i, v in enumerate(a):
    b.append([v, i])
b.sort(reverse = True)
dp = [[0] * (n+1) for _ in range(n+1)]
ans = 0

for i in range(n+1):
    for j in range(n+1-i):
        if i > 0:
            dp[i][j] = max(dp[i][j], dp[i-1][j] + b[i+j-1][0] * (b[i+j-1][1] - (i - 1)))
        if j > 0:
            dp[i][j] = max(dp[i][j], dp[i][j-1] + b[i+j-1][0] * ((n - j) - b[i+j-1][1]))

for i in range(n+1):
    ans = max(ans, dp[i][n-i])

print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.buffer.readline

n = int(input())
a = list(map(int, input().split()))
b = []
for i, v in enumerate(a):
    b.append([v, i])
b.sort(reverse = True)
dp = [[0] * (n+1) for _ in range(n+1)]
ans = 0

for i in range(n+1):
    for j in range(n+1-i):
        if i > 0:
            dp[i][j] = max(dp[i][j], dp[i-1][j] + b[i+j-1][0] * (b[i+j-1][1] - (i - 1)))
        if j > 0:
            dp[i][j] = max(dp[i][j], dp[i][j-1] + b[i+j-1][0] * ((n - j) - b[i+j-1][1]))

for i in range(n+1):
    ans = max(ans, dp[i][n-i])

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

#19 B - RGB Boxes
R,G,B,N = map(int,input().split())
lis = sorted([R,G,B])
MA, MID, MI = lis[0],lis[1],lis[2]

cnt = 0
for ma in range(N//MA+1):
    for mid in range(N//MID+1):
        mi = (N - (MA*ma+MID*mid))//MI
        mi_judge = (N - (MA*ma+MID*mid))%MI
        if mi>= 0 and (mi_judge == 0):
            cnt += 1
print(cnt)

### FIXED CODE ###

#19 B - RGB Boxes
R,G,B,N = map(int,input().split())
lis = sorted([R,G,B])
MA, MID, MI = lis[0],lis[1],lis[2]

cnt = 0
for ma in range(N//MA+1):
    for mid in range(N//MID+1):
        mi = (N - (MA*ma+MID*mid))//MI
        mi_judge = (N - (MA*ma+MID*mid))%MI
        if mi>= 0 and (mi_judge == 0):
            cnt += 1
print(cnt)
### BUGGY CODE ###

import heapq
n, k = map(int, input().split())
list_A = list(map(int, input().split()))
list_H = []

for a in list_A:
    heapq.heappush(list_H, ((-1)*a, a, 1))

for _ in range(k):
    l = heapq.heappop(list_H)
    c = 0
    if l[1] % (l[2]+1) == 0:
        c = l[1] // (l[2]+1)
    else:
        c = l[1] // (l[2]+1) + 1

    h = ((-1)*c, l[1], l[2]+1)
    heapq.heappush(list_H, h)

ans = heapq.heappop(list_H)
print(abs(ans[0]))

### FIXED CODE ###

n, k = map(int, input().split())
list_A = list(map(int, input().split()))
l, r = 0, max(list_A)

while l + 1 < r:
    m, cnt = (r + l) // 2, 0

    for a in list_A:
        cnt += (a-1)//m

    if cnt > k:
        l = m
    else:
        r = m

print(r)
### BUGGY CODE ###

n=int(input())
import fractions 
from functools import reduce
arr = list(map(int,input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcma(numbers):
    return reduce(lcm_base, numbers, 1)
def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)    
a = lcm(*arr)
ans = 0

for i in arr:
    ans+= (a//i)%1000000007
    ans=ans%1000000007
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
 
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
 
a = 1
for i in range(N):
    a = lcm(a, A[i])
 
ans = 0
for i in range(N):
    ans += a * pow(A[i], mod-2, mod)
 
print(ans % mod)
### BUGGY CODE ###

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N, Q = in_nn()
    C = in_nl()
    LR = []

    for q in range(Q):
        l, r = in_nn()
        LR.append((r + 0.1, l, q))

    t = [-1] * (N + 1)
    sect = []
    for i in range(N):
        x = C[i]
        if t[x] != -1:
            sect.append((i + 1, t[x], -1))
        t[x] = i + 1

    sect += LR
    sort_index = sorted(list(range(len(sect))), key=lambda i: sect[i][0])

    bit = [0] * (N + 1)

    def add(i, val):
        while i <= N:
            bit[i] += val
            i += i & -i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i & -i
        return res

    ans = [0] * Q
    for i in sort_index:
        r, l, n = sect[i]
        r = int(r)

        if n == -1:
            add(l, 1)
        else:
            t = sum(r) - sum(l - 1)
            ans[n] = r - l + 1 - t

    print('\n'.join(map(str, ans)))


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N, Q = in_nn()
    C = in_nl()
    LR = []

    for q in range(Q):
        l, r = in_nn()
        LR.append((r + 0.1, l, q))

    t = [-1] * (N + 1)
    sect = []
    for i in range(N):
        x = C[i]
        if t[x] != -1:
            sect.append((i + 1, t[x], -1))
        t[x] = i + 1

    sect += LR
    sort_index = sorted(list(range(len(sect))), key=lambda i: sect[i][0])

    bit = [0] * (N + 1)

    def add(i, val):
        while i <= N:
            bit[i] += val
            i += i & -i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i & -i
        return res

    ans = [0] * Q
    for i in sort_index:
        r, l, n = sect[i]
        r = int(r)

        if n == -1:
            add(l, 1)
        else:
            t = sum(r) - sum(l - 1)
            ans[n] = r - l + 1 - t

    print('\n'.join(map(str, ans)))


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

N,M = map(int,input().split())
A = list(map(int,input().split()))
index = 0
for i in range(M):
  max_value = max(A)
  index = A.index(max_value)
  A[index] = int(A[index]/2)
print(sum(A))

### FIXED CODE ###

import heapq

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

#全要素に-1かける
for i in range(len(A)):
    A[i] *= -1 

heapq.heapify(A) 
for _ in range(M):
    x = heapq.heappop(A)
    heapq.heappush(A, (-x//2)*(-1))

print(-sum(A))
### BUGGY CODE ###

from collections import deque


def main():
    from sys import stdin
    readline = stdin.readline

    INF = float('inf')

    H, W = map(int, readline().split())
    Ch, Cw = map(lambda x: int(x) - 1, readline().split())
    Dh, Dw = map(lambda x: int(x) - 1, readline().split())
    S = [readline()[:-1] for _ in range(H)]

    t = [[INF] * W for _ in range(H)]
    for h in range(H):
        th = t[h]
        Sh = S[h]
        for w in range(W):
            if Sh[w] == '#':
                th[w] = -1

    t[Ch][Cw] = 0
    q = deque([(Ch, Cw)])
    while q:
        warpq = []
        while q:
            h, w = q.popleft()
            a = t[h][w]
            warpq.append((h, w))
            if h - 1 >= 0 and t[h - 1][w] > a:
                q.append((h - 1, w))
                t[h - 1][w] = a
            if h + 1 < H and t[h + 1][w] > a:
                q.append((h + 1, w))
                t[h + 1][w] = a
            if w - 1 >= 0 and t[h][w - 1] > a:
                q.append((h, w - 1))
                t[h][w - 1] = a
            if w + 1 < W and t[h][w + 1] > a:
                q.append((h, w + 1))
                t[h][w + 1] = a

        for h, w in warpq:
            for i in range(max(0, h - 2), min(H - 1, h + 2) + 1):
                ti = t[i]
                for j in range(max(0, w - 2), min(W - 1, w + 2) + 1):
                    if ti[j] <= a + 1:
                        continue
                    ti[j] = a + 1
                    q.append((i, j))

    if t[Dh][Dw] == INF:
        print(-1)
    else:
        print(t[Dh][Dw])


main()


### FIXED CODE ###

from collections import deque

INF = float('inf')

H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
S = [input() for _ in range(H)]

t = [[INF] * W for _ in range(H)]
t[Ch][Cw] = 0
q = deque([(Ch, Cw)])

while q:
    h, w = q.popleft()
    a = t[h][w]
    if h - 1 >= 0 and S[h - 1][w] != '#' and t[h - 1][w] > a:
        q.appendleft((h - 1, w))
        t[h - 1][w] = a
    if h + 1 < H and S[h + 1][w] != '#' and t[h + 1][w] > a:
        q.appendleft((h + 1, w))
        t[h + 1][w] = a
    if w - 1 >= 0 and S[h][w - 1] != '#' and t[h][w - 1] > a:
        q.appendleft((h, w - 1))
        t[h][w - 1] = a
    if w + 1 < W and S[h][w + 1] != '#' and t[h][w + 1] > a:
        q.appendleft((h, w + 1))
        t[h][w + 1] = a

    for i in range(max(0, h - 2), min(H - 1, h + 2) + 1):
        for j in range(max(0, w - 2), min(W - 1, w + 2) + 1):
            if S[i][j] == '#':
                continue
            if t[i][j] <= a + 1:
                continue
            t[i][j] = a + 1
            q.append((i, j))

if t[Dh][Dw] == INF:
    print(-1)
else:
    print(t[Dh][Dw])
### BUGGY CODE ###

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

num = 0

for _ in range(q):
    t = input()
    if t == '1':
        num += 1
    else:
        t, f, c = t.split()
        if num%2 == 0:
            if f == '1':
                s = c + s
            else:
                s = s + c
        else:
            if f == '2':
                s = c + s
            else:
                s = s + c

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

### FIXED CODE ###

#TLE
from collections import deque
s = deque(input())
q = int(input())
num = 0
for _ in range(q):
    t = input()
    if t == '1':
        if num == 0:
            num = 1
        else:
            num = 0
    else:
        t, f, c = t.split()
        if num == 0:
            if f == '1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == '2':
                s.appendleft(c)
            else:
                s.append(c)
s = ''.join(s)
if num == 1:
    s = s[::-1]
print(s)