used sieve of eratosthenes to find the nth prime number but is showing time limit please help me to optimize the code
package challenges;
import java.util.Scanner;
public class prateklovescandy2 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int T=scan.nextInt();
boolean flag=true;
for(int p=1;p<=T;p++)
{
flag=false;
boolean[] isprime=new boolean[1000001];
int N=scan.nextInt();
for(int a=1;a<=1000000;a++)
{
isprime[a]=true;
}
for(int i=2;i<=1000000;i++)
{
isprime[1]=false;
if(isprime[i]==true)
{
for(int j=2;i*j<=1000000;j++)
{
isprime[i*j]=false;
}
}
}
int counter=0;
for(int i=0;i<isprime.length;i++)
{
if(isprime[i]==true)
{
counter++;
if(counter==N)
{
System.out.println(i);
flag=true;
break;
}
if(flag==true)
{
break;
}
}
if(flag==true)
{
break;
}
}
}
}
}