Alternative for this problem?

alternative for this problem ?
after computing the pre prime seive . can we just iterate from
a to b. for the q queries.

like -
while(n–)
{
for(int i=a;i<=b;i++)
{
if(p[i]==1
cout<<i;
}
}

@mehulbhandari358 yeah this way is also correct but there may be chances of getting TLE.

why are there chances of tle
it has same complexity b-a ?

yeaah for this for problem it may work fine i said just in case of larger constraints.