You should learn Eratosthenes Sieve methods.
This code is by far a too naive one.
You should try some cases by your own to check the speed of your program.
eg :
5
100 200
1000 1100
10000 10100
100000 100100
1000000 1000100
and after that
5
100000 200000
1000000 1100000
10000000 10100000
100000000 100100000
900000000 1000000000
Good luck