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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <fstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <random>
#include <numeric>

using namespace std;

#define INT_MAX_VALUE 2147483647
#define LONG_LONG_MAX_VALUE 9223372036854775807
#define ll long long
#define ld long double

struct XX{
    int b;
    ll c;
};

class xxGreater {
public:
    bool operator()(const XX& riLeft, const XX& riRight) const {
        //第2条件
        if((riLeft.c) == (riRight.c)){
            return riLeft.b > riRight.b;//<:昇順(小さいものから順番)、>:降順(大きいものから順番)
            //プライオリティキューの場合は > で、top()すると値の小さいものがとれる
        }
        //第1条件
        return (riLeft.c) > (riRight.c);
    }
};


//map<long long,long long> prime_f(long long n){
//    map<long long,long long>res;
//    for(int i=2;i*i<=n;i++){
//        while(n%i==0){
//            ++res[i];
//            n/=i;
//        }
//    }
//    if(n!=1)res[n]=1;
//    return res;
//}

//int n;
////int dat[2*10000000];
////int dat2[2*10000000];
//int dat[10];
//int dat2[10];
//
//void init(int n_){
//    n=1;
//    while(n<n_)n*=2;
//    for(int i=0;i<2*n-1;i++){
//        dat[i]=0;
//        dat2[i]=0;
//    }
//}
//
//void initset(int k,int a){
//    k+=n-1;
//    dat[k]=a;
//    while(k>0){
//        k=(k-1)/2;
//        dat[k]=dat[k*2+1]+dat[k*2+2];
//    }
//}
//
////[a,b)の間を[l,r]区間で比較しアップデート
////引数のindexに注意
////nは固定。initで計算すみ
////update2(L[i],R[i]+1,0,0,n,D[i]);
//void update2(int a,int b,int k,int l,int r,int v){//v更新値、区間は0-index
//    if(r<=a || b<=l)return;
//    if(a<=l && r<=b){
//        dat[k]+=dat2[k];
//        if(r-l>1){
//            dat2[k*2+1]+=dat2[k]/2;
//            dat2[k*2+1]+=dat2[k]/2;
//        }
//        dat2[k]=v*(r-l);
//        return;
//    }else{
//        update2(a,b,k*2+1,l,(l+r)/2,v);
//        update2(a,b,k*2+2,(l+r)/2,r,v);
//        return;
//    }
//}
//
//int query(int a,int b,int k,int l,int r){
//    if(r<=a || b<=l)return 0;
//    if(a<=l && r<=b){
//        dat[k]+=dat2[k];
//        if(r-l>1){
//            dat2[k*2+1]+=dat2[k]/2;
//            dat2[k*2+1]+=dat2[k]/2;
//        }
//        dat2[k]=0;
//        return dat[k];
//    }
//    else{
//        int vl=query(a,b,k*2+1,l,(l+r)/2);
//        int vr=query(a,b,k*2+2,(l+r)/2,r);
//        return vl+vr;
//    }
//}


int main(int argc, const char * argv[])
{
    //scanf("%s",S);
    //scanf("%d",&N);
    //scanf("%lld %lld",&target1,&target2);
    //sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]);
    //getline(cin, target);
    ifstream ifs("a.txt");//テスト用
    //ifs >> a;
    //ここから
    
    //入力高速化
    ios::sync_with_stdio(false);
    ifs.tie(0);
    
    int A,B,Q;
    ifs>>A>>B>>Q;
    ll a[100000];
    ll b[100000];

    for(int i=0;i<A;i++){
        ifs>>a[i];
    }
    a[A]=10000000000;

    for(int i=0;i<B;i++){
        ifs>>b[i];
    }
    b[B]=10000000000;
    ll q[100000];
    for(int i=0;i<Q;i++){
        ifs>>q[i];
    }
    for(int i=0;i<Q;i++){
        auto it1=lower_bound(a,a+A,q[i]);
        auto it2=lower_bound(b,b+B,q[i]);
        if(*it1==a[0]){
            if(*it2==b[0]){
                cout << max(a[0]-q[i],b[0]-q[i]) << endl;
                continue;
            }else if(*it2==b[B]){
                cout << min(abs(a[0]-b[B-1])+a[0]-q[i],abs(a[0]-b[B-1])+q[i]-b[B-1]) << endl;
                continue;
            }else{
                ll tmp=min(abs(a[0]-q[i]),abs(q[i]-*(it2-1)));
                cout << min(max(*it1-q[i],*it2-q[i]),*it1-q[i]+q[i]-*(it2-1)+tmp) << endl;
                continue;
            }
        }else if(*it1==a[A]){
            if(*it2==b[0]){
                cout << min(abs(a[A-1]-b[0])+b[0]-q[i],abs(a[A-1]-b[0])+q[i]-a[A-1]) << endl;
                continue;
            }else if(*it2==b[B]){
                cout << max(q[i]-a[A-1],q[i]-b[B-1]) << endl;
                continue;
            }else{
                ll tmp=min(abs(a[A-1]-q[i]),abs(q[i]-*(it2)));
                cout << min(max(q[i]-a[A-1],q[i]-*(it2-1)),q[i]-a[A-1]+*it2-q[i]+tmp) << endl;
                continue;
            }
        }
        if(*it2==b[0]){
            ll tmp=min(abs(*it2-q[i]),abs(q[i]-*(it1-1)));
            cout << min(max(*it2-q[i],*it1-q[i]),*it2-q[i]+q[i]-*(it1-1)+tmp) << endl;
        }else if(*it2==b[B]){
            ll tmp=min(abs(q[i]-*it1),abs(q[i]-*(it2-1)));
            cout << min(max(q[i]-b[B-1],q[i]-*(it1-1)),*it1-q[i]+q[i]-b[B-1]+tmp) << endl;
        }else{
            //q A B
            ll ta=max(abs(q[i]-*(it1)),abs(q[i]-*(it2)));
            //A B q
            ta=min(ta,max(abs(q[i]-*(it1-1)),abs(q[i]-*(it2-1))));
            //A q B
            ll tmp=min(abs(q[i]-*(it1-1)),abs(q[i]-*(it2)));
            ta=min(ta,abs(q[i]-*(it1-1))+abs(q[i]-*(it2))+tmp);
            //B q A
            tmp=min(abs(q[i]-*(it1)),abs(q[i]-*(it2-1)));
            ta=min(ta,abs(q[i]-*(it1))+abs(q[i]-*(it2-1))+tmp);
            cout << ta << endl;
        }
    }
    //ここまで
    //cout << "ans" << endl;
    //printf("%.0f\n",ans);//小数点以下表示なし
    //printf("%.7f\n",p);
    
    return 0;
}