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

public class Contest
{
	public static void Main()
	{
        int q;
		VT<int,int>[] lrl;
		MyIO.ReadMulti(out q);
		MyIO.ReadLines(q, out lrl);
 
		int n = 100000;
		bool[] p = PrimeTable(n);
		BinaryIndexedTree bit = new BinaryIndexedTree(n+1);
		List<int> test = new List<int>();
		for(int i=3;i<=n;i+=2){
			if(p[i]&&p[(i+1)/2]) bit.Add(i,1);
		}
		List<long> ans = new List<long>(); 
		foreach(var lr in lrl) ans.Add(bit.Sum(lr.Item2)-bit.Sum(lr.Item1-1));

		MyIO.WriteArray(test.ToArray());
		MyIO.WriteLines(ans.ToArray());

	}


    public static bool[] PrimeTable(int n) {
		bool[] e = Enumerable.Repeat(true, n+1).ToArray();
		if(n >= 0) e[0] = false;
		if(n >= 1) e[1] = false;
		for(int j = 4; j < e.Length; j += 2) e[j] = false;
		for(int j = 9; j < e.Length; j += 6) e[j] = false;
        long sqrt = (long)Math.Sqrt(n);
		for(int i = 5, g = 2; i <= sqrt; i += g, g ^= 6) {
			if(!e[i]) continue;
			for(int j = i * i; j < e.Length; j += i * 2) e[j] = false;
		}
		
		return e;
    }

public class BinaryIndexedTree {
	private long[] data;

	public BinaryIndexedTree(int sz) { data = new long[++sz]; }

	public long Sum(int k) {
		long ret = 0;
		for(++k; k > 0; k -= k & -k) ret += data[k];
		return (ret);
	}

	public void Add(int k, long x) { for(++k; k < data.Length; k += k & -k) data[k] += x; }
}

}













public struct VT<T, U>
{
	public T Item1; public U Item2;
    public VT(T a, U b) { Item1 = a; Item2 = b; }
}
public struct VT<T, U, V>
{
	public T Item1; public U Item2; public V Item3;
    public VT(T a, U b, V c) { Item1 = a; Item2 = b; Item3 = c; }
}
public struct VT<T, U, V, W>
{
	public T Item1; public U Item2; public V Item3; public W Item4;
    public VT(T a, U b, V c, W d) { Item1 = a; Item2 = b; Item3 = c; Item4 = d; }
}

public class Printer : System.IO.StreamWriter
{
	public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
	public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
}
public static class MyIO
{
	private static char[] splitter = new char[] {' '};
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) => eq<T, int>()    ? ct<T, int>(int.Parse(s))
	                                  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	                                  : eq<T, double>() ? ct<T, double>(double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
	                                  : eq<T, char>()   ? ct<T, char>(s[0])
	                                                    : ct<T, string>(s);				
	public static string[] ReadMulti<T>(out T a) {
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] ReadMulti<T, U>(out T a, out U b) {
		var ar = ReadMulti<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V>(out T a, out U b, out V c) {
		var ar = ReadMulti<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = ReadMulti<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = ReadMulti<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = ReadMulti<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
	public static string[] ReadArray<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
		a = ar.Select(cv<T>).ToArray();
		return ar;
	}		
	public static string[][] ReadLines<T>(int n, out T[] a) {
		a = new T[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadMulti(out a[i]);
		return arr;
	}
	public static string[][] ReadLines<T, U>(int n, out VT<T, U>[] a) {
		a = new VT<T, U>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p;
			arr[i] = ReadMulti(out o, out p);
			a[i] = new VT<T, U>(o, p);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V>(int n, out VT<T, U, V>[] a) {
		a = new VT<T, U, V>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q;
			arr[i] = ReadMulti(out o, out p, out q);
			a[i] = new VT<T, U, V>(o, p, q);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V, W>(int n, out VT<T, U, V, W>[] a) {
		a = new VT<T, U, V, W>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q; W r;
			arr[i] = ReadMulti(out o, out p, out q, out r);
			a[i] = new VT<T, U, V, W>(o, p, q, r);
		}
		return arr;
	}
	public static string[][] ReadMatrix<T>(int n, out T[][] a) {
		a = new T[n][];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadArray(out a[i]);
		return arr;
	}

	private static void wwp(Action act){
		var sw = new Printer(Console.OpenStandardOutput()){AutoFlush = false};
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	public static void WriteMulti(params object[] a) {
		wwp(()=>{
			foreach(object x in a) Console.Write("{0} ", x);
			Console.WriteLine();
		});
	}
	public static void WriteArray<T>(T[] a) {
		wwp(()=>{
			foreach(T x in a) Console.Write("{0} ", x);
			Console.WriteLine();
		});
	}
	public static void WriteLines<T>(T[] a) {
		wwp(()=>{
			foreach(T x in a) Console.WriteLine(x);
		});
	}



}





Measured memory_bytes: 34052