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

void scan(T...)(ref T args) {
    auto 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);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}


enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;

bool isPrime(int N) {
    if (N <= 1) return false;
    for (int i = 2; i*i <= N; i++) {
        if (N % i == 0) return false;
    }
    return true;
}

enum lim = 10^^5 + 1;

void main() {
    int Q;
    scan(Q);

    auto b = new bool[](lim);
    foreach (i ; 1 .. lim) {
        b[i] = (i % 2) && isPrime(i) && isPrime((i + 1) / 2);
    }
    auto c = new int[](lim + 1);
    foreach (i ; 1 .. lim + 1) {
        c[i] = c[i - 1] + b[i - 1];
    }

    foreach (_ ; 0 .. Q) {
        int li, ri;
        scan(li, ri);
        auto ans = c[ri + 1] - c[li - 1];
        writeln(ans);
    }
}
