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

import copy
H,W=map(int, input().split())
Ch,Cw=map(int, input().split())
Dh,Dw=map(int, input().split())
S=[list(str(input())) for i in range(H)]

field2=copy.deepcopy(S)
move=[[1,0],[-1,0],[0,1],[0,-1]]
ans=0
temp=[]

def dfs2(x,y):
    field2[x][y]='#'
    temp.append([x,y])
    for i in range(4):
        nx=x+move[i][0]
        ny=y+move[i][1]
        if 0<=nx and nx<H and 0<=ny and ny<W and field2[nx][ny]=='.':
            dfs2(nx,ny)
    return temp

start=dfs2(Ch-1,Cw-1)
while True:
    if field2[Dh-1][Dw-1]=='#':
        print(ans)
        exit()
    else:
        for i in range(len(start)):
            for hh in range(-2,3):
                for ww in range(-2,3):
                    nnx=start[0][0]+hh
                    nny=start[0][1]+ww
                    if 0<=nnx and nnx<H and 0<=nny and nny<W and field2[nnx][nny]=='.':
                        dfs2(nnx,nny)
            del(start[0])
        ans+=1
    if len(start)==0:
        print(-1)
        exit()


### FIXED CODE ###

from collections import deque
import copy
H,W=map(int, input().split())
Ch,Cw=map(int, input().split())
Dh,Dw=map(int, input().split())
S=[list(str(input())) for i in range(H)]

field2=copy.deepcopy(S)
move=[[1,0],[-1,0],[0,1],[0,-1]]
ans=0
temp=deque()

def dfs2(x,y):
    field2[x][y]='#'
    temp.append([x,y])
    for i in range(4):
        nx=x+move[i][0]
        ny=y+move[i][1]
        if 0<=nx and nx<H and 0<=ny and ny<W and field2[nx][ny]=='.':
            dfs2(nx,ny)
    return temp

start=dfs2(Ch-1,Cw-1)

while True:
    if field2[Dh-1][Dw-1]=='#':
        print(ans)
        exit()
    else:
        for i in range(len(start)):
            ttemp=start.popleft()
            for hh in range(-2,3):
                for ww in range(-2,3):
                    nnx=ttemp[0]+hh
                    nny=ttemp[1]+ww
                    if 0<=nnx and nnx<H and 0<=nny and nny<W and field2[nnx][nny]=='.':
                        dfs2(nnx,nny)
        ans+=1
    if len(start)==0:
        print(-1)
        exit()
### BUGGY CODE ###

from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

n,m,k = map(int,stdin.readline().rstrip().split())
mod = 998244353
point = 0

def combination(n,r,mod=mod):
    r=min(r,n-r)
    up,down=1,1
    for i in range(r):
        up=up*(n-i)%mod
        down=down*(i+1)%mod
    return up*pow(down,mod-2,mod)%mod

for i in range(k+1):
    now = 1
    now *= combination(n-1,i)%mod
    now *= m*(m-1)**(n-i-1)%mod
    now %= mod
    point += now
    point %= mod

print(point%mod)

### FIXED CODE ###

from sys import stdin
from sys import setrecursionlimit

setrecursionlimit(10 ** 7)

n,m,k = map(int,stdin.readline().rstrip().split())
mod = 998244353
wariate = 1
point = 0

def prepare(n, MOD=mod):
    facts = [1]*(n+1)
    for i in range(1, n+1):
        facts[i] = facts[i-1]*i%MOD
    invs = [1]*(n+1)
    invs[n] = pow(facts[n], MOD-2, MOD)
    for i in range(0, n)[::-1]:
        invs[i] = invs[i+1] * (i+1) % MOD
    return facts, invs
facts, invs = prepare(n, mod)
for i in range(n-1,-1,-1):
    now = m
    if i != n-1:
        wariate *= m-1
        wariate %= mod
    now *= wariate%mod

    now *= facts[n-1]*invs[n-1-i]*invs[i]
    if i <= k:
        point += now%mod

print(point%mod)
### BUGGY CODE ###

#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

def func():
    N = int(input())

    A = list(map(int, input().split()))
    P = [[(A[0], 0), (0, 0)] for i in range(2 ** N)]

    for i in range(1, 2 ** N):
        Ai = A[i]
        Pi = P[i]
        if (Ai, i) > Pi[0]:
            Pi[1] = Pi[0]
            Pi[0] = (Ai, i)
        elif (Ai, i) > Pi[1]:
            Pi[1] = (Ai, i)
        for j in range(N):
            if (i & (1 << j)) == 0:
                Pindex = P[i + (1 << j)]
                for k in range(2):
                    if Pi[k] > Pindex[0]:
                        Pindex[1] = Pindex[0]
                        Pindex[0] = Pi[k]
                    elif Pi[k] > Pindex[1] and Pi[k] != Pindex[0]:
                        Pindex[1] = Pi[k]
    ans = 0
    for i in range(1, 2 ** N):
        ans = max(ans, P[i][0][0] + P[i][1][0])
        print(ans)
    #print(P)

func()

### FIXED CODE ###

#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

def func():
    N = int(input())

    A = list(map(int, input().split()))
    P = [[(A[0], 0), (0, 0)] for i in range(2 ** N)]

    for i in range(1, 2 ** N):
        Ai = A[i]
        Pi = P[i]
        if (Ai, i) > Pi[0]:
            Pi[1] = Pi[0]
            Pi[0] = (Ai, i)
        elif (Ai, i) > Pi[1]:
            Pi[1] = (Ai, i)
        for j in range(N):
            if (i & (1 << j)) == 0:
                Pindex = P[i + (1 << j)]
                for k in range(2):
                    if Pi[k] > Pindex[0]:
                        Pindex[1] = Pindex[0]
                        Pindex[0] = Pi[k]
                    elif Pi[k] > Pindex[1] and Pi[k] != Pindex[0]:
                        Pindex[1] = Pi[k]
    ans = 0
    for i in range(1, 2 ** N):
        ans = max(ans, P[i][0][0] + P[i][1][0])
        print(ans)
    #print(P)

func()
### BUGGY CODE ###

R, C, K = map(int, input().split())
V = [[0] * (C + 1) for _ in range(R + 1)]
for _ in range(K):
    r, c, v = map(int, input().split())
    V[r][c] = v

X = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for i in range(1, R + 1):
    Xi = X[i]
    Xi1 = X[i-1]
    Vi = V[i]
    for j in range(1, C + 1):
        Vij = Vi[j]
        Xij = Xi[j]
        Xij1 = Xi[j-1]
        Xij[0] = max(max(Xi1[j]), Xij1[0])
        Xij[1] = max(Xij[0] + Vij, Xij1[1])
        Xij[2] = max(Xij1[1] + Vij, Xij1[2])
        Xij[3] = max(Xij1[2] + Vij, Xij1[3])

print(max(X[R][C]))

### FIXED CODE ###

R, C, K = map(int, input().split())
V = [[0] * (R + 1) for _ in range(C + 1)]
for _ in range(K):
    r, c, v = map(int, input().split())
    V[c][r] = v

X = [[0] * 4 for _ in range(R + 1)]
for i in range(1, C + 1):
    Vi = V[i]
    for j in range(1, R + 1):
        v = Vi[j]
        Xj = X[j]
        if v:
            Xj[3] = max(Xj[3], Xj[2] + v)
            Xj[2] = max(Xj[2], Xj[1] + v)
            Xj[0] = max(Xj[0], max(X[j-1]))
            Xj[1] = max(Xj[1], Xj[0] + v)
        else:
            Xj[0] = max(Xj[0], max(X[j-1]))

print(max(X[R]))
### BUGGY CODE ###

import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


def eratosthenes(n):
    sieve = [True for _ in range(n + 1)]
    sieve[0] = False
    sieve[1] = False

    def _update(p):
        if sieve[p]:
            for i in range(p * 2, n + 1, p):
                sieve[i] = False

    if n > 1:
        _update(2)
    p = 3
    while p * p <= n:
        _update(p)
        p += 2
    return sieve


def factorize(n, primes):
    import collections
    if n < 2:
        return dict()
    counter = collections.Counter()
    for p in primes:
        while n % p == 0:
            counter[p] += 1
            n //= p
        if n == 1:
            break
    if n != 1:
        counter[n] = 1
    return counter


sieve = eratosthenes(10**3)
primes = [i for i, b in enumerate(sieve) if b]

MOD = 10**9 + 7

n = ri()
ls = ril()
if n == 1:
    print(1)
    exit()

factorizations = []
for x in ls:
    factorizations.append(factorize(x, primes))
lcm = collections.Counter()
for counter in factorizations:
    for p, k in counter.items():
        lcm[p] = max(lcm[p], k)

res = 0
for counter in factorizations:
    s = 1
    for p, k in lcm.items():
        s *= p**(k - counter[p])
    res += s
    res %= MOD
print(res)

### FIXED CODE ###

import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


def gcd(a, b):
    while True:
        a, b = b, a % b
        if b == 0:
            break
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


MOD = 10**9 + 7

n = ri()
ls = ril()
if n == 1:
    print(1)
    exit()

x = ls[0]
for i in range(1, n):
    x = lcm(x, ls[i])
ds = [x // y for y in ls]
print(sum(ds) % MOD)
### BUGGY CODE ###

n = int(input())
s = input()
cnt = 0

for i in range(n):
    for j in range(i, n):
        for k in range(j, n):
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                if j-i != k-j:
                    cnt +=1

print(cnt)

### FIXED CODE ###

n = int(input())
s = input()
ls = list(s)
all = ls.count('R')* ls.count('G')* ls.count('B')

cnt = 0

for i in range(n):
    for j in range(i+1, n):
        if 2*j - i < n:
            if s[i] != s[j] and s[i] != s[2*j-i] and s[j] != s[2*j -i]:
                cnt +=1

print(all - cnt)
### BUGGY CODE ###

n,m=map(int, input().split())
a=list(map(int, input().split()))
for i in range(m):
  ma=max(a)
  a.remove(ma)
  a.append(ma//2)
print(sum(a))



### FIXED CODE ###

import heapq
n,m=map(int, input().split())
a=[-int(j) for j in input().split()]
heapq.heapify(a)
for i in range(m):
  v=heapq.heappop(a)
  heapq.heappush(a, -(-v//2))
print(-sum(a))
### BUGGY CODE ###

import math


K = int(input())

sum = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            sum += math.gcd(i, math.gcd(j, k))

print(sum)



### FIXED CODE ###

import math


K = int(input())

sum = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        sum += math.gcd(i, j)
        if j == K:
            break
        else:
            for k in range(j + 1, K + 1):
                sum += math.gcd(i, math.gcd(j, k)) * 2

print(sum)
### BUGGY CODE ###

s = input()
Q = int(input())

for i in range(Q):
    q = input()
    if q == "1":
        s = s[::-1]
    else:
        t, f, c = q.split(" ")
        if f == "1":
            s = c + s
        else:
            s = s + c
print(s)

### FIXED CODE ###

from collections import deque
s = deque(input())
Q = int(input())

reverse = False
for i in range(Q):
    q = input()
    if q == "1":
        reverse = False if reverse else True
    else:
        t, f, c = q.split(" ")
        if f == "1":
            if reverse:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if reverse:
                s.appendleft(c)
            else:
                s.append(c)
ans = ''.join(s)
if reverse:
    print(ans[::-1])
else:
    print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
import numpy as np

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

T_Height = [int(h) for h in input().split()]

Roads = [[0] * n for _ in range(n)]

for i in range(m):
    a, b = map(int, input().split())
    Roads[a-1][b-1] = T_Height[b-1]
    Roads[b-1][a-1] = T_Height[a-1]


i = 0
ans = 0
for E in Roads:
    if T_Height[i] > max(E):
        ans += 1
    i+=1

print(ans)


### FIXED CODE ###

import sys
input = sys.stdin.readline
import numpy as np

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

T_Height = [int(h) for h in input().split()]

Roads = [0] * n

for i in range(m):
    a, b = map(int, input().split())
    b_h = T_Height[b-1]
    a_h = T_Height[a-1]
    if b_h > Roads[a-1]:
        Roads[a-1] = b_h
    if a_h > Roads[b-1]:
        Roads[b-1] = a_h


i = 0
ans = 0
for e in Roads:
    if T_Height[i] > e:
        ans += 1
    i+=1

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

N,K = map(int, input().split())
d = [0]*2*K
for i in range(K):
    d[2*i], d[2*i+1] = map(int, input().split())
a=[0]*(N+1)
b=[0]*(N+1)
a[1]=1
b[1]=1
for i in range(2,N+1):
    t=0
    for j in range(K):
        if i-d[2*j]>0:
            t+=b[i-d[2*j]]
        if i-d[2*j+1]-1>0:
            t-=b[i-d[2*j+1]-1]
    a[i]=t
    b[i]=b[i-1]+a[i]
print(a[N]%998244353)

### FIXED CODE ###

N,K = map(int, input().split())
d = [0]*2*K
for i in range(K):
    d[2*i], d[2*i+1] = map(int, input().split())
a=[0]*(N+1)
b=[0]*(N+1)
a[1]=1
b[1]=1
for i in range(2,N+1):
    t=0
    for j in range(K):
        if i-d[2*j]>0:
            t+=b[i-d[2*j]]
        if i-d[2*j+1]-1>0:
            t-=b[i-d[2*j+1]-1]
    a[i]=t
    b[i]=(b[i-1]+a[i])%998244353
print(a[N]%998244353)
### BUGGY CODE ###

def cycle_getter(N, start):
    """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """
    p = start
    front, cycle, end = [], [], []
    cnt = 0
    visit = {p:0}
    L, R = N, -1
    for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
    p = start
    for _ in range(L):
        front.append(p)
        p = lift(p)
    if L != N:
        for _ in range(L,R):
            cycle.append(p)
            p = lift(p)
        for _ in range(N-(N-L)%period,N):
            end.append(p)
            p = lift(p)
        cnt = (N-L)//period
    return front, cycle, end, cnt

################################################################################


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

def lift(x): return P[x-1]

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
res = -float("inf")
for i in range(1,N+1):
    front, cycle, end, cnt = cycle_getter(K,i)
    S = sum(C[j-1] for j in front)
    T = sum(C[j-1] for j in cycle)
    if cycle+end:
        U = max(0, max(accumulate(C[j-1] for j in cycle+end)))
    else: U=0
    if front:
        R = max(accumulate(C[j-1] for j in front))
    else: R=0
    res = max(res, S+T*(cnt-1)+U, S+U, R)
print(res)

### FIXED CODE ###

def max2(x,y):
    return x if x > y else y

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

off = max(C)
m2 = off
for i in range(N):
    tmp = 0
    m1 = off
    l = i
    S = 0
    k = 0
    for j in range(N):
        S += C[l]
        l = P[l] - 1
        if l==i:
            k = j+1
            break
    cnt = K // k - (K % k == 0)
    cnt = max2(0, cnt -2)
    a = K-cnt*k
    l = i
    for j in range(a):
        tmp += C[l]
        m1 = max2(m1, tmp)
        l = P[l] - 1
    if S > 0:
        m1 += cnt*S
    m2 = max2(m1, m2)

print(m2)
"""
2 4
2 1
10 -7
"""
### BUGGY CODE ###

import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H+1)
    ws = [0]*(W+1)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h, w])
        hs[h] += 1
        ws[w] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxhindex = [i for i, x in enumerate(hs) if x == maxh]
    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    for h in maxhindex:
        if isfound == True:
            break
        for w in maxwindex:
            if [h, w] not in bombs:
                isfound = True
                break
    if isfound == False:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H)
    ws = [0]*(W)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append(tuple([h-1, w-1]))
        hs[h-1] += 1
        ws[w-1] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxhindex = [i for i, x in enumerate(hs) if x == maxh]
    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    bombs = set(bombs)
    for i in maxhindex:
        for j in maxwindex:
            if (i, j) not in bombs:
                print(ans)
                exit()

    print(ans-1)

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

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

l = [[0] * w for i in range(h)]
r = [[0] * w for i in range(h)]
u = [[0] * w for i in range(h)]
d = [[0] * w for i in range(h)]

st = ""
std = ""

for i in range(h):
    st = s[i]
    std = s[h-i-1]
    for j in range(w):
        if st[j] == ".":
            if j>0:
                l[i][j] = l[i][j-1]+1
            elif j==0:
                l[i][j] = 1
        else:
            l[i][j] = 0
        if st[w-j-1] == ".":
            if j>0:
                r[i][w-j-1] = r[i][w-j] + 1
            elif j==0:
                r[i][w-j-1] = 1
        else:
            r[i][w-j-1] = 0
        if st[j] == ".":
            if i>0:
                u[i][j] = u[i-1][j] + 1
            elif i == 0:
                u[i][j] = 1
        else:
            u[i][j] = 0
        if std[j] == ".":
            if i>0:
                d[h-i-1][j] = d[h-i][j] + 1
            elif i == 0:
                d[h-i-1][j] = 1
        else:
            d[h-i-1][j] = 0

ans = 0

for i in range(h):
    for j in range(w):
        ans = max(ans, l[i][j]+r[i][j]+u[i][j]+d[i][j]-3)

print(ans)

### FIXED CODE ###

import numpy as np
h, w = map(int,input().split())

map=[list(input()) for _ in range(h)]
map = [[1 if s == '.' else 0 for s in map[i]] for i in range(h)]

map = np.array(map,dtype=int)
l=map.copy()
r=map.copy()
u=map.copy()
d=map.copy()

for i in range(1,h):
    u[i] += u[i-1]
    u[i] *= map[i]
for i in range(h-1, 0, -1):
    d[i-1] += d[i]
    d[i-1] *= map[i-1]
for i in range(1,w):
    l[:, i] += l[:, i-1]
    l[:, i] *= map[:, i]
for i in range(w-1, 0, -1):
    r[:, i-1] += r[:, i]
    r[:, i-1] *= map[:, i-1]

print((u+d+l+r).max()-3)
### BUGGY CODE ###

from bisect import*
def main():
    root=[[],[]]
    def search_higher(key):
        ptr=root
        ret=None
        while ptr[1]:
            i=bisect(ptr[0],key)
            if i!=len(ptr[0]):ret=ptr[0][i]
            ptr=ptr[1][i]
        i=bisect(ptr[0],key)
        if i!=len(ptr[0]):ret=ptr[0][i]
        return ret
    def search_lower(key):
        ptr=root
        ret=None
        while ptr[1]:
            i=bisect_left(ptr[0],key)
            if i!=0:ret=ptr[0][i-1]
            ptr=ptr[1][i]
        i=bisect_left(ptr[0],key)
        if i!=0:ret=ptr[0][i-1]
        return ret
    def insert_rec(ptr,key):
        b_size=512
        if not ptr[1]:
            insort(ptr[0],key)
            if len(ptr[0])==b_size*2-1:
                ret=[ptr[0][:b_size],[]]
                ptr[0]=ptr[0][b_size:]
                return ret
        else:
            i=bisect(ptr[0],key)
            tmp=insert_rec(ptr[1][i],key)
            if tmp:
                ptr[0].insert(i,tmp[0].pop())
                ptr[1].insert(i,tmp)
                if len(ptr[1])==b_size*2:
                    ret=[ptr[0][:b_size],ptr[1][:b_size]]
                    ptr[0],ptr[1]=ptr[0][b_size:],ptr[1][b_size:]
                    return ret
    def insert(key):
        tmp=insert_rec(root,key)
        if tmp:root[0],root[1]=[tmp[0].pop()],[tmp,root]
    n,*a=map(int,open(0).read().split())
    l=[0]*n
    for i,v in enumerate(a,1):l[v-1]=i
    insert(0)
    insert(n+1)
    c=0
    for i,v in enumerate(l,1):
        c+=(search_higher(v)-v)*(v-search_lower(v))*i
        insert(v)
    print(c)
main()

### FIXED CODE ###

root=lambda x:x if par[x]==x else root(par[x])
same=lambda x,y:root(x)==root(y)
def unite(*z):
  x,y=map(root,z)
  if x!=y:
    if rank[x]<rank[y]:
      par[x]=y
      size[y]+=size[x]
    else:
      par[y]=x
      size[x]+=size[y]
      if rank[x]==rank[y]:rank[x]+=1
n,*a=map(int,open(0).read().split())
*par,=range(n)
rank=[1]*n
size=[1]*n
b=[0]*n
for i,t in enumerate(a):b[t-1]=i
c=0
for k in b[::-1]:
  l=r=1
  if k>0 and a[k-1]>a[k]:
    l+=size[root(k-1)]
    unite(k-1,k)
  if k+1<n and a[k+1]>a[k]:
    r+=size[root(k+1)]
    unite(k+1,k)
  c+=l*r*a[k]
print(c)
### BUGGY CODE ###

A, B, Q = list(map(int, raw_input().split()))

jinja = list()
for a in xrange(A): jinja.append(long(raw_input()))
tera = list()
for b in xrange(B): tera.append(long(raw_input()))

max_val = 10000000000000000.0

for q in xrange(Q):
    question = long(raw_input())
    if jinja[0] >= question:
        min_jinja = max_val
        max_jinja = jinja[0] - question
    elif jinja[-1] <= question:
        min_jinja = question - jinja[-1]
        max_jinja = max_val
    else:
        for a in xrange(A-1):
            if jinja[a] <= question and jinja[a+1] >= question:
                min_jinja = question - jinja[a]
                max_jinja = jinja[a+1] - question
    if tera[0] >= question:
        min_tera = max_val
        max_tera = tera[0] - question
    elif tera[-1] <= question:
        min_tera = question - tera[-1]
        max_tera = max_val
    else:
        for b in xrange(B-1):
            if tera[b] <= question and tera[b+1] >= question:
                min_tera = question - tera[b]
                max_tera =  tera[b+1] - question

    if max_tera == max_val and max_jinja == max_val:
        if min_jinja >= min_tera: print min_jinja
        else: print min_tera
    elif min_tera == max_val and min_jinja == max_val:
        if max_jinja >= max_tera: print max_jinja
        else: print max_tera
    elif min_jinja > min_tera and max_tera < max_jinja:
        if min_jinja >= max_jinja: print max_jinja
        else: print min_jinja
    elif min_tera < min_jinja and min_jinja < max_tera:
        if min_tera >= max_tera: print max_tera
        else: print min_tera
    elif min_tera <= min_jinja:
        dist = [min_jinja, min_tera*2 + max_jinja, min_tera + max_jinja*2, max_tera]
        dist.sort()
        print dist[0]
    elif min_jinja <= min_tera:
        dist = [min_tera, min_jinja*2 + max_tera, min_jinja + max_tera*2, max_jinja]
        dist.sort()
        print dist[0]

### FIXED CODE ###

import bisect

A, B, Q = list(map(int, raw_input().split()))

max_val = 10.0 ** 15

jinja = list()
jinja.append(-max_val)
for a in xrange(A): jinja.append(long(raw_input()))
jinja.append(max_val)
tera = list()
tera.append(-max_val)
for b in xrange(B): tera.append(long(raw_input()))
tera.append(max_val)

for q in xrange(Q):
    question = long(raw_input())
    idx_tera, idx_jinja = bisect.bisect_right(tera, question), bisect.bisect_right(jinja, question)

    cost = max_val
    for T in [tera[idx_tera-1], tera[idx_tera]]:
        for S in [jinja[idx_jinja-1], jinja[idx_jinja]]:
            d_T = abs(T - question) + abs(S - T) # pattern1: question -> T -> S
            d_S = abs(S - question) + abs(T - S) # pattern2: question -> S -> T
            cost = min(cost, d_T, d_S)
    print cost