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

from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(input())
r = []
g = []
b = []
for i in range(N):
    if S[i] == 'R':
        r.append(i+1)
    elif S[i] == 'G':
        g.append(i+1)
    else:
        b.append(i+1)
# print(r)
# print(g)
# print(b)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            if r[i] != min(r[i], g[j], b[k]) and r[i] != max((r[i], g[j], b[k])):
                if r[i]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-r[i]:
                    cnt += 1
            elif g[j] != min(r[i], g[j], b[k]) and g[j] != max((r[i], g[j], b[k])):
                if g[j]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-g[j]:
                    cnt += 1
            else:
                if b[k]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-b[k]:
                    cnt += 1
print(cnt)


### FIXED CODE ###

from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(input())
r = []
g = []
b = []
for i in range(N):
    if S[i] == 'R':
        r.append(i+1)
    elif S[i] == 'G':
        g.append(i+1)
    else:
        b.append(i+1)
# print(r)
# print(g)
# print(b)
cnt = len(r)*len(g)*len(b)
for i in range(N):
    for j in range(i+1, N):
        k = 2*j-i
        if k < N and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            cnt -= 1
print(cnt)
### BUGGY CODE ###

from collections import Counter
N = int(input())
X = str(input())
X10 = int(X,2)

one_num = Counter(X)["1"]

for i in range(N):
    target = int(X[i])
    if target == 0:
        plus = pow(2,N-i-1)
        ones = one_num + 1
    else:
        plus = -pow(2,N-i-1)
        ones = one_num-1
    
    Xnow = X10+plus
    count = 0
    if Xnow == 0:
        print(count)
        continue
    else:
        Xnow = Xnow%ones
        count+=1
        while Xnow > 0:
            Xnow2 = format(Xnow, 'b')
            ones = Counter(Xnow2)["1"]
            Xnow = Xnow%ones
            count+=1
        print(count)

### FIXED CODE ###

N = int(input())
x2 = input()
x10 = int(x2, 2)
popc =  x2.count("1")
#print("x10", x10)
#print("popc", popc)
#popc: Nを2進数で表したときの1の個数

#以下で1つフリップさせた時の入力に対する余りを事前に計算
if popc <= 1:
    xp = x10%(popc+1)
    xm = 0
else:
    xp = x10%(popc+1)
    xm = x10%(popc-1)
#print("xp", xp)
#print("xm", xm)

#以下でフリップさせた場所のみを計算
for i in range(N):
    if x2[i] == "1":
        if popc == 1:
            print(0)
            continue
        tmp = (xm - pow(2, N-i-1, popc-1))%(popc-1)
    else:
        tmp = (xp + pow(2, N-i-1, popc+1))%(popc+1)
        
    #print("tmp", tmp)
    ans = 1
    while tmp > 0:
        tmp %= bin(tmp).count("1")
        ans += 1
    print(ans)
### BUGGY CODE ###

r,g,b,n=map(int,input().split())

rlim=n//r
glim=n//g
blim=n//b

"""
from bisect import bisect_left
def index(a, x):
    i = bisect_left(a, x)
    if blist.count(i)==1:
        return True
    else:
        return False
"""

blist=[]
for i in range(blim+1):
    blist.append(i*b)

cnt=0
for i in range(rlim+1):
    for j in range(glim+1):
        if (n-i*r-j*g) in blist:
            cnt+=1
print(cnt)

### FIXED CODE ###

r,g,b,n=map(int,input().split())

lst=[r,g,b]
lst.sort()
r=lst[2]
g=lst[1]
b=lst[0]

rlim=n//r
glim=n//g
blim=n//b

"""
blist = [i*b for i in range(blim+1)]

glist = [i*g for i in range(glim+1)]

def binary_search(list, item):
    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return True
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return False
"""

cnt=0

for i in range(rlim+1):
    glim=(n-i*r)//g
    for j in range(glim+1):
        if (n-i*r-j*g)%b==0:
            cnt+=1
print(cnt)
### BUGGY CODE ###

S = input()
Q = int(input())
count = 0
from collections import deque
q1 = deque()
q2 = deque()
for i in range(Q):
    li = input().split()
    if len(li) == 1:
        count +=1
        q11 = q1
        q22 = q2
        q1 = q22
        q2 = q11
        q1.reverse()
        q2.reverse()
    if len(li) == 3:
        if li[1] == '1':
            q1.appendleft(li[2])
        if li[1] == '2':
            q2.append(li[2])
s1 = ''.join(list(q1))
s2 = ''.join(list(q2))
if count % 2 == 1:
    K = reversed(list(S))
    S = ''.join(K)
print(s1+S+s2)

### FIXED CODE ###

S = input()
Q = int(input())
count = 0
from collections import deque
q1 = deque()
q2 = deque()
for i in range(Q):
    li = input().split()
    if len(li) == 1:
        count +=1
        q11 = q1
        q22 = q2
        q1 = q22
        q2 = q11
    if len(li) == 3:
        if li[1] == '1':
            if count % 2 == 1:
                q1.append(li[2])
            if count % 2 == 0:
                q1.appendleft(li[2])
        if li[1] == '2':
            if count % 2 == 1:
                q2.appendleft(li[2])
            if count % 2 == 0:
                q2.append(li[2])
s1 = ''.join(list(q1))
s2 = ''.join(list(q2))
if count % 2 == 1:
    K = reversed(list(S))
    S = ''.join(K)
    s1 = ''.join(reversed(list(s1)))
    s2 = ''.join(reversed(list(s2)))
print(s1+S+s2)
### BUGGY CODE ###

n=int(input(""))
ain=input("").split(" ")
a=[]
for i in range(n):
    a+=[int(ain[i])]
q=int(input(""))
c=[]
listb=[]
listc=[]
for i in range(q):
    c+=[input("").split(" ")]
    listb+=[int(c[i][0])]
    listc+=[int(c[i][1])]
    if (listb[i] in a):
       
        for k in range(n):
            if (a[k]==listb[i]):
                a[k]=listc[i]
    print(sum(a))
        

        


### FIXED CODE ###

n=int(input(""))
ain=input("").split(" ")
a={1:0,}
c=[]
for i in range(n):
    c+=[int(ain[i])]
s=sum(c)
for i in range(n):
    if (c[i] in a):
        a[c[i]]+=1
    else:
        a[c[i]]=1
q=int(input(""))
c=[]

for i in range(q):
    c=input("").split(" ")
    bb=int(c[0])
    cc=int(c[1])
    if (bb in a and a[bb]!=0):
        s+=(cc-bb)*a[bb]
        if (cc in a):
            a[cc]+=a[bb]
        else:
            a[cc]=a[bb]
        a[bb]=0
                              
    print(s)
### BUGGY CODE ###

s = input()
mod = int(1e9 + 7)
if s[-1]=="?":
    dp = [1]*13
    dp[12] = 0
    dp[11] = 0
    dp[10] = 0
else:
    dp = [0]*13
    dp[int(s[-1])] = 1

if len(s)==1:
    print(dp[5]%mod)
    exit()
else:
    mul = 10
    for i in reversed(range(len(s)-1)):
        nextdp = [0] * 13
        if s[i]=="?":
            for j in range(13):
                for k in range(10):
                    l = (k*mul) % 13
                    for m in range(13):
                        if ((l+m)%13)==j:
                            nextdp[j] += dp[m]
                        else:
                            nextdp[j] += 0
        else:
            for j in range(13):
                l = (int(s[i])*mul) % 13
                for m in range(13):
                    if ((l+m)%13)==j:
                        nextdp[j] += dp[m]
                    else:
                        nextdp[j] += 0
        mul *= 10
        dp = nextdp
print(dp[5]%mod)

### FIXED CODE ###

s = input()
mod = 10**9 + 7
N = len(s)
if s[-1]=="?":
    dp = [1]*13
    dp[10:13] = [0,0,0]
else:
    dp = [0]*13
    dp[int(s[-1])] = 1
if N==1:
    print(dp[5])
    exit()
else:
    mul = 10
    for i in reversed(range(N-1)):
        nextdp = [0] * 13
        if s[i]=="?":
            for j in range(10):
                for k in range(13):
                    l = (j*mul+k) % 13
                    nextdp[l] += dp[k]
                    nextdp[l] %= mod
        else:
            for k in range(13):
                l = (int(s[i])*mul+k) % 13
                nextdp[l] += dp[k]
                nextdp[l] %= mod
        mul *= 10
        mul %= 13
        dp = nextdp
    print(dp[5])
    exit()
### BUGGY CODE ###

x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=sorted([int(i) for i in input().split()],reverse=True)
ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[]
for i in range(l):
    for j in range(z):
        abc.append(ab[i]+c[j])
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])


### FIXED CODE ###

import heapq
x,y,z,k=map(int,input().split())
#heapqは小さい順にしか並べられないので、-をつければ大きい順になる
a=sorted([-int(i) for i in input().split()])
b=sorted([-int(i) for i in input().split()])
c=sorted([-int(i) for i in input().split()])
check=set()
ans=[]
heapq.heappush(ans,(a[0]+b[0]+c[0],0,0,0))
check.add((0,0,0))
for i in range(k):
    h=heapq.heappop(ans)
    print(-h[0])
    if h[1]+1<x:
        if (h[1]+1,h[2],h[3]) not in check:
            heapq.heappush(ans,(a[h[1]+1]+b[h[2]]+c[h[3]],h[1]+1,h[2],h[3]))
            check.add((h[1]+1,h[2],h[3]))
    if h[2]+1<y:
        if (h[1],h[2]+1,h[3]) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]+1]+c[h[3]],h[1],h[2]+1,h[3]))
            check.add((h[1],h[2]+1,h[3]))
    if h[3]+1<z:
        if (h[1],h[2],h[3]+1) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]]+c[h[3]+1],h[1],h[2],h[3]+1))
            check.add((h[1],h[2],h[3]+1))
'''
while len(ans)!=0:
    print(heapq.heappop(ans)[0])
'''
### BUGGY CODE ###

import re
n,q=map(int,input().split())
s=input()
for i in range(q):
  l,r=map(int,input().split())
  b=s[(l-1):r]
  m=[p.group(0)for p in re.finditer('(AC){1,1}',b)]
  print(len(m))

### FIXED CODE ###

import re
n,q=map(int,input().split())
s=input()
a=[0]*n
b=[0]*n
i=[p.span()[0]for p in re.finditer('(AC){1,1}',s)]
c=0
for m in range(n):
  if c>=len(i):
    if m<n-1:a[m+1]=a[m]
    b[m]=b[m-1]
    continue
  if m==i[c]:
    if m>0:
      if m<n-1:a[m+1]=a[m]+1
      b[m]=b[m-1]+1
    else:
      a[m]=0
      a[m+1]=1
      b[m]+=1
    c+=1
  else:
    a[m+1]=a[m]
    b[m]=b[m-1]
for _ in range(q):
  l,r=map(int,input().split())
  if l>=2:
    print(a[r-1]-b[l-2])
  else:
    print(a[r-1])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()

    used = 0
    while used < M:
        ma = max(A)
        idx = A.index(ma)
        A[idx] = A[idx] // 2
        used += 1
    print(sum(A))


    # TLE
    # for _ in range(M):
    #     A.sort()
    #     A[-1] = A[-1] // 2
    # print(sum(A))


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
import heapq 
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    A = list(map(lambda x : int(x) * -1, input().split()))
    heapq.heapify(A)

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

    print(sum(A) * -1)


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

# coding: utf-8

from functools import lru_cache
from itertools import accumulate as accum
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

@lru_cache(maxsize = None)
def f(l, r):
    if l == r:
        return(0)
    return(min(f(l, x) + f(x + 1, r) for x in range(l, r)) \
        + acsum[r] - acsum[l - 1])

n = int(input()) # 2 <= n <= 400
acsum = list(accum(map(int, input().split()))) + [0]
print(f(0, n - 1))


### FIXED CODE ###

# coding: utf-8

from itertools import accumulate as accum
import sys
input = sys.stdin.readline

def f2(n, a):
    acsum = list(accum(a)) + [0]
    dp = [[float('inf')] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = 0
        if i + 1 != n:
            dp[i][i + 1] = a[i] + a[i + 1]
    for i in range(n - 3, -1, -1):
        for j in range(i + 2, n):
            c = float('inf')
            dpi = dp[i]
            for x in range(i, j):
                c_ = dpi[x] + dp[x + 1][j]
                if c_ < c:
                    c = c_
            dpi[j] = c + acsum[j] - acsum[i - 1]
    return(dp[0][n - 1])

n = int(input()) # 2 <= n <= 400
a = list(map(int, input().split()))
print(f2(n, a))
### BUGGY CODE ###

s = input()

cnt = 0
#for c in range( int(s) // 2019 ):
#    d = ( c + 1 ) * 2019
#    print( d )
#    for n in range( len( str(s) ) - len( str(d) ) ):
#        if str( d ) in str( s )[n:]:
#            cnt += 1

for i in range( len(s) ):
    for j in range( i+3, len(s)+1 ):
        d = int( s[i:j] )
        if d % 2019 == 0:
            cnt += 1
            #print(i,j)

print( cnt )

### FIXED CODE ###

s = str(input())
N = len(s)
array = [0]*2019
array[0] += 1
count = 0
d = 1
r = 0
for i in reversed(range(0, N)):
    #x = s[i:N]
    #y = int(x)
    r = ( r + int( s[i] ) * d ) % 2019
    d = d * 10 % 2019
    array[r] += 1
for i in array:
    count += i*(i-1)/2
print(int(count))
### BUGGY CODE ###

A,B=map(int,input().split())
for i in range(1,10**10):
  if i%A==0 and i%B==0:
    print(i)
    break

### FIXED CODE ###

import math
A,B=map(int,input().split())
def lcm(A, B):
    return (A * B) // math.gcd(A, B)
print(lcm(A,B))
### BUGGY CODE ###

from sys import stdin
data = stdin.readlines()

n = int(data[0])
a = [int(s) for s in data[1].split()]
q = int(data[2])

i = 1
l = []
while i <= q:
    l.append(int(data[i+2].split()[0]))
    l.append(int(data[i+2].split()[1]))
    i += 1

i = 0
while i <= q-1:
    m = 0
    while m <= n-1:
        if a[m] ==  l[2*i]:
            a[m] = l[2*i+1]
        m += 1
    print(sum(a))
    i += 1

### FIXED CODE ###

import collections
from sys import stdin
data = stdin.readlines()

n = int(data[0].split()[0])
a = [int(s) for s in data[1].split()]
q = int(data[2].split()[0])

sum = sum(a)

l = []
for i in range(q):
    l.append(int(data[i+3].split()[0]))
    l.append(int(data[i+3].split()[1]))

d = collections.Counter(a)

for i in range(q):
    sum += (l[2*i+1]-l[2*i]) * d[l[2*i]]
    d[l[2*i+1]] += d[l[2*i]]
    d[l[2*i]] = 0
    print(sum)
### BUGGY CODE ###

# coding: utf-8
# Your code here!
N=int(input())

print(0)
who=input()
if who=="male":
    start=0
elif who=="female":
    start=1
else:
    exit()

high=N-1
low=0
while True:
    mid=(high+low)//2
    print(mid)
    who=input()
    if who=="male":
        temp=0
    elif who=="female":
        temp=1
    else:
        exit()
    
    if (N%2)^start==temp:
        low=mid
    else:
        high=mid
    


### FIXED CODE ###

# coding: utf-8
# Your code here!
N=int(input())

print(N-1)
if input()=="Vacant":
    exit()
print(0)
who=input()
if who=="Male":
    start=0
elif who=="Female":
    start=1
else:
    exit()

high=N-1
low=0
while True:
    mid=(high+low)//2
    print(mid)
    who=input()
    if who=="Male":
        temp=0
    elif who=="Female":
        temp=1
    else:
        exit()
    
    if (mid%2)^start==temp:
        low=mid
    else:
        high=mid
### BUGGY CODE ###

n,k=map(int,input().split())
A=[int(i) for i in input().split()]
AA=[0]*n
B=[2*n+1]*(2*(10**5)+1)
for i in reversed(range(n)):
  B[A[i]]=min(B[A[i]],n+i+1)
for i in reversed(range(n)):
  AA[i]=B[A[i]]-(i+1)
  B[A[i]]=i+1
count,index,inc=0,0,0
while count<n*k:
  index=(index+inc)%n
  inc=AA[index]+1
  count+=inc  
  if count%n==0:
    count*=(n*k)//count

count-=inc
d=n*k-count
Ans1=A+A
Ans1=Ans1[index:index+d]
Ans2=AA+AA
Ans2=Ans2[index:index+d]
Ans=[]
i=0
while i<d:
  if i+Ans2[i]>=d:
    Ans.append(Ans1[i])
    i+=1
  else:
    i+=Ans2[i]+1
print(*Ans)

### FIXED CODE ###

n,k=map(int,input().split())
A=[int(i) for i in input().split()]
AA=[0]*n
B=[2*n+1]*(2*(10**5)+1)
for i in reversed(range(n)):
  B[A[i]]=n+i+1
for i in reversed(range(n)):
  AA[i]=B[A[i]]-(i+1)
  B[A[i]]=i+1
count,index,inc=0,0,0
Chk=[0]*n
flag=False
while count<n*k:

  inc=AA[index]+1
  count+=inc  
  index=(index+inc)%n
  if Chk[index]!=0 or index==0:
    if flag==False:
      dd=count-Chk[index]
      count+=(((n*k)-count)//dd)*dd
      flag=True
  Chk[index]=count

count-=inc
index=(index+n-inc)%n

d=n*k-count
Ans1=A+A
Ans2=AA+AA
Ans=[]
i=index
while i<d+index:
  if i+Ans2[i]>=d+index:
    Ans.append(Ans1[i])
    i+=1
  else:
    i+=Ans2[i]+1
print(*Ans)
### BUGGY CODE ###


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

s=[1]*n
t=[[] for i in range(n+1)]
a=[0]*n
for i in range(n):
    a[i]=int(input())
    ast=max(a[i]-k,0)
    aed=min(a[i]+k,n)
    for ai in range(ast,aed+1):
        t[ai].append(i)
    
for i in range(n):
    for ti in t[a[i]]:
        if ti<i:
            s[i]=max(s[i],s[ti]+1)
            
print(max(s))


### FIXED CODE ###


class SegmentTree():
  def __init__(self, n, oper, e):
    self.n = n
    self.oper = oper
    self.e = e
    self.log = (n - 1).bit_length()
    self.size = 1 << self.log
    self.data = [e] * (2 * self.size)

  def update(self, k):
    self.data[k] = self.oper(self.data[2 * k], self.data[2 * k + 1])

  def build(self, arr):
    #assert len(arr) <= self.n
    for i in range(self.n):
      self.data[self.size + i] = arr[i]
    for i in range(self.size-1,0,-1):
      self.update(i)

  def set(self, p, x):
    #assert 0 <= p < self.n
    p += self.size
    self.data[p] = x
    for i in range(1, self.log + 1):
      self.update(p >> i)

  def get(self, p):
    #assert 0 <= p < self.n
    return self.data[p + self.size]

  def prod(self, l, r):
    #assert 0 <= l <= r <= self.n
    sml = smr = self.e
    l += self.size
    r += self.size
    while l < r:
      if l & 1:
        sml = self.oper(sml, self.data[l])
        l += 1
      if r & 1:
        r -= 1
        smr = self.oper(self.data[r], smr)
      l >>= 1
      r >>= 1
    return self.oper(sml, smr)

  def all_prod(self):
    return self.data[1]

  def max_right(self, l, f):
    #assert 0 <= l <= self.n
    #assert f(self.)
    if l == self.n: return self.n
    l += self.size
    sm = self.e
    while True:
      while l % 2 == 0: l >>= 1
      if not f(self.oper(sm, self.data[l])):
        while l < self.size:
          l = 2 * l
          if f(self.oper(sm, self.data[l])):
            sm = self.oper(sm, self.data[l])
            l += 1
        return l - self.size
      sm = self.oper(sm, self.data[l])
      l += 1
      if (l & -l) == l: break
    return self.n

  def min_left(self, r, f):
    #assert 0 <= r <= self.n
    #assert f(self.)
    if r == 0: return 0
    r += self.size
    sm = self.e
    while True:
      r -= 1
      while r > 1 and (r % 2): r >>= 1
      if not f(self.oper(self.data[r], sm)):
        while r < self.size:
          r = 2 * r + 1
          if f(self.oper(self.data[r], sm)):
            sm = self.oper(self.data[r], sm)
            r -= 1
        return r + 1 - self.size
      sm = self.oper(self.data[r], sm)
      if (r & -r) == r: break
    return 0

#import sys
#input = sys.stdin.readline

n,k=map(int,input().split())
a=[0]*n
for i in range(n):
    a[i]=int(input())
maxa=max(a)

st = SegmentTree(maxa+1,max,0)
#st.build(a)


ans=0
for i in range(n):
    l=max(a[i]-k,0)
    r=min(a[i]+k,300000)
    tmp=st.prod(l,r+1)+1
#    print(i,l,r+1,tmp)
    ans=max(ans,tmp)
    st.set(a[i],tmp)

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

import itertools

def solution():
    n, k = map(int, input().split())
    candidates = [10 ** 8 + i for i in range(n+1)]
    ans = 0
    for i in range(k, n+2):
        a = set([sum(b) for b in list(itertools.combinations(candidates, i))])
        ans += len(a)
    ans %= 10**9 + 7
    print(ans)

solution()


### FIXED CODE ###

def solution():
    n, k = map(int, input().split())
    ans = 0
    for i in range(k, n+2):
        smallest = (i-1) * i // 2
        largest = (n-i+1) * i + smallest
        ans += largest - smallest + 1
    ans %= 10 ** 9 + 7
    print(ans)

solution()
### BUGGY CODE ###

import copy

N = int(input())

a_list = []
for i in range(N):
    a_list.append(int(input()))

max_value = max(a_list)

for i in range(N):
  if a_list.count(max_value) > 1:
    print(max_value)
  else:
    if a_list[i] == max_value:
      print(sorted(a_list)[-2])
    else:
      print(max_value)
  


### FIXED CODE ###

N = int(input())

a_list = []
for i in range(N):
    a_list.append(int(input()))

max_value = max(a_list)

if a_list.count(max_value) > 1:
  for i in range(N):
    print(max_value)
else:
  for i in range(N):
    if a_list[i] == max_value:
      print(sorted(a_list)[-2])
    else:
      print(max_value)
### BUGGY CODE ###

word = input().rstrip()
n = int(input())
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        if len(word) == 1:
            pass
        else:
            word = word[::-1]
            # print(word)
    else:
        if a[1] == "2":
            word = word + a[2]
            # print("rtuika")
        else:
            word = a[2] + word
            # print("ltuika")
print(word)

### FIXED CODE ###

word = input().rstrip()
n = int(input())
hanten = 0
lhantenlist = []
rlist = []
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        hanten = (hanten + 1) % 2
        # word = word[::-1]
        #print(word)
    else:
        if (a[1] == "2" and hanten == 0) or (a[1] == "1" and hanten == 1):
            rlist.append(a[2])
            #print("rtuika: ",rlist)
        else:
            lhantenlist.append(a[2])
            #print("ltuika: ",lhantenlist)

lhanten="".join(lhantenlist)
r="".join(rlist)

if hanten == 0:
    anslist=[lhanten[::-1],word,r]
    ans="".join(anslist)
else:
    anslist = [r[::-1], word[::-1], lhanten]
    ans="".join(anslist)
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python
# coding: utf-8

# In[20]:


N = int(input())
S = input()
"""
5
ababa
2

13
strangeorange
5
"""


# In[25]:


dp = [[0]*(N+1) for i in range(N+1)]


# In[26]:


max_cont = 0
for i in range(N)[::-1]:
    for j in range(N)[::-1]:
        if i < j:
            if S[i] == S[j]:
                dp[i][j] = min(dp[i+1][j+1]+1, j-i)
                max_cont = max(max_cont, dp[i][j])
            else:
                continue
        else:
            continue


# In[27]:


print(max_cont)


# In[ ]:






### FIXED CODE ###

#!/usr/bin/env python
# coding: utf-8

# In[49]:


#import numpy as np
N = int(input())
S = input()
"""
5
ababa
2

13
strangeorange
5
"""


# In[54]:


i = 0
j = 1
max_cont = 0

while j < N:
    if S[i:j] in S[j:]:
        max_cont = max(max_cont, len(S[i:j]))
        j += 1
    else:
        i += 1
    if i == j:
        i += 1
        j += 2


# In[55]:


print(max_cont)


# In[ ]:
### BUGGY CODE ###

from collections import defaultdict as dd
from collections import deque

n,m=map(int,input().split())
dic=dd(list)
for i in range(n):
    a,b=map(int,input().split())
    dic[m-a].append(b)

for k,v in dic.items():
    dic[k]=deque(sorted(dic[k],reverse=True))

#print(dic)

ans=0
for i in range(m):
    tv=0
    j=i
    tk=-1
    while i<=j<=m:
        if len(dic[j])==0: j+=1; continue;
        if tv<dic[j][0]:
            tv=dic[j][0]
            tk=j
        j+=1
    if tk!=-1:
        dic[tk].popleft()
        ans+=tv

print(ans)

### FIXED CODE ###

import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

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

import math
import sys
input = sys.stdin.readline
a,b=map(int,input().split())
for i in range(1, a*b +1):
  if i%a == 0 and i%b == 0:
    print(i)
    break

### FIXED CODE ###

import math
import sys
input = sys.stdin.readline
a,b=map(int,input().split())
i=1
while True:
  if a*i % b == 0:
    print(a*i)
    break
  i+=1
### BUGGY CODE ###

n=int(input())
a=input()
q=int(input())
for _ in range(q):
    b,c = input().split(' ')
    a = a.replace(b,c)
    print(sum(map(int,a.split(' '))))

### FIXED CODE ###

from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
counter = Counter(a)
sum_res = sum(a)
for _ in range(q):
    before, after = map(int, input().split())
    sum_res -= before * counter[before]
    sum_res += after * counter[before]
    counter[after] += counter[before]
    counter[before] = 0
    print(sum_res)