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

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)

def main():
    h,w = LI()
    a = [S() for _ in range(h)]
    t = set()
    for i in range(h):
        ai = a[i]
        for j in range(w):
            if ai[j] == '#':
                t.add((i,j))

    r = 0
    u = t | set()
    while 1:
        k = set()
        for i,j in t:
            for di,dj in dd:
                ni = i + di
                nj = j + dj
                if ni < 0 or ni >= h or nj < 0 or nj >= w:
                    continue
                nk = (ni,nj)
                if (ni,nj) in u:
                    continue
                k.add(nk)
        if not k:
            break
        r += 1
        t = k
        u |= k

    return r


print(main())




### FIXED CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)

def main():
    h,w = LI()
    a = [S() for _ in range(h)]
    t = set()
    for i in range(h):
        ai = a[i]
        for j in range(w):
            if ai[j] == '#':
                t.add(i*w+j)


    r = 0
    u = t | set()
    while 1:
        k = set()
        for ij in t:
            i = ij // w
            j = ij % w
            for di,dj in dd:
                ni = i + di
                nj = j + dj
                if ni < 0 or ni >= h or nj < 0 or nj >= w:
                    continue
                nk = ni*w + nj
                if nk in u:
                    continue
                k.add(nk)
        if not k:
            break
        r += 1
        t = k
        u |= k

    return r


print(main())
### BUGGY CODE ###

# -*- coding: utf-8 -*-
n = int(input())
numlist = list(map(int, input().split()))
dic = {}
for num in numlist:
  if num not in dic:
    dic[num] = 1
  else:
    dic[num] += 1
      
for num in numlist:
  sum = 0
  for key in dic.keys():
    n = dic[key]
    if num == key:
      n -= 1
    sum += int(n*(n-1)/2)
  print(sum)

### FIXED CODE ###

# -*- coding: utf-8 -*-
n = int(input())
numlist = list(map(int, input().split()))
dic = {}
for num in numlist:
  if num not in dic:
    dic[num] = 1
  else:
    dic[num] += 1
    
tmpsum = 0
for value in dic.values():
  tmpsum += int(value*(value-1)/2)
ansdic = {}
for key in dic.keys():
  n = dic[key]
  ansdic[key] = tmpsum - n + 1      
    
for num in numlist:
  print(ansdic[num])
### BUGGY CODE ###

string=input()
length=len(string)
mod=[1]+[0]*2018
s=0
for i in range(length):
  s=s+int(string[-1-i])*pow(10,i)
  mod[s%2019]+=1
print(sum([i*(i-1)//2 for i in mod]))

### FIXED CODE ###

string=input()
length=len(string)
mod=[1]+[0]*2018
s=0
for i in range(length):
  s=(s+int(string[-1-i])*pow(10,i,2019))%2019
  mod[s]+=1
print(sum([i*(i-1)//2 for i in mod]))
### BUGGY CODE ###

import collections


def friend_candidates(i, friends, visited=None):
    visited = set([i])
    stack = list(friends[i])

    while stack:
        v = stack.pop()
        if v in visited:
            continue

        stack.extend(friends[v])
        visited.add(v)

    return visited


N, M, K = map(int, input().split())

friends = collections.defaultdict(list)
for _ in range(M):
    A, B = map(int, input().split())
    friends[A].append(B)
    friends[B].append(A)

blockings = collections.defaultdict(list)
for _ in range(K):
    C, D = map(int, input().split())
    blockings[C].append(D)
    blockings[D].append(C)


table = {}
for i in range(1, N + 1):
    if i in table:
        continue

    candidates = list(friend_candidates(i, friends))
    for j in candidates:
        table[j] = candidates

print(
    " ".join(
        str(
            len(table[i])
            - len(friends[i])
            - len(set(blockings[i]).intersection(table[i]))
            - 1
        )
        for i in range(1, N + 1)
    )
)


### FIXED CODE ###

def dfs(graph, start):
    visited = set()
    stack = [start]

    while stack:
        v = stack.pop()
        if v not in visited:
            stack.extend(graph[v])
            visited.add(v)
            yield v


def read_graph(nodes, edges):
    graph = [[] for _ in range(nodes)]
    for _ in range(edges):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].append(b)
        graph[b].append(a)

    return graph


def count_candidates(n, counts, groups, block, friend):
    for i in range(n):
        count = counts[groups[i]]
        for v in block[i]:
            if groups[i] == groups[v]:
                count -= 1

        count -= len(friend[i])
        count -= 1
        yield count


def main():
    n, m, k = map(int, input().split())
    friend = read_graph(n, m)
    block = read_graph(n, k)

    groups = [-1 for _ in range(n)]
    counts = []
    i = 0
    group_id = 0
    while i < n:
        if groups[i] != -1:
            i += 1
            continue

        for j, v in enumerate(dfs(friend, i)):
            groups[v] = group_id

        counts.append(j + 1)
        group_id += 1

    print(" ".join(str(i) for i in count_candidates(n, counts, groups, block, friend)))


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

import sys
input = sys.stdin.readline


def f(N, x):
    n = 1
    for i in range(1, N):
        if i != x:
            n *= i
    return n


def main():
    N = int(input())
    X = list(map(int, input().split()))
    D = [0 for i in range(N)]

    for i in range(N-1):
        D[i] = X[i+1] - X[i]

    ans = 0
    r = 0
    for i in range(1, N):
        r += f(N, i)
        ans += D[i-1] * r
        ans %= 10**9+7
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import math
input = sys.stdin.readline

mod = 10**9+7


def main():
    N = int(input())
    r1 = [1]
    for i in range(N-1):
        r1.append(r1[i] * (i+1) % mod)
    r2 = [1]
    for i in range(N-1):
        r2.append(r2[i] * (N-1-i) % mod)

    X = list(map(int, input().split()))
    D = [0 for i in range(N)]
    for i in range(N-1):
        D[i] = X[i+1] - X[i]

    ans = 0
    r = 0
    for i in range(N):
        r += r1[i] * r2[N-i-2]
        ans += D[i] * r
        ans %= mod
    print(ans)


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


N,M=map(int, input().split())
A=list(map(int, input().split()))
A=sorted(A)

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

L=sorted(L,reverse=True, key=lambda x: x[1])
X=[]
for l in L:
    b=l[0]
    c=l[1]
    n=min(b,N-len(X))
    X=X+[c]*n
    if len(X)==N:
        break
#print(X)
ans=0
for i in range(N):
    if len(X)>i:
        ans+=max(X[i],A[i])
    else:
        ans+=A[i]

print(ans)

### FIXED CODE ###

N, M=map(int, input().split())
A=list(map(int, input().split()))
bc=[]
for i in range(M):
  b, c=map(int, input().split())
  bc.append((c,b))
for i in range(N):
  bc.append((A[i], 1))
  
bc=sorted(bc, key=lambda x : x[0], reverse=True)

ans=0
res=N
for x in bc:
  c=x[0]
  b=min(x[1], res)
  ans+=c*b
  res-=b
  if res==0:
    break
    
print(ans)
### BUGGY CODE ###

import numpy as np
 
N = int(input())
x = list(map(int, input().split()))
half = N//2

idx = np.argsort(x)

nums = [x[idx[half-1]], x[idx[half]]]
mask = idx[:half]

for i in range(N):
    if i in mask:
        print(nums[1])
    else:
        print(nums[0])

### FIXED CODE ###

N = int(input())
X = list(map(int, input().split()))
center = N // 2
X_ = sorted(X)
ans = [X_[center-1], X_[center]]
center = sum(ans) / 2

for x in X:
    if x < center:
        print(ans[1])
    else:
        print(ans[0])
### BUGGY CODE ###

n, m, k = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
cd = [list(map(int, input().split())) for _ in range(k)]
con = [1 for _ in range(n+1)]
uni = [-1 for i in range(n+1)]

def root(num, pea):
    while pea[num] > 0:
        num = pea[num]
    return num

for i in range(m):
    a, b = ab[i]
    con[a] += 1
    con[b] += 1
    if uni[a] != uni[b] or uni[a] < 0 or uni[b] < 0:
        root_a = root(a, uni)
        root_b = root(b, uni)
        root_a, root_b = sorted([root_a, root_b])
        uni[root_a] += uni[root_b]
        uni[root_b] = root_a

for i in range(k):
    c, d = cd[i]
    if root(c, uni) == root(d, uni):
        con[c] += 1
        con[d] += 1

result = []
for i in range(1, n+1):
    result.append(-uni[root(i, uni)]-con[i])
print(*result)

### FIXED CODE ###

class UnionFind():
    # 作りたい要素数nで初期化
    # 使用するインスタンス変数の初期化
    def __init__(self, n):
        self.n = n
        # root[x]<0ならそのノードが根かつその値が木の要素数
        # rootノードでその木の要素数を記録する
        self.root = [-1]*(n+1)
        # 木をくっつける時にアンバランスにならないように調整する
        self.rnk = [0]*(n+1)

    # ノードxのrootノードを見つける
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            # ここで代入しておくことで、後の繰り返しを避ける
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    # 木の併合、入力は併合したい各ノード
    def Unite(self, x, y):
        # 入力ノードのrootノードを見つける
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        # すでに同じ木に属していた場合
        if(x == y):
            return 
        # 違う木に属していた場合rnkを見てくっつける方を決める
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            # rnkが同じ（深さに差がない場合）は1増やす
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    # xとyが同じグループに属するか判断
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    # ノードxが属する木のサイズを返す
    def Count(self, x):
        return -self.root[self.Find_Root(x)]


n, m, k = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
cd = [list(map(int, input().split())) for _ in range(k)]
con = [1 for _ in range(n+1)]
uni = UnionFind(n+1)


for i in range(m):
    a, b = ab[i]
    con[a] += 1
    con[b] += 1
    uni.Unite(a, b)

for i in range(k):
    c, d = cd[i]
    if uni.isSameGroup(c,d):
        con[c] += 1
        con[d] += 1

result = []
for i in range(1, n+1):
    result.append(uni.Count(i)-con[i])
print(*result)
### BUGGY CODE ###

n,k=map(int,input().split())
s=list(map(int,input().split()))
e=[0]*n
e_table=[0]*1000
tmp=0
for i in range(1000):
    tmp+=(i+1)
    e_table[i] = tmp/(i+1)


e = list(map(lambda x:e_table[x-1],s))

ans=0

for i in range(n-k+1):
    ans=max(ans,sum(e[i:i+k]))
print(ans)

### FIXED CODE ###

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

ans=0
tmp=0
tmp2=sum(s[0:k])
kukan=[0,n]
for i in range(n-k):
    tmp2 -= s[i]
    tmp2 += s[k+i]
    if tmp < tmp2:
        tmp = tmp2
        kukan = [i+1,i+k+1]

for i in range(kukan[0],kukan[1]):
    ans += (s[i]+1)/2
    
print(ans)
### BUGGY CODE ###

mod = 10**9+7
k = int(input())
s = input()

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, len(s)+k+5):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

ans = 0
for i in range(len(s), len(s)+k+1):
  ans += pow(25, i-len(s), mod)*pow(26, k+len(s)-i, mod)*cmb(i-1, len(s)-1, mod)
  ans %= mod
  
print(ans)

### FIXED CODE ###

mod = 10**9+7
k = int(input())
s = input()

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, len(s)+k+5):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

ans = 0
pow1 = 1
for i in range(len(s), len(s)+k+1):
  ans += pow1 * pow(26, k+len(s)-i, mod)*cmb(i-1, len(s)-1, mod)
  pow1 *= 25
  pow1 %= mod
  ans %= mod
  
print(ans)
### BUGGY CODE ###

import math
n=int(input())
A=list(map(int,input().split()))

def comb(n, r):
  if n<=1:
    return 0
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
cnt,combs,total={},{},0
for a in set(A):
  cnt[a]=A.count(a)
  tmp=comb(cnt[a],2)
  combs[a]=tmp
  total+=tmp
for a in A:
  print(total-combs[a]+comb(cnt[a]-1,2))

### FIXED CODE ###

from operator import mul
from functools import reduce
import collections
n=int(input())
A=list(map(int,input().split()))

def comb(n,r):
    r = min(n-r,r)
    if n<=1: return 0
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
c=collections.Counter(A)
combs,total=[0]*n,0
for a in set(A):
  tmp=comb(c[a],2)
  combs[a-1]=tmp
  total+=tmp
for a in A:
  print(total-combs[a-1]+comb(c[a]-1,2))
### BUGGY CODE ###

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

S = [0] * N
S[0] = 0

for i in range(1, N):
    value = S[i - 1] + abs(h[i] - h[i - 1])
    for step in range(1, K + 1):
        # print("i, k:", i, k)
        if i - step < 0:
            break
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
        #print("value", value)
    S[i] = value
    # print(S)
# print(S)
print(S[N - 1])


### FIXED CODE ###

N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*(N)
dp[1]=abs(h[0]-h[1])
for i in range(2,N):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-K),i))
print(dp[N-1])
### BUGGY CODE ###

# -*- coding: utf-8 -*-


def move(s, score):
    new_score = [0] * len(s)
    for i, v in enumerate(score):
        if v == 0:
            continue
        if s[i] == 'R':
            new_score[i + 1] += v
        else:
            new_score[i - 1] += v
    return new_score


if __name__ == '__main__':
    s = input()
    score = _score = [1] * len(s)
    i = 0
    while True:
        tmp_score = score
        score = move(s, score)
        if score == _score:
            if i % 2 == 0:
                score = move(s, score)
            break
        _score = tmp_score
        i += 1
    print(' '.join(map(str, score)))


### FIXED CODE ###

# -*- coding: utf-8 -*-


if __name__ == '__main__':
    S = input()
    score = _score = [0] * len(S)
    r_num = 0
    l_num = 0
    _s = None
    change_idx = None
    for i, s in enumerate(S):
        if s == 'L' and _s == 'R':
            change_idx = i
        if s == 'R' and _s == 'L':
            #計算
            if r_num == l_num or (r_num + l_num) % 2 == 0:
                score[change_idx - 1] = (r_num + l_num) / 2
                score[change_idx] = (r_num + l_num) / 2
            elif r_num % 2 == 0:
                score[change_idx - 1] = (r_num + l_num) // 2
                score[change_idx] = (r_num + l_num) // 2 + 1
            else:
                score[change_idx - 1] = (r_num + l_num) // 2 + 1
                score[change_idx] = (r_num + l_num) // 2
            #初期化
            r_num = 0
            l_num = 0
            change_idx = None
        if s == 'R':
            r_num += 1
        else:
            l_num += 1
        _s = s

    #計算
    if r_num == l_num or (r_num + l_num) % 2 == 0:
        score[change_idx - 1] = (r_num + l_num) / 2
        score[change_idx] = (r_num + l_num) / 2
    elif r_num % 2 == 0:
        score[change_idx - 1] = (r_num + l_num) // 2
        score[change_idx] = (r_num + l_num) // 2 + 1
    else:
        score[change_idx - 1] = (r_num + l_num) // 2 + 1
        score[change_idx] = (r_num + l_num) // 2

    print(' '.join(map(str, map(int, score))))
### BUGGY CODE ###

import math
import itertools
from functools import reduce
from operator import mul

N = int(input())
l =list(itertools.product(range(1,N+1), repeat=3))
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
ans=0
for i in l:
    ans+=gcd_list(i)
print(ans)

### FIXED CODE ###

import math
import itertools
from functools import reduce
from operator import mul
import collections
N = int(input())
l =list(itertools.combinations_with_replacement(range(1,N+1),3))
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
ans=0
for i in l:
    c=collections.Counter(i)
    if len(c)==1:
        ans+=gcd_list(i)
    elif len(c)==2:
        ans+=3*gcd_list(i)
    elif len(c)==3:
        ans+=6*gcd_list(i)
print(ans)
### BUGGY CODE ###

import heapq
INF = 10 ** 16


def dijkstra(n, train, start):
    d = []
    queue = []
    for i in range(n):
        d.append(INF)
    d[start] = 0
    heapq.heappush(queue, (0, start))
    # 都市kに行く時の円またはスヌークのの最小値
    while len(queue) != 0:
        distance, v = queue.pop()
        if d[v] < distance:
            continue
        for u in range(len(train[v])):
            if d[train[v][u][0]] > d[v] + train[v][u][1]:
                d[train[v][u][0]] = d[v] + train[v][u][1]
                heapq.heappush(queue, (d[train[v][u][0]], train[v][u][0]))

    return d


def main():
    n, m, s, t = map(int, input().split())
    s -= 1
    t -= 1
    money = 10 ** 15
    train_en = []
    train_snuuk = []

    for i in range(n):
        train_en.append([])
        train_snuuk.append([])
    for i in range(m):
        fro, to, a, b = map(int, input().split())
        fro -= 1
        to -= 1
        train_en[fro].append((to, a))
        train_en[to].append((fro, a))
        train_snuuk[fro].append((to, b))
        train_snuuk[to].append((fro, b))

    a = dijkstra(n, train_en, s)
    b = dijkstra(n, train_snuuk, t)

    total = []
    for i in range(n):
        heapq.heappush(total, (a[i] + b[i], i + 1))
    mon, year = heapq.heappop(total)
    for i in range(n):
        while year <= i:
            mon, year = heapq.heappop(total)
        print(money - mon)


if __name__ == '__main__':
    main()


### FIXED CODE ###

from heapq import heapify, heappop, heappush


def dijkstra(n, train, start):
    d = []
    queue = []
    INF = 10 ** 16
    for i in range(n):
        d.append(INF)
    d[start] = 0
    heapify(queue)
    heappush(queue, (0, start))
    # 都市kに行く時の円またはスヌークのの最小値
    while len(queue) != 0:
        distance, v = heappop(queue)
        if d[v] < distance:
            continue
        for u in range(len(train[v])):
            if d[train[v][u][0]] > d[v] + train[v][u][1]:
                d[train[v][u][0]] = d[v] + train[v][u][1]
                heappush(queue, (d[train[v][u][0]], train[v][u][0]))
    return d


def main():
    n, m, s, t = map(int, input().split())
    s -= 1
    t -= 1
    money = 10 ** 15
    train_en = []
    train_snuuk = []

    for i in range(n):
        train_en.append([])
        train_snuuk.append([])
    for i in range(m):
        fro, to, a, b = map(int, input().split())
        fro -= 1
        to -= 1
        train_en[fro].append((to, a))
        train_en[to].append((fro, a))
        train_snuuk[fro].append((to, b))
        train_snuuk[to].append((fro, b))

    a = dijkstra(n, train_en, s)
    b = dijkstra(n, train_snuuk, t)

    cost_sum = []
    for i in range(n):
        cost_sum.append((a[i] + b[i], i + 1))
    heapify(cost_sum)
    m, y = heappop(cost_sum)
    for i in range(n):
        while y <= i:
            m, y = heappop(cost_sum)
        print(money - m)


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

import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    N, M, Q = map(int, input().split())
    L = [0]*M
    R = [0]*M
    p = [0]*Q
    q = [0]*Q
    for i in range(M):
        L[i], R[i] = map(int, input().split())
    for i in range(Q):
        p[i], q[i] = map(int, input().split())

    temp = [[0]*N for _ in range(N)]
    #s[i][j]: 左がi、右が0からjまでのものの累積個数
    s = [[0]*(N+1) for _ in range(N)]
    for m in range(M):
        temp[L[m]-1][R[m]-1] += 1
    for i in range(N):
        for j in range(N):
            s[i][j+1] = s[i][j] + temp[i][j]

    for i in range(Q):
        ans = 0
        for j in range(p[i]-1, q[i]):
            ans += s[j][q[i]]-s[j][p[i]-1]
        print(ans)

if __name__ == '__main__':
    resolve()


### FIXED CODE ###

import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    N, M, Q = map(int, input().split())
    L = [0]*M
    R = [0]*M
    p = [0]*Q
    q = [0]*Q
    for i in range(M):
        L[i], R[i] = map(int, input().split())
    for i in range(Q):
        p[i], q[i] = map(int, input().split())

    temp = [[0]*N for _ in range(N)]
    #s[i][j]: 左がi、右が0からjまでのものの累積個数
    s = [[0]*(N+1) for _ in range(N)]
    for m in range(M):
        temp[L[m]-1][R[m]-1] += 1
    for i in range(N):
        for j in range(N):
            s[i][j+1] = s[i][j] + temp[i][j]

    for i in range(Q):
        ans = 0
        for j in range(p[i]-1, q[i]):
            ans += s[j][q[i]]-s[j][p[i]-1]
        print(ans)

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

import bisect
A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
def check(lst, x): #検索するリストと現在地を受け取る
    ans = [[x, 10 ** 10], [x, 10 ** 10]]
    a1 = bisect.bisect_left(lst, x) -1 #自分の左側のindex
    a2 = a1 + 1 #自分の右側のindex
    if a1 < 0:
        pass
    else:
        ans[0][0] = lst[a1]
        ans[0][1] = abs(x-lst[a1])
    if a2 >= len(lst):
        pass
    else:
        ans[1][0] = lst[a2]
        ans[1][1] = abs(x-lst[a2])
    
    return ans

#S-->Tの動き方
def ST(x):
    lst1 = check(S, x)
    lst2 = check(T, lst1[0][0])
    lst3 = check(T, lst1[1][0])
    ans1 = lst1[0][1] + min(lst2[0][1], lst2[1][1])
    ans2 = lst1[1][1] + min(lst3[0][1], lst3[1][1])
    return min(ans1, ans2)

#T-->Sの動き方
def TS(x):
    lst1 = check(T, x)
    lst2 = check(S, lst1[0][0])
    lst3 = check(S, lst1[1][0])
    ans1 = lst1[0][1] + min(lst2[0][1], lst2[1][1])
    ans2 = lst1[1][1] + min(lst3[0][1], lst3[1][1])
    return min(ans1, ans2)

for i in range(Q):
    x = int(input())
    print (min(ST(x), TS(x)))


### FIXED CODE ###

import bisect
A, B, Q = map(int, input().split())
INF  = 10 ** 18
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]

for _ in range(Q):
    x = int(input())
    s1 = bisect.bisect_left(S, x) -1 #自分の左側のindex
    s2 = s1 + 1 #自分の右側のindex
    t1 = bisect.bisect_left(T, x) -1 #自分の左側のindex
    t2 = t1 + 1 #自分の右側のindex
    res = 10 ** 18
    for s in [S[s1], S[s2]]:
        for t in [T[t1], T[t2]]:
            d1, d2 = abs(s-x) + abs(t-s), abs(t-x) + abs(s-t)
            res = min(res, d1, d2)
    print (res)