#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef unsigned long long LL;
int prime[100];
const LL INF = (LL) 1e19;
LL memo[65][65][2];
vector <int> a;
LL dp (int index, int sum, int tight) {
LL &res = memo[index][sum][tight];
if (res == -1) {
res = 0;
if (index == a.size()) {
if (prime[sum])
res = 1;
} else {
if (tight) {
for (int i = 0; i <= a[index]; i++) {
if (i == a[index]) {
res += dp (index + 1, sum + i, tight);
} else {
res += dp (index + 1, sum + i, false);
}
}
} else {
for (int i = 0; i < 2; i++) {
res += dp (index + 1, sum + i, false);
}
}
}
}
return res;
}
LL solve (LL n) {
if (n == 0)
return 0;
a.clear();
while (n) {
a.push_back (n % 2);
n /= 2;
}
reverse (a.begin(), a.end());
memset (memo, -1, sizeof (memo));
LL ans = dp (0, 0, 1);
return ans;
}
LL digit_sum (LL n) {
LL ans = 0;
while (n) {
ans += n % 2;
n /= 2;
}
return ans;
}
LL brute (LL a, LL b) {
LL ans = 0;
for (LL i = a; i <= b; i++) {
if (prime [digit_sum (i)]) {
ans ++;
}
}
return ans;
}
int isPrime (int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
void pre () {
for (int i = 2; i < 100; i++) {
prime[i] = isPrime (i);
}
}
int main() {
pre();
LL a, b;
int T;
cin >> T;
assert (T >= 1 && T <= 100000);
while (T--) {
cin >> a >> b;
assert (a <= b);
assert (a >= 1 && a <= INF);
assert (b >= 1 && b <= INF);
LL ans = solve (b) - solve (a - 1);
/*
if (b - a <= 10000) {
LL ans1 = brute (a, b);
assert (ans == ans1);
}
*/
cout << ans << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE4gPSAxMDAwMTA7Cgp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyBMTDsKCmludCBwcmltZVsxMDBdOwpjb25zdCBMTCBJTkYgPSAoTEwpIDFlMTk7CgpMTCBtZW1vWzY1XVs2NV1bMl07CnZlY3RvciA8aW50PiBhOwoKTEwgZHAgKGludCBpbmRleCwgaW50IHN1bSwgaW50IHRpZ2h0KSB7CiAgICBMTCAmcmVzID0gbWVtb1tpbmRleF1bc3VtXVt0aWdodF07CiAgICBpZiAocmVzID09IC0xKSB7CiAgICAgICAgcmVzID0gMDsKICAgICAgICBpZiAoaW5kZXggPT0gYS5zaXplKCkpIHsKICAgICAgICAgICAgaWYgKHByaW1lW3N1bV0pCiAgICAgICAgICAgICAgICByZXMgPSAxOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGlmICh0aWdodCkgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gYVtpbmRleF07IGkrKykgewogICAgICAgICAgICAgICAgICAgIGlmIChpID09IGFbaW5kZXhdKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHJlcyArPSBkcCAoaW5kZXggKyAxLCBzdW0gKyBpLCB0aWdodCk7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgcmVzICs9IGRwIChpbmRleCArIDEsIHN1bSArIGksIGZhbHNlKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IDI7IGkrKykgewogICAgICAgICAgICAgICAgICAgIHJlcyArPSBkcCAoaW5kZXggKyAxLCBzdW0gKyBpLCBmYWxzZSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIHJlczsKfQoKTEwgc29sdmUgKExMIG4pIHsKICAgIGlmIChuID09IDApCiAgICAgICAgcmV0dXJuIDA7CgogICAgYS5jbGVhcigpOwogICAgd2hpbGUgKG4pIHsKICAgICAgICBhLnB1c2hfYmFjayAobiAlIDIpOwogICAgICAgIG4gLz0gMjsKICAgIH0KICAgIHJldmVyc2UgKGEuYmVnaW4oKSwgYS5lbmQoKSk7CgogICAgbWVtc2V0IChtZW1vLCAtMSwgc2l6ZW9mIChtZW1vKSk7CiAgICBMTCBhbnMgPSBkcCAoMCwgMCwgMSk7CiAgICByZXR1cm4gYW5zOwp9CgpMTCBkaWdpdF9zdW0gKExMIG4pIHsKICAgIExMIGFucyA9IDA7CiAgICB3aGlsZSAobikgewogICAgICAgIGFucyArPSBuICUgMjsKICAgICAgICBuIC89IDI7CiAgICB9CiAgICByZXR1cm4gYW5zOwp9CgpMTCBicnV0ZSAoTEwgYSwgTEwgYikgewogICAgTEwgYW5zID0gMDsKICAgIGZvciAoTEwgaSA9IGE7IGkgPD0gYjsgaSsrKSB7CiAgICAgICAgaWYgKHByaW1lIFtkaWdpdF9zdW0gKGkpXSkgewogICAgICAgICAgICBhbnMgKys7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGFuczsKfQoKaW50IGlzUHJpbWUgKGludCBuKSB7CiAgICBmb3IgKGludCBpID0gMjsgaSAqIGkgPD0gbjsgaSsrKQogICAgICAgIGlmIChuICUgaSA9PSAwKQogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICByZXR1cm4gdHJ1ZTsKfQoKdm9pZCBwcmUgKCkgewogICAgZm9yIChpbnQgaSA9IDI7IGkgPCAxMDA7IGkrKykgewogICAgICAgIHByaW1lW2ldID0gaXNQcmltZSAoaSk7CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgcHJlKCk7CiAgICBMTCBhLCBiOwogICAgaW50IFQ7CiAgICBjaW4gPj4gVDsKICAgIGFzc2VydCAoVCA+PSAxICYmIFQgPD0gMTAwMDAwKTsKICAgIHdoaWxlIChULS0pIHsKICAgICAgICBjaW4gPj4gYSA+PiBiOwogICAgICAgIGFzc2VydCAoYSA8PSBiKTsKICAgICAgICBhc3NlcnQgKGEgPj0gMSAmJiBhIDw9IElORik7CiAgICAgICAgYXNzZXJ0IChiID49IDEgJiYgYiA8PSBJTkYpOwoKICAgICAgICBMTCBhbnMgPSBzb2x2ZSAoYikgLSBzb2x2ZSAoYSAtIDEpOwogICAgICAgIC8qCiAgICAgICAgaWYgKGIgLSBhIDw9IDEwMDAwKSB7CiAgICAgICAgICAgIExMIGFuczEgPSBicnV0ZSAoYSwgYik7CiAgICAgICAgICAgIGFzc2VydCAoYW5zID09IGFuczEpOwogICAgICAgIH0KICAgICAgICAqLwogICAgICAgIGNvdXQgPDwgYW5zIDw8IGVuZGw7CiAgICB9CiAgICByZXR1cm4gMDsKfQoK