#include <bits/stdc++.h>#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using i64 = long long;
const int N = 1e6 + 5;
int sum[N];
int num[N];
vector<int>isp(N,1);
vector<int>bas(N,1);
vector<int>sz(N),vis(N);
vector<int>u[N];
int check(int x,int y) {
return num[x]+num[y]-num[__gcd(x,y)];
}
void Euler_prime(int n){
isp[0] = 0;
isp[1] = 0;
for ( int i = 2 ; i <= n ; i++ ) {
if ( !isp[i] ) continue;
for ( int j = 1; i*j <= n ; j++ ) {
num[i*j]++;
bas[i*j] *= i;
if ( j != 1 ) isp[i*j] = 0;
u[i*j].emplace_back(i);
}
}
for ( int i = 1 ; i <= n ; i++ ) {
sum[i] += sum[i-1]+isp[i];
}
}
void solve() {
int l,r;
cin >> l >> r;
if ( l == 1 ) {
int res = 0;
for ( int i = 2 ; i <= r ; i++ ) {
res += num[i];
}
cout << res << '\n';
return;
}
int t = sum[r]-sum[l-1];
i64 ans = 0;
if ( t ) {
for (int i=1;i<=r;++i) sz[i]=vis[i]=0;
for (int i=l;i<=r;++i) ++sz[bas[i]];
for (int i=2;i<=r;++i) if (!vis[i]&&sz[i])
{
ans+=num[i]*(sz[i]-1)+(num[i]+1); vis[i]=1;
for (int j=i*2;j<=r;j+=i) if (!vis[j]&&sz[j])
vis[j]=1,ans+=num[j]*sz[j];
}
ans -= 2;
} else {
int n = r-l+1;
vector<int>dis(n,1e9);
dis[0] = 0;
int idx = 0;
vector<int>f(n,0);
for ( int i = 1 ; i < n ; i++ ) {
int mx = 1e9;
int id = 0;
f[idx] = 1;
for ( int j = 0 ; j < n ; j++ ) {
if ( f[j] ) continue;
dis[j] = min(dis[j],dis[idx]+check(idx+l,j+l));
if ( dis[j] < mx ) {
mx = dis[j];
id = j;
}
}
ans += dis[id];
dis[id] = 0;
idx = id;
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
Euler_prime(1000000);
int T = 1;
cin >> T;
while ( T-- ) {
solve();
}
}
// 1 2 3
// 1 2 3
// 1 2 3
using namespace std;
using ull = unsigned long long;
using i64 = long long;
const int N = 1e6 + 5;
int sum[N];
int num[N];
vector<int>isp(N,1);
vector<int>bas(N,1);
int check(int x,int y) {
return num[x]+num[y]-num[__gcd(x,y)];
}
void Euler_prime(int n){
isp[0] = 0;
isp[1] = 0;
for ( int i = 2 ; i <= n ; i++ ) {
if ( !isp[i] ) continue;
for ( int j = 1; i*j <= n ; j++ ) {
num[i*j]++;
if ( j != 1 ) isp[i*j] = 0;
bas[i*j] *= i;
}
}
for ( int i = 1 ; i <= n ; i++ ) {
sum[i] += sum[i-1]+isp[i];
}
}
void solve() {
int l,r;
cin >> l >> r;
if ( l == 1 ) {
int res = 0;
for ( int i = 2 ; i <= r ; i++ ) {
res += num[i];
}
cout << res << '\n';
return;
}
int t = sum[r]-sum[l-1];
i64 ans = 0;
if ( t ) {
vector<int>vis(r+1,1);
map<int,int>mp;
for ( int i = l ; i <= r ; i++ ) {
mp[bas[i]]++;
}
int cnt = 0;
for ( auto [i,k] : mp ) {
for ( int j = 2 ; i*j <= 1000000 ; j++ ) {
if ( mp.contains(i*j) ) {
ans += num[i*j]*mp[i*j];
mp.erase(i*j);
}
}
ans += k*num[i];
cnt++;
}
ans += cnt-2;
} else {
int n = r-l+1;
vector<int>dis(n,1e9);
dis[0] = 0;
int idx = 0;
vector<int>f(n,0);
for ( int i = 1 ; i < n ; i++ ) {
int mx = 1e9;
int id = 0;
f[idx] = 1;
for ( int j = 0 ; j < n ; j++ ) {
if ( f[j] ) continue;
dis[j] = min(dis[j],dis[idx]+check(idx+l,j+l));
if ( dis[j] < mx ) {
mx = dis[j];
id = j;
}
}
ans += dis[id];
dis[id] = 0;
idx = id;
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
Euler_prime(1000000);
int T = 1;
cin >> T;
while ( T-- ) {
solve();
}
}
// 1 2 3
// 1 2 3
// 1 2 3