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

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

enum lim = 2 * 10^^5;

void main() {
    auto s = eratos(lim);
    auto t = new bool[](lim);
    foreach (i ; 3 .. lim) {
        if (s[i] && s[(i + 1) / 2]) {
            t[i] = true;
        }
    }

    auto ts = new int[](lim + 1);
    foreach (i ; 1 .. lim + 1) {
        ts[i] = ts[i - 1] + t[i - 1];
    }

    int q;
    scan(q);

    foreach (i ; 0 .. q) {
        int li, ri;
        scan(li, ri);
        ri++;
        int ans = ts[ri] - ts[li];
        writeln(ans);
    }
}

bool[] eratos(int n) {
    auto s = new bool[](n);
    s[] = true;
    s[0] = s[1] = false;

    for (int p = 2; p*p < n; p++) {
        if (s[p]) {
            for (int d = p*p; d < n; d += p) {
                s[d] = false;
            }
        }
    }

    return s;
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    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);
        }
    }
}
