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

from bisect import bisect_left, bisect_right
import sys

input = sys.stdin.buffer.readline

n = int(input())
L = list(map(int, input().split()))
L.sort()
# a,b,cの順序に大きいとして、6倍…はしなくていいのか。組み合わせらしい
# a-c<b<a+c
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        p = L[i]
        q = L[j]
        hidari = max(i + 1, bisect_left(L, q - p + 1))
        migi = min(j - 1, bisect_right(L, p + q - 1))
        delta = migi - hidari
        if delta >= 0:
            cnt += delta + 1
print(cnt)


### FIXED CODE ###

from bisect import bisect_left, bisect_right

n = int(input())
L = list(map(int, input().split()))
L.sort()
# a,b,cの順序に大きいとして、6倍…はしなくていいのか。組み合わせらしい
# a-c<b<a+c
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        p = L[i]
        q = L[j]
        hidari = max(i + 1, bisect_left(L, q - p + 1))
        migi = min(j - 1, bisect_right(L, p + q - 1))
        delta = migi - hidari
        if delta >= 0:
            cnt += delta + 1
print(cnt)
### BUGGY CODE ###

n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
def bitsum(BIT,i):
  s=0
  while i>0:
    s+=BIT[i]
    i-=i&(-i)
  return s

def bitadd(BIT,i,x):
  while i<=2**n:
    BIT[i]+=x
    i+=i&(-i)
  return BIT
nn=2**n
ans=float("inf")
for i in range(nn):
  E,O=[],[]
  for j in range(n):
    if i&(1<<j)==0:
      if j%2==0:
        E.append(A[j]*100+j)
      else:
        O.append(A[j]*100+j)
    else:
      if j%2==0:
        O.append(B[j]*100+j)
      else:
        E.append(B[j]*100+j)

  E.sort()
  O.sort()

  chk=True
  if len(E)!=(n+1)//2:
    chk=False
  else:
    C=[E[0]]
    for i in range(1,n):
      if i%2==0:
        C.append(E[i//2])
        
      else:
        C.append(O[i//2])
      if C[-1]//100<C[-2]//100:
          chk=False
          break
    
  if chk:
    ct=0
    BIT=[0]*nn
    BIT.insert(0,0)
    for i1 in range(n):
      c=(C[i1]%100)+1
      ct+=i1-bitsum(BIT,c)
      bitadd(BIT,c,1)


    
    ans=min(ans,ct)
print(-1 if ans==float("inf") else ans)



### FIXED CODE ###

import itertools
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
def bitsum(BIT,i):
  s=0
  while i>0:
    s+=BIT[i]
    i-=i&(-i)
  return s

def bitadd(BIT,i,x):
  while i<=64:
    BIT[i]+=x
    i+=i&(-i)
  return BIT

def tentou(X):
  ret=0
  BIT=[0]*64
  BIT.insert(0,0)
  for i1 in range(n):
    x=X[i1]+1
    ret+=i1-bitsum(BIT,x)
    bitadd(BIT,x,1)
  return ret
N=[int(i) for i in range(n)]
nn=2**n
ans=float("inf")
for _ in range(n//2+1):
  for com in itertools.combinations(N,2*_):
    i=0
    for ura in com:
      i+=2**ura
    E,O=[],[]
    for j in range(n):
      if i&(1<<j)==0:
        if j%2==0:
          E.append(A[j]*100+j)
        else:
          O.append(A[j]*100+j)
      else:
        if j%2==0:
          O.append(B[j]*100+j)
        else:
          E.append(B[j]*100+j)
    if len(E)!=(n+1)//2:
      continue
    else:
      E.sort()
      O.sort()

      chk=True
      C=[E[0]%100]
      prev=E[0]//100
      for i in range(1,n):
        ii=i//2
        if i%2==0:
          C.append(E[ii]%100)
          if E[ii]//100<prev:
            chk=False
            break
          else:
            prev=E[ii]//100
        else:
          C.append(O[ii]%100)
          if O[ii]//100<prev:
            chk=False
            break
          else:
            prev=O[ii]//100
    
    if chk:
      ans=min(ans,tentou(C))
print(-1 if ans==float("inf") else ans)
### BUGGY CODE ###

n=int(input())
a=[]

for i in range(n):
    a.append(int(input()))

for i in range(n):
    b=a[:]
    del b[i]
    print(max(b))

### FIXED CODE ###

n = int(input())
a = []

for i in range(n):
    a.append(int(input()))

Max = max(a)
if(a.count(Max)>1):
    for _ in range(n):
        print(Max)
else:
    for i in range(n):
        if(a[i]==Max):
            a[i]=0
            print(max(a))
        else:
            print(Max)
### BUGGY CODE ###

n,m=map(int,input().split())
a=[int(input()) for _ in range(m)]

dp=[0]*(n+1)
dp[0]=1

for i in range(1,n+1):
    if i in a:
        dp[i]=0
        continue
    if dp[i-1]==dp[i-2]==0:break
    dp[i]=dp[i-1]
    if i>1:dp[i]+=dp[i-2]
    dp[i]%=1000000007
print(dp[-1])

### FIXED CODE ###

import sys
n,m=map(int,input().split())
a=set(map(int,sys.stdin))
dp=[0]*(n+1)
dp[0]=1
if 1 not in a:dp[1]=1
for i in range(2,n+1):
    if i in a:continue
    if dp[i-1]==dp[i-2]==0:break
    dp[i]=(dp[i-1]+dp[i-2])%1000000007
print(dp[-1])
### BUGGY CODE ###

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

for i in range(1,n+1):
  cnt = 0
  for ai in a:
    if ai == i:
      cnt += 1
  print(cnt)

### FIXED CODE ###

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

d = {}
for i in range(0,n-1):
  if a[i] in d:
    d[a[i]] += 1
  else:
    d[a[i]] = 1
    
for i in range(1,n+1):
  if i in d.keys():
    print(d[i])
  else:
    print(0)
### BUGGY CODE ###

#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

S = input()
N=len(S)

#l=0
#r=4
#print(S[l:r])

ans=0
for l in range(N):
	for r in range(l+4,N+1):
		i = int(S[l:r])
		if i<2019: continue
		if (i % 2019) == 0:
			ans += 1
#			p("l,r =", l,r)


print(ans)


### FIXED CODE ###

#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

S = input()
S = S[::-1]

m = 2019

A=[0]*m
A[0]=1

a = 0
ans=0
for i,s in enumerate(S):
	a = a + (int(s) * pow(10,i,m))
	a %= m
	
	ans += A[a]
	A[a] += 1

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

N=int(input())
A=list(map(int,input().split()))
s=0
for i in range(N-1):
  s+=sum([A[i]^a for a in A[i+1:]])
print(s%(10**9+7))

### FIXED CODE ###

mod=10**9+7
N=int(input())
A=list(map(int,input().split()))
ans=0
 
for i in range(60):
  x=0
  for j in range(N):
    if A[j]>>i&1: x+=1
  ans+=x*(N-x)*(2**i)
print(ans%mod)
### BUGGY CODE ###

N,K=map(int,input().split())
S=list(map(int,input().split()))

best=0

for i in range(N-K+1):
    goukei=0
    for j in range(K):
        goukei+=S[i+j]
    if goukei>best:
        best=goukei
    
print((best+K)/2)
    

### FIXED CODE ###

N,K=map(int,input().split())
S=list(map(int,input().split()))

L=[0]*(N-K+1)

target=0


for i in range(N-K+1):
    if i==0:
        for j in range(K):
            target+=S[j]
        L[0]=target
    else:    
        target+=S[K+i-1]-S[i-1]
        L[i]=target
best=max(L)    
print((best+K)/2)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N,Q =map(int,input().split())
bond = [[]for _ in range(N)]
for i in range(N-1):
    a,b = [int(i)-1 for i in input().split()]
    bond[a].append(b)
    bond[b].append(a)

#qu = [[int(i)for i in input().split()]for _ in range(Q)]
qlis = [0]*N
for _ in range(Q):
    p,x = map(int,input().split())
    qlis[p-1] += x

ans = [0]*N
ans[0] += qlis[0]
que =[]
for i in bond[0]:
    que.append([0,i])
    bond[i].remove(0)

for _ in range(N-1):
    ue,sita = que.pop(0)
    for i in bond[sita]:
        que.append([sita,i])
        bond[i].remove(sita)
    ans[sita] = ans[ue]+qlis[sita]
anstr = " ".join([str(i)for i in ans])
print(anstr)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N,Q =map(int,input().split())
bond = [[]for _ in range(N)]
for i in range(N-1):
    a,b = [int(i)-1 for i in input().split()]
    bond[a].append(b)
    bond[b].append(a)

qlis = [0]*N
for _ in range(Q):
    p,x = map(int,input().split())
    qlis[p-1] += x

stat = [True]*N
stat[0] =False
que =[]
for i in bond[0]:
    que.append([0,i])

for _ in range(N-1):
    ue,sita = que.pop()
    if stat[sita]:
        for i in bond[sita]:
            if stat[i]:
                que.append([sita,i])
        qlis[sita] += qlis[ue]
    stat[sita] = False
anstr = " ".join([str(i)for i in qlis])
print(anstr)
### BUGGY CODE ###

#

import sys
input=sys.stdin.readline

MOD=10**9+7
kl = [0]*(10**6+1)

            
def main():
    N=int(input())
    A=list(map(int,input().split()))
    if N==1:
        print(1)
        exit()
    for j in range(N):
        n=A[j]
        for i in range(2,int((n)**0.5)+1):
            if n%i==0:
                cnt=0
                while(n%i==0):
                    n//=i
                    cnt+=1
                if kl[i]<cnt:
                    kl[i]=cnt
        if n!=1:
            if kl[n]<1:
                kl[n]=1
                
    lcm=1
    for i in range(2,10**6+1):
        lcm*=pow(i, kl[i], MOD)

    modsum=0
    for i in range(N):
        modsum+=pow(A[i],MOD-2,MOD)

    ans = lcm*modsum%MOD
    print(ans)
    
    
    
if __name__=="__main__":
    main()


### FIXED CODE ###

#

import sys
input=sys.stdin.readline

MOD=10**9+7
kl = [0]*(10**6+1)

            
def main():
    N=int(input())
    A=list(map(int,input().split()))
    if N==1:
        print(1)
        exit()
    for j in range(N):
        n=A[j]
        for i in range(2,int((n)**0.5)+1):
            if n%i==0:
                cnt=0
                while(n%i==0):
                    n//=i
                    cnt+=1
                if kl[i]<cnt:
                    kl[i]=cnt
        if n!=1:
            if kl[n]<1:
                kl[n]=1
                
    lcm=1
    for i in range(2,10**6+1):
        lcm*=pow(i, kl[i], MOD)

    modsum=0
    for i in range(N):
        modsum+=pow(A[i],MOD-2,MOD)

    ans = lcm*modsum%MOD
    print(ans)
    
    
    
if __name__=="__main__":
    main()
### BUGGY CODE ###

import math
ans=0

K=int(input())
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1, K+1):
      ans+=math.gcd(i,(math.gcd(j,k)))
print(ans)

### FIXED CODE ###

import math
ans=0

K=int(input())
for i in range(1,K+1):
  for j in range(1,K+1):
    num=math.gcd(i,j)
    for k in range(1,K+1):
      if num==1:
        ans+=1
      else:
        ans+=math.gcd(num,k)
print(ans)
### BUGGY CODE ###

n, y = map(int, input().split())

for a in range(n + 1):
   for b in range(n + 1):
       for c in range(n + 1):
           if y - (a * 10000 + b * 5000 + c * 1000) == 0 and a + b + c == n:                                      
               print(a, b, c)
               exit()

print("-1 -1 -1")

### FIXED CODE ###

n, y = map(int, input().split())

for a in range(n + 1):
   for b in range(n - a + 1):
           if a * 10000 + b * 5000 + (n - a - b) * 1000 == y:
               print(a, b, n - a - b)
               exit()

print("-1 -1 -1")
### BUGGY CODE ###

n=int(input())
ans=1
for i in range(1,n+1):
    ans*=i
  
print(ans%(10**9+7))


### FIXED CODE ###

n=int(input())
ans=1
for i in range(1,n+1):
    ans=(ans*i)%(10**9+7)
  
print(ans%(10**9+7))
### BUGGY CODE ###

N, M = [int(x) for x in input().split()]
a = list(int(input()) for _ in range(M))

MOD = 10 ** 9 + 7
dp = [0] * (N + 1)
dp[0] = 1

for i in range(1, N + 1):
  if i in a:
    continue
  x = dp[i - 1]
  if i > 1:
    x += dp[i - 2]
  dp[i] = x % MOD

ans = dp[-1]
print(ans)

### FIXED CODE ###



N, M = [int(x) for x in input().split()]
a = set(int(input()) for _ in range(M))

MOD = 10 ** 9 + 7
dp = [0] * (N + 1)
dp[0] = 1

for i in range(1, N + 1):
  if i in a:
    continue
  x = dp[i - 1]
  if i > 1:
    x += dp[i - 2]
  dp[i] = x % MOD

ans = dp[N]
print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
work = [0] * N
for i in range(N):
    work[i] = [0] * 2
    work[i][1], work[i][0] = map(int, input().split())
ans = 0
work_money = sorted(work)
schedule = [0]*M
for j in range(N):
    day = M - work_money[N - 1 - j][1]
    day_index = day
    while day_index >= 0:
        if schedule[day_index] == -1:
            break
        if schedule[day_index] == 0:
            if day_index > 0:
                if schedule[day_index - 1] == -1:
                    schedule[day_index] = -1
                    ans += work_money[N - 1 - j][0]
                    break
            schedule[day_index] = 1
            ans += work_money[N - 1 - j][0]
            break
        day_index -= 1
        if day_index == -1:
            schedule[day] = -1
print(ans)


### FIXED CODE ###

import heapq
N, M = map(int, input().split())
work = [0] * N
for i in range(N):
    work[i] = [0] * 2
    a, b = map(int, input().split())
    if M-a >= 0:
        work[i][0], work[i][1] = a, -1 * b
    else:
        work[i][0], work[i][1] = 10**9, -1 * b
ans = 0
work_day = sorted(work)
index = 0
heap = []
heapq.heapify(heap)
for j in range(M+1):
    if index >= N:
        if len(heap) > 0:
            ans += heapq.heappop(heap)*(-1)
        continue
    while (work_day[index][0] <= j):
        heapq.heappush(heap, work_day[index][1])
        index += 1
        if index >= N:
            break
    if len(heap) > 0:
        ans += heapq.heappop(heap)*(-1)


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

from collections import defaultdict
import math
def generate_primes(num):
    if num == 1: return []
    primes = list(range(2, num + 1))
    i, p = 0, 1
    while p <= math.sqrt(num):
        if primes[i] != 0:
            p = primes[i]
            primes = [prime if (prime % p != 0 or prime == p) else 0 for prime in primes]
        i += 1
    return [p for p in primes if p != 0]

def main():  
    n = int(input())
    A = list(map(int, input().split()))
    d = defaultdict(int)
    primes = generate_primes(max(A))
    for a in A:
        for p in primes:
            
            if a % p == 0:
                d[p] += 1
    d[1] = 0
    m = max(d.values())
    if m == n:
        print('not coprime')
    elif m >= 2:
        print('setwise coprime')
    else:
        print('pairwise coprime')
    

if __name__ == '__main__':
    main()



### FIXED CODE ###

from functools import reduce
from math import gcd
def main():   
    n = int(input())
    A = list(map(int, input().split()))
    C = [0] * 1000001
    for a in A: C[a] += 1
    if reduce(gcd, A) != 1:
        print('not coprime')
    elif all(sum(C[i::i]) <= 1 for i in range(2, 1000001)):
        print('pairwise coprime')
    else:
        print('setwise coprime')

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

N = int(input())
A = list(map(int,input().split()))
sum_r = 0
# print(A[0::2])
# print(A[1::2])
sum_odd = 0
sum_even = 0
for i in range(N):
    a = A[i]
    sum_r += a
    if i % 2 == 0:
        sum_odd += a
    else:
        sum_even += a

ans = ''
rolling_even = sum_even 
rolling_odd = sum_odd - A[0]
#print(sum_r,sum_even)
ans += str(sum_r - 2 * rolling_even) + ' ' + str(sum_r - 2 * rolling_odd)
mepo = A[1]
mope = A[0]
for i in range(2,N):
    tmp = sum_r

    if i % 2 == 0:
        # rolling_even += A[(i-2)%N] - A[(i-1)%N]
        mepo = A[(i-1)%N]
        rolling_even += mope - mepo
        tmp = sum_r - 2 * rolling_even
    else:
        mope = A[(i-1)%N]
        rolling_odd += mepo - mope
        tmp = sum_r - 2 * rolling_odd
    ans += ' ' + str(tmp)
print(ans)




### FIXED CODE ###

import sys
# input = sys.stdin.readline()
N = int(input())
A = list(map(int,input().split()))
sum_r = 0
# print(A[0::2])
# print(A[1::2])
sum_odd = 0
sum_even = 0
for i in range(N):
    a = A[i]
    sum_r += a
    if i % 2 == 0:
        sum_odd += a
    else:
        sum_even += a

ans = ''
rolling_even = sum_even 
rolling_odd = sum_odd - A[0]
#print(sum_r,sum_even)
X_i = sum(A) - 2 * sum(A[1::2])
sys.stdout.write(str(X_i))
for i in range(N-1):
    X_i_plus_1 = 2 * A[i] - X_i
    sys.stdout.write(' ' + str(X_i_plus_1))
    X_i = X_i_plus_1
# ans += str(sum_r - 2 * rolling_even) + ' ' + str(sum_r - 2 * rolling_odd)
# sys.stdout.write(ans + ' ')
# mepo = A[1]
# mope = A[0]
# for i in range(2,N):
#     tmp = sum_r

#     if i % 2 == 0:
#         # rolling_even += A[(i-2)%N] - A[(i-1)%N]
#         mepo = A[(i-1)]
#         rolling_even += mope - mepo
#         tmp = sum_r - 2 * rolling_even
#     else:
#         mope = A[(i-1)]
#         rolling_odd += mepo - mope
#         tmp = sum_r - 2 * rolling_odd
#     sys.stdout.write(' '+str(tmp))
#     ans += ' ' + str(tmp)
# sys.stdout.write('\n')
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
for i in range(1, n):
    a[i] = a[i] * a[i-1]
if a[n-1] > 10**18:
    print(-1)
else:
    print(a[n-1])

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = 0
for i in range(1, n):
    a[i] = a[i] * a[i-1]
    if a[i] > 10**18:
        b += 1
        break
if 0 in a:
    print(0)
elif b == 1:
    print(-1)
else:
    print(a[n-1])
### BUGGY CODE ###

N = int(input())

A = []
for _ in range(N):
    A.append(int(input()))

for i in range(N):
    tmp = A[i]
    A[i] = 0
    print(max(A))
    A[i] = tmp

### FIXED CODE ###

N = int(input())
A = [next(map(int, input().split())) for _ in range(N)]

AA = sorted(A, reverse=True)

for i in A:
    if i != AA[0]:
        print(AA[0])
    else:
        print(AA[1])
### BUGGY CODE ###

from sys import stdin,stdout
def fn(i):
    return pro[i]//(pro[i-k] if i-k>=0 else 1)
n,k=map(int,stdin.readline().split())
a=list(map(int,stdin.readline().split()))
pro=[a[0]]
for v in a[1:]:
    pro+=[pro[-1]*v]
for i in range(k,n):
    #print(fn(i),fn(i-1))
    if fn(i)>fn(i-1):print('Yes')
    else:print('No')


### FIXED CODE ###

from sys import stdin,stdout
n,k=map(int,stdin.readline().split())
a=list(map(int,stdin.readline().split()))
for i in range(k,n):
    print('Yes' if a[i]>a[i-k] else 'No')
### BUGGY CODE ###

# -*- coding: utf-8 -*-
n=int(input())
#答え
sumy=0
#約数の個数を各値ごとにリストに入れる
cnt1=[]
#各値ごとの約数の個数
cnt2=0
for i in range(n+1):
  for j in range(i):
#0で割るのを防ぐ
    k=j+1
#商が割る数より小さいときは二重に数えることになるので省く
    if i/k<k:
      break
    else:
#約数であるかを確認
      if i%k==0:
        if k==i/k:
          cnt2+=1
#計算量を減らすためにペアで数える(商と割る数)
        else:
          cnt2+=2
  cnt1.append(cnt2)
#cntを初期化
  cnt2=0    
  sumy+=cnt1[i]*i
print(sumy)

### FIXED CODE ###

# -*- coding: utf-8 -*-
n=int(input())
sumy=0
#nの半分より上の倍数は1個なのでそこで分ける
numhalf=int(n/2)
#nの半分までは倍数ごとに個数を求めて足していく
for i in range(numhalf):
  t=i+1
  k=int(n/t)
  sumy+=int(t*k*(k+1)/2)
#半分より上の倍数はすべて1個ずつなのでまとめて足す
sumy+=int((n+numhalf+1)*(n-numhalf)/2)
print(sumy)
### BUGGY CODE ###

import sys
import collections
input = sys.stdin.readline
N = int(input())
S = input().strip()
assert 1 <= N <= 4000
assert N == len(S)
r = S.count("R")
g = S.count("G")
b = S.count("B")
total = 0
for j in range(1, N-1):
    for i in range(j):
        k = 2*j-i
        if k < len(S):
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                total += 1
# print(c["R"]*c["G"]*c["B"]-total)
print(r*g*b - total)


### FIXED CODE ###

n = int(input())
s = input()
r,g,b =0,0,0
ls = len(s)
for i in range(ls):
    if s[i]=='R':
        r+=1
    elif s[i]=='G':
        g+=1
    elif s[i]=='B':
        b+=1
cnt =0
for j in range(1,ls-1):
    for i in range(j):
        k = 2*j-i
        if k<ls:
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                cnt+=1
print(r*g*b-cnt)
### BUGGY CODE ###

N=int(input())
G=[[] for _ in range(N)]
for i in range(N-1):
  x,y,d=map(int,input().split())
  x-=1
  y-=1
  G[x].append((y,d))
  G[y].append((x,d))
Q,K=map(int,input().split())
stack=[K-1]
seen=[]
dist=[float('inf')]*N
dist[K-1]=0
while stack:
  u=stack.pop()
  seen.append(u)
  for v,d in G[u]:
    if v not in seen:
      dist[v]=min(dist[v],dist[u]+d)
      stack.append(v)
for q in range(Q):
  x,y=map(int,input().split())
  print(dist[x-1]+dist[y-1])

### FIXED CODE ###

N=int(input())
G=[[] for _ in range(N)]
for i in range(N-1):
  x,y,d=map(int,input().split())
  x-=1
  y-=1
  G[x].append((y,d))
  G[y].append((x,d))
Q,K=map(int,input().split())
stack=[K-1]
unseen=[1]*N
dist=[float('inf')]*N
dist[K-1]=0
while stack:
  u=stack.pop()
  unseen[u]=0
  for v,d in G[u]:
    if unseen[v]:
      dist[v]=min(dist[v],dist[u]+d)
      stack.append(v)
for q in range(Q):
  x,y=map(int,input().split())
  print(dist[x-1]+dist[y-1])
### BUGGY CODE ###

s=input()
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    s=s[::-1]
  else:
    if l[1]=='1':
      s=l[2]+s
    else:
      s=s+l[2]
print(s)

### FIXED CODE ###

s=input()
count=0
s1=str()
s2=str()
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    count+=1
  elif count%2==0:
    if l[1]=='1':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
  else:
    if l[1]=='2':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
s=s1+s+s2
if count%2!=0:
  s=s[::-1]
print(s)
### BUGGY CODE ###

import sys
try:
    from scipy.special import comb
except ImportError:
    from scipy.misc import comb

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    MOD = 10**9 + 7

    used = set()
    first = 0
    second = 0
    for i in range(N+1):
        if A[i] in used:
            second = i
            first = A.index(A[i])
        else:
            used.add(A[i])

    m = first + (N - second)
    for i in range(1, N+2):
        ans = comb(N+1, i, exact=True) - comb(m, i-1, exact=True)
        print(int(ans) % MOD)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    MOD = 10**9 + 7

    used = set()
    first = 0
    second = 0
    for i in range(N+1):
        if A[i] in used:
            second = i
            first = A.index(A[i])
        else:
            used.add(A[i])

    factrial = [1] * (N+2)
    for k in range(1, N+2):
        factrial[k] = (factrial[k-1] * k) % MOD

    fact_inv = [1] * (N+2)
    fact_inv[N+1] = pow(factrial[N+1], MOD - 2, MOD)
    for k in range(N, -1, -1):
        fact_inv[k] = (fact_inv[k+1] * (k+1)) % MOD

    def comb(n, r, MOD):
        if n < 0 or r < 0 or n < r:
            return 0
        else:
            return (factrial[n] * fact_inv[r] * fact_inv[n-r]) % MOD

    m = first + (N - second)
    for i in range(1, N+2):
        ans = comb(N+1, i, MOD) - comb(m, i-1, MOD)
        print(int(ans) % MOD)


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

N = int(input())
A = list(map(int, input().split()))
RedWine = 1
for i in range(N):
    RedWine *= A[i]
if RedWine > 1000000000000000000:
    print(-1)
else:
    print(RedWine)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
RedWine = 1
for i in range(N):
    RedWine *= A[i]
    if RedWine > 1000000000000000000:
        break
if RedWine > 1000000000000000000:
    if A.count(0) == 0:
        print(-1)
    else:
        print(0)
else:
    print(RedWine)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N,M=(int(x) for x in input().rstrip('\n').split())
As = [int(x) for x in input().rstrip('\n').split()]
d = {}
for x in As:
  if x in d:
    d[x] += 1
  else:
    d[x] = 1
for _ in range(M):
  B,C = (int(x) for x in input().rstrip('\n').split())
  if C in d:
    d[C] += B
  else:
    d[C] = B
res = 0
rest = N
for i in range(N):
  m = max(d.keys())
  if rest < d[m]:
    res += rest*m
    break
  else:
    res += d[m]*m
    rest -= d[m]
    del d[m]
print(res)


### FIXED CODE ###

import sys
input = sys.stdin.readline

N,M=(int(x) for x in input().rstrip('\n').split())
As = [int(x) for x in input().rstrip('\n').split()]
d = {}
for x in As:
  if x in d:
    d[x] += 1
  else:
    d[x] = 1
for _ in range(M):
  B,C = (int(x) for x in input().rstrip('\n').split())
  if C in d:
    d[C] += B
  else:
    d[C] = B
res = 0
rest = N
d2 = sorted([x for x in d.keys()],reverse=True)
for m in d2:
  x = d[m]
  if rest < x:
    res += rest*m
    break
  else:
    res += x*m
    rest -= x
print(res)
### BUGGY CODE ###

n = int(input())

ans = []
ans.append(int(input()))

for i in range(n - 1):
    t = int(input())
    ch = 0
    for j in range(len(ans)):
        if t > ans[j]:
            ans[j] = t
            ch += 1
            break
    if ch == 0:
        ans.append(t)

print(len(ans))


### FIXED CODE ###

def reverse_insort(a, x):
    lo = 0
    hi = len(a)
    while lo < hi:
        mid = (lo + hi) // 2
        if x > a[mid]:
            hi = mid
        else:
            lo = mid + 1
    return lo


n = int(input())

ans = []
ans.append(int(input()))

for i in range(n - 1):
    t = int(input())
    ch = 0
    if t <= ans[-1]:
        ans.append(t)
    else:
        ans[reverse_insort(ans, t)] = t

print(len(ans))