the mistake in ur code was that u were calculating the prime only till sqrt(lim), so the multiples of prime numbers greater than sqrt(lim) were ignored. this was the cause of WA.
Eg:
consider if the test case was :
1
87313 87313 1
since u were calculating prime only till sqrt(lim)=(316), the output should have been 1 since 87313 is a prime number but ur solution outputs 0.