module Sort_Module
    implicit none
    public
    contains
    recursive subroutine mergesort(a, p, r, b)
        integer(8) a(:), b(:)
        integer(8) p, r
        integer(8) q
        if (p < r) then
            q = (p+r)/2
            call mergesort(a, p, q, b)
            call mergesort(a, q+1, r, b)
            call merge(a, p, q, r, b)
        end if
    end subroutine mergesort
    subroutine merge(a, p, q, r, b)
        integer(8) a(:), b(:)
        integer(8) p, r, q, i, j, k
        integer(8) t
        k = p;   i = p;   j = q+1
        do
            if (.not. (k <= r)) exit
            if (j > r) then
                t = a(i);   i = i+1
            else if (i > q) then
                t = a(j);   j = j+1
            else if (a(i) <= a(j)) then
                t = a(i);   i = i+1
            else
                t = a(j);   j = j+1
            end if
            b(k) = t;   k = k+1
        end do
        do i = p, r
            a(i) = b(i)  
        end do
    end subroutine merge
    subroutine sort(a)
      integer(8) a(:),b(ubound(a,1)-size(a)+1:ubound(a,1)),n,k
      n = ubound(a,1)
      k = ubound(a,1)-size(a)+1
      call mergesort(a,k,n,b)
    end subroutine sort
    function POSL(a,v) result(r)!find first i:a(i)>=v
        implicit none
        integer(8) :: a(:),l,r,idx,v
        r = ubound(a,1) + 1
        l = ubound(a,1)-size(a)
        do while(r-l>1)
            idx = l + (r-l)/2
            if(a(idx) >= v)then
                r = idx
            else
                l = idx
            endif
        enddo
    end function
    function POSU(a,v) result(r)!find first i:a(i)>v
        implicit none
        integer(8) :: a(:),l,r,idx,v
        r = ubound(a,1) + 1
        l = ubound(a,1)-size(a)
        do while(r-l>1)
            idx = l + (r-l)/2
            if(a(idx) > v)then
                r = idx
            else
                l = idx
            endif
        enddo
    end function
    function NUM(a,v) result(n)
        implicit none
        integer(8) :: a(:),v,n
        n = POSU(a,v) - POSL(a,v)
    end function
end module Sort_Module
  

program atcoder
    use Sort_Module
    implicit none
    integer(8) :: A,B,Q,x,s01(0:1),t01(0:1),i,ans,j,k;
    integer(8),allocatable,dimension(:) :: s,t;
    read*,A,B,Q
    allocate(s(0:A+1))
    allocate(t(0:B+1))
    read(*,*)(s(i),i=1,A)
    read(*,*)(t(i),i=1,B)
    s(0) = -10000000000000000_8
    t(0) = -10000000000000000_8
    s(A+1) = 10000000000000000_8
    t(B+1) = 10000000000000000_8
    do i=1,Q
        read*,x
        do j=0,1
            s01(j) = s( POSL(s,x) - j - 1 );
            t01(j) = t( POSL(t,x) - j - 1 );
        enddo
        ans = s(A+1)
        do j=0,1
            do k=0,1
                ans = min( ans, minlen(s01(j),t01(k),x) )
            enddo
        enddo
        write(*,'(I0)')ans
    enddo
    stop 
contains

function minlen(s,t,x)
    integer(8) minlen,s,t,x
    if(s>=x .and. t>=x)then
        minlen = max(abs(s-x),abs(t-x))
    else if(s<=x .and. t<=x)then
        minlen = max(abs(s-x),abs(t-x))
    else
        minlen = max(abs(s-x),abs(t-x)) + min(abs(s-x),abs(t-x))*2_8
    endif
end function minlen

end program atcoder

Measured memory_bytes: 3456