program D_solution

implicit none
integer :: Q,l(10**5),r(10**5)
integer :: i,j
logical :: is_prime(10**5)
integer :: like_2017(0:50001)

read(*,*) Q
do i = 1, Q
  read(*,*) l(i),r(i)
enddo

is_prime(1) = .false.

do i = 2, 10**5
  is_prime(i) = .true.
  do j = 2, int((i)**0.5)
    if(mod(i,j) == 0) then
      is_prime(i) = .false.
    endif
  enddo
enddo

like_2017 = 0

do i = 1, 10**5, 2
    if(is_prime(i) .and. is_prime(int((i+1)/2))) then
      like_2017(int((i+1)/2):) = like_2017(int((i+1)/2):) + 1
    endif
enddo

do i=1,Q
    write(*,*) like_2017((r(i)+1)/2) - like_2017((l(i)-1)/2)
enddo
end program