import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;


void main() {
    auto PrimeList = () {
        enum N = 100_000;
        auto primeList = new bool[N+1];
        auto list = iota(2, N+1).array;
        while (list.front * list.front <= N) {
            primeList[list.front] = true;
            list = list[1..$].filter!(x => x % list.front).array;
        }
        foreach (x; list) {
            primeList[x] = true;
        }
        return primeList;
    }();
    auto Q = readln.chomp.to!int;
    int[][] ranges;
    foreach (i; 0..Q) {
        auto range = readln.split.to!(int[]);
        ranges ~= range;
    }
    foreach (range; ranges) {
        iota(range[0], range[1]+1).filter!(x => PrimeList[x] && PrimeList[(x+1)/2]).walkLength.writeln;
    }
}
 