using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("1");
            WillReturn.Add("3 7");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            WillReturn.Add("13 13");
            WillReturn.Add("7 11");
            WillReturn.Add("7 11");
            WillReturn.Add("2017 2017");
            //1
            //0
            //0
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("6");
            WillReturn.Add("1 53");
            WillReturn.Add("13 91");
            WillReturn.Add("37 55");
            WillReturn.Add("19 51");
            WillReturn.Add("73 91");
            WillReturn.Add("13 49");
            //4
            //4
            //1
            //1
            //1
            //2
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int[] mSosuuArr;

    //エラトステネスの篩
    static void Eratosthenes(int pJyougen)
    {
        var IsSosuuArr = new System.Collections.BitArray(pJyougen + 1);

        for (int I = 2; I <= IsSosuuArr.Count - 1; I++) {
            IsSosuuArr[I] = true;
        }
        for (int I = 2; I * I <= IsSosuuArr.Count - 1; I++) {
            if (I != 2 && I % 2 == 0) continue;

            if (IsSosuuArr[I]) {
                for (int J = I * 2; J <= IsSosuuArr.Count - 1; J += I) {
                    IsSosuuArr[J] = false;
                }
            }
        }
        var SosuuList = new List<int>();
        for (int I = 2; I <= IsSosuuArr.Count - 1; I++)
            if (IsSosuuArr[I]) SosuuList.Add(I);

        mSosuuArr = SosuuList.ToArray();
    }

    struct IRInfoDef
    {
        internal int I;
        internal int R;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray();

        var IRList = new List<IRInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            IRInfoDef WillAdd;
            WillAdd.I = wkArr[0];
            WillAdd.R = wkArr[1];
            IRList.Add(WillAdd);
        }

        int RMax = IRList.Max(X => X.R);
        Eratosthenes(RMax);

        //累積和の配列
        int[] RuisekiwaArr = new int[RMax + 1];
        int Ruisekiwa = 0;
        for (int I = 0; I <= RuisekiwaArr.GetUpperBound(0); I++) {
            if (Is2017Like(I)) Ruisekiwa++;
            RuisekiwaArr[I] = Ruisekiwa;
        }

        foreach(IRInfoDef EachIRInfo in IRList){
            int FromInd = EachIRInfo.I - 1;
            int ToInd = EachIRInfo.R;
            Console.WriteLine(RuisekiwaArr[ToInd] - RuisekiwaArr[FromInd]);
        }
    }

    //2017Likeな数かを判定
    static bool Is2017Like(int pInt)
    {
        if (pInt % 2 == 0) return false;
        if (Array.BinarySearch(mSosuuArr, pInt) < 0) return false;
        if (Array.BinarySearch(mSosuuArr, (pInt + 1) / 2) < 0) return false;
        return true;
    }
}


Measured memory_bytes: 28856