You might not want to resieve for primes for each test case.
With this example, you’ve already sieved for all the primes you need after the first test case, why repeat it for the second? But, even with this change, it’s likely to still be too slow.
You may need a different algorithm. Try searching this forum for some ideas.