program D_solution

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

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

is_prime(1) = 0

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

like_2017 = 0

do i = 1, 10**5, 2
  like_2017((i+1)/2) = is_prime(i) * is_prime((i+1)/2)
enddo

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