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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int A, B, Q;
        public long MOD = 1000000007;
        public long[] S, T, X;
        public BinarySearch BS, BSB;

        public void Run()
        {
            {
                var line = Input.ReadIntArray();
                A = line[0];
                B = line[1];
                Q = line[2];
            }

            S = new long[A];
            T = new long[B];
            X = new long[Q];

            for (int i = 0; i < A; i++)
            {
                S[i] = Input.ReadLong();
            }

            for (int i = 0; i < B; i++)
            {
                T[i] = Input.ReadLong();
            }
            for (int q = 0; q < Q; q++)
            {
                X[q] = Input.ReadLong();
            }

            BS = new BinarySearch();
            for (int q = 0; q < Q; q++)
            {
                Console.WriteLine(Answer(X[q]));
            }
        }

        public long Answer(long x)
        {
            long ret = long.MaxValue;

            ret = Math.Min(ret, Answer(x, true, S, T));
            ret = Math.Min(ret, Answer(x, false, S, T));
            ret = Math.Min(ret, Answer(x, true, T, S));
            ret = Math.Min(ret, Answer(x, false, T, S));
            return ret;
        }

        public long Answer(long x, bool left, long[] Ary1, long[] Ary2)
        {
            long ret = long.MaxValue;

            int a = -1, b = -1;
            long xa = -1, xb1 = -1, xb2 = -1;
            a = SearchIndex(x, Ary1) + (left ? -1 : 0);

            if (a >= 0 && a < Ary1.Length)
            {
                xa = Ary1[a];
                b = SearchIndex(xa, Ary2);
                if (b >= 0 && b < Ary2.Length)
                {
                    xb1 = Ary2[b];
                    ret = Math.Min(ret, Math.Abs(x - xa) + Math.Abs(xa - xb1));
                }

                b -= 1;
                if (b >= 0 && b < Ary2.Length)
                {
                    xb2 = Ary2[b];
                    ret = Math.Min(ret, Math.Abs(x - xa) + Math.Abs(xa - xb2));
                }
            }

            // Console.Error.WriteLine($"x:{x} a:{a} b:{b} {xa} {xb1} {xb2}");
            return ret;
        }

        public int SearchIndex(long x, long[] Ary)
        {
            return BS.Search(x, Ary);
        }


    }

    // libs ----------
    internal class BinarySearch
    {
        public long[] S;
        public int N { get { return S.Length; } }

        // 左側がfalse, 右側がtrueである必要がある
        public bool Cond(int i, long x)
        {
            if (S[i] >= x) return true;
            return false;
        }

        // 初めてCondがtrueを返すindexを返す
        // 全て条件を満たさない場合はNを返す
        public int Search(long x, long[] ary)
        {
            S = ary;

            int left = 0;
            int right = N;

            if (Cond(0, x)) return 0;
            if (!Cond(N - 1, x)) return N;

            while (left + 1 < right)
            {
                int mid = (left + right) / 2;
                if (!Cond(mid, x))
                {
                    left = mid;
                }
                else
                {
                    right = mid;
                }
            }
            return right;
        }
    }

    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();            
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}