Scanf.scanf "%d %d %d" (fun a b q ->
    let s = Array.init a (fun _ -> Scanf.scanf " %d" (fun s -> s)) in
    let t = Array.init b (fun _ -> Scanf.scanf " %d" (fun t -> t)) in
    let co x y z = abs (x - y) + abs (y - z) in
    let calc l = List.fold_left min max_int l in
    let find arr v =
        let rec bin l r =
            if l = r then l - 1 else
                let m = (l + r) / 2 in
                if arr.(m) <= v then bin (m + 1) r else bin l m
        in
        let n = Array.length arr in
        if n = 1 then arr.(0), arr.(0) else
            let x = bin 0 n in
            if x = n - 1 then arr.(x - 1), arr.(x)     else
            if x = -1    then arr.(x + 1), arr.(x + 2) else
                         arr.(x), arr.(x + 1)
    in
    for i = 1 to q do
        Scanf.scanf " %d" (fun x ->
            let a1, a2 = find s x in
            let b11, b12 = find t a1 in
            let b21, b22 = find t a2 in
            let c1, c2 = find t x in
            let d11, d12 = find s c1 in
            let d21, d22 = find s c2 in

            calc [
                co x a1 b11; co x a1 b12; co x b11 a1; co x b12 a1;
                co x a2 b21; co x a2 b22; co x b21 a2; co x b22 a2;
                co x c1 d11; co x c1 d12; co x d11 c1; co x d12 c1;
                co x c2 d21; co x c2 d22; co x d21 c2; co x d22 c2
            ] |> Printf.printf "%d\n"
        )
    done
)