Problem:
Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.

Solution in Python (3.4.3) (judge verdict: Time Limit Exceeded)

import bisect

A, B, Q = map(int, input().split())
s = [None] * A
t = [None] * B
x = [None] * Q

for i in range(A):
    s[i] = int(input())
for i in range(B):
    t[i] = int(input())
for i in range(Q):
    x[i] = int(input())

s.sort()
t.sort()

def calc(s, t, x):
    len_s = len(s)
    len_t = len(t)

    def f_nishi_pos(l, x):
        p1 = bisect.bisect_left(l, x)
        p2 = bisect.bisect_right(l, x)
        if p2 == len(l):
            return p1-1
        if l[p1] == l[p2]:
            return max(p2-1, 0)
        else:
            return p1

    def f_higashi_pos(l, x):
        p1 = bisect.bisect_left(l, x)
        p2 = bisect.bisect_right(l, x)
        if p1 == len(l):
            return p1-1
        if l[p1] == l[p2]:
            return min(p1, len(l)-1)
        else:
            return p1

    # 神社 -> 寺
    ## 西の神社
    j_nishi_pos = f_nishi_pos(s, x)
    j_nishi = s[j_nishi_pos]
    ## 東の神社
    j_higashi_pos = f_higashi_pos(s, x)
    j_higashi = s[j_higashi_pos]

    ## 西の神社の西の寺
    j_nishi_t_nishi_pos = f_nishi_pos(t, j_nishi)
    j_nishi_t_nishi = t[j_nishi_t_nishi_pos]
    ## 西の神社の東の寺
    j_nishi_t_higashi_pos = f_higashi_pos(t, j_nishi)
    j_nishi_t_higashi = t[j_nishi_t_higashi_pos]
    ## 東の神社の西の寺
    j_higashi_t_nishi = t[f_nishi_pos(t, j_higashi)]
    ## 東の神社の東の寺
    j_higashi_t_higashi = t[f_higashi_pos(t, j_higashi)]


    # 寺 -> 神社
    t_nishi = t[f_nishi_pos(t, x)]
    t_higashi = t[f_higashi_pos(t, x)]
    
    ## 西の寺の西の神社
    t_nishi_j_nishi = s[f_nishi_pos(s, t_nishi)]
    ## 西の寺の東の神社
    t_nishi_j_higashi = s[f_higashi_pos(s, t_nishi)]
    ## 東の寺の東の神社
    t_higashi_j_nishi = s[f_nishi_pos(s, t_higashi)]
    ## 東の寺の東の神社
    t_higashi_j_higashi = s[f_higashi_pos(s, t_higashi)]


    l = [
        abs(x - j_nishi) + abs(j_nishi - j_nishi_t_nishi),
        abs(x - j_nishi) + abs(j_nishi - j_nishi_t_higashi),
        abs(x - j_higashi) + abs(j_higashi - j_higashi_t_nishi),
        abs(x - j_higashi) + abs(j_higashi - j_higashi_t_higashi),
        abs(x - t_nishi) + abs(t_nishi - t_nishi_j_nishi),
        abs(x - t_nishi) + abs(t_nishi - t_nishi_j_higashi),
        abs(x - t_higashi) + abs(t_higashi - t_higashi_j_nishi),
        abs(x - t_higashi) + abs(t_higashi - t_higashi_j_higashi)
    ]


    return min(l)

for xx in x:
    print(calc(s, t, xx))