import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;


void main() {
    auto s = new bool[](10^^5 + 1);
    s[] = 1;
    s[0] = s[1] = 0;

    for (int p = 2; p*p <= 10^^5; p++) {
        if (s[p]) {
            for (int q = p*p; q <= 10^^5; q += p) {
                s[q] = 0;
            }
        }
    }

    auto t = new int[](10^^5 + 1);

    foreach (i ; 3 .. 10^^5 + 1) {
        if (s[i] && s[(i + 1) / 2]) {
            t[i] = 1;
        }
    }

    foreach (i ; 1 .. 10^^5 + 1) {
        t[i] += t[i-1];
    }

    int q;
    int li,ri;

    scan(q);

    foreach (i ; 0 .. q) {
        scan(li, ri);
        writeln(t[ri] - t[li-1]);
    }
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}