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

h, w, m = map(int,input().split())
maze = [[0 for _ in range(w)] for _ in range(h)]
for i in range(m):
    hh, ww = map(lambda x:int(x)-1,input().split())
    maze[hh][ww] = 1

#print(maze)

#yoko
row = [0]*h
for r in range(h):
    row[r] = sum(maze[r])
#tate
col = [0]*w
for c in range(w):
    tmp = 0
    for r in range(h):
        tmp += maze[r][c]
    col[c] = tmp

rmax = max(row)
cmax = max(col)

rind = [n for n, v in enumerate(row) if v == rmax]
cind = [n for n, v in enumerate(col) if v == cmax]

#print(rind)
#print(cind)
ans = 0

for r in rind:
    for c in cind:
        if maze[r][c] == 1:
            ans = rmax+cmax-1
        else:
            ans = rmax+cmax
            print(ans)
            exit()
print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

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

row = [0]*h
col = [0]*w
enemy = []

for i in range(m):
    hh, ww = map(lambda x:int(x)-1,input().split())
    row[hh] += 1
    col[ww] += 1
    enemy.append((hh,ww))

enemy = set(enemy)
rmax = max(row)
cmax = max(col)

on_r = row.count(rmax)
on_c = col.count(cmax)

on = 0
for hh, ww in enemy:
    if row[hh] == rmax and col[ww] == cmax:
        on += 1
if on == on_r*on_c:
    print(rmax+cmax-1)
else:
    print(rmax+cmax)
### BUGGY CODE ###

import heapq
import sys
sys.setrecursionlimit(100000)
N=int(input())
xy=[list(map(int,input().split()))+[i] for i in range(N)]
xy.sort()
path = []

def get_par(n):
    if par[n] is None:return n
    return get_par(par[n])

def same(i,j):
    return get_par(i) == get_par(j)

def unite(i,j):
    j = get_par(j)
    par[j] = get_par(i)
    return

for k in range(N-1):
    x,y,i = xy[k]
    x2,y2,j = xy[k+1]
    if i>j:i,j = j,i
    heapq.heappush(path,(abs(x-x2),i,j))
xy.sort(key=lambda x:x[1])
for k in range(N-1):
    x,y,i = xy[k]
    x2,y2,j = xy[k+1]
    if i>j:i,j = j,i
    heapq.heappush(path,(abs(y-y2),i,j))
par = [None]*N
cnt = 0
ans = 0
while cnt < N-1:
    c,i,j = heapq.heappop(path)
    if same(i,j):continue
    unite(i,j)
    ans += c
    cnt += 1
print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)
N=int(input())
xy=[list(map(int,input().split()))+[i] for i in range(N)]
xy.sort()
path = []

def get_par(n):
    if par[n] is None:return n
    par[n] = get_par(par[n])
    return par[n]

def same(i,j):
    return get_par(i) == get_par(j)

def unite(i,j):
    j = get_par(j)
    par[j] = get_par(i)
    return

for k in range(N-1):
    x,y,i = xy[k]
    x2,y2,j = xy[k+1]
    path.append((abs(x-x2),i,j))
xy.sort(key=lambda x:x[1])
for k in range(N-1):
    x,y,i = xy[k]
    x2,y2,j = xy[k+1]
    path.append((abs(y-y2),i,j))
path.sort(key = lambda x:x[0],reverse=True)
par = [None]*N
cnt = 0
ans = 0
while cnt < N-1:
    c,i,j = path.pop()
    if same(i,j):continue
    unite(i,j)
    ans += c
    cnt += 1
print(ans)
### BUGGY CODE ###

n = input()
 
print '',
for i in xrange(1, n+1):
    x = i
    if x % 3 == 0:
        print i,
    else:
        while x:
            if x % 10 == 3:
                print i,
                break

### FIXED CODE ###

n = input()
 
print '',
for i in xrange(1, n+1):
    x = i
    if x % 3 == 0:
        print i,
    else:
        while x:
            if x % 10 == 3:
                print i,
                break
            x /= 10
### BUGGY CODE ###

x = int(input())

def f(x,b):
    return int((x+b**5)**(1/5))

b=0
plus=1
while True:
    ans = f(x,b)
    #print(ans)
    if ans**5-b**5 == x:
        print(ans, b)
        break
    elif plus == 1:
        b=b*(-1)
        plus = 0
    elif plus == 0:
        b=b*(-1)
        b+=1

### FIXED CODE ###

def solve():
   x = int(input())
   for a in range(-500,500):
      for b in range(-500,500):
         n = a**5 - b**5
         if n == x:
            print(a,end=" ")
            print(b)
            return
solve()
### BUGGY CODE ###

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

def f(num, cnt):
    if num == 0:
        return cnt
    popcount = format(num, 'b').count('1')
    return f(num % popcount, cnt+1)

for i in range(n):
    b = ''.join([str(int(not(int(v)))) if j == i else v for j, v in enumerate(x)])
    print(f(int(b, 2), 0))

### FIXED CODE ###

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

count = x.count('1')
one_count = count - 1
zero_count = count + 1

one_mod = 0
zero_mod = 0
for b in x:
    if one_count:
        one_mod = (one_mod * 2 + int(b)) % one_count
    zero_mod = (zero_mod * 2 + int(b)) % zero_count

f = [0] * 2000001
pop_count = [0] * 200001
for i in range(1, 200001):
    pop_count[i] = pop_count[i//2] + i % 2
    f[i] = f[i % pop_count[i]] + 1

for i in range(n):
    if x[i] == '1':
        if one_count:
            nxt = one_mod
            nxt -= pow(2, n-i-1, one_count)
            nxt %= one_count
            print(f[nxt] + 1)
        else:
            print(0)
    if x[i] == '0':
        nxt = zero_mod
        nxt += pow(2, n-i-1, zero_count)
        nxt %= zero_count
        print(f[nxt] + 1)
### BUGGY CODE ###

r,g,b,n=map(int,input().split())
ans=0
for i in range(n//r+1):
  for j in range(n//g+1):
    if (n-i*r-j*g)%b==0 and (n-i*r-j*g)//b>=0:
      ans+=1
print(ans)


### FIXED CODE ###

r,g,b,n=map(int,input().split())
ans=0
for i in range(n//r+1):
  for j in range(n//g+1):
    if (n-i*r-j*g)%b==0 and (n-i*r-j*g)//b>=0:
      ans+=1
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, K: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)

    def dfs(idx, cur, tmp, visited):
        visited[idx] = True
        #print(idx, cur, tmp)
        tmp[idx] = max(K - cur, 0)
        cur = min(cur + 1, 2)
        for nex in conn[idx]:
            if not visited[nex]:
                tmp = dfs(nex, cur, tmp, visited)
                cur += 1
        return tmp

    tmp = [0] * N
    visited = [False] * N
    tmp = dfs(0, 0, tmp, visited)
    #print(tmp)

    ret = 1
    for t in tmp:
        ret *= t
        ret %= MOD

    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, K, a, b)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, K: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)

    def dfs(idx, cur, tmp, visited, ret):
        visited[idx] = True
        #print(idx, cur, tmp)

        #tmp[idx] = max(K - cur, 0)
        ret *= max(K - cur, 0)
        ret %= MOD

        cur = min(cur + 1, 2)
        for nex in conn[idx]:
            if not visited[nex]:
                ret = dfs(nex, cur, tmp, visited, ret)
                cur += 1
        return ret

    tmp = [0] * N
    visited = [False] * N
    ret = 1
    ret = dfs(0, 0, tmp, visited, ret)
    #print(tmp)

    #for t in tmp:
    #    ret *= t
    #    ret %= MOD

    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, K, a, b)

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

n,k=map(int,input().split())
x,y=[[0,0] for i in range(n)],[[0,0] for i in range(n)]
xy=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    x_sub,y_sub=xy[i]
    x[i][0]=x_sub
    x[i][1]=i
    y[i][0]=y_sub
    y[i][1]=i
x.sort()
y.sort()
w=[(i1,l1) for i1 in range(n) for l1 in range(n-i1,1,-1)]
v=[(i2,l2) for i2 in range(n) for l2 in range(n-i2,1,-1)]
#print(w)
#print(v)
ans=(10**18)*5
for w_sub in w:
    for v_sub in v:
        i1,l1=w_sub
        i2,l2=v_sub
        if l1*l2<k:
            continue
        x_sub1=x[i1][0]
        x_sub2=x[i1+l1-1][0]
        y_sub1=y[i2][0]
        y_sub2=y[i2+l2-1][0]
        z=[0]*n
        for i in range(n):
            if x_sub1<=xy[i][0]<=x_sub2 and y_sub1<=xy[i][1]<=y_sub2:
                z[i]=1
        if sum(z)>=k:
            ans=min((x_sub2-x_sub1)*(y_sub2-y_sub1),ans)
print(ans)


### FIXED CODE ###

n,k=map(int,input().split())
x,y=[[0,0] for i in range(n)],[[0,0] for i in range(n)]
xy=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    x_sub,y_sub=xy[i]
    x[i][0]=x_sub
    x[i][1]=i
    y[i][0]=y_sub
    y[i][1]=i
x.sort()
y.sort()
ans=(10**18)*5
for i1 in range(n):
    for l1 in range(n-i1,1,-1):
        for i2 in range(n):
            for l2 in range(n-i2,1,-1):
                x_sub1=x[i1][0]
                x_sub2=x[i1+l1-1][0]
                y_sub1=y[i2][0]
                y_sub2=y[i2+l2-1][0]
                z=[0]*n
                for i in range(n):
                    if x_sub1<=xy[i][0]<=x_sub2 and y_sub1<=xy[i][1]<=y_sub2:
                        z[i]=1
                if sum(z)>=k:
                    ans=min((x_sub2-x_sub1)*(y_sub2-y_sub1),ans)
                else:
                    break
print(ans)
### BUGGY CODE ###

from bisect import insort_left

X, Y, A, B, C = [int(i) for i in input().split()]
P = sorted([int(i) for i in input().split()])[-X:]
Q = sorted([int(i) for i in input().split()])[-Y:]
R = sorted([int(i) for i in input().split()])[::-1]
i = 0
j = 0
k = 0
tmp = P if P[0] < Q[0] else Q
while(True):
    try:
        if P[j] > Q[k]:
            if Q[k] < R[i]:
                insort_left(Q, R[i])
                k += 1
                i += 1
                continue
        else:
            if P[j] < R[i]:
                insort_left(P, R[i])
                j += 1
                i += 1
                continue
        break
    except IndexError:
        break
print(sum(P[j:]) + sum(Q[k:]))

### FIXED CODE ###

import heapq
 
X, Y, A, B, C = [int(i) for i in input().split()]
P = sorted([int(i) for i in input().split()])[-X:]
Q = sorted([int(i) for i in input().split()])[-Y:]
R = sorted([int(i) for i in input().split()])[::-1]
heapq.heapify(P)
heapq.heapify(Q)
i = 0
while(True):
    if P[0] >= R[i] and Q[0] >= R[i]:
        break
    if P[0] > Q[0]:
        if Q[0] < R[i]:
            heapq.heappop(Q)
            heapq.heappush(Q, R[i])
    else:
        if P[0] < R[i]:
            heapq.heappop(P)
            heapq.heappush(P, R[i])
    i += 1
    if i == len(R):
        break
print(sum(P) + sum(Q))
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
lst=[]
for i in range(1,N+1):
  index=A.index(i)+1
  lst.append(str(index))
print(' '.join(lst))

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
lst=[0]*N
for i in range(N):
  lst[A[i]-1]=str(i+1)
print(' '.join(lst))
### BUGGY CODE ###

# 6
# 三井住友信託銀行プログラミングコンテスト 2019 D - Lucky PIN

import itertools
from collections import deque

n = int(input())
s = input()

ans = 0
iter = itertools.product("0123456789", repeat=3)

for it in iter:
    _s_dq = deque(s)
    for i in it:
        while len(_s_dq) > 0:
            _s = _s_dq.popleft()
            if _s == i:
                break
        else:
            break
    else:
        ans += 1
        
print(ans)


### FIXED CODE ###

# 6
# 三井住友信託銀行プログラミングコンテスト 2019 D - Lucky PIN

import itertools
import copy
from collections import deque

n = int(input())
s = input()

ans = 0
iter = itertools.product("0123456789", repeat=3)

for it in iter:
    _s = copy.deepcopy(s)
    for i in it:
        try:
            _idx = _s.index(i)
            _s = _s[_idx+1:]
        except:
            break
    else:
        ans += 1
    
print(ans)
### BUGGY CODE ###

n= int(input())
li=list(map(int,input().split()))
ans=[]
for i in range(n):
  print(li.count(i+1))

### FIXED CODE ###

import collections

n= int(input())
li=list(map(int,input().split()))
c=collections.Counter(li)

for i in range(1,n+1):
  print(c[i])
### BUGGY CODE ###

N=int(input())
A1=[int(s) for s in input().split()]
B1=[int(s) for s in input().split()]
A2=[(A1[i],i) for i in range(N)]
B2=[(B1[i],i) for i in range(N)]
A=sorted(A2)
B=sorted(B2)
#print(A)
#print(B)
for i in range(N):
  if B[i][0]<A[i][0]:
    print("No")
    break
else:
  for i in range(N-1):
    if B[i][0]>=A[i+1][0]:
      print("Yes")
      break
  else:
    C=[e[1] for e in A]
    a=C[0]
  #  print(a)
    k=B[0][1]
    for i in range(N-2):
      k=B[C.index(k)][1]
   #   print(k)
      if k==a :
        print("Yes")
        break
    else:
      print("No")
        


### FIXED CODE ###

N=int(input())
A1=[int(s) for s in input().split()]
B1=[int(s) for s in input().split()]
A2=[(A1[i],i) for i in range(N)]
B2=[(B1[i],i) for i in range(N)]
A=sorted(A2)
B=sorted(B2)
#print(A)
#print(B)
for i in range(N):
  if B[i][0]<A[i][0]:
    print("No")
    break
else:
  for i in range(N-1):
    if B[i][0]>=A[i+1][0]:
      print("Yes")
      break
  else:
    C=[(A[i][1],B[i][1]) for i in range(N)]
    C.sort()
#    print(C)
    k=0
    for i in range(N-1):
      k=C[k][1]
      if k==0:
        print("Yes")
        break
    else:
      print("No")
### BUGGY CODE ###

n,k=map(int,input().split())
L=[[] for i in range(n+1)]

for i in range(n-1):
    a,b=map(int,input().split())
    L[a].append(b)
    L[b].append(a)
    
C=[k for i in range(n+1)]
V=[0 for i in range(n+1)]
mod=1_000_000_007
ans=1
que=[1]
V[1]=1
head=0

while len(que)>head:
    now=que[head]
    head+=1
    ans=ans*C[now]%mod
    for nex in L[now]:
        if V[nex]==0:
            V[nex]=1
            que.append(nex)
        for nexnex in L[nex]:
            C[nexnex]-=1
        C[nex]-=1

print(ans)


### FIXED CODE ###

n,k=map(int,input().split())
L=[[] for i in range(n+1)]

for i in range(n-1):
    a,b=map(int,input().split())
    L[a].append(b)
    L[b].append(a)
    
C=[k for i in range(n+1)]
V=[0 for i in range(n+1)]
mod=1_000_000_007
ans=1
que=[(1,0)]
V[1]=1
head=0

cnt=1
while len(que)>head:
    now=que[head]
    head+=1
    ans=ans*(k-now[1])%mod
    for i in range(len(L[now[0]])):
        nex=L[now[0]][i]
        if V[nex]==0:
            V[nex]=1
            que.append((nex,cnt))
            cnt+=1
    cnt=2

#ans=ans*pow(k-1,mod-2,mod)*k%mod
print(ans)
### BUGGY CODE ###

import numpy as np

H, W = map(int, input().split())
field = []
for i in range(H):
    field.append(list(map(int, input().split())))

rd = [(0,1,0), (1,0,1)]

move_records = []

if H != 1 or W != 1:
    for i in range(H):
        for j in range(W):
            if i == H-1 and j == W-1:
                continue
            if field[i][j] % 2 == 0:
                continue
            
            adj_values = np.array([-1, -1])
            if not j == W-1:
                adj_values[0] = field[i][j+1]
            if not i == H-1:
                adj_values[1] = field[i+1][j]

            if sum(adj_values[adj_values != -1] % 2) == 0:
                h,w,k = rd[np.where(adj_values != -1)[0][0]]
                field[i][j] -= 1
                field[i+h][j+w] += 1
                
            else:
                for h,w,k in rd:
                    if adj_values[k] == -1:
                        continue

                    if adj_values[k] % 2 != 0:
                        field[i][j] -= 1
                        field[i+h][j+w] += 1
                        break
            
            move_records.append([i+1,j+1,i+h+1,j+w+1])

M = len(move_records)
print(M)
for i in range(M):
    print('{} {} {} {}'.format(move_records[i][0],
                               move_records[i][1],
                               move_records[i][2],
                               move_records[i][3]))

### FIXED CODE ###

import numpy as np
H, W = map(int, input().split())
field = []
for i in range(H):
    field.append(list(map(int, input().split())))
rd = [(0,1,0), (1,0,1)]
move_records = []
if H != 1 or W != 1:
    for i in range(H):
        for j in range(W):
            if field[i][j] % 2 == 0:
                continue
            if i == H-1 and j == W-1:
                continue
            if i == H-1 and j == W-2:
                # import ipdb; ipdb.set_trace()
                if (field[H-1][W-1] + field[H-1][W-2]) % 2 != 0:
                    continue
                else:
                    field[H-1][W-2] -= 1
                    field[H-1][W-1] += 1
                    move_records.append([i+1,j+1,i+1,j+2])
            elif j != W-1:
                field[i][j] -= 1
                field[i][j+1] += 1
                move_records.append([i+1,j+1,i+1,j+2])
            else:
                field[i][j] -= 1
                field[i+1][j] += 1
                move_records.append([i+1,j+1,i+2,j+1])
M = len(move_records)
print(M)
for i in range(M):
    print('{} {} {} {}'.format(move_records[i][0],
                               move_records[i][1],
                               move_records[i][2],
                               move_records[i][3]))
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
dp2 = [0] * (t + 1)
for i in range(t, -1, -1):
  for j in range(3):
    y = a[j]
    x = b[j]
    if i - x >= 0:
      dp2[i - x] = max(dp2[i - x], dp2[i] + y)
for i in range(t + 1): dp2[i] += i
print(max(dp2))

### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d1 = []
d2 = []
for i in range(3):
  if a[i] < b[i]: d1.append((a[i], b[i]))
  if a[i] > b[i]: d2.append((b[i], a[i]))

dp = [0] * (N + 1)

for i in range(N, -1, -1):
  for t in d1:
    x = t[0]
    y = t[1]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], y + dp[i])
for i in range(N + 1): dp[i] += i

def calc(x, r1):
  z = r1 - d2[0][0] * x
  return (z // d2[1][0]) * d2[1][1] + z % d2[1][0] + d2[0][1] * x

res1 = max(dp)
if len(d2) == 0:
  print(res1)
if len(d2) == 1:
  x, y = d2.pop()
  print((res1 // x) * y + (res1 % x))
if len(d2) == 2:
  x0 = 0
  x3 = res1 // d2[0][0]
  res2 = res1 + 0
  for x in range(x0, x3 + 1):
    res2 = max(res2, calc(x, res1))
  print(res2)

if len(d2) == 3:
  dp2 = [0] * (res1 + 1)
  for i in range(res1, -1, -1):
    for t in d2:
      x = t[0]
      y = t[1]
      if i - x >= 0:
        dp2[i - x] = max(dp2[i - x], y + dp2[i])
  for i in range(res1 + 1): dp2[i] += i
  print(max(dp2))
### BUGGY CODE ###

import collections
import math

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

    Cou = collections.Counter(A)
    ans = 0

    oo = 0
    for c in Cou.values():
        oo += c * (c-1) // 2
    #print(Cou)
    for k in range(1, N+1):
        tmp = Cou.copy()
        c = tmp[A[k-1]] - 1

        print(oo - c)



    return 0


if __name__ == '__main__':
    main()

### FIXED CODE ###

import collections
import math

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

    Cou = collections.Counter(A)
    ans = 0

    oo = 0
    for c in Cou.values():
        oo += c * (c-1) // 2
    #print(Cou)
    for k in range(1, N+1):
        c = Cou[A[k-1]] - 1
        print(oo - c)
    return 0


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

from collections import Counter


def solve():
    H, W, M = map(int, input().split())
    dh, dw = Counter(), Counter()
    used = set()

    for _ in range(M):
        h, w = map(int, input().split())
        dh[h] += 1
        dw[w] += 1
        used.add((h, w))

    ih = dh.most_common()
    iw = dw.most_common()

    h, counth = ih[0]
    w, countw = iw[0]
    ans = counth + countw - ((h, w) in used)

    for h, counth in ih:
        for w, countw in iw:
            b = counth + countw - ((h, w) in used)
            if b<ans:
                break
            ans = b
    print(ans)


solve()

### FIXED CODE ###

from collections import Counter


def solve():
    H, W, M = map(int, input().split())
    dh, dw = Counter(), Counter()
    used = set()

    for _ in range(M):
        h, w = map(int, input().split())
        dh[h] += 1
        dw[w] += 1
        used.add((h, w))

    ih = dh.most_common()
    iw = dw.most_common()

    h, counth = ih[0]
    w, countw = iw[0]

    s = counth + countw
    ans = counth + countw - ((h, w) in used)

    for h, counth in ih:
        for w, countw in iw:
            if counth+countw < s or ans == s:
                break
            b = counth + countw - ((h, w) in used)
            ans = max(ans, b)
    print(ans)


solve()
### BUGGY CODE ###

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

AB = []
for a in A:
  for b in B:
    AB.append(a + b)

AB.sort(reverse=True)
AB = AB[:min(3001, X * Y * Z + 1)]
ABC = []
for ab in AB:
  for c in C:
    ABC.append(ab + c)
ABC.sort(reverse=True)
for i in range(K):
  print(ABC[i])


### FIXED CODE ###

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

AB = []
for a in A:
  for b in B:
    AB.append(a + b)

AB.sort(reverse=True)
AB = AB[:min(3001, X * Y * Z + 1)]
ABC = []
for c in C:
  for ab in AB:
    ABC.append(ab + c)
ABC.sort(reverse=True)
for i in range(K):
  print(ABC[i])
### BUGGY CODE ###

H, W, D = map(int, input().split())
Board = [0] * (H*W+1)
for h in range(H):
    A = list(map(int, input().split()))
    for w in range(W):
        Board[A[w]] = [w+1, h+1]

ansdict = dict()
for i in range(int(input())):
    L, R = map(int, input().split())
    if L*100000 + R in ansdict.keys():
        print(ansdict[L*100000 + R])
    else:
        ans = 0
        for j in range(L+D, R+1, D):
            ans += abs(Board[j][0] - Board[j-D][0]) + abs(Board[j][1] - Board[j-D][1])
        ansdict[L*100000 + R] = ans
        print(ans)

### FIXED CODE ###

H, W, D = map(int, input().split())
Board = [0] * (H*W+1)
path = [[0] for _ in range(D)] 
for h in range(H):
    A = list(map(int, input().split()))
    for w in range(W):
        Board[A[w]] = [w+1, h+1]
for i in range(1, D+1):
    d = i % D
    for j in range(i+D, H*W+1, D):
        path[d].append(path[d][(j+(D-1))//D-2] + abs(Board[j][0] - Board[j-D][0]) + abs(Board[j][1] - Board[j-D][1]))

for i in range(int(input())):
    L, R = map(int, input().split())
    d = L % D
    print(path[d][(R+(D-1))//D-1] - path[d][(L+(D-1))//D-1])