import qualified Data.Map as M

isP::Int->Bool
isP n 
   |n<2 =False
   |n==2 =True
   |n `mod` 2==0 =False
   |otherwise = try n 3
   where
        try :: Int->Int->Bool
        try m i
           |i*i > m=True
           |m `mod` i==0 =False
           |otherwise=try m (i+2)

f3::Maybe Int->Int
f3 (Just x)=x
f3 Nothing=0

f2::M.Map Int Int->[String]->[Int]
f2 _ [] = []
f2 ms (x:xs) =let (a:b:[])=map read (words x)::[Int]
                  c1=f3 (M.lookup (a-1) ms)
                  c2=f3 (M.lookup (b) ms)
              in c2-c1:f2 ms xs

main =do
      e <- getLine
      es <- getContents 
      let ms=tail $ scanl (\acc x-> acc + if isP x && isP ((x+1) `div` 2) then 1 else 0) 0 [0..100000]
          ms2=zip [0..] ms
          ms3=M.fromList ms2
      mapM_ putStrLn $ map show $ f2 ms3 (lines es)