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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		SC sc = new SC(System.in);
		int A=sc.nextInt();
		int B=sc.nextInt();
		int Q=sc.nextInt();
		long[] jinjya=new long[A];
		long[] tera=new long[B];
		for(int i=0; i<A; i++) {
			jinjya[i]=sc.nextLong();
		}
		for(int i=0; i<B; i++) {
			tera[i]=sc.nextLong();
		}
		Arrays.sort(jinjya);
		Arrays.sort(tera);
		for(int i=0; i<Q; i++) {
			long ans=11451419810364364L;
			long start=sc.nextLong();
			long jinjya_higasi=lowerBound(jinjya,start);
			long jinjya_nisi=-114514810364364L;
			long tera_higasi=lowerBound(tera,start);
			long tera_nisi=-114514810364364L;
			if(jinjya_higasi>0 && jinjya_higasi!=-1) {
				int a=(int)jinjya_higasi;
				jinjya_higasi=jinjya[a];
				jinjya_nisi=jinjya[a-1];
			}
			else if(jinjya_higasi==0){
				int a=(int)jinjya_higasi;
				jinjya_higasi=jinjya[a];
			}
			else if(jinjya_higasi==-1) {
				jinjya_nisi=jinjya[A-1];
				jinjya_higasi=114514810364364L;
			}

			if(tera_higasi>0 && tera_higasi!=-1) {
				int a=(int)tera_higasi;
				tera_higasi=tera[a];
				tera_nisi=tera[a-1];
			}
			else if(tera_higasi==0){
				tera_higasi=tera[0];
			}
			else if(tera_higasi==-1) {
				tera_nisi=tera[B-1];
				tera_higasi=114514810364364L;
			}

			//pl("寺西"+tera_nisi);
			//pl("寺東"+tera_higasi);
			//pl("神社西"+jinjya_nisi);
			//pl("神社東"+jinjya_higasi);
			//神社に→　寺に→
			long tmp=0;
			if(tera_higasi>100000000000L || jinjya_nisi>100000000000L) {
				//
			}
			else {
				tmp=Math.max(tera_higasi-start, jinjya_higasi-start);
				ans=Math.min(tmp, ans);
			}


			//神社→　寺←
			if(jinjya_higasi>100000000000L || tera_nisi<0) {
				//
			}
			else {
				tmp=jinjya_higasi-start+jinjya_higasi-tera_nisi;
				ans=Math.min(tmp, ans);
			}


			//寺←　神社→

			if(tera_nisi<0 || jinjya_higasi>100000000000L) {
				//
			}
			else {
				tmp=start-tera_nisi+jinjya_higasi-tera_nisi;
				ans=Math.min(tmp, ans);
			}


			//神社←　寺→
			if(jinjya_nisi<0 || tera_higasi>100000000000L) {
				//
			}
			else {
				tmp=start-jinjya_nisi+tera_higasi-jinjya_nisi;
				ans=Math.min(tmp, ans);
			}


			//寺→　神社←　
			if(tera_higasi>100000000000L || jinjya_nisi<0) {
				//
			}
			else {
				tmp=tera_higasi-start+tera_higasi-jinjya_nisi;
				ans=Math.min(tmp, ans);
			}


			//神社→寺←
			if(jinjya_higasi>100000000000L || tera_nisi<0) {
				//
			}
			else {
				tmp=jinjya_higasi-start+jinjya_higasi-tera_nisi;
				ans=Math.min(tmp, ans);
			}


			//寺←　神社→
			if(tera_nisi<0 || jinjya_higasi>100000000000L) {
				//
			}
			else {
				tmp=start-tera_nisi+jinjya_higasi-start;
				ans=Math.min(tmp, ans);
			}


			//神社左寺左
			if(tera_nisi<0 || jinjya_nisi<0) {
				//
			}
			else {
				tmp=Math.max(start-tera_nisi, start-jinjya_nisi);
				ans=Math.min(tmp, ans);
			}

			pl(ans);
			//pl();
		}
	}
	static class SC {
		private BufferedReader reader = null;
		private StringTokenizer tokenizer = null;
		public SC(InputStream in) {
			reader = new BufferedReader(new InputStreamReader(in));
		}
		public String next() {
			if (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new UncheckedIOException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}
	}
	public static void pl(Object o) {
		System.out.println(o);
	}
	public static void pl() {
		System.out.println();
	}
	public static void p(Object o) {
		System.out.print(o);
	}
	public static final int lowerBound(final long[] arr, final long value) {
		int low = 0;
		int high = arr.length;
		int mid;
		while (low < high) {
			mid = ((high - low) >>> 1) + low;    //(low + high) / 2 (オーバーフロー対策)
			if (arr[mid] < value) {
				low = mid + 1;
			} else {
				high = mid;
			}
		}
		if(low<arr.length) {
			return low;
		}
		else {
			return -1;
		}
	}
}