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

import itertools


def get_discomfort(group, color, D):
    s = 0
    for c in group:
        s += D[c - 1][color - 1]
    return s


N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]


grids = [[], [], []]
for i in range(N):
    c = list(map(int, input().split()))
    for j in range(N):
        grids[(i + j) % 3].append(c[j])

ans = float("inf")
for colors in itertools.permutations(list(range(C)), 3):
    colors = list(colors)
    s = 0
    for i, c in enumerate(colors):
        s += get_discomfort(grids[i], c, D)
    ans = min(ans, s)

print(ans)


### FIXED CODE ###

import itertools


def get_discomfort(group_dic, color, D):
    s = 0
    for c, num in group_dic.items():
        s += D[c - 1][color - 1] * num
    return s


N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]


from collections import Counter


grids = [Counter() for _ in range(3)]

for i in range(N):
    c = list(map(int, input().split()))
    for j in range(N):
        grids[(i + j) % 3][c[j]] += 1


ans = float("inf")
for colors in itertools.permutations(list(range(C)), 3):
    colors = list(colors)
    s = 0
    for i, c in enumerate(colors):
        s += get_discomfort(grids[i], c, D)
    ans = min(ans, s)

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

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max = max(a)

for i in range(n):
    if a[i] == a_max:
        tmp = a.pop(i)
        print(max(a))
        a.insert(i, tmp)
    else:
        print(a_max)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max_1 = max(a)
b = sorted(a, reverse=True)
a_max_2 = b[1]

for i in range(n):
    if a[i] == a_max_1:
        print(a_max_2)
    else:
        print(a_max_1)
### BUGGY CODE ###

h, w = map(int, input().split())

s = []

for i in range(h):
    s.append(input())

_h = [[0 for i in range(w)] for j in range(h)]
_w = [[0 for i in range(w)] for j in range(h)]
_h2 = [[0 for i in range(w)] for j in range(h)]
_w2 = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
    _h[i][0] = 1 if s[i][0] == "." else 0

for i in range(w):
    _w[0][i] = 1 if s[0][i] == "." else 0

for i in range(h):
    for j in range(1, w):
        _h[i][j] = _h[i][j - 1] + 1 if s[i][j] == "." else 0

for i in range(1, h):
    for j in range(w):
        _w[i][j] = _w[i - 1][j] + 1 if s[i][j] == "." else 0

for i in range(h):
    _h2[i][w - 1] = 1 if s[i][w - 1] == "." else 0

for i in range(w):
    _w2[h - 1][i] = 1 if s[h - 1][i] == "." else 0

for i in range(h):
    for j in range(w - 2, -1, -1):
        _h2[i][j] = _h2[i][j + 1] + 1 if s[i][j] == "." else 0

for i in range(h - 2, -1, -1):
    for j in range(w):
        _w2[i][j] = _w2[i + 1][j] + 1 if s[i][j] == "." else 0

maximum = 0

for i in range(h):
    for j in range(w):
        ans = _h[i][j] + _h2[i][j] + _w[i][j] + _w2[i][j] - 3
        if ans >= maximum:
            maximum = ans

print(maximum)

### FIXED CODE ###

h, w = map(int, input().split())

s = []

for i in range(h):
    s.append(input())

_h = [[0 for i in range(w)] for j in range(h)]
_w = [[0 for i in range(w)] for j in range(h)]
_h2 = [[0 for i in range(w)] for j in range(h)]
_w2 = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
    _h[i][0] = 1 if s[i][0] == "." else 0

for i in range(w):
    _w[0][i] = 1 if s[0][i] == "." else 0

for i in range(h):
    for j in range(1, w):
        _h[i][j] = _h[i][j - 1] + 1 if s[i][j] == "." else 0

for i in range(1, h):
    for j in range(w):
        _w[i][j] = _w[i - 1][j] + 1 if s[i][j] == "." else 0

for i in range(h):
    _h2[i][w - 1] = 1 if s[i][w - 1] == "." else 0

for i in range(w):
    _w2[h - 1][i] = 1 if s[h - 1][i] == "." else 0

for i in range(h):
    for j in range(w - 2, -1, -1):
        _h2[i][j] = _h2[i][j + 1] + 1 if s[i][j] == "." else 0

for i in range(h - 2, -1, -1):
    for j in range(w):
        _w2[i][j] = _w2[i + 1][j] + 1 if s[i][j] == "." else 0

maximum = 0

for i in range(h):
    for j in range(w):
        ans = _h[i][j] + _h2[i][j] + _w[i][j] + _w2[i][j] - 3
        if ans >= maximum:
            maximum = ans

print(maximum)
### BUGGY CODE ###

import bisect

S = input()
Q = int(input())

S = [s for s in S]

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S.insert(len(S), c)
            else:
                S.insert(0, c)
        else:
            if is_filp:
                S.insert(0, c)
            else:
                S.insert(len(S), c)

if is_filp:
    S.reverse()
    print(''.join(S))
else:
    print(''.join(S))

### FIXED CODE ###

import bisect

S = input()
Q = int(input())

S = {i: s for i, s in enumerate(S)}


head = -1
tail = len(S)

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S[tail] = c
                tail += 1
            else:
                S[head] = c
                head -= 1
        else:
            if is_filp:
                S[head] = c
                head -= 1
            else:
                S[tail] = c
                tail += 1

if is_filp:
    print(''.join([S[i] for i in range(tail - 1, head, -1)]))
else:
    print(''.join([S[i] for i in range(head + 1, tail)]))
### BUGGY CODE ###

import functools
import fractions
"""最大公約数"""
def gcd(a,b):
    return fractions.gcd(a,b)
"""最小公倍数"""
def lcm(a,b):
    f = gcd(a,b)
    return a*b//f
def m(nums):
    return functools.reduce(lcm, nums)

n = int(input())
A = list(map(int,input().split()))
mod = 10**9+7
l = m(A)
res = 0
for a in A:
    res += l//a
    res %= mod
print(res%mod)

### FIXED CODE ###

import functools
"""最大公約数"""
def gcd(a,b):
    import fractions
    return fractions.gcd(a,b)

"""最小公倍数"""
def lcm(a,b):
    f = gcd(a,b)
    return a*b//f

def m(nums):
    return functools.reduce(lcm, nums)

def main():
    n = int(input())
    A = list(map(int,input().split()))
    mod = 10**9+7
    l = m(A)
    res = 0
    print(sum(l// i for i in A) % mod)

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

def resolve():
    import sys
    input = sys.stdin.readline
    from collections import defaultdict
    n, s = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 998244353
    ans = 0
    dp = [defaultdict(int) for _ in range(s + 1)]
    dp[0][0] = 1
    for i in a:
        for j in range(s - i, -1, -1):
            for k, v in dp[j].items():
                dp[j + i][k + 1] += v
    for k, v in dp[-1].items():
        ans += pow(2, n - k, mod) * v
    ans %= mod
    print(ans)


if __name__ == '__main__':
    resolve()


### FIXED CODE ###

def resolve():
    n, s = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 998244353
    modinv = pow(2, mod - 2, mod)
    dp = [0] * (s + 1)
    dp[0] = pow(2, n, mod)
    for i in a:
        for j in range(s - i, -1, -1):
            dp[j + i] += dp[j] * modinv
            dp[j + i] %= mod
    print(dp[-1])


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


v, e = map(int, input().split()) # 頂点数vと辺数eを入力

g = [[] for i in range(v)] # グラフの要素数を確保

for i in range(e):
 s, t = map(int, input().split()) 
 g[s].append(t) # グラフに辺を挿入


ans = 0 # 答え
color = ['WHITE' for i in range(v)] # 全て未探索
stack = []
color[0] = 'GRAY'
stack.append(0)

while len(stack) != 0:
 u = stack.pop()

 for adj in g[u]:
  if color[adj] == 'GRAY':
   ans = 1
   stack.clear()
   break
  elif color[adj] == 'WHITE':
   stack.append(adj)
   color[adj] == 'GRAY'

print(ans)


### FIXED CODE ###


v, e = map(int, input().split()) # 頂点数vと辺数eを入力

g = [[] for i in range(v)] # グラフの要素数を確保

for i in range(e):
 s, t = map(int, input().split()) 
 g[s].append(t) # グラフに辺を挿入


color = ['WHITE' for i in range(v)] # 全て未探索

def dfs(u):
 color[u] = 'GRAY'

 for adj in g[u]:
  if color[adj] == 'GRAY':
   return 1
  elif color[adj] == 'WHITE':
   if dfs(adj) == 1:
    return 1

 color[u] = 'BLACK'
 return 0

ans = 0
for u in range(v):
 color[i in range(v)] = 'WHITE'
 if dfs(u) == 1:
  ans = 1
  break

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

import sys
def main():
  S=list(sys.stdin.readline())[:-1]
  T=list(sys.stdin.readline())[:-1]
  lS=len(S)
  lT=len(T)
  ans=[[[] for _ in range(lS)] for _ in range(lT)]
  for iT in range(lT):
    for iS in range(lS):
      if S[iS]==T[iT]:
        if iS-1>=0 and iT-1>=0:
          ans[iT][iS].extend(ans[iT-1][iS-1])
        ans[iT][iS].append(S[iS])
      else:
        if iS-1>=0 and (iT-1<0 or len(ans[iT][iS-1])>=len(ans[iT-1][iS])):
          ans[iT][iS].extend(ans[iT][iS-1])
        elif iT-1>=0 and (iS-1<0 or len(ans[iT-1][iS])>=len(ans[iT][iS-1])):
          ans[iT][iS].extend(ans[iT-1][iS])
  print(*ans[lT-1][lS-1],sep='')

if __name__ == '__main__':
  main()

### FIXED CODE ###

import sys
def main():
  S=list(sys.stdin.readline())[:-1]
  T=list(sys.stdin.readline())[:-1]
  lS=len(S)
  lT=len(T)
  dp=[[0]*(lS+1) for _ in range(lT+1)]
  for iT in range(lT):
    for iS in range(lS):
      if S[iS]==T[iT]:
        dp[iT+1][iS+1] = max(dp[iT+1][iS],dp[iT][iS+1],dp[iT][iS]+1)
      else:
        dp[iT+1][iS+1] = max(dp[iT+1][iS],dp[iT][iS+1])

  iS = lS
  iT = lT
  ans = ""
  while(iS>0 and iT>0):
    if dp[iT][iS]==dp[iT-1][iS-1]:
      iS -= 1
      iT -= 1
    elif dp[iT][iS]==dp[iT][iS-1]:
      iS -= 1
    elif dp[iT][iS]==dp[iT-1][iS]:
      iT -= 1
    else:
      ans = S[iS-1]+ans
      iS -= 1
      iT -= 1
  print(ans)

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

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

lim=60
to=[[0]*n for i in range(lim)]

for i in range(n):
  aft=a[i]
  aft-=1
  to[0][i]=aft

for i in range(lim-1):
  for j in range(n):
    to[i+1][j]=to[i][to[i][j]]

v=0
for i in range(lim,-1,-1):
  l=2**i
  if l<=k:
    v=to[i][v]
    k-=l

print(v+1)

### FIXED CODE ###

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

start=1
list=[1]
kenti=[0 for i in range(n)]
kenti[0]=1

zan=-1

for i in range(k):
  start-=1
  next=a[start]
  start=next

  if kenti[start-1]!=0:
    zan=k-(i+1)
    inx=list.index(start)
    list=list[inx:]
    break
  else:
    list.append(start)
    kenti[start-1]+=1

if zan==-1:
  print(list[-1])
  exit()
elif zan==0:
  print(list[0])
  exit()

#zan-=1
#print(list,zan)
zan%=len(list)
#print(zan)
'''
if zan==0:
  print(list[-1])
else:
  print(list[zan])
'''
print(list[zan])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

x,y,z,k = map(int,input().split())
lisA = list(map(int,input().split()))
lisB = list(map(int,input().split()))
lisC = list(map(int,input().split()))
lisAns = []
lisAns2 = []

for i in range(x):
  for j in range(y):
    lisAns.append(lisA[i]+lisB[j])

for i in range(len(lisAns)):
  for j in range(z):
    lisAns2.append(lisAns[i]+lisC[j])

lisAns2.sort(reverse = True)
for i in range(k):
  print(lisAns2[i])

### FIXED CODE ###

import sys
input = sys.stdin.readline

x,y,z,k = map(int,input().split())
lisa = sorted(map(int,input().split()))
lisb = sorted(map(int,input().split()))
lisc = sorted(map(int,input().split()))

ab = sorted(a+b for a in lisa for b in lisb)[:-k-1:-1]
abc = sorted(ab+c for ab in ab[:k] for c in lisc)[:-k-1:-1]

print(*abc, sep="\n")
### BUGGY CODE ###

S = input()
Q = int(input())
count = 0
for q in range(Q):
    Query = input().split()
    if Query == ['1']:
        count += 1
    else:
        if count % 2 == 0:
            if Query[1] == "1":
                S = Query[2] + S
            else:
                S = S + Query[2]
        else:
            if Query[1] == "1":
                S = S + Query[2]
            else:
                S = Query[2] + S
if count % 2 == 1:
    print(S[::-1])
else:
    print(S)


### FIXED CODE ###

from collections import deque

S = deque(list(input()))
Q = int(input())
count = 0
for q in range(Q):
    Query = input().split()
    if Query == ['1']:
        count += 1
    else:
        if count % 2 == 0:
            if Query[1] == "1":
                S.appendleft(Query[2])
            else:
                S.append(Query[2])
        else:
            if Query[1] == "1":
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
if count % 2 == 1:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))
### BUGGY CODE ###

import numpy as np 

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

ans = np.zeros(n)

for i in range(1, n):
    j = 0 if i-k<0 else i-k
    min_cost = float('inf')
    for l in range(j, i):
        min_cost = min(min_cost, (abs(h[i] - h[l]) + ans[l]))
    
    ans[i] = min_cost

print(int(ans[-1]))

### FIXED CODE ###


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

ans = [0]

for i in range(1, n):
    j = 0 if i-k<0 else i-k
    min_cost = float('inf')
    for l in range(j, i):
        min_cost = min(min_cost, (abs(h[i] - h[l]) + ans[l]))
    
    ans.append(min_cost)

print(int(ans[-1]))
### BUGGY CODE ###

N = int(input())

dic = {}
for n in range(1,N+1):
  dic[n] = int(input())
  
lis = []
now = 1
count = 0
flag = False
while True:
  #count += 1
  if now in lis:
    break
  if dic[now] == 2:
    flag = True
    break
  lis.append(now)
  now = dic[now]

if flag:
  print(len(lis)+1)
else:
  print(-1)

### FIXED CODE ###

N = int(input())

dic = {}
for n in range(1,N+1):
  dic[n] = int(input())

now = 1
count = 0
flag = False
while True:
  if now==2:
    flag = True
    break
  if now not in dic:
    break
  now = dic.pop(now)
  #del dic[now]
  count += 1

if flag:
  print(count)
else:
  print(-1)
### BUGGY CODE ###


import collections
from copy import copy


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


def f(n):
    facs = factorization(n)
    res = set()

    def g(arr, total):
        if len(arr) == 0:
            res.add(total)
            return

        a, b = arr.pop()
        for i in range(0, b + 1):
            num = a ** i
            res.add(num)
            g(copy(arr), total * num)

    g(facs, 1)

    return res


def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    D = collections.defaultdict(int)
    for a in A:
        D[a] += 1

    ans = n
    for i in range(n):
        a = A[i]

        if D[a] > 1:
            ans -= 1
            continue

        if a == 1:
            continue

        res = f(a)
        res.remove(a)
        for num in res:
            if num in D:
                ans -= 1
                break

    print(ans)


main()


### FIXED CODE ###


import collections

def main():
    n = int(input())
    A = sorted(map(int, input().split()))

    D = collections.defaultdict(int)
    for a in A:
        D[a] += 1

    m = max(A)
    S = set()

    ans = 0
    for i in range(n):
        a = A[i]

        if a in S:
            continue

        if D[a] == 1:
            ans += 1

        num = a
        while num <= m:
            S.add(num)
            num += a

    print(ans)


main()
### BUGGY CODE ###

n,k,q = map(int,input().split())
a = list (int(input()) for hogehoge in range(q))
for i in range(1,n+1):
         if a.count(i) >= (q-k+1):
                  print("Yes")
         else:
                  print("No")


### FIXED CODE ###

from collections import Counter
n,k,q = map(int,input().split())
a = list (int(input()) for hogehoge in range(q))
acount = Counter(a)
for i in range(1,n+1):
         if acount[i] >= (q-k+1):
                  print("Yes")
         else:
                  print("No")
### BUGGY CODE ###

# import math
import fractions
from functools import reduce

N = int(input())
A = list(map(int, input().split()))
ans = 1
for i in range(N):
    ans = max(ans, reduce(fractions.gcd, [A[j] for j in range(N) if i!=j]))
print(ans)


### FIXED CODE ###

import math
import fractions
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
from_left = [0 for i in range(N)]
from_right = [0 for i in range(N)]
from_left[0] = A[0]
from_right[-1] = A[-1]
for i in range(1, N):
    from_left[i] = fractions.gcd(A[i], from_left[i-1])
for i in range(N-2, -1, -1):
    from_right[i] = fractions.gcd(A[i], from_right[i+1])
# print(from_left)
# print(from_right)
ans = max(from_left[-2], from_right[1])
for i in range(1, N-1):
    ans = max(ans, fractions.gcd(from_left[i-1], from_right[i+1]))
print(ans)
### BUGGY CODE ###

N = int(input())
A = []
for _ in range(N):
  A.append(int(input())-1)
#[int(input())-1]
 
count = [-1]*N
n = 0
ans = 1
for k in range(N+1):
  if A[n] == 2-1:
    break
 
  elif A[n] in count:
    ans = -1
    break
  
  else:
    count[n] = A[n]
    ans += 1
    n = A[n]
 
print(ans)

### FIXED CODE ###

n = int(input())
al = []
for i in range(n):
    a = int(input())
    al.append(a)
 
al.insert(0, 0)
ans = -1
push = 1
for i in range(1, n+2):
    light = al[push]
    push = light
    if light == 2:
        ans = i
        break
print(ans)
### BUGGY CODE ###

from collections import deque

n = int(input())
x = eval('0b' + input())

memo = {0: 0}

def solve(X):
    x = X
    stack = deque([])
    while x:
        if x in memo:
            break
        stack.append(x)
        x = x % bin(x).count("1")
    cnt = memo[x] + 1
    while stack:
        x = stack.pop()
        memo[x] = cnt
        cnt += 1
    return cnt - 1



ans = []

for i in range(n):
    y = 0
    if x & (1 << i):
        y = x - 2 ** i
    else:
        y= x + 2 ** i
    ans.append(solve(y))

for ansi in reversed(ans):
    print(ansi)


    

        


### FIXED CODE ###

from collections import deque

n = int(input())
x = input()

memo = {0: 0}

def solve(X):
    x = X
    stack = deque([])
    while x:
        if x in memo:
            break
        stack.append(x)
        x = x % bin(x).count("1")
    cnt = memo[x] + 1
    while stack:
        x = stack.pop()
        memo[x] = cnt
        cnt += 1
    return cnt - 1



ans = []
px = x.count("1")

memo1 = [1 % (px - 1)] if px > 1 else []
memo2 = [1 % (px + 1)]
x1 = 0
x2 = 0

for i in range(n):
    if px > 1:
        memo1.append(memo1[-1] * 2 % (px-1))
    memo2.append(memo2[-1] * 2 % (px+1))

for i in range(n):
    if x[-i-1] == '1':
        if px > 1:
            x1 += memo1[i]
            x1 %= (px-1)
        x2 += memo2[i]
        x1 %= (px+1)

for i in range(n):
    if x[-i-1] == "1":
        if px > 1:
            ans.append(solve((x1-memo1[i])%(px-1)) + 1)
        else:
            ans.append(0)
    else:
        ans.append(solve((x2+memo2[i])%(px+1)) + 1)


for ansi in reversed(ans):
    print(ansi)
### BUGGY CODE ###

import numpy as np


def main():
    n = int(input())
    As = np.array(list(map(int, input().split())))

    for i in range(n):
        print(np.sum(As == i+1))


if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    n = int(input())
    As = list(map(int, input().split()))

    anss = [0]*n
    for A in As:
        anss[A-1] += 1
    for ans in anss:
        print(ans)


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

n,m = map(int,input().split())
dp = [float("inf")]*(1<<n)
dp[0]=0
kagi = []
for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    kagi.append([a,b,c])
for j in range(m):
    for i in range(1<<n):
        if dp[i] == -1:
            continue
        tmp = i+0
        for k in kagi[j][2]:
            tmp = tmp | 1<<(k-1)
        dp[tmp] = min(dp[tmp],dp[i]+kagi[j][0])
print(dp[(1<<n)-1] if dp[(1<<n)-1] != float("inf") else -1)

### FIXED CODE ###

n,m = map(int,input().split())
dp = [float("inf")]*(1<<n)
dp[0]=0
kagi = []
for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    kagi.append([a,b,c])
for j in range(m):
    for i in range(1<<n):
        tmp = i+0
        for k in kagi[j][2]:
            tmp = tmp | 1<<(k-1)
        dp[tmp] = min(dp[tmp],dp[i]+kagi[j][0])
print(dp[(1<<n)-1] if dp[(1<<n)-1] != float("inf") else -1)
### BUGGY CODE ###

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

for i in range(m):
    a.append(a[0]/2)
    del a[0]
    a.sort(reverse = True)
    
ans = 0
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()))
a = [-i for i in a]
heapify(a)

for i in range(m):
    x = heappop(a)
    heappush(a, int(x/2))

ans = 0
for i in range(n):
    ans += -a[i]
print(ans)
### BUGGY CODE ###

h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _i in range(w)] for _j in range(h)]

hw = [list(map(int, input().split())) for _i in range(m)]

for s, t in hw:
    s -= 1
    t -= 1
    bord[s][t] += 1
    bord_x[s] += 1
    bord_y[t] += 1

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

r = x_max + y_max - 1
for i in a:
    for j in b:
        if bord[i][j] >= 1:
            continue
        else:
            r += 1
            import sys
            print(r)
            sys.exit()
print(r)

### FIXED CODE ###

h, w, m = map(int, input().split())
bord_x, bord_y = [0]*h, [0]*w
 
hw = []
for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord_x[s] += 1
    bord_y[t] += 1
    hw.append([s, t])
 
x_max, y_max = max(bord_x), max(bord_y)
 
cnt = 0
for i, j in hw:
    if bord_x[i] == x_max and bord_y[j] == y_max:
        cnt += 1
 
if cnt == bord_x.count(x_max)*bord_y.count(y_max):
    x_max -= 1
 
print(x_max+y_max)
### BUGGY CODE ###

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

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

print(s)

### FIXED CODE ###

from collections import deque

s = deque(input())
q = int(input())
first = 1

for i in range(q):
    t = input().split()
    if int(t[0]) == 1:
        first *= -1
    else:
        f = int(t[1])
        c = t[2]
        
        if first == 1:
            if f == 1:
                s.appendleft(c)
            elif f == 2:
                s.append(c)
        else:
            if f == 1:
                s.append(c)
            elif f == 2:
                s.appendleft(c)

S = "".join(s)
if first == 1:
    print(S)
else:
    print(S[::-1])