#include<bits/stdc++.h>
using namespace std;
int SieveOfEratosthenes(int s, int e)
{
bool prime[e+1];
memset(prime, true, sizeof(prime));
int count=0;
prime[1]=false;
for (int p=2; p*p<=e; p++)
{
if (prime[p] == true)
{
for (int i=p*p; i<=e; i += p){
prime[i] = false;
}
}
}
for(int j=s; j<=e; j++){
if(prime[j]){
count++;
}
}
return count;
}
int main() {
int t;
cin>>t;
while(t–){
int s, e;
cin>>s>>e;
cout<<SieveOfEratosthenes(s,e)<<"\n";
}
return 0;
}