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 BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    struct Index : IEquatable<Index>
    {
        public int h;
        public int w;
        public Index(int i, int j)
        {
            this.h = i;
            this.w = j;
        }
        public bool Equals(Index other) => this.h == other.h && this.w == other.w;
    }
    public class Program
    {
        public object Calc()
        {
            var line = consoleReader.SplitInt32();
            var A = line[0];
            var B = line[1];
            var Q = line[2];
            var jinja = (new long[] { -1000000000000 }).Concat(consoleReader.RepeatInt64(A)).Concat(new[] { 1000000000000 }).ToArray();
            var tera = (new long[] { -1000000000000 }).Concat(consoleReader.RepeatInt64(B)).Concat(new[] { 1000000000000 }).ToArray();
            var xs = consoleReader.RepeatInt64(Q).ToArray();
            var res = new Dictionary<long, long>();

            int j = 1;
            int t = 1;
            foreach (var x in xs.OrderBy(i => i))
            {
                while (true)
                {
                    if (x < jinja[j])
                    {
                        while (x > tera[t]) t++;
                        res[x] = CalcPoint(x, tera[t - 1], tera[t], jinja[j - 1], jinja[j]);
                        break;
                    }
                    if (x < tera[t])
                    {
                        while (x > jinja[j]) j++;
                        res[x] = CalcPoint(x, tera[t - 1], tera[t], jinja[j - 1], jinja[j]);
                        break;
                    }
                    j++; t++;
                }
            }
            return string.Join("\n", xs.Select(x => res[x].ToString()));
        }

        long CalcPoint(long x, long tera1, long tera2, long jinja1, long jinja2)
        {
            long v;
            v = x - jinja1 + Math.Abs(jinja1 - tera1);
            v = Math.Min(v, x - jinja1 + Math.Abs(jinja1 - tera2));
            v = Math.Min(v, x - tera1 + Math.Abs(jinja1 - tera1));
            v = Math.Min(v, x - tera1 + Math.Abs(jinja2 - tera1));
            v = Math.Min(v, tera2 - x + Math.Abs(jinja1 - tera2));
            v = Math.Min(v, tera2 - x + Math.Abs(jinja2 - tera2));
            v = Math.Min(v, jinja2 - x + Math.Abs(jinja2 - tera1));
            v = Math.Min(v, jinja2 - x + Math.Abs(jinja2 - tera2));
            return v;
        }


        const int mod = 1000000007;
        private readonly ConsoleReader consoleReader;
        public Program() : this(new ConsoleReader()) { }
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program().Calc());
    }

    public class ConsoleReader
    {
        public virtual string String() => Console.ReadLine();
        public int Int32() => int.Parse(String());
        public long Int64() => long.Parse(String());
        public ulong UInt64() => ulong.Parse(String());
        public double Double() => double.Parse(String());
        public string[] Split() => String().Split().ToArray();
        public int[] SplitInt32() => String().Split().Select(s => int.Parse(s)).ToArray();
        public long[] SplitInt64() => String().Split().Select(s => long.Parse(s)).ToArray();
        public ulong[] SplitUInt64() => String().Split().Select(s => ulong.Parse(s)).ToArray();
        public double[] SplitDouble() => String().Split().Select(s => double.Parse(s)).ToArray();

        public IEnumerable<string> Repeat(int count)
        {
            for (int i = 0; i < count; i++)
                yield return String();
        }
        public IEnumerable<int> RepeatInt32(int count)
        {
            for (int i = 0; i < count; i++)
                yield return Int32();
        }
        public IEnumerable<long> RepeatInt64(int count)
        {
            for (int i = 0; i < count; i++)
                yield return Int64();
        }
        public IEnumerable<string[]> RepeatSplit(int count)
        {
            for (int i = 0; i < count; i++)
                yield return Split();
        }
        public IEnumerable<int[]> RepeatSplitInt32(int count)
        {
            for (int i = 0; i < count; i++)
                yield return SplitInt32();
        }
    }
    static class DicExt
    {
        public static V GetOrDefault<K, V>(this Dictionary<K, V> dic, K key, V defaultValue = default(V))
        {
            V val;
            return dic.TryGetValue(key, out val) ? val : defaultValue;
        }
    }
}