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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte(){
        if(ptr < buflen){
            return true;
        }else{
            ptr = 0;
            try{
                buflen = in.read(buffer);
            }catch(IOException e){
                e.printStackTrace();
            }
            if(buflen <=0){
                return false;
            }
        }
        return true;
    }

    private int readByte(){
        if(hasNextByte())return buffer[ptr++];
        else return -1;
    }

    private static boolean isPrintableChar(int c){
        return 33<=c && c<=126;
    }
    public boolean hasNext(){
        while(hasNextByte() && !isPrintableChar(buffer[ptr]))ptr++;
        return hasNextByte();
    }

    public String next(){
        if(!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong(){
        if(!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        if(b < '0' || '9' < b){
            throw new NumberFormatException();
        }
        while(true){
            if('0' <= b && b<='9'){
                n*=10;
                n+=b-'0';
            }else if(b==-1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt(){
        long nl = nextLong();
        if(nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDoutble(){return Double.parseDouble(next());}
}
public class Main {
    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        int a = fs.nextInt();
        int b = fs.nextInt();
        int q = fs.nextInt();
        long[] s = new long[a+2];
        long[] t = new long[b+2];
        long[] x = new long[q];
        Map<Long,Integer> map = new HashMap<>();
        s[0] = t[0] = -100000000000L;
        s[a+1] = t[b+1] = 100000000000L;
        for(int i=1;i<=a;i++)s[i] = fs.nextLong();
        for(int i=1;i<=b;i++)t[i] = fs.nextLong();
        for(int i=0;i<q;i++){
            x[i] = fs.nextLong();
            map.put(x[i],i);
        }
        Arrays.sort(x);

        long[] dist = new long[q];
        int snow = 0,tnow = 0;
        long res;
        for(int i=0;i<q;i++){
            res = Long.MAX_VALUE;
            while(!(s[snow]<= x[i] && s[snow+1] > x[i]))snow++;
            while(!(t[tnow]<= x[i] && t[tnow+1] > x[i]))tnow++;
            if(s[snow] <= t[tnow]){
                if(s[snow+1] <= t[tnow+1]){
                    res = x[i] - s[snow];
                    res = Math.min(res,x[i]-t[tnow]+s[snow+1]-t[tnow]);
                    res = Math.min(res,s[snow+1] - x[i]+s[snow+1] - t[tnow]);
                    res = Math.min(res,t[tnow+1] - x[i]);
                }else{
                    res = x[i] - s[snow];
                    res = Math.min(res,s[snow+1] - x[i]);
                }
            }else if(s[snow] > t[tnow]){
                if(t[tnow+1] <= s[snow+1]){
                    res = x[i] - t[tnow];
                    res = Math.min(res,x[i]-s[snow]+t[tnow+1]-s[snow]);
                    res = Math.min(res,t[tnow+1]-x[i]+t[tnow+1]-s[snow]);
                    res = Math.min(res,s[snow+1]-x[i]);
                }else{
                    res = x[i] - t[tnow];
                    res = Math.min(res,t[tnow+1]-x[i]);
                }
            }
            dist[map.get(x[i])] = res;
        }

        StringBuilder sb = new StringBuilder();
        for(int i=0;i<q;i++){
            sb.append(dist[i]);
            sb.append('\n');
        }

        System.out.println(sb.toString());

    }
}