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: Accepted)

#![allow(unused_mut, unused_macros, unused_variables, unused_assignments, non_snake_case, unused_imports, dead_code, non_camel_case_types)]

use std::collections::{HashMap, BinaryHeap, BTreeMap};

// [Rustで競技プログラミングの入力をスッキリ記述するマクロ - Qiita]
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 から拝借
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    // ABC128-C-Switches用に拡張
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, $t);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

// 可愛い可愛い、お手製のライブラリ達
// 出力
macro_rules! p {
    ($t:expr) => {
        println!("{}", $t);
    };
}
macro_rules! debug {
    ($t:expr) => {
        println!("{:?}", $t);
    };
}
// 最大公約数 / 最小公倍数
fn gcd(a:usize, b:usize) -> usize {
    if b == 0 {return a};
    gcd(b, a % b)
}
fn lcm(a:usize, b:usize) -> usize {
    a / gcd(a, b) * b
}
// 置換
macro_rules! chmax {
    ($x : expr, $y : expr) => {
        if $x < $y {$x = $y;}
    }
}
macro_rules! chmin {
    ($x : expr, $y : expr) => {
        if $x > $y {$x = $y;}
    }
}
// Union-Find木
struct uft {
    g: Vec<isize>,
}
impl uft {
    fn new(n:usize) -> uft {
        uft {g:vec![-1; n]}
    }
    fn root(&mut self, a:usize) -> usize {
        let va = self.g[a];
        if va.is_negative() {a}
        else {
            let na = self.root(va as usize);
            self.g[a] = na as isize;
            na
        }
    }
    fn merge(&mut self, a:usize, b:usize) -> bool {
        let (na, nb) = (self.root(a), self.root(b));
        if na == nb {return false;}
        self.g[na] += self.g[nb];
        self.g[nb] = na as isize;
        true
    }
    fn size(&mut self, a:usize) -> usize {
        let na = self.root(a);
        (self.g[na] * -1) as usize
    }
}
// グラフ(深さ優先探索DFS/幅優先探索BFS)

// 検索(二分探索：右)
fn bsr(g:&[isize], v:isize, lo:usize, hi:usize) -> usize {
    if hi < lo {return lo;}
    let mi = (hi + lo) / 2;
    if g[mi] < v {
        return bsr(&g, v, mi + 1, hi);
    } else {
        return bsr(&g, v, lo, mi - 1);
    }
}

// エントリーポイント
fn main() {
    input! {
        A : usize,
        B : usize,
        Q : usize,
        S : [isize; A],
        T : [isize; B],
        X : [isize; Q],
    }
    let s = {
        let mut s = vec![-1_000_000_000_000];
        s.extend(S.iter().cloned());
        s.push(1_000_000_000_000);
        s
    };
    let t = {
        let mut t = vec![-1_000_000_000_000];
        t.extend(T.iter().cloned());
        t.push(1_000_000_000_000);
        t
    };
    for x in &X {
        let sr = bsr(&s, *x, 0, s.len() - 1);
        let tr = bsr(&t, *x, 0, t.len() - 1);
        let (sl, tl) = (sr - 1, tr - 1);
        let mut cost = vec![];
        for st in [s[sl], s[sr]].iter() {
            for tt in [t[tl], t[tr]].iter() {
                cost.push((tt - x).abs() + (tt - st).abs());
                cost.push((st - x).abs() + (tt - st).abs());
            }
        }
        p!(*cost.iter().min().unwrap());
    }
}