program main
  implicit none
  integer :: A,B,Q
  integer(8),allocatable :: S(:)
  integer(8),allocatable :: T(:)
  integer(8) :: xtmp
  integer(8) :: xtmp_loc(1)
  integer(8) :: cand(2)
  integer(8),allocatable :: x(:)
  integer :: i,j
  integer :: is,ie
  read(*,*) A,B,Q
  allocate(S(A),T(B),x(Q))
  do i = 1,A
     read(*,*) S(i)
  end do
  do i = 1,B
     read(*,*) T(i)
  end do
  do i = 1,Q
     read(*,*) X(i)
  end do

  do i = 1,Q
     ! first visit T
     cand(1)=0
     xtmp=X(i)

     call near(is,ie,B,T,xtmp)
     cand(1) = cand(1) + minval( abs(T(is:ie)-xtmp))
     xtmp_loc = T( minloc( abs(T(is:ie)-xtmp)) )
     xtmp = xtmp_loc(1)
     call near(is,ie,A,S,xtmp)
     cand(1) = cand(1) + minval( abs(S(is:ie)-xtmp))

     ! first visit S
     cand(2)=0
     xtmp=X(i)
     call near(is,ie,A,S,xtmp)
     cand(2) = cand(2) + minval( abs(S(is:ie)-xtmp))
     xtmp_loc = S( minloc( abs(S(is:ie)-xtmp)) )
     xtmp = xtmp_loc(1)
     call near(is,ie,B,T,xtmp)
     cand(2) = cand(2) + minval( abs(T(:)-xtmp))
     write(*,'(i0)') minval(cand)
  end do
  stop
contains
  subroutine near(is,ie,B,T,xtmp)
    integer,intent(out) :: is,ie
    integer(8) :: xtmp
    integer  :: B
    integer(8)  :: T(B)
    is = 1 ; ie = B
    if ( B/2 /= 1 ) then
       if (T(B/2) < xtmp ) then
          is = B/2 ; ie = B
       else
          is = 1 ; ie = B/2
       end if
    else
       return
    end if
!@     if ( T(B/8*7) < xtmp  ) then
!@        is = B/8*7 ; ie =B
!@     elseif( T(B/4*3)<xtmp .and. xtmp <= T(B/8*7) ) then
!@        is = B/4*3 ; ie =B/8*7
!@     elseif( T(B/2) < xtmp .and. xtmp <= T(B/4*3) ) then
!@        is = B/2 ; ie = B/4*3
!@     elseif(   xtmp <= T(B/4)) then
!@        is = 1; ie =B/4
!@     elseif(   xtmp <= T(B/2) .and. T(B/4) < xtmp)then
!@        is = B/4 ; ie =B/2
!@     end if
    return
  end subroutine near
end program main
