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

from collections import defaultdict as dd
h, w, m = map(int, input().split())

h_dic = dd(set)
w_dic = dd(set)

for i in range(m):
    h_c, w_c = map(int, input().split())
    h_dic[h_c].add(i)
    w_dic[w_c].add(i)
#print(h_dic, w_dic)

max_counter = 0
for i in h_dic.keys():
    for j in w_dic.keys():
        counter = len(h_dic[i] | w_dic[j])
        #print(i, j, h_dic[i] | w_dic[j],counter)
        max_counter = max(max_counter, counter)
print(max_counter)

### FIXED CODE ###

from sys import exit
from collections import defaultdict as dd
h, w, m = map(int, input().split())

#dic[row, col] -> set(ids)
h_dic = dd(int)
w_dic = dd(int)
spot = set()

for i in range(m):
    h_c, w_c = map(int, input().split())
    h_dic[h_c] += 1
    w_dic[w_c] += 1
    spot.add((h_c, w_c))
#print(h_dic, w_dic)

#H_lis -> [i, num_spot(int)]
max_h_num = 0
for key, val in h_dic.items():
    max_h_num = max(max_h_num, val)
max_w_num = 0
W_lis = []
for key, val in w_dic.items():
    max_w_num = max(max_w_num, val)


#print(H_lis, W_lis)
#print(max_h)

ans = 0
max_h_lis = [key for key in h_dic if h_dic[key] == max_h_num]
max_w_lis = [key for key in w_dic if w_dic[key] == max_w_num]
for i in max_h_lis:
    for j in max_w_lis:
        if (i, j) not in spot:
            print(max_h_num + max_w_num)
            exit()
else:
    print(max_h_num + max_w_num - 1)

"""    
max_counter = 0
for i in h_dic.keys():
    for j in w_dic.keys():
        counter = len(h_dic[i] | w_dic[j])
        #print(i, j, h_dic[i] | w_dic[j],counter)
        max_counter = max(max_counter, counter)
print(max_counter)

"""
### BUGGY CODE ###

H, W = map(int, input().split())
lab = ["" for _ in range(H)]
vir = [[0 for _ in range(W+2)] for _ in range(H+2)]
hor = [[0 for _ in range(W+2)] for _ in range(H+2)]
for h in range(H):
    lab[h] = input()
for h in range(H):
    for w in range(W):
        if lab[h][w] == ".":
            if vir[h][w+1] > 0:
                vir[h+1][w+1] = vir[h][w+1]
            else:
                for h2 in range(h, H):
                    if lab[h2][w] == '.':
                        vir[h+1][w+1] += 1
                    else:
                        break
            if hor[h+1][w] > 0:
                hor[h+1][w+1] = hor[h+1][w]
            else:
                for w2 in range(w, W):
                    if lab[h][w2] == '.':
                        hor[h+1][w+1] += 1
                    else:
                        break
m = 0
for h in range(H):
    for w in range(W):
        m = max(m, vir[h+1][w+1] + hor[h+1][w+1] - 1)
print(m)


### FIXED CODE ###

import sys
import numpy as np

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

H, W = lr()

S = [list(sr()) for _ in range(H)]
S = np.array([[1 if x == '.' else 0 for x in row] for row in S])

U = np.zeros((H, W), np.int32)
D = np.zeros((H, W), np.int32)
L = np.zeros((H, W), np.int32)
R = np.zeros((H, W), np.int32)

for h in range(H-1):
    U[h+1] = (U[h] + 1) * S[h]
    D[H-h-2] = (D[H-h-1] + 1) * S[H-h-1]
for w in range(W-1):
    L[:, w+1] = (L[:, w] + 1) * S[:, w]
    R[:, W-w-2] = (R[:, W-w-1] + 1) * S[:, W-w-1]


print(((U + D + L + R) * S).max() + 1)
### BUGGY CODE ###

import math

ans = []
while True:
	try:
		n = int(raw_input())
		isPrime = [-1] * (n + 1)
		isPrime[0], isPrime[1] = False, False
		i = 2
		while i * i < n:
			j = i 
			while j < len(isPrime):
				isPrime[j] = False
				j += i
			isPrime[i] = True
			i = isPrime.index(-1)
		ans.append(isPrime.count(True) + isPrime.count(-1))
	except EOFError:
		break
for num in ans:
	print num

### FIXED CODE ###

MAX = 1000000
prime = [1] * MAX
prime[0], prime[1] = 0, 0
i = 2
while i * i < MAX:
    j = 2
    while i * j < MAX:
        prime[i * j] = 0
        j += 1
    i += 1
for k in range(1, len(prime)):
    prime[k] += prime[k-1]

while True:
    try:
        n = int(raw_input())
        print prime[n]
    except EOFError:
        break
### BUGGY CODE ###

h, w = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
ti, tj = list(map(int, input().split()))
s = [None] * h
for i in range(h):
    s[i] = input()

# 移動Aで探索する方向
d_a = [(-1,0), (0,-1),(1,0),(0,1)]
# 移動Bで探索する方向
d_b = [ (i, j) for j in range(-2, 3) for i in range(-2, 3)]

# 距離情報を一番遠い値で初期化
#import numpy as np
INF = int(1e9)
#dist = np.full((h+4, w+4), INF)
dist = [[INF for j in range(w+4)] for i in range(h+4)]

# マップ境界と壁を距離0にして探索させない
for i in range(len(dist)):
    dist[i][0] = 0
    dist[i][1] = 0
    dist[i][-1] = 0
    dist[i][-2] = 0
for j in range(len(dist[0])):
    dist[0][j] = 0
    dist[1][j] = 0
    dist[-1][j] = 0
    dist[-2][j] = 0
for i in range(h):
    for j in range(w):
        if s[i][j]=='#': dist[i+2][j+2] = 0

# 01-BFS探索用キュー
from collections import deque
q = deque()

# 開始位置、終了位置を調整
si, sj = si+1, sj+1
ti, tj = ti+1, tj+1
dist[si][sj] = 0
q.append((si,sj))

while (len(q)>0):
    i, j = q[0]
    d = dist[i][j]
    q.popleft()
    # 移動Aによるコスト
    for v in range(len(d_a)):
        di, dj = d_a[v][0]+i, d_a[v][1]+j
        if (di, dj) == (ti, tj):
            print(d)
            exit(0)
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d
        q.appendleft((di, dj))
    # 移動Bによるコスト
    for v in range(len(d_b)):
        di, dj = d_b[v][0]+i, d_b[v][1]+j
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d+1
        q.append((di, dj))

ans = dist[ti][tj]
if ans == INF:
    ans = -1
print(ans)


### FIXED CODE ###

h, w = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
ti, tj = list(map(int, input().split()))
s = [None] * h
for i in range(h):
    s[i] = input()

# 移動Aで探索する方向
d_a = [(-1,0), (0,-1),(1,0),(0,1)]
# 移動Bで探索する方向
d_b = [ (i, j) for j in range(-2, 3) for i in range(-2, 3)]

# 距離情報を一番遠い値で初期化
#import numpy as np
INF = int(1e9)
#dist = np.full((h+2, w+2), INF)
dist = [[INF for j in range(w+2)] for i in range(h+2)]

# マップ境界と壁を距離0にして探索させない
for i in range(h+2):
    dist[i][0] = 0
    dist[i][-1] = 0
for j in range(w+2):
    dist[0][j] = 0
    dist[-1][j] = 0
for i in range(h):
    for j in range(w):
        if s[i][j]=='#': dist[i+1][j+1] = 0
#dist = [[INF for j in range(w)] for i in range(h)]

# 01-BFS探索用キュー
from collections import deque
q = deque()

# 開始位置
dist[si][sj] = 0
q.append((si,sj))

while (len(q)>0):
    i, j = q[0]
    d = dist[i][j]
    q.popleft()
    # 移動Aによるコスト
    for v in range(len(d_a)):
        di, dj = d_a[v][0]+i, d_a[v][1]+j
        if (di, dj) == (ti, tj):
            print(d)
            exit(0)
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d
        q.appendleft((di, dj))
    # 移動Bによるコスト
    for v in range(len(d_b)):
        # マップ境界で2以上移動したときに、反対の境界に向かうように剰余を取る
        di, dj = (d_b[v][0]+i)%(h+2), (d_b[v][1]+j)%(w+2)
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d+1
        q.append((di, dj))

ans = dist[ti][tj]
if ans == INF:
    ans = -1
print(ans)
### BUGGY CODE ###

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

i = 0
ans = 0
mul = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        mul = (b[i]-a[i])//(b[(i-1)%n]+b[(i+1)%n])
        b[i] =  b[i]-(b[(i-1)%n]+b[(i+1)%n])*mul
        ans += mul
        i = 0
    else:
        i += 1
if a==b:
    print(ans)
else:
    print(-1)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)
### BUGGY CODE ###

N = int(input())

print(sum([i for i in range(N)]))

### FIXED CODE ###

from decimal import *
N = int(input())

ans = Decimal(N)/Decimal(2)
ans *= Decimal((N-1))
print(int(ans))
### BUGGY CODE ###

from heapq import heappush, heappop

INF = float("inf")

N, *S = map(int, open(0).read().split())

S.sort(reverse=True)

P = [S[0]]
for _ in range(N):
    Q = P[:]
    while Q:
        q = heappop(Q)

        for i, s in enumerate(S):
            if s < q:
                heappush(P, s)
                S[i] = INF
                break
        else:
            print("No")
            quit()

print("Yes")

### FIXED CODE ###

INF = float("inf")

N, *S = map(int, open(0).read().split())
M = len(S)

S.sort(reverse=True)

Q = [S[0]]
S[0] = INF
for _ in range(N):
    Q.sort()
    T = Q[:]

    cur = 1
    while Q:
        p = Q.pop()
        while S[cur] >= p:
            cur += 1
            if cur == M:
                print("No")
                exit()
        T.append(S[cur])
        S[cur] = INF

    Q = T

print("Yes")
### BUGGY CODE ###

n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    cand.append(c)
cand.sort()
cand = cand[::-1]
chan = []
for i in range(min(len(List),len(cand))):
  if cand[i] >=List[i]:
    chan.append(cand[i])
  else:
    break
leng = len(chan)
ans = chan +List[leng:]
print(sum(ans))

### FIXED CODE ###

n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))
### BUGGY CODE ###

def main():
    N = int(input())
    ans = 0

    for i in range(1, N+1):
        if i != N:
            ans += i%(i+1)

    print(ans)

    return 0
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    N = int(input())
    ans = (N-1)*N//2

    print(ans)

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

h,w=map(int,input().split())
g=["#"*(w+1)]+["#"+input()+"#"for _ in range(h)]+["#"*(w+1)]
def check(i,j):
  global g
  a=1
  k=1
  while g[i-k][j]!="#":k+=1;a+=1
  k=1
  while g[i+k][j]!="#":k+=1;a+=1
  k=1
  while g[i][j-k]!="#":k+=1;a+=1
  k=1
  while g[i][j+k]!="#":k+=1;a+=1
  return a
ans=0
for i in range(1,h+1):
  for j in range(1,w+1):
    if g[i][j]!="#":ans=max(ans,check(i,j))
print(ans)

### FIXED CODE ###

import sys
input=sys.stdin.readline
h,w=map(int,input().split())
g=[["#"]*(w+1)]+[["#"]+list(input())+["#"]for _ in range(h)]+[["#"]*(w+1)]
gg=[[0]*w for _ in range(h)]
for i in range(1,h+1):
  k=1
  for j in range(1,w+1):
    if g[i][j]==".":
      gg[i-1][j-1]=k;k+=1
    else:k=1
  d=gg[i-1][-1]
  for j in range(1,w+1):
    if gg[i-1][-j]!=0:gg[i-1][-j]=d
    elif j!=w:d=gg[i-1][-j-1]
ggg=[[0]*w for _ in range(h)]
for j in range(1,w+1):
  k=1
  for i in range(1,h+1):
    if g[i][j]==".":
      ggg[i-1][j-1]=k;k+=1
    else:k=1
  d=ggg[-1][j-1]
  for i in range(1,h+1):
    if ggg[-i][j-1]!=0:ggg[-i][j-1]=d
    elif i!=h:d=ggg[-i-1][j-1]
ans=0
for i in range(h):
  for j in range(w):ans=max(ans,gg[i][j]+ggg[i][j])
print(ans-1)
### BUGGY CODE ###

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

MAX_VALUE = 100001
dp = [[False for _ in range(MAX_VALUE)] for _ in range(N+1)]

dp[0][0] = True
for i in range(N):
    score = s[i]
    for j in range(MAX_VALUE):
        dp[i+1][j] |= dp[i][j]
        if j >= score:
            dp[i+1][j] |= dp[i][j-score]
ans = 0

for i in range(MAX_VALUE):
    if dp[N][i] == True and i % 10 != 0:
        ans = i
print(ans)



### FIXED CODE ###

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

MAX_VALUE = 100001
dp = [[False for _ in range(MAX_VALUE)] for _ in range(N+1)]

dp[0][0] = True
for i in range(N):
    score = s[i]
    for j in range(MAX_VALUE):
        dp[i+1][j] |= dp[i][j]
        if j >= score:
            dp[i+1][j] |= dp[i][j-score]
ans = 0

for i in range(MAX_VALUE):
    if dp[N][i] == True and i % 10 != 0:
        ans = i
print(ans)
### BUGGY CODE ###

def main():
    n, q = map(int, input().split())
    s = input()
    # l = list()
    # r = list()
    s_list = list()
    # s_tuple = tuple()
    for _ in range(q):
        l_i, r_i = map(int, input().split())
        # l.append(l_i)
        # r.append(r_i)
        s_list.append(s[l_i - 1:r_i])
        # s_tuple = s_tuple + tuple(s[l_i - 1:r_i])
        s_tuple = tuple(s_list)

    for i in s_tuple:
        print(i.count('AC'))


if __name__ == '__main__':
    main()



### FIXED CODE ###

def main():
    # n, q = map(int, input().split())
    # s = input()
    # # l = list()
    # # r = list()
    # s_list = list()
    # # s_tuple = tuple()
    # for _ in range(q):
    #     l_i, r_i = map(int, input().split())
    #     # l.append(l_i)
    #     # r.append(r_i)
    #     s_list.append(s[l_i - 1:r_i])
    #     # s_tuple = s_tuple + tuple(s[l_i - 1:r_i])
    # 
    # s_tuple = tuple(s_list)
    # 
    # for i in s_tuple:
    #     print(i.count('AC'))
    N, Q = map(int, input().split())
    S = input()
    cnt = [0] * (N + 1)
    for i in range(N):
        cnt[i + 1] = cnt[i] + (1 if S[i:i+2] == 'AC' else 0)
    for i in range(Q):
        l, r = map(int, input().split())
        print(cnt[r - 1] - cnt[l - 1])


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

import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

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

arr = np.array(a)
csr = csr_matrix(arr)
f = floyd_warshall(csr)

if np.sum(arr == f) != n**2:
    print(-1)
    exit()
total = np.sum(arr) // 2
for i in range(n-1):
    for j in range(i+1,n):
        for x in range(n):
            if x == i or x == j:
                continue
            if a[i][j] == a[i][x] + a[j][x]:
                total -= a[i][j]
print(total)

### FIXED CODE ###

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

total = 0
for i in range(n-1):
    for j in range(i+1,n):
        flg = True
        for x in range(n):
            if x == i or x == j:
                continue
            if a[i][j] > a[i][x] + a[j][x]:
                print(-1)
                exit()
            elif a[i][j] == a[i][x] + a[j][x]:
                flg = False
                break
        if flg:
            total += a[i][j]
            
print(total)
### BUGGY CODE ###

a=list(map(int,input().split()))
b=[]
c=[1]*(a[0]-1)
for i in range(a[1]):
  b.append(list(map(int,input().split())))
b=sorted(b,key=lambda b:b[1])
for i in range(a[1]):
  if 0 in c[(b[i][0]-1):b[i][1]]:
    continue
  else:
    c[(b[i][1])-2]=0

print(c.count(0))

### FIXED CODE ###

a=list(map(int,input().split()))
b=[]
c=0
r=0
for i in range(a[1]):
  b.append(list(map(int,input().split())))
b=sorted(b,key=lambda b:b[1])

for i in range(a[1]):
  if b[i][0]<=r:
    continue
  else:
    c=c+1
    r=b[i][1]-1

print(c)
### BUGGY CODE ###

import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n, k = i_map()
    num_list = i_list()

    ans_list = []
    tmp = 1
    after_score = num_list[0]
    for i in range(k):
        tmp *= num_list[i]

    ans_list.append(tmp)

    for i in range(n-k):
        ima = tmp / after_score
        ima *= num_list[i+k]
        after_score = num_list[i+1]
        
        if ima > tmp:
            print('Yes')
        else:
            print('No')
        tmp = ima



if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n, k = i_map()
    num_list = i_list()

    for i in range(n-k):
        if num_list[i] < num_list[i+k]:
            print('Yes')
        else:
            print('No')

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


from fractions import gcd
def main():

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

    MOD = 10**9 + 7

    a_lcm = a_list[0]
    for a in a_list[1:]:
        a_lcm *= a // gcd(a_lcm, a)

    res = 0
    for a in a_list:
        res += a_lcm // a
        res %= MOD
    print(res)

if __name__ == "__main__":
    main()

### FIXED CODE ###


from fractions import gcd
def main():

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

    MOD = 10**9 + 7

    a_lcm = a_list[0]
    for a in a_list[1:]:
        a_lcm *= a // gcd(a,a_lcm)

    res = 0
    for a in a_list:
        res += a_lcm // a
    print(res%MOD)

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

n = int(input())
for _ in range(n):
    a = input()
    b = input()
    na=len(a)
    nb=len(b)
    array = [[0]*nb for i in range(na)]
    #array = [[0 for i in range(nb)] for j in range(na)]
    for i in range(na):
        for j in range(nb):
            c = 0
            if i==0:
                if a[i]==b[j]:
                    array[i][j]=1
            elif j==0:
                if a[i]==b[j]:
                    array[i][j]=1
            else:
                if a[i] == b[j]:
                    c = 1
                array[i][j] = max(
                    array[i-1][j],
                    array[i][j-1],
                    array[i-1][j-1]+c)
    print(array[na-1][nb-1])


### FIXED CODE ###

#dj 16D8101012J ito jun
import sys
def search(a,b):
    na=len(a)
    nb=len(b)
    array = [[0]*(nb+1) for _ in range(na+1)]
    for i,x in enumerate(a,1):
        prerow=array[i-1]
        row=array[i]
        for j,y in enumerate(b,1):
            if x == y:
                row[j]=prerow[j-1]+1
            elif prerow[j] < row[j-1]:
                row[j] = row[j-1]
            else:
                row[j] = prerow[j]
    return array[-1][-1]

n = int(input())
strs = [list(i) for i in sys.stdin.read().splitlines()]
result = [search(strs[i*2], strs[i*2+1]) for i in range(n)]
print(*result, sep="\n")
### BUGGY CODE ###

s = input()
q = int(input())
reverse_now = 0
first = ""
latter = ""
for i in range(q):
    k = list(input().split())
    if k[0] == "1":
        reverse_now = 1 - reverse_now
    else:
        f,c = k[1], k[2]
        if f == "1" and reverse_now == 0:
            first = c + first
        if f == "1" and reverse_now == 1:
            latter = latter + c
        if f == "2" and reverse_now == 0:
            latter = latter + c
        if f == "2" and reverse_now == 1:
            first = c + first
if reverse_now == 1:
    ans = list(first + s + latter)
    ans.reverse()
    ans = "".join(ans)
else:
    ans = first + s + latter
print(ans)




### FIXED CODE ###

s = input()
q = int(input())
reverse_now = 0
first = ""
latter = ""
for i in range(q):
    k = list(input().split())
    if k[0] == "1":
        reverse_now = 1 - reverse_now
    else:
        f,c = k[1], k[2]
        if f == "1" and reverse_now == 0:
            first = c + first
        if f == "1" and reverse_now == 1:
            latter += c
        if f == "2" and reverse_now == 0:
            latter +=  c
        if f == "2" and reverse_now == 1:
            first = c + first
if reverse_now == 1:
    ans = list(first + s + latter)
    ans.reverse()
    ans = "".join(ans)
else:
    ans = first + s + latter
print(ans)
### BUGGY CODE ###

s=input()
q=int(input())
for i in range(q):
  ss=input()
  if ss[0]=='1':
    s=s[::-1]
    #s=t
  else:
    t,f,c=ss.split()
    if f=='1':
      s=c+s
      #s=t
    else:
      s=s+c
      
    







print(s)

### FIXED CODE ###

s=input()
q=int(input())
mae=''
ato=''
ten=0

for i in range(q):
  ss=input()
  if ss[0]=='1':
    ten+=1
    ten%=2
  else:
    t,f,c=ss.split()
    if f=='1':
      if ten==0:
        mae+=c
      else:
        ato+=c        
    else:
      if ten==0:
        ato+=c
      else:
        mae+=c
mae=mae[::-1]
s=mae+s+ato
if ten==1:
  s=s[::-1]
print(s)
### BUGGY CODE ###

N = int(input())
s = [c for c in input()]

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

for i in range(1, N+1):
    for j in range(i+1, N+1):
        if s[j-1] == s[i-1]:
            prev = dpt[i-1][j-1]
            if j - prev > i:
                dpt[i][j] = prev + 1

result = 0
for i in range(N + 1):
    result = max(result, max(dpt[i]))

print(result)


### FIXED CODE ###

import numpy as np
from numba import njit
 
@njit(cache=True)
def main(N, s):
    dpt = np.zeros((N+1, N+1), dtype=np.int16)
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if s[j-1] == s[i-1]:
                if j - dpt[i-1][j-1] > i:
                    dpt[i][j] = dpt[i-1][j-1] + 1
 
    return np.amax(dpt)
 
if __name__ == '__main__':
    N = int(input())
    s = np.array([ord(c) for c in input()]) 
 
    print(main(N, s))
### BUGGY CODE ###

N,K = list(map(int,input().split()))
TDlist = []

for x in range(0,N):
    TDlist.append(list(map(int,input().split())))

TDlist.sort(key = lambda x: x[1],reverse=True)
kinds = []
seconds = []
answer = 0

for x in TDlist[:K]:
    if not x[0] in kinds:
        kinds.append(x[0])
    else:
        seconds.append(x[1])
    answer += x[1]

answer += len(kinds)**2
score = answer
max_score = answer

for x in TDlist[K:]:
    if seconds == []:
        break
    if x[0] in kinds:
        continue
    kinds.append(x[0])
    y = seconds.pop()
    score += (x[1] - y) + (len(kinds)*2 -1)
    max_score = max(score,answer)
    

answer = max(answer,max_score)

print(answer)

### FIXED CODE ###

N,K = list(map(int,input().split()))
TDlist = []

for x in range(0,N):
    TDlist.append(list(map(int,input().split())))

TDlist= sorted(TDlist, key = lambda x: x[1],reverse=True)
kinds = set()
seconds = []
answer = 0

for x in TDlist[:K]:
    if not x[0] in kinds:
        kinds.add(x[0])
    else:
        seconds.append(x[1])
    answer += x[1]

answer += len(kinds)**2
score = answer

for x in TDlist[K:]:
    if seconds == []:
        break
    if x[0] in kinds:
        continue
    kinds.add(x[0])
    y = seconds.pop()
    score += (x[1] - y) + (len(kinds)*2 -1)
    answer = max(score,answer)

print(answer)