TLE even after applying SOE

I have attached my code below(the main ) I don’t understand why still TLE is coming. The result on submission says Test Case 0 compile message, and then a green tick for other 4 cases. In fact on downloading the input and running on my IDE (Eclipse) gives correct answer. Please help me out!

Scanner sc = new Scanner(System.in);

	int m = sc.nextInt();

	for (int d = 1; d <= m; d++) {

		int n = sc.nextInt();
		int counter = 1;
		int ans = 1;
		boolean prime[] = new boolean[1000001];

		for (int i = 2; i <= 1000000; i++) {
			prime[i] = true;

		}

		for (int p = 2; p <= 1000; p++) {
			if (prime[p] == true) {

				for (int j = p*p;  j <= 1000000; j=j+p)
					prime[j] = false;

			}

		}
		for (int i = 2; i <= 1000000 && counter <= n; i++) {

			if (prime[i] == true) {
				counter++;
				ans = i;

				// System.out.print(i);
			}

		}

		
		System.out.println(ans);
	}

Hey @prayu
here’s my suggestion to you. While doing these kinds of questions try to minimize the redundant operations.

Here it is calculating primes.
Think if T is like 10^4 and n is 10^6 and in worst case each of the test cases has very large value so you will be computing primes again and again and that will give you a TLE.

So precompute the primes once upto the given constraints, you will be good to go