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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

// -----------------------------------------------
// Framework <https://github.com/vain0x/procon>
//
// See the bottom of file for solution.
// -----------------------------------------------

#![allow(unused_imports)]
#![allow(non_snake_case)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Display, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;

/// Print values to standard error if debug mode.
#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), stderr());
            writeln!(err, "\x1B[33m{}\x1B[0m = {:?}", e, $e).unwrap()
        })*
    };
}

/// Read from standard input and parse each word.
/// - `read!(T, U, ..)` parses a line as a tuple of words.
/// - `read![[T]]` parses a line as an array of words.
/// - `read![..; N]` parses `N` lines, using `read!(..)` repeatedly.
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

/// Read a line from standard input.
#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    buf.trim_right().to_owned()
}

// -----------------------------------------------
// Solution
// -----------------------------------------------

fn main() {
    use std::io::{BufReader, BufWriter};

    let (A, B, Q) = read!(usize, usize, usize);

    let stdin = stdin();
    let mut stdin = BufReader::new(stdin.lock());
    let mut line = String::new();
    let mut scan = move || {
        line.clear();
        stdin.read_line(&mut line).unwrap();
        line.trim_right().parse::<i64>().unwrap()
    };
    let stdout = std::io::stdout();
    let mut stdout = BufWriter::new(stdout.lock());

    let mut S = Vec::with_capacity(A);
    for _ in 0..A {
        S.push(scan());
    }
    let mut T = Vec::with_capacity(B);
    for _ in 0..B {
        T.push(scan());
    }
    let mut X = Vec::with_capacity(Q);
    for _ in 0..Q {
        X.push(scan());
    }

    for q in 0..Q {
        let x = X[q];

        // 位置 x からみてすぐ右にある神社の番号 (なければ A)
        let si = if x > S[A - 1] {
            A
        } else if x <= S[0] {
            0
        } else {
            let mut l = 0; // ng
            let mut r = A; // ok
            while r - l > 1 {
                let m = (r - l) / 2;
                if x <= S[m] {
                    r = m;
                } else {
                    l = m;
                }
            }
            r
        };

        let ti = if x > T[B - 1] {
            B
        } else if x <= T[0] {
            0
        } else {
            let mut l = 0; // ng
            let mut r = B; // ok
            while r - l > 1 {
                let m = (r - l) / 2;
                if x <= T[m] {
                    r = m;
                } else {
                    l = m;
                }
            }
            r
        };

        let mut min_d = std::i64::MAX;
        for si in (si - min(1, si))..min(A, si + 1) {
            for ti in (ti - min(1, ti))..min(B, ti + 1) {
                let ds = (S[si] - x).abs();
                let dt = (T[ti] - x).abs();
                let d = ds + dt + (S[si] - T[ti]).abs() - max(ds, dt);
                min_d = min(min_d, d);
            }
        }

        writeln!(stdout, "{}", min_d).unwrap();
    }

    // debug!(S, T, Y, D);

    stdout.flush().unwrap();
}