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: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			D();
		}

		public static void D()
		{
			Scanner cin = new Scanner();
			int a = cin.NextInt();
			int b = cin.NextInt();
			int q = cin.NextInt();
			long[] s = new long[a];
			for (int n = 0; n < a; ++n) {
				s[n] = cin.NextInt();
			}

			long[] t = new long[b];
			for (int n = 0; n < b; ++n) {
				t[n] = cin.NextInt();
			}

			long[] x = new long[q];
			for (int n = 0; n < q; ++n) {
				x[n] = cin.NextInt();
			}

			for (int n = 0; n < q; ++n) {
				long start = x[n];
				
				if (s[0] > start) {
					if (t[0] > start) {
						Console.WriteLine(Math.Max(t[0] - start, s[0] - start));
					} else if (t[b - 1] < start) {
						Console.WriteLine(Math.Min(
							s[0] - start * 2 + start - t[b - 1],
							start - t[b - 1] * 2 + s[0] - start));
					} else {
						int indexT = Array.IndexOf(t, t.First(h => h >= start));
						long t1 = t[indexT];
						long t0 = t[indexT - 1];

						long[] answers = new long[2];

						{
							answers[0] = Math.Abs(start - t0)
								+ Math.Abs(s[0] - t0);
						}

						{
							answers[1] = Math.Abs(start - t1)
								+ Math.Abs(s[0] - t1);
						}

						Console.WriteLine(answers.Min());
					}
				} else if (t[0] > start) {
					if (s[a - 1] < start) {
						Console.WriteLine(Math.Min(
							t[0] - start * 2 + start - s[b - 1],
							start - s[b - 1] * 2 + t[0] - start));
					} else {
						int indexS = Array.IndexOf(s, s.First(h => h >= start));
						long s1 = s[indexS];
						long s0 = s[indexS - 1];
						long[] answers = new long[2];

						{
							answers[0] = Math.Abs(start - s0)
								+ Math.Abs(t[0] - s0);
						}

						{
							answers[1] = Math.Abs(start - s1)
								+ Math.Abs(t[0] - s1);
						}

						Console.WriteLine(answers.Min());
					}
				} else if (s[a - 1] < start) {
					if (t[b - 1] < start) {
						Console.WriteLine(Math.Max(start - t.Last(), start - s.Last()));
					} else {
						int indexT = Array.IndexOf(t, t.First(h => h >= start));
						long t1 = t[indexT];
						long t0 = t[indexT - 1];

						long[] answers = new long[2];

						{
							answers[0] = Math.Abs(start - t0)
								+ Math.Abs(s.Last() - t0);
						}

						{
							answers[1] = Math.Abs(start - t1)
								+ Math.Abs(s.Last() - t1);
						}

						Console.WriteLine(answers.Min());
					}
				} else if (t[b - 1] < start) {
					int indexS = Array.IndexOf(s, s.First(h => h >= start));
					long s1 = s[indexS];
					long s0 = s[indexS - 1];
					long[] answers = new long[2];

					{
						answers[0] = Math.Abs(start - s0)
							+ Math.Abs(t[b - 1] - s0);
					}

					{
						answers[1] = Math.Abs(start - s1)
							+ Math.Abs(t[b - 1] - s1);
					}

					Console.WriteLine(answers.Min());
				} else {
					long[] answers = new long[4];
					int indexT = Array.IndexOf(t, t.First(h => h >= start));
					int indexS = Array.IndexOf(s, s.First(h => h >= start));
					long t1 = t[indexT];
					long t0 = t[indexT - 1];
					long s1 = s[indexS];
					long s0 = s[indexS - 1];
					{
						int it2 = Array.IndexOf(t, t.First(h => h >= s1));
						answers[0] = Math.Min(
							s1 - start + t[it2] - s1,
							s1 - start + s1 - t[it2 - 1]);
					}

					{
						int it2 = Array.IndexOf(t, t.First(h => h >= s0));
						answers[1] = Math.Min(
							start - s0 + t[it2] - s1,
							start - s0 + s1 - t[it2 - 1]);
					}

					{
						int is2 = Array.IndexOf(s, s.First(h => h >= t1));
						answers[2] = Math.Min(
							t1 - start + s[is2] - t1,
							t1 - start + t1 - s[is2 - 1]);
					}

					{
						int is2 = Array.IndexOf(s, s.First(h => h >= t0));
						answers[3] = Math.Min(
							start - t0 + s[is2] - t1,
							start - t0 + t1 - s[is2 - 1]);
					}

					Console.WriteLine(answers.Min());
				}
			}
		}

		
	}

	public class Scanner
	{
		string[] s;
		int i;

		char[] cs = new char[] { ' ' };

		public Scanner()
		{
			s = new string[0];
			i = 0;
		}

		public string Next()
		{
			if (i < s.Length) return s[i++];
			string st = Console.ReadLine();
			while (st == "") st = Console.ReadLine();
			s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
			if (s.Length == 0) return Next();
			i = 0;
			return s[i++];
		}

		public int NextInt()
		{
			return int.Parse(Next());
		}
		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = NextInt() + add;
			}
			return Array;
		}

		public long NextLong()
		{
			return long.Parse(Next());
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = NextLong() + add;
			}
			return Array;
		}

		public double NextDouble()
		{
			return double.Parse(Next());
		}


		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = NextDouble() + add;
			}
			return Array;
		}
	}
}