#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define all(x) x.begin(), x.end()
using namespace std;
const int maxn = 1e6 + 1;
int D[maxn], prime[maxn], pre[maxn];
void Sieve() {
D[0] = 0; D[1] = 1;
for (int i = 2; i <= sqrt(maxn); ++i) {
for (int j = 2; j <= maxn / i; ++j) {
D[i * j]++;
}
}
for (int i = 2; i <= maxn; ++i) D[i] += 2;
}
void SangNT() {
prime[0] = prime[1] = 1;
for (int i = 2; i <= sqrt(maxn); ++i) {
if (prime[i] == 0) {
for (int j = i; j <= maxn / i; ++j) {
prime[i * j] = 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
SangNT();
Sieve();
for (int i = 1; i <= maxn; ++i) {
if (prime[D[i]] == 0) {
pre[i] = pre[i - 1] + 1;
}
else pre[i] = pre[i - 1];
}
int q, l, r;
cin >> q;
while (q--) {
cin >> l >> r;
cout << pre[r] - pre[l - 1] << endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSB1bGwgdW5zaWduZWQgbG9uZyBsb25nCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSwgeC5lbmQoKQp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgbWF4biA9IDFlNiArIDE7CmludCBEW21heG5dLCBwcmltZVttYXhuXSwgcHJlW21heG5dOwp2b2lkIFNpZXZlKCkgewogICAgRFswXSA9IDA7IERbMV0gPSAxOwogICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gc3FydChtYXhuKTsgKytpKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDI7IGogPD0gbWF4biAvIGk7ICsraikgewogICAgICAgICAgICBEW2kgKiBqXSsrOwogICAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IGkgPSAyOyBpIDw9IG1heG47ICsraSkgRFtpXSArPSAyOwp9CnZvaWQgU2FuZ05UKCkgewogICAgcHJpbWVbMF0gPSBwcmltZVsxXSA9IDE7CiAgICBmb3IgKGludCBpID0gMjsgaSA8PSBzcXJ0KG1heG4pOyArK2kpIHsKICAgICAgICBpZiAocHJpbWVbaV0gPT0gMCkgewogICAgICAgICAgICBmb3IgKGludCBqID0gaTsgaiA8PSBtYXhuIC8gaTsgKytqKSB7CiAgICAgICAgICAgICAgICBwcmltZVtpICogal0gPSAxOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICBTYW5nTlQoKTsKICAgIFNpZXZlKCk7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBtYXhuOyArK2kpIHsKICAgICAgICBpZiAocHJpbWVbRFtpXV0gPT0gMCkgewogICAgICAgICAgICBwcmVbaV0gPSBwcmVbaSAtIDFdICsgMTsKICAgICAgICB9CiAgICAgICAgZWxzZSBwcmVbaV0gPSBwcmVbaSAtIDFdOwogICAgfQoKICAgIGludCBxLCBsLCByOwogICAgY2luID4+IHE7CiAgICB3aGlsZSAocS0tKSB7CiAgICAgICAgY2luID4+IGwgPj4gcjsKICAgICAgICBjb3V0IDw8IHByZVtyXSAtIHByZVtsIC0gMV0gPDwgZW5kbDsKICAgIH0KCn0KCg==