program main
  implicit none
  integer :: A,B,Q
  integer(8),allocatable :: S(:),S2(:)
  integer(8),allocatable :: T(:),T2(:)
  integer(8) :: xtmp
  integer(8) :: xtmp_loc(1)
  integer(8) :: cand(2)
  integer(8),allocatable :: x(:)
  integer(8),allocatable :: sorted(:)
  integer :: mindex
  logical :: flag
  integer(8) :: A1,A2,AA
  integer :: i,j
  integer :: is,ie
  read(*,*) A,B,Q
  allocate(S(A),T(B),x(Q))
  allocate(S2(A),T2(B))
  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)
     ! T(1) < T(2)
     A1=T(1) - xtmp
     if (A1 >0 ) then
        mindex = 1
        goto 990
     end if
     A2=T(B) - xtmp
     if (A2 < 0 ) then
        mindex = B
        goto 990
     end if
     call near(mindex,B,T,xtmp,A1,A2)
990     xtmp_loc = T( mindex )
     cand(1) = cand(1) + abs(xtmp - xtmp_loc(1))
     xtmp = xtmp_loc(1)
     !
     A1=S(1) - xtmp
     if (A1 >0 ) then
        mindex = 1
        goto 991
     end if
     A2=S(A) - xtmp
     if (A2 < 0 ) then
        mindex = A
        goto 991
     end if
     call near(mindex,A,S,xtmp,A1,A2)
991  cand(1) = cand(1) + abs( S(mindex)-xtmp )

     ! first visit S
     cand(2)=0
     xtmp=X(i)
     A1=S(1) - xtmp
     if (A1 >0 ) then
        mindex = 1
        goto 992
     end if
     A2=S(A) - xtmp
     if (A2 < 0 ) then
        mindex = A
        goto 992
     end if
     call near(mindex,A,S,xtmp,A1,A2)
992  xtmp_loc = S( mindex )
     !
     cand(2) = cand(2) + abs(xtmp - xtmp_loc(1))
     xtmp = xtmp_loc(1)
     A1=T(1) - xtmp
     if (A1 >0 ) then
        mindex = 1
        goto 993
     end if
     A2=T(B) - xtmp
     if (A2 < 0 ) then
        mindex = B
        goto 993
     end if
     call near(mindex,B,T,xtmp,A1,A2)
993  cand(2) = cand(2) + abs( T(mindex) -xtmp)
     write(*,'(i0)') minval(cand)
  end do
  stop
contains
  subroutine near(mindex,B,T,xtmp,A1,A2)
    integer,intent(out) :: mindex
    integer,intent(in)  :: B
    integer(8),intent(in)   :: T(B)
    integer(8),intent(in)  :: xtmp
    integer(8),intent(in) :: A1,A2
    integer :: j
     if ( abs(A1 )< abs(A2) )then
        ! near A1
        do j = 1,B
           AA = T(j) -xtmp
           if ( AA > 0) then
              if (abs(T(j) -xtmp) > abs(T(j-1)-xtmp)) then
                 mindex = j-1
              else
                 mindex = j
              end if
              exit
           end if
        end do
     else
        do j = B,1,-1
           AA = T(j) -xtmp
           if ( AA < 0) then
              if (abs(T(j) -xtmp) > abs(T(j+1)-xtmp)) then
                 mindex = j+1
              else
                 mindex = j
              end if
              exit
           end if
        end do
     end if
    return
  end subroutine near
end program main
