import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int t = console.nextInt();
    while (t-- > 0) {
      long n = console.nextLong(), k = console.nextLong();
      while (k-- > 0) {
        int factor = 1;
        for (int i = 2; i <= n; i++) {
          if (n % i == 0) {
            factor = i;
            break;
          }
        }
        n += factor;
      }
      System.out.println(n);
    }
  }
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int t = console.nextInt();
    while (t-- > 0) {
      long n = console.nextLong(), k = console.nextLong();
      long total;
      if (n % 2 == 0 && n != 2) {
        total = n + 2 * k;
      } else if (n % 3 == 0 && n != 3) {
        total = n + 3 + (k-1)*2;
      } else if ((n*2) % 2 == 0) {
        total = n*2 + (k-1) * 2;
      } else {
        total = n*2 + 3 + (k-1) * 2;
      }
      System.out.println(total);
    }
  }
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int t = console.nextInt();
    while (t-- > 0) {
      double n = console.nextDouble(), k = console.nextDouble();
      double total;
      if (n % 2 == 0 && n != 2) {
        total = n + 2 * k;
      } else if (n % 3 == 0 && n != 3) {
        total = n + 3 * k;
      } else if ((n*2) % 2 == 0) {
        total = n*2 + (k-1) * 2;
      } else {
        total = n*2 + (k-1) * 3;
      }
      System.out.println(total);
    }
  }
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int t = console.nextInt();
    while (t-- > 0) {
      long n = console.nextLong(), k = console.nextLong();
      while (k-- > 0) {
        long factor = n;
        long length = 100;
        for (int i = 2; i <= n; i++) {
          if (n % i == 0) {
            factor = i;
            break;
          } else if (length == 0) {
            break;
          }
          length--;
        }
        n += factor;
      }
      System.out.println(n);
    }
  }
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int t = console.nextInt();
    while (t-- > 0) {
      long n = console.nextLong(), k = console.nextLong();
      long total;
      if (n % 2 == 0 && n != 2) {
        total = n + 2 * k;
      } else if (n % 3 == 0 && n != 3) {
        total = n + 3 * k;
      } else if ((n*2) % 2 == 0) {
        total = n*2 + (k-1) * 2;
      } else {
        total = n*2 + (k-1) * 3;
      }
      System.out.println(total);
    }
  }
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int t = console.nextInt();
    while (t-- > 0) {
      long n = console.nextLong(), k = console.nextLong();
      while (k-- > 0) {
        long factor = n;
        if (factor % 2 == 0) {
          factor = 2;
        } else if (factor % 3 == 0) {
          factor = 3;
        }
        n += factor;
      }
      System.out.println(n);
    }
  }
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int t = 0; t<T; t++){
		    int a = sc.nextInt();
		    int b = sc.nextInt();

		    System.out.println(helper(a, b));
		}
	}
	public static int helper(int a, int count){
	    if(count <= 0)return a;
	    else{
	        int div = 0;
	        for(int i = 2; i<=a; i++){
	            if(a % i == 0){
	                div = i;
	                break;
	            }
	        }
	        return helper(a + div, count - 1);
	    }
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int t = 0; t<T; t++){
		    int a = sc.nextInt();
		    int b = sc.nextInt();
		    if(a % 2 == 0){
		        System.out.println(helper(a + 2, b - 1));
		    }
		    else{
		        int div = 0;
	            for(int i = 2; i<=a; i++){
	                if(a % i == 0){
	                    div = i;
	                    break;
	                }
	            }
	            System.out.println(helper(a + div, b - 1));
		    }

		}
	}
	public static int helper(int a, int count){
	    while(count > 0){
	        a += 2;
	        count--;
	    }
	    return a;
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int t = 0; t<T; t++){
		    int a = sc.nextInt();
		    int b = sc.nextInt();
		    if(a % 2 == 0){
		        System.out.println(a + 2 * b);
		    }
		    else{
		        int div = 0;
	            for(int i = 2; i<=a; i++){
	                if(a % i == 0){
	                    div = i;
	                    break;
	                }
	            }
	            b--;
	            System.out.println(a + div + 2 * b);
		    }

		}
	}
}


import java.util.*;
public class a641{
	public static void main(String args[]){
		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while(t-->0){
			long n = s.nextLong();
			long k = s.nextLong();
			long num = n;
			long ans = 0;
			for(int i=2;i<Math.pow(n,0.5);i++){
				if(n%i==0){
					num = i;
					break;
				}
			}
			if(num==n){
				ans = 2*num + 2*(k-1);
			}else{
				ans = num*(k)+n;
			}
			System.out.println(ans);

		}
	}
}


import java.util.*;
public class a641{
	public static void main(String args[]){
		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while(t-->0){
			long n = s.nextLong();
			long k = s.nextLong();
			while(k-->0){
				long num = n;
				for(int i=2;i<Math.pow(n,0.5);i++){
					if(n%i==0){
						num = i;
						break;
					}
				}
				n+=num;
			}
			System.out.println(n);

		}
	}
}


import java.util.*;
public class a641{
	public static void main(String args[]){
		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while(t-->0){
			long n = s.nextLong();
			long k = s.nextLong();
			long num = n;
			long ans = 0;
			for(int i=2;i<=Math.pow(n,0.5);i++){
				if(n%i==0){
					num = i;
					break;
				}
			}
			if(num==n||(n%2!=0&&num%2!=0)||(n%2==0&&num%2==0)){
				ans = num+n + 2*(k-1);
			}else{
				ans = num*(k)+n;
			}
			System.out.println(ans);

		}
	}
}


import java.util.Scanner;

public class pb2
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);

		int t;
		t = in.nextInt();

		for(int y=1; y<=t; y++)
		{
			int n,k;
			n = in.nextInt();
			k = in.nextInt();

			if(n%2 == 0)
				System.out.pritnln(n + k*2);
			else
			{
				for(int j=2; j<10 ; j++)
				{
					if(n%j == 0)
					{
						n += j;
						k -= 1;
						break;
					}
				}
				System.out.println(n + k*2);
			}

		}
	}
}


import java.util.Scanner;

public class pb2
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);

		int t;
		t = in.nextInt();

		for(int y=1; y<=t; y++)
		{
			int n,k;
			n = in.nextInt();
			k = in.nextInt();

			if(n%2 == 0)
				n += k*2;
			else
			{
				for(int i=0;i<k; i++)
				{
					for(int j=2; j<10 ; j++)
					{
						if(n%j == 0)
						{
							n += j;
							break;
						}
					}
				}
			}
			System.out.println(n);
		}
	}
}


import java.util.Scanner;

public class pb2
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);

		int t;
		t = in.nextInt();

		for(int y=1; y<=t; y++)
		{
			int n,k;
			n = in.nextInt();
			k = in.nextInt();

			if(n%2 == 0)
				System.out.println(n + k*2);
			else
			{
				for(int j=2; j<10 ; j++)
				{
					if(n%j == 0)
					{
						n += j;
						k -= 1;
						break;
					}
				}
				System.out.println(n + k*2);
			}
		}
	}
}


import java.util.Scanner;

public class pb2
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);

		int t;
		t = in.nextInt();

		for(int y=1; y<=t; y++)
		{
			int n,k;
			n = in.nextInt();
			k = in.nextInt();

			if(n%2 == 0)
				n += k*2;
			else
			{
				for(int j=2; j<10 ; j++)
				{
					if(n%j == 0)
					{
						n += j;
						k -= 1;
						n += k*2;
						break;
					}
				}
			}
			System.out.println(n);
		}
	}
}


import java.util.Scanner;

public class pb2
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);

		int t;
		t = in.nextInt();

		for(int y=1; y<=t; y++)
		{
			int n,k;
			n = in.nextInt();
			k = in.nextInt();

			for(int i=0; i<k; i++)
			{
				for(int j=2;j<10; j++)
				{
					if(n%j == 0)
					{
						n += j;
						break;
					}
				}
			}

			System.out.println(n);

		}
	}
}


import java.util.Scanner;

public class pb2
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);

		int t;
		t = in.nextInt();

		for(int y=1; y<=t; y++)
		{
			int n,k;
			n = in.nextInt();
			k = in.nextInt();

			if(n%2 == 0)
				System.out.println(n + k*2);
			else
			{
				for(int j=2; j<=n; j++)
				{
					if(n%j == 0)
					{
						n += j;
						k -= 1;
						break;
					}
				}
				System.out.println(n + k*2);
			}
		}
	}
}


import java.util.*;
import java.io.*;

public class HelloWorld{
     public static void main(String []args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for(int i=0;i<t;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int n = Integer.parseInt(st.nextToken());
            int k = Integer.parseInt(st.nextToken());
            int ans = n;
            for(int j=0;j<k;j++){
                ans += sd(n);
                n = ans;
            }
            System.out.println(ans);
        }
     }

     public static int sd(int n){
        for(int i=2;i*i<=n;i++){
            if(n%i==0){
                return i;
            }
        }
        return n;
     }
}


import java.util.*;
import java.io.*;

public class HelloWorld{
     public static void main(String []args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for(int i=0;i<t;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int n = Integer.parseInt(st.nextToken());
            int k = Integer.parseInt(st.nextToken());
            int ans = n;
            if(n%2==0){
                ans = n + (k*2);
            }else{
                int first = sd(n);
                ans = n + first + 2*(k-1);
            }
            System.out.println(ans);
        }
     }
     public static int sd(int n){
        for(int i=3;i*i<=n;i+=2){
            if(n%i==0){
                return i;
            }
        }
        return n;
     }
}


import java.util.*;
import java.io.*;

public class HelloWorld{
     public static void main(String []args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for(int i=0;i<t;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int n = Integer.parseInt(st.nextToken());
            int k = Integer.parseInt(st.nextToken());
            int ans = n;
            if(n%2==0){
                ans = n + (k*2);
            }else{
                ans = 2*(n+k-1);
            }
            System.out.println(ans);
        }
     }
}


import java.util.*;
import java.io.*;
import java.text.*;

    /**
     *
     * @author Tareq
     */

    public class Code {
           static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
     public  static void p (int c){
         if(c>=1){
             System.out.println("YES");}else{

             System.out.println("NO");
         }

              }

        public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in);
     int t=sc.nextInt();

       while(t>0){
             int n =sc.nextInt();
             long k =sc.nextLong();
             int f=0;

             while(k>0){
                 if(n%2==0){
                            f=2+n;
                 }else{
                     if(n%3==0){
                          f=3+n;
                     }else if(n%5==0){
                          f=5+n;
                     }
                     else if(n%7==0){
                          f=7+n;
                     }
                    else if(n%9==0){
                      f=9+n;
                     }else{
                    f= n+n;

                    }

                 }

                 n=f;

             k--;}

             System.out.println(f);

       t--;}

        }}


import java.util.*;
import java.io.*;
import java.text.*;

    /**
     *
     * @author Tareq
     */

    public class Code {
           static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
     public  static void p (int c){
         if(c>=1){
             System.out.println("YES");}else{

             System.out.println("NO");
         }

              }

      public  static void Wartools(int c){
       //two for
              for(int i =0 ; i < 10;i++){
          for(int j=0;j<10;j++){

          }
      }
              //one for
                for(int i =0 ; i < 10;i++){
      }

                                   }

      public  static int f(int  c){

for(int i =2; i<=c;i++){
    if(c%i==0){
    return i;
    }

}

                              return 0;}

        public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in);
     int t=sc.nextInt();

       while(t>0){
             int n =sc.nextInt();
             int k =sc.nextInt();
             int f=0;

             while(k>0){
                 if(n%2==0){
                            f=2+n;
                 }else{
                     if(n%3==0){
                          f=3+n;
                     }else if(n%5==0){
                          f=5+n;
                     }
                     else if(n%7==0){
                          f=7+n;
                     }
                    else if(n%9==0){
                      f=9+n;
                     }else{
                    f= n+n;

                    }

                 }

                 n=f;

             k--;}

             System.out.println(f);

       t--;}

        }}

     *
     * @author Tareq
     */

    public class Code {
           static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
     public  static void p (int c){
         if(c>=1){
             System.out.println("YES");}else{

             System.out.println("NO");
         }

              }

      public  static void Wartools(int c){
       //two for
              for(int i =0 ; i < 10;i++){
          for(int j=0;j<10;j++){

          }
      }
              //one for
                for(int i =0 ; i < 10;i++){
      }

                                   }

      public  static int f(int  c){

for(int i =2; i<=c;i++){
    if(c%i==0){
    return i;
    }

}

                              return 0;}

        public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in);
     int t=sc.nextInt();

       while(t>0){
             int n =sc.nextInt();
             int k =sc.nextInt();
             int f=0;

             while(k>0){
                 if(n%2==0){
                            f=2+n;
                 }else{
                           f=3+n;
                 }

                 n=f;

             k--;}

             System.out.println(f);

       t--;}

        }}


import java.util.*;
import java.io.*;
import java.text.*;

    /**
     *
     * @author Tareq
     */

    public class Code {
           static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
     public  static void p (int c){
         if(c>=1){
             System.out.println("YES");}else{

             System.out.println("NO");
         }

              }

      public  static void Wartools(int c){
       //two for
              for(int i =0 ; i < 10;i++){
          for(int j=0;j<10;j++){

          }
      }
              //one for
                for(int i =0 ; i < 10;i++){
      }

                                   }

      public  static int f(int  c){

for(int i =2; i<=c;i++){
    if(c%i==0){
    return i;
    }

}

                              return 0;}

        public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in);
     int t=sc.nextInt();

       while(t>0){
             int n =sc.nextInt();
             int k =sc.nextInt();
             int f=0;

             while(k>0){
                 f=f(n)+n;

                 n=f;

             k--;}

             System.out.println(f);

       t--;}

        }}


import java.util.*;
import java.io.*;
import java.text.*;

    /**
     *
     * @author Tareq
     */

    public class Code {
           static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
     public  static void p (int c){
         if(c>=1){
             System.out.println("YES");}else{

             System.out.println("NO");
         }

              }

      public  static void Wartools(int c){
       //two for
              for(int i =0 ; i < 10;i++){
          for(int j=0;j<10;j++){

          }
      }
              //one for
                for(int i =0 ; i < 10;i++){
      }

                                   }

      public  static long f(long  c){

for(long i =2; i<=c/2;++i){
    if(c%i==0){
    return i;
    }

}

                              return c;}

        public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in);
     int t=sc.nextInt();

       while(t>0){
             long n =sc.nextLong();
             long k =sc.nextLong();
             long f=0;

                 while(k>0){
                     if(n%2==0){
                         f=2+n;
                     }else{
                     f=f(n)+n;
                     }

                     n=f;

             k--;}

             System.out.println(f);

       t--;}

        }}


import java.util.*;
import java.io.*;
import java.text.*;

    /**
     *
     * @author Tareq
     */

    public class Code {
           static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
     public  static void p (int c){
         if(c>=1){
             System.out.println("YES");}else{

             System.out.println("NO");
         }

              }

      public  static void Wartools(int c){
       //two for
              for(int i =0 ; i < 10;i++){
          for(int j=0;j<10;j++){

          }
      }
              //one for
                for(int i =0 ; i < 10;i++){
      }

                                   }

      public  static int f(int  c){

for(int i =2; i<=c;i++){
    if(c%i==0){
    return i;
    }

}

                              return 0;}

        public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in);
     int t=sc.nextInt();

       while(t>0){
             int n =sc.nextInt();
             int k =sc.nextInt();
             int f=0;

             while(k>0){
                 if(n%2==0){
                            f=2+n;
                 }else{
                     if(n%3==0){
                          f=3+n;
                     }else if(n%5==0){
                          f=5+n;
                     }
                     else if(n%7==0){
                          f=7+n;
                     }
                    else if(n%9==0){
                      f=9+n;
                     }else{
                    f= n+n;

                    }

                 }

                 n=f;

             k--;}

             System.out.println(f);

       t--;}

        }}


import java.util.*;
import java.io.*;
import java.text.*;

    /**
     *
     * @author Tareq
     */

    public class Code {
           static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
     public  static void p (int c){
         if(c>=1){
             System.out.println("YES");}else{

             System.out.println("NO");
         }

              }

      public  static void Wartools(int c){
       //two for
              for(int i =0 ; i < 10;i++){
          for(int j=0;j<10;j++){

          }
      }
              //one for
                for(int i =0 ; i < 10;i++){
      }

                                   }

      public  static long f(long  c){

for(long i =2; i<=c/2;++i){
    if(c%i==0){
    return i;
    }

}

                              return c;}

        public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in);
     int t=sc.nextInt();

       while(t>0){
             long n =sc.nextLong();
             long k =sc.nextLong();
             long f=0;

                     if(n%2==0){
                         f=n+(2*k);
                     }else{
                     f=f(n)+n;
                     f+= 2*(k-1);

                     }

             System.out.println(f);

       t--;}

        }}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 *
 * @author User
 */
public class JavaApplication73 {

    /**
     * @param args the command line arguments
     */
     static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
        // TODO code application logic here
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        while(t--!=0)
        {
            long n=sc.nextLong();
            long k=sc.nextLong();

                if(n%2==0)
                     System.out.println((n+2*k));
                else if(n%3==0)
                     System.out.println(n+3+(k-1)*2);
                else if(n%5==0)
                     System.out.println(n+5+(k-1)*2);
                else

                {   long p=0;
                    for(long i=7;i<=Math.sqrt(n);i++)
                    {
                        if(n%i==0)
                        {
                            p=i;break;
                        }
                    }
                    if(p==0)
                    {
                          System.out.println(n+n+(k-1)*2);

                    }
                    else{
                          System.out.println(n+p+(k-1)*2);
                    }
                }

        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 *
 * @author User
 */
public class JavaApplication73 {

    /**
     * @param args the command line arguments
     */
     static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
        // TODO code application logic here
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        while(t--!=0)
        {
            long n=sc.nextLong();
            long k=sc.nextLong();

                if(n%2==0)
                     System.out.println((n+2*k));
                else if(n%3==0)
                     System.out.println(n+3+(k-1)*2);
                else if(n%5==0)
                     System.out.println(n+5+(k-1)*2);
                else
                     System.out.println(n+n+(k-1)*2);

        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 *
 * @author User
 */
public class JavaApplication73 {

    /**
     * @param args the command line arguments
     */
     static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
        // TODO code application logic here
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        while(t--!=0)
        {
            long n=sc.nextLong();
            long k=sc.nextLong();
            for(long i=1;i<=k;i++)
            {
                if(n%2==0)
                    n+=2;
                else if(n%3==0)
                    n+=3;
                else if(n%5==0)
                    n+=5;
                else
                    n+=n;
            }
            System.out.println(n);
        }
    }

}


import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
	public static void main (String[] args) {
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while(t!=0){
	    t--;
	    int n = sc.nextInt();
	    int k = sc.nextInt();
	    int m =-1;
	    while(k>0){

	            if(n%2!=0){
	                m=-1;
	            for(int i=2;i*i<=n;i++){
	                if(n%i==0){
	                    m=i;
	                    break;
	                }
	            }
	            if(m==-1){
	                m=n;
	            }

	            n+=m;

	            }
	            else{
	                n+=(k*2);
	                k=1;
	            }
	            k--;
	       }
	       System.out.println(n);
    	}

	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
	public static void main (String[] args) {
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while(t!=0){
	    t--;
	    int n = sc.nextInt();
	    int k = sc.nextInt();
	    int m =-1;
	    while(k>0){

	            if(n%2!=0){
	                m=-1;
	            for(int i=2;i*i<n;i++){
	                if(n%i==0){
	                    m=i;
	                    break;
	                }
	            }
	            if(m==-1){
	                m=n;
	            }

	            n+=m;

	            }
	            else{
	                n+=(k*2);
	                k=1;
	            }
	            k--;
	       }
	       System.out.println(n);
    	}

	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
	public static void main (String[] args) {
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while(t!=0){
	    t--;
	    int n = sc.nextInt();
	    int k = sc.nextInt();
	    int m =-1;
	    while(k!=0){
	            k--;
	            m=-1;
	            for(int i=2;i*i<n;i++){
	                if(n%i==0){
	                    m=i;
	                    break;
	                }
	            }
	            if(m==-1){
	                m=n;
	            }
	            n+=m;
	       }
	       System.out.println(n);
    	}

	}
}


import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t!=0){
          int n = scanner.nextInt();
          int k = scanner.nextInt();
          int f = Func(n);
           n = (n + f)+(2*(k-1));

            System.out.println(n);
          t--;
        }

        System.out.println();

    }
   public static int Func(int n){
       if(n%2==0)
        return 2;
        int res = 1;
       for (int i = 3; i <= Math.sqrt(n); i= i+2) {
           if(n % i ==0) {
               res = i;
            break;
           }
       }
      if(res==1)
        return n;
    return res;
    }
}


import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t!=0){
          int n = scanner.nextInt();
          int k = scanner.nextInt();
          int f = Func(n);
         t--;
            for (int i = 0; i < k; i++) {
               n = n + f;
              f = Func(n);
            }
            System.out.println(n);
        }

        System.out.println();

    }
   public static int Func(int n){
        int res = 1;
       for (int i = 2; i <=n/2; i++) {
           if(n % i ==0) {
               res = i;
            break;
           }
       }
      if(res==1)
        return n;
    return res;
    }
}


import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t!=0){
          int n = scanner.nextInt();
          int k = scanner.nextInt();
          int f = Func(n);
         t--;
            for (int i = 0; i < k; i++) {
               n = n + f;
              f = Func(n);
            }
            System.out.println(n);
        }

        System.out.println();

    }
   public static int Func(int n){
       if(n%2==0)
        return 2;
        int res = 1;
       for (int i = 3; i <= Math.sqrt(n); i= i+2) {
           if(n % i ==0) {
               res = i;
            break;
           }
       }
      if(res==1)
        return n;
    return res;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        FastReader scan = new FastReader();
        int test = scan.nextInt();
        for(int j=0;j<test;j++){
            int n = scan.nextInt();
            int k = scan.nextInt();
            int num = (int)(Math.sqrt(n));
            num+=1;
            int smallest_factor = num;
            if(n%2 == 0){
                n+= (2*k);
            }
            else{
                int flag = 0;
                for(int kk=2;kk<smallest_factor+1;kk++){
                    if(n%kk == 0){
                        n+=kk;
                        flag = 1;
                        break;
                    }
                }
                if(flag ==0){
                    n+=n;
                    n+=2*(k-1);
                }
                else{
                    n+=2*(k-1);
                }

            }
            System.out.println(n);
        }

    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}

// Java program to send email


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        FastReader scan = new FastReader();
        int test = scan.nextInt();
        for(int j=0;j<test;j++){
            int n = scan.nextInt();
            int k = scan.nextInt();
            int num = (int)(Math.sqrt(n));
            num+=1;
            int smallest_factor = num;
            for(int i=0;i<k;i++){
                int flag = 0;
                for(int kk=2;kk<smallest_factor+1;kk++){
                    if(n%kk == 0){
                        n+=kk;
                        smallest_factor = kk;
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0){
                    n+=n;
                    smallest_factor = 2;
                }
            }
            System.out.println(n);
        }

    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}

// Java program to send email


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        FastReader scan = new FastReader();
        int test = scan.nextInt();
        for(int j=0;j<test;j++){
            int n = scan.nextInt();
            int k = scan.nextInt();
            int num = (int)(Math.sqrt(n));
            num+=1;
            int smallest_factor = n;
            for(int i=0;i<k;i++){
                for(int kk=2;kk<smallest_factor+1;kk++){
                    if(n%kk == 0){
                        n+=kk;
                        smallest_factor = kk;
                        break;
                    }
                }
            }
            System.out.println(n);
        }

    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}

// Java program to send email


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.*;
import java.util.function.DoubleToIntFunction;

public class Main {
    public static void main(String[] args) {
        FastReader scan = new FastReader();
        int test = scan.nextInt();
        for(int j=0;j<test;j++){
            int n = scan.nextInt();
            int k = scan.nextInt();
            for(int i=0;i<k;i++){
                int num = (int)(Math.sqrt(n));
                num+=1;
                int flag=0;
                for(int kk=2;kk<num;kk++){
                    if(n%kk == 0){
                        n+=kk;
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0){
                    n+=n;
                }
            }
            System.out.println(n);
        }

    }

    public static double min(double a,double b){
        return a>b?b:a;
    }

    public static double max(double a,double b){
        return a>b?a:b;
    }

    public static double knapsack(int[] skill,int[] price, double max_ratio, ArrayList<Integer> arrli,int i,int K){
        if(arrli.size() == K ){
            double p=0;
            for(int ii=0;ii<arrli.size();ii++){
                p += (max_ratio)*skill[arrli.get(ii)];
                //System.out.print(arrli.get(ii)+" ");
            }
            //System.out.println();
            //System.out.println(p+" "+max_ratio);
            return p;
        }

        else if( i < 0){
            return Integer.MAX_VALUE;
        }
        double pri = price[i];
        double ski = skill[i];
        double ratio = pri/ski;
        ratio = max(max_ratio,ratio);
        ArrayList<Integer> arrll = new ArrayList<>();
        for(int ii=0;ii<arrli.size();ii++){
            arrll.add(arrli.get(ii));
        }
        arrll.add(i);
        return min(knapsack(skill,price,ratio,arrll,i-1,K) , knapsack(skill,price,max_ratio,arrli,i-1,K));

    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}

// Java program to send email


import java.util.*;
public class OracAndFactors12 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int count=0;
		for(int i=0;i<n;i++) {
			int a=sc.nextInt();
			int b=sc.nextInt();
			for(int k=0;k<b;k++) {
				if(a%2==0){
					a=a+2;
				}
				else if(a%3==0) {
					a=a+3;
				}
				else if(a%5==0) {
					a=a+5;
				}
				else if(a%7==0) {
					a=a+7;
				}
				else if(a%9==0) {
					a=a+9;
				}
				else {
					a=a+a;
				}
			}
			System.out.println(a);
		}

	}

}


import java.util.*;
public class OracAndFactors {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int count=0;
		for(int i=0;i<n;i++) {
			int a=sc.nextInt();
			int b=sc.nextInt();
			for(int k=0;k<b;k++) {
			for(int j=0;j<a/2+1;j++) {
				if(a%(j+2)==0) {
					a=a+j+2;
					count++;
					break;
				}
			}
			if(count==0) {
				a=a+a;
			}
			}
			System.out.println(a);
		}

	}

}


import java.util.*;
public class OracAndFactors12 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int count=0;
		for(int i=0;i<n;i++) {
			int a=sc.nextInt();
			int b=sc.nextInt();
			for(int k=0;k<b;k++) {
				if(a%2==0){
					a=a+2;
				}
				else if(a%3==0) {
					a=a+3;
				}
				else if(a%5==0) {
					a=a+5;
				}
				else {
					a=a+a;
				}
			}
			System.out.println(a);
		}

	}

}


import java.util.*;
public class CodeForces_1350A {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		for(int i=0;i<a;i++)
		{
			int n=sc.nextInt();
			int k=sc.nextInt();
			for(int j=0;j<k;j++)
			{
				if(n%2==0)
				{
					n=n+(2*(k-j));
					break;
				}
				else
					for(int l=3;l<=n;l+=2)
					{
						if(n%l==0)
						{
							n+=l;
							break;
						}
					}
			}
			System.out.println(n);
		}

	}

}


public class OracAndFactors12 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int count=0;
		for(int i=0;i<n;i++) {
			int a=sc.nextInt();
			int b=sc.nextInt();
			for(int k=0;k<b;k++) {
				if(a%2==0){
					a=a+2;
				}
				else if(a%3==0) {
					a=a+3;
				}
				else if(a%5==0) {
					a=a+5;
				}
				else {
					a=a+a;
				}
			}
			System.out.println(a);
		}

	}

}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=n;
           while(k-->0)
           {
               int temp=0;
              if(n%2!=0)
			  {
				  for(int i=3;i*i<=n;i++)
				  {
					  if(n%i==0)
					  {
						  temp=i;
						  break;
					  }
				  }

                                  if(temp==0)
                                  {
                                      temp=n;
                                  }
			  }
			  else
			  {
				  temp=2;
			  }

               sum=sum+temp;
               n=sum;
           }
           System.out.println(sum);
       }

    }
}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=0;
		   int temp=0;

				  for(int i=2;i<=n;i++)
				  {
					  if(n%i==0)
					  {
						  temp=i;
						  break;
					  }
				  }

               sum=n+temp+(k-1)/2;

           System.out.println(sum);
       }

    }
}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=0;

				  for(int i=2;i<=n;i++)
				  {
					  if(n%i==0)
					  {
						  temp=i;
						  break;
					  }
				  }

               sum=n+temp+(k-1)/2;

           System.out.println(sum);
       }

    }
}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=0;

				  for(int i=2;i<=n;i++)
				  {
					  if(n%i==0)
					  {
						  temp=i;
						  break;
					  }
				  }

               sum=n+temp+(k-1)/2;

           }
           System.out.println(sum);
       }

    }
}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=n;
           while(k-->0)
           {
               int temp=0;
              if(n%2!=0)
			  {
				  for(int i=3;i<=n;i++)
				  {
					  if(n%i==0)
					  {
						  temp=i;
						  break;
					  }
				  }
			  }
			  else
			  {
				  temp=2;
			  }

               sum=sum+temp;
               n=sum;
           }
           System.out.println(sum);
       }

    }
}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=n;
           while(k-->0)
           {
               int temp=0;
              if(n%2!=0)
			  {
				  for(int i=3;i*i<=n;i++)
				  {
					  if(n%i==0)
					  {
						  temp=i;
						  break;
					  }
				  }
			  }
			  else
			  {
				  temp=2;
			  }

               sum=sum+temp;
               n=sum;
           }
           System.out.println(sum);
       }

    }
}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=0;
		   int temp=0;

				  for(int i=2;i<=n;i++)
				  {
					  if(n%i==0)
					  {
						  temp=i;
						  break;
					  }
				  }

               sum=n+temp+(k-1)*2;

           System.out.println(sum);
       }

    }
}


import java.util.*;
public class Main
{
    public static void main(String z[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t-->0)
       {
           Integer n=sc.nextInt();
           Integer k=sc.nextInt();
           int sum=n;
           while(k-->0)
           {
               int temp=0;
               for(int i=2;i<=n;i++)
               {
                   if(n%i==0)
                   {
                       temp=i;
                       break;
                   }
               }
               sum=sum+temp;
               n=sum;
           }
           System.out.println(sum);
       }

    }
}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int[] arr = new int[1000001];
        for(int i=3;i<1000001;i+=2) {
            int loc = i;
            int max = Integer.MAX_VALUE;
            for(int j=2;j*j<=i;j++) {
                if(loc % j == 0) {
                    max = j;
                    break;
                }
            }
            if(max != Integer.MAX_VALUE) {
                arr[i] = Math.min(loc / max, max);
            } else {
                arr[i] = i;
            }

        }

        int t = scan.nextInt();

        while(t-- > 0) {
            int n = scan.nextInt(), k = scan.nextInt();

            while(k-- >  0) {
                if(n % 2 == 0) {
                    n += 2;
                } else {
                    n += arr[n];
                }
            }

            System.out.println(n);
        }

    }

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int[] arr = new int[1000001];
        for(int i=3;i<1000001;i+=2) {
            int loc = i;
            int max = Integer.MAX_VALUE;
            for(int j=2;j*j<=i;j++) {
                if(loc % j == 0) {
                    max = j;
                    break;
                }
            }
            if(max != Integer.MAX_VALUE) {
                arr[i] = Math.min(loc / max, max);
            } else {
                arr[i] = i;
            }

        }

        int t = scan.nextInt();

        while(t-- > 0) {
            int n = scan.nextInt(), k = scan.nextInt();

            while(k-- >  0) {
                if(n % 2 == 0) {
                    n += (k+1)*2;
                    break;
                } else {
                    n += arr[n];
                }
            }

            System.out.println(n);
        }

    }

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int[] arr = new int[1000001];
        for(int i=2;i<1000001;i++) {
            int loc = i;
            int max = Integer.MAX_VALUE;
            for(int j=2;j*j<=i;j++) {
                if(loc % j == 0) {
                    max = j;
                    break;
                }
            }
            if(max != Integer.MAX_VALUE) {
                arr[i] = Math.min(loc / max, max);
            } else {
                arr[i] = i;
            }

        }

        int t = scan.nextInt();

        while(t-- > 0) {
            int n = scan.nextInt(), k = scan.nextInt();

            while(k-- >  0) {
                n += arr[n];
            }

            System.out.println(n);
        }

    }

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int t = scan.nextInt();

        while(t-- > 0) {
            long n = scan.nextInt(), k = scan.nextInt();

            while(k-- >  0) {
                long loc = n;

                long max = 1;
                for(int i=2;i*i<=n;i++) {
                    if(n % i == 0) {
                        max = i;
                         break;
                    }
                }

                if(max != 1) {
                    loc = max;
                }
                n += loc;
            }

            System.out.println(n);
        }

    }

}


import java.util.*;
public class Main
{

	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t>0){
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    long div = 2;
		    int flag = 0;
		    while(k>0){
		        while(Math.sqrt(n)>div){
		            if(n%div==0){
		                flag = 1;
		                break;
		            }
		            div++;
		        }
		        if(flag == 1){
		            n += div;
		        }
		        else{
		            n += n;
		        }
		        k--;
		    }
            System.out.println(n);
		    t--;
		}
	}
}


import java.util.*;
public class Main
{

	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t>0){
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    long div = 2;
		    int flag = 0;
		    while(k>0){
		        while(n/2>div){
		            if(n%div==0){
		                flag = 1;
		                break;
		            }
		            div++;
		        }
		        if(flag == 1){
		            n += div;
		        }
		        else{
		            n += n;
		        }
		        k--;
		    }
            System.out.println(n);
		    t--;
		}
	}
}


import java.util.*;
public class Main
{

	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t>0){
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    if(n%2==0){
		        System.out.println((n+(2*k)));
		    }
		    else{
		        long p = 0;
		        for(long i=n;i>=2;i--){
		            if(n%i==0){
		                p=i;
		            }
		        }
		        System.out.println((n+p+(2*(k-1))));
		    }

		    t--;
		}
	}
}


import java.util.*;
public class d{
	static ArrayList<Integer> l = new ArrayList<>();
	static boolean prime[] = new boolean[1000001];
	static void seive(){
	     Arrays.fill(prime,true);
	     prime[0] = false;
	     prime[1] = false;
	     for(int i=2;i*i<1000001;i++){
	     	if(prime[i]){
	     		l.add(i);
	     		for(int j = i*i;j<1000001;j+=i){
	     			prime[j] = false;
	     		}
	     	}
	     }
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		StringBuilder str = new StringBuilder();
		seive();
		while(t--!=0){
			int n = sc.nextInt();
			long k = sc.nextLong();
			long ans = n;
            if(n%2==1){
            	int i = 0;
            	for(i=0;i*i<=n;i++){
                    if(n%l.get(i)==0)break;
            	}
            	ans+=l.get(i);
            	k--;
            }
            ans += 2*k;
            str.append(""+ans+"\n");

		}
		System.out.println(str.toString());
	}
}


import java.util.*;
public class d{
	static ArrayList<Integer> l = new ArrayList<>();
	static boolean prime[] = new boolean[1000001];
	static void seive(){
	     Arrays.fill(prime,true);
	     prime[0] = false;
	     prime[1] = false;
	     for(int i=2;i*i<1000001;i++){
	     	if(prime[i]){
	     		l.add(i);
	     		for(int j = i*i;j<1000001;j+=i){
	     			prime[j] = false;
	     		}
	     	}
	     }
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		StringBuilder str = new StringBuilder();
		//seive();
		while(t--!=0){
			int n = sc.nextInt();
			long k = sc.nextLong();
			long ans = n;
            if(n%2==1){
            	ans+=n;
            	k--;
            }
            ans += 2*k;
            str.append(""+ans+"\n");

		}
		System.out.println(str.toString());
	}
}


import java.util.*;
public class d{
	static ArrayList<Integer> l = new ArrayList<>();
	static boolean prime[] = new boolean[1000001];
	static void seive(){
	     Arrays.fill(prime,true);
	     prime[0] = false;
	     prime[1] = false;
	     for(int i=2;i<1000001;i++){
	     	if(prime[i]){
	     		l.add(i);
	     		for(int j = i*2;j<1000001;j+=i){
	     			prime[j] = false;
	     		}
	     	}
	     }
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		StringBuilder str = new StringBuilder();
		seive();
		//System.out.println(l);
		while(t--!=0){
			int n = sc.nextInt();
			long k = sc.nextLong();
			long ans = n;
            if(n%2==1){
            	int i = 0;
            	while(i<l.size()){
                    if(n%l.get(i)==0)break;
                    i++;
            	}
            	ans+=l.get(i);
            	k--;
            }
            ans += 2*k;
            str.append(""+ans+"\n");

		}
		System.out.println(str.toString());
	}
}


import java.util.*;
public class d{
	static boolean prime[] = new boolean[1000001];
	static void seive(){
	     Arrays.fill(prime,true);
	     prime[0] = false;
	     prime[1] = false;
	     for(int i=2;i*i<1000001;i++){
	     	if(prime[i]){
	     		for(int j = i*i;j<1000001;j+=i){
	     			prime[j] = false;
	     		}
	     	}
	     }
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		StringBuilder str = new StringBuilder();
		seive();
		while(t--!=0){
			int n = sc.nextInt();
			long k = sc.nextLong();
			long ans = n;
            if(prime[n]){
            	ans+=n;
            	k--;
            }
            ans += 2*k;
            str.append(""+ans+"\n");

		}
		System.out.println(str.toString());
	}
}


import java.util.*;
public class d{
	static ArrayList<Integer> l = new ArrayList<>();
	static boolean prime[] = new boolean[1000001];
	static void seive(){
	     Arrays.fill(prime,true);
	     prime[0] = false;
	     prime[1] = false;
	     for(int i=2;i<1000001;i++){
	     	if(prime[i]){
	     		l.add(i);
	     		for(int j = i*2;j<1000001;j+=i){
	     			prime[j] = false;
	     		}
	     	}
	     }
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		StringBuilder str = new StringBuilder();
		seive();
		//System.out.println(l);
		while(t--!=0){
			int n = sc.nextInt();
			long k = sc.nextLong();
			long ans = n;
            if(n%2==1){
            	int i = 0;
            	for(i=0;i*i<=n;i++){
                    if(n%l.get(i)==0)break;
            	}
            	ans+=l.get(i);
            	k--;
            }
            ans += 2*k;
            str.append(""+ans+"\n");

		}
		System.out.println(str.toString());
	}
}


//IOI 2021

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int inf = 2e9 + 17;

int q;

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		int a[n + 17] = {}, F[n + 17], L[n + 17] = {}, dp[n + 17][3] = {};
		fill(F + 1, F + n + 1, inf);
		for (int i = 1; i <= n; i++)
			cin >> a[i], F[a[i]] = min(F[a[i]], i), L[a[i]] = i;
		int t = 0;
		for (int i = 1; i <= n; i++) {
			if (!L[i]) {
				dp[i][0] = dp[i - 1][0];
				dp[i][1] = dp[i - 1][1];
				dp[i][2] = dp[i - 1][2];
				continue;
			}

			if (F[i] < t) {
				dp[i][1] = min(dp[i - 1][1] + 1, dp[i - 1][2]);
				dp[i][0] = dp[i - 1][2];
				dp[i][2] = min(dp[i - 1][0] + 2, dp[i - 1][2] + 1);
			}
			else {
				dp[i][1] = min(dp[i - 1][1], dp[i - 1][0]);
				dp[i][0] = dp[i - 1][0];
				dp[i][2] = min(dp[i - 1][0] + 2, dp[i - 1][2] + 1);
			}
			t = L[i];
		/*
		for (int j = 0; j < 3; j++) {
			for (int i = 1; i <= n; i++)
				cerr << dp[i][j] << " ";
			cerr << endl;
		}
		*/
		cout << dp[n][1] << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll                  long long
#define dl                 double
#define pb                push_back
#define F                  first
#define S                  second
#define endl              "\n"
#define rep(i,a,b)       for(i=a;i<b;i++)
#define all(v)             v.begin(),v.end()
#define allr(v)            v.rbegin(),v.rend()
#define mod              1000000007
#define CIN(V,s,n)      for(int i=s;i<n;i++){cin >> V[i];}
#define COUT(V,s,n)   {for(int i=s;i<n;i++){cout <<  V[i] << " "  ;} cout << endl;}
#define fast               ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define CLEAR(V);      for(ll i=0;i<V.size();i++) V[i] = 0;
#define N                  1000LL
#define tmod            998244353
using namespace std;

bool sBs(const pair<int,int> &a,const pair<int,int> &b)
{   return (a.second < b.second);   }

ll SUMD(ll n);
ll BS(vector<ll> &PS,ll s,ll e,ll ser);
ll MI(ll a, ll m);
bool P[N+1];
void Sieve(int n=N);
ll Powb(ll b,ll n);

int main()
{
fast;
ll q=1,t;
cin >> q;
t = q;
while(q--)
{

    ll h,n,m,k,i,j,l,r,c=0,b,a,g,counter=0,u,v,d,e,w,p,x,y,fl=1;
    //n=m=i=j=l=r=a=t=b=c=u=v=w=p=x=y=0;
    cin >>n ;
    map<ll,pair<ll,ll> > M;
    vector<ll> V(n),D;
     //cout << M[1].F << " ";
    for(i=0;i<n;i++)
    {
        cin >> V[i];
        if(M[V[i]].F == 0 )
        {
            M[V[i]] = {i+1,i+1};
            D.pb(V[i]);
        }
        else
        {
            M[V[i]].F = min(i+1,M[V[i]].F);
            M[V[i]].S = max(i+1,M[V[i]].S);
        }
    }
    sort(all(D));
    m = D.size();
    vector<ll> DP(m+1);
    DP[0] = 1;
    ll temp = 1;
    for(i=1;i<m;i++)
    {
        if(M[D[i-1]].S > M[D[i]].F) DP[i] = 1;
        else    DP[i] = DP[i-1] + 1;
        temp = max(temp,DP[i]);
    }

    cout <<(m-temp) << endl;

}
    return 0;
}

//*****************************************************************************************************************************************

ll Powb(ll b,ll n)
{
    if(n==0) return 1LL;
    if(n==1) return b;
    ll temp = Powb(b,n/2);
    if(n%2==0){return (temp*temp)%mod;}
    else{return (b*((temp*temp)%mod))%mod;}
}

ll SUMD(ll n)
{   ll sum=0;
    while(n>0){sum += n%10;n = n/10;}
    return sum;

}
ll BS(vector<ll> &PS,ll s,ll e,ll ser)
{
    if(s>e)
        return s;
    ll mid = (s+e)/2;
    if(PS[mid]==ser)
    {
        return mid;
    }
    else if(PS[mid] > ser)
    {
        return BS(PS,s,mid-1,ser);
    }
    else
        return BS(PS,mid+1,e,ser);
}
ll MI(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1)
      return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}

void Sieve(int n)
{
    memset(P,true,sizeof(P));
    P[0] = false;
    P[1]= false;
    for(ll i=2;i*i<=n;i++)
    {
        if(P[i])
        {
            for(ll j=i*i;j<=n;j+=i)
            {
                P[j]=false;
            }
        }
    }

}


#include<bits/stdc++.h>
#define ll                  long long
#define dl                 double
#define pb                push_back
#define F                  first
#define S                  second
#define endl              "\n"
#define rep(i,a,b)       for(i=a;i<b;i++)
#define all(v)             v.begin(),v.end()
#define allr(v)            v.rbegin(),v.rend()
#define mod              1000000007
#define CIN(V,s,n)      for(int i=s;i<n;i++){cin >> V[i];}
#define COUT(V,s,n)   {for(int i=s;i<n;i++){cout <<  V[i] << " "  ;} cout << endl;}
#define fast               ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define CLEAR(V);      for(ll i=0;i<V.size();i++) V[i] = 0;
#define N                  1000LL
#define tmod            998244353
using namespace std;

bool sBs(const pair<int,int> &a,const pair<int,int> &b)
{   return (a.second < b.second);   }

ll SUMD(ll n);
ll BS(vector<ll> &PS,ll s,ll e,ll ser);
ll MI(ll a, ll m);
bool P[N+1];
void Sieve(int n=N);
ll Powb(ll b,ll n);

int main()
{
fast;
ll q=1,t;
cin >> q;
t = q;
while(q--)
{

    ll h,n,m,k,i,j,l,r,c=0,b,a,g,counter=0,u,v,d,e,w,p,x,y,fl=1;
    //n=m=i=j=l=r=a=t=b=c=u=v=w=p=x=y=0;
    cin >>n ;
    map<ll,pair<ll,ll> > M;
    vector<ll> V(n),D;
     //cout << M[1].F << " ";
    for(i=0;i<n;i++)
    {
        cin >> V[i];
        if(M[V[i]].F == 0 )
        {
            M[V[i]] = {i+1,i+1};
            D.pb(V[i]);
        }
        else
        {
            M[V[i]].F = min(i+1,M[V[i]].F);
            M[V[i]].S = max(i+1,M[V[i]].S);
        }
    }
    sort(all(D));
    m = D.size();
    vector<ll> DP(m+1);
    DP[0] = 1;
    ll temp = 1;
    for(i=1;i<m;i++)
    {
        if(M[D[i-1]].S > M[D[i]].F) DP[i] = 1;
        else    DP[i] = DP[i-1] + 1;
        temp = max(temp,DP[i]);
    }

    cout << max(0LL,(m-temp)) << endl;

}
    return 0;
}

//*****************************************************************************************************************************************

ll Powb(ll b,ll n)
{
    if(n==0) return 1LL;
    if(n==1) return b;
    ll temp = Powb(b,n/2);
    if(n%2==0){return (temp*temp)%mod;}
    else{return (b*((temp*temp)%mod))%mod;}
}

ll SUMD(ll n)
{   ll sum=0;
    while(n>0){sum += n%10;n = n/10;}
    return sum;

}
ll BS(vector<ll> &PS,ll s,ll e,ll ser)
{
    if(s>e)
        return s;
    ll mid = (s+e)/2;
    if(PS[mid]==ser)
    {
        return mid;
    }
    else if(PS[mid] > ser)
    {
        return BS(PS,s,mid-1,ser);
    }
    else
        return BS(PS,mid+1,e,ser);
}
ll MI(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1)
      return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}

void Sieve(int n)
{
    memset(P,true,sizeof(P));
    P[0] = false;
    P[1]= false;
    for(ll i=2;i*i<=n;i++)
    {
        if(P[i])
        {
            for(ll j=i*i;j<=n;j+=i)
            {
                P[j]=false;
            }
        }
    }

}


#include<bits/stdc++.h>
#define ll                  long long
#define dl                 double
#define pb                push_back
#define F                  first
#define S                  second
#define endl              "\n"
#define rep(i,a,b)       for(i=a;i<b;i++)
#define all(v)             v.begin(),v.end()
#define allr(v)            v.rbegin(),v.rend()
#define mod              1000000007
#define CIN(V,s,n)      for(int i=s;i<n;i++){cin >> V[i];}
#define COUT(V,s,n)   {for(int i=s;i<n;i++){cout <<  V[i] << " "  ;} cout << endl;}
#define fast               ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define CLEAR(V);      for(ll i=0;i<V.size();i++) V[i] = 0;
#define N                  1000LL
#define tmod            998244353
using namespace std;

bool sBs(const pair<int,int> &a,const pair<int,int> &b)
{   return (a.second < b.second);   }

ll SUMD(ll n);
ll BS(vector<ll> &PS,ll s,ll e,ll ser);
ll MI(ll a, ll m);
bool P[N+1];
void Sieve(int n=N);
ll Powb(ll b,ll n);

int main()
{
fast;
ll q=1,t;
cin >> q;
t = q;
while(q--)
{

    ll h,n,m,k,i,j,l,r,c=0,b,a,g,counter=0,u,v,d,e,w,p,x,y,fl=1;
    //n=m=i=j=l=r=a=t=b=c=u=v=w=p=x=y=0;
    cin >>n ;
    map<ll,pair<ll,ll> > M;
    vector<ll> V(n),D;
    for(i=0;i<n;i++)
    {
        cin >> V[i];
        if(M[V[i]].F == 0 )
        {
            M[V[i]] = {i,i};
            D.pb(V[i]);
        }
        else
        {
            M[V[i]].F = min(i,M[V[i]].F);
            M[V[i]].S = max(i,M[V[i]].S);
        }
    }
    sort(all(D));
    m = D.size();
    vector<ll> DP(m+1);
    DP[0] = 1;
    ll temp = 0;
    for(i=1;i<m;i++)
    {
        if(M[D[i-1]].S > M[D[i]].F) DP[i] = 1;
        else    DP[i] = DP[i-1] + 1;
        temp = max(temp,DP[i]);
    }

    cout << m-temp << endl;

}
    return 0;
}

//*****************************************************************************************************************************************

ll Powb(ll b,ll n)
{
    if(n==0) return 1LL;
    if(n==1) return b;
    ll temp = Powb(b,n/2);
    if(n%2==0){return (temp*temp)%mod;}
    else{return (b*((temp*temp)%mod))%mod;}
}

ll SUMD(ll n)
{   ll sum=0;
    while(n>0){sum += n%10;n = n/10;}
    return sum;

}
ll BS(vector<ll> &PS,ll s,ll e,ll ser)
{
    if(s>e)
        return s;
    ll mid = (s+e)/2;
    if(PS[mid]==ser)
    {
        return mid;
    }
    else if(PS[mid] > ser)
    {
        return BS(PS,s,mid-1,ser);
    }
    else
        return BS(PS,mid+1,e,ser);
}
ll MI(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1)
      return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}

void Sieve(int n)
{
    memset(P,true,sizeof(P));
    P[0] = false;
    P[1]= false;
    for(ll i=2;i*i<=n;i++)
    {
        if(P[i])
        {
            for(ll j=i*i;j<=n;j+=i)
            {
                P[j]=false;
            }
        }
    }

}


#include<bits/stdc++.h>
#define ll                  long long
#define dl                 double
#define pb                push_back
#define F                  first
#define S                  second
#define endl              "\n"
#define rep(i,a,b)       for(i=a;i<b;i++)
#define all(v)             v.begin(),v.end()
#define allr(v)            v.rbegin(),v.rend()
#define mod              1000000007
#define CIN(V,s,n)      for(int i=s;i<n;i++){cin >> V[i];}
#define COUT(V,s,n)   {for(int i=s;i<n;i++){cout <<  V[i] << " "  ;} cout << endl;}
#define fast               ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define CLEAR(V);      for(ll i=0;i<V.size();i++) V[i] = 0;
#define N                  1000LL
#define tmod            998244353
using namespace std;

bool sBs(const pair<int,int> &a,const pair<int,int> &b)
{   return (a.second < b.second);   }

ll SUMD(ll n);
ll BS(vector<ll> &PS,ll s,ll e,ll ser);
ll MI(ll a, ll m);
bool P[N+1];
void Sieve(int n=N);
ll Powb(ll b,ll n);

int main()
{
fast;
ll q=1,t;
cin >> q;
t = q;
while(q--)
{

    ll h,n,m,k,i,j,l,r,c=0,b,a,g,counter=0,u,v,d,e,w,p,x,y,fl=1;
    //n=m=i=j=l=r=a=t=b=c=u=v=w=p=x=y=0;
    cin >>n ;
    map<ll,pair<ll,ll> > M;
    vector<ll> V(n),D;
    for(i=0;i<n;i++)
    {
        cin >> V[i];
        if(M[V[i]].F == 0 )
        {
            M[V[i]] = {i,i};
            D.pb(V[i]);
        }
        else
        {
            M[V[i]].F = min(i,M[V[i]].F);
            M[V[i]].S = max(i,M[V[i]].S);
        }
    }
    sort(all(D));
    m = D.size();
    vector<ll> DP(m+1);
    DP[0] = 1;
    ll temp = 0;
    for(i=1;i<=m;i++)
    {
        if(M[D[i-1]].S > M[D[i]].F) DP[i] = 1;
        else    DP[i] = DP[i-1] + 1;
        temp = max(temp,DP[i]);
    }

    cout << m-temp << endl;

}
    return 0;
}

//*****************************************************************************************************************************************

ll Powb(ll b,ll n)
{
    if(n==0) return 1LL;
    if(n==1) return b;
    ll temp = Powb(b,n/2);
    if(n%2==0){return (temp*temp)%mod;}
    else{return (b*((temp*temp)%mod))%mod;}
}

ll SUMD(ll n)
{   ll sum=0;
    while(n>0){sum += n%10;n = n/10;}
    return sum;

}
ll BS(vector<ll> &PS,ll s,ll e,ll ser)
{
    if(s>e)
        return s;
    ll mid = (s+e)/2;
    if(PS[mid]==ser)
    {
        return mid;
    }
    else if(PS[mid] > ser)
    {
        return BS(PS,s,mid-1,ser);
    }
    else
        return BS(PS,mid+1,e,ser);
}
ll MI(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1)
      return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}

void Sieve(int n)
{
    memset(P,true,sizeof(P));
    P[0] = false;
    P[1]= false;
    for(ll i=2;i*i<=n;i++)
    {
        if(P[i])
        {
            for(ll j=i*i;j<=n;j+=i)
            {
                P[j]=false;
            }
        }
    }

}


#include<bits/stdc++.h>
#define ll                  long long
#define dl                 double
#define pb                push_back
#define F                  first
#define S                  second
#define endl              "\n"
#define rep(i,a,b)       for(i=a;i<b;i++)
#define all(v)             v.begin(),v.end()
#define allr(v)            v.rbegin(),v.rend()
#define mod              1000000007
#define CIN(V,s,n)      for(int i=s;i<n;i++){cin >> V[i];}
#define COUT(V,s,n)   {for(int i=s;i<n;i++){cout <<  V[i] << " "  ;} cout << endl;}
#define fast               ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define CLEAR(V);      for(ll i=0;i<V.size();i++) V[i] = 0;
#define N                  1000LL
#define tmod            998244353
using namespace std;

bool sBs(const pair<int,int> &a,const pair<int,int> &b)
{   return (a.second < b.second);   }

ll SUMD(ll n);
ll BS(vector<ll> &PS,ll s,ll e,ll ser);
ll MI(ll a, ll m);
bool P[N+1];
void Sieve(int n=N);
ll Powb(ll b,ll n);

int main()
{
fast;
ll q=1,t;
cin >> q;
t = q;
while(q--)
{

    ll h,n,m,k,i,j,l,r,c=0,b,a,g,counter=0,u,v,d,e,w,p,x,y,fl=1;
    //n=m=i=j=l=r=a=t=b=c=u=v=w=p=x=y=0;
    cin >>n ;
    map<ll,pair<ll,ll> > M;
    vector<ll> V(n),D;
     //cout << M[1].F << " ";
    for(i=0;i<n;i++)
    {
        cin >> V[i];
        if(M[V[i]].F == 0 )
        {
            M[V[i]] = {i+1,i+1};
            D.pb(V[i]);
        }
        else
        {
            M[V[i]].F = min(i+1,M[V[i]].F);
            M[V[i]].S = max(i+1,M[V[i]].S);
        }
    }
    sort(all(D));
    m = D.size();
    vector<ll> DP(m+1);
    DP[0] = 1;
    ll temp = 0;
    for(i=1;i<m;i++)
    {
        if(M[D[i-1]].S > M[D[i]].F) DP[i] = 1;
        else    DP[i] = DP[i-1] + 1;
        temp = max(temp,DP[i]);
    }

    cout << max(0LL,(m-temp)) << endl;

}
    return 0;
}

//*****************************************************************************************************************************************

ll Powb(ll b,ll n)
{
    if(n==0) return 1LL;
    if(n==1) return b;
    ll temp = Powb(b,n/2);
    if(n%2==0){return (temp*temp)%mod;}
    else{return (b*((temp*temp)%mod))%mod;}
}

ll SUMD(ll n)
{   ll sum=0;
    while(n>0){sum += n%10;n = n/10;}
    return sum;

}
ll BS(vector<ll> &PS,ll s,ll e,ll ser)
{
    if(s>e)
        return s;
    ll mid = (s+e)/2;
    if(PS[mid]==ser)
    {
        return mid;
    }
    else if(PS[mid] > ser)
    {
        return BS(PS,s,mid-1,ser);
    }
    else
        return BS(PS,mid+1,e,ser);
}
ll MI(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1)
      return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}

void Sieve(int n)
{
    memset(P,true,sizeof(P));
    P[0] = false;
    P[1]= false;
    for(ll i=2;i*i<=n;i++)
    {
        if(P[i])
        {
            for(ll j=i*i;j<=n;j+=i)
            {
                P[j]=false;
            }
        }
    }

}


#include<bits/stdc++.h>

using namespace std;

int lis( int arr[], int n )
{
    int lis[n];

    lis[0] = 1;

    for (int i = 1; i < n; i++ )
    {
        lis[i] = 1;
        for (int j = 0; j < i; j++ )
            if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
                lis[i] = lis[j] + 1;
    }

    return *max_element(lis, lis+n);
}

int main() {
    int q;
    cin >> q;
    while(q--) {
        int n;
        cin >> n;
        int a[n];
        vector<int> min(n+1, n);
        vector<int> max(n+1, 0);
        for(int i = 0; i < n; ++i) {
            cin >> a[i];
            if(min[a[i]] > i) min[a[i]] = i;
            if(max[a[i]] < i) max[a[i]] = i;
        }
        sort(a, a+n);
        vector<int> s;
        for(int i = 1; i <= n; ++i) {
            if(max[i]) s.push_back(i);
        }
        int dp[s.size()];
        dp[0] = 1;
        int ans = 1;
        for(int i = 1; i < (int) s.size(); ++i) {
            if(max[s[i-1]] < min[s[i]]) dp[i] = dp[i-1] + 1;
            else dp[i] = 1;
            if(ans < dp[i]) ans = dp[i];
        }
        ans = s.size() - ans;
        cout << ans << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int lis( int arr[], int n )
{
    int lis[n];

    lis[0] = 1;

    for (int i = 1; i < n; i++ )
    {
        lis[i] = 1;
        for (int j = 0; j < i; j++ )
            if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
                lis[i] = lis[j] + 1;
    }

    return *max_element(lis, lis+n);
}

int main() {
    int q;
    cin >> q;
    while(q--) {
        int n;
        cin >> n;
        int a[n], b[n], hash[100001];
        memset(hash, 0, sizeof(hash));
        for(int i = 0; i < n; ++i) {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b, b+n);
        int count_dist = 1;
        for(int i = 1; i < n; ++i) {
            if(b[i] != b[i-1]) count_dist++;
        }
        vector<int> c;
        c.push_back(a[0]);
        for(int i = 1; i < n; ++i) {
            if(c[c.size()-1] != a[i]) {
                c.push_back(a[i]);
            }
        }
        int e[n], k = 0;

        for(int i = 0; i < c.size(); ++i) {
            hash[c[i]]++;
        }
        // cout << '\n';
        for(int i = 0; i < c.size(); ++i) {
            if(hash[c[i]] == 1) {
                e[k++] = c[i];
            }
        }

        // for(int i = 0; i < c.size(); ++i) printf("%d: %d, ", c[i], hash[c[i]]);
        // cout << '\n';
        // for(int i = 0; i < k; ++i) cout << e[i] << ' ';
        // cout << '\n';

        int index = lis(e, k);
        cout << count_dist - index<< '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#define MAX 300001
using namespace std;

int main() {
    int q;
    cin >> q;
    while(q--) {
        int n;
        cin >> n;
        int a[n];
        map<int, int> min, max;
        for(int i = 0; i < n; ++i) {
            cin >> a[i];
            if(!min.count(a[i])) min[a[i]] = i;
            max[a[i]] = i;
        }
        vector<int> s;
        for(auto& i: max) {
            s.push_back(i.first);
        }
        int dp[s.size()];
        dp[0] = 1;
        int ans = 1;
        for(int i = 1; i < (int) s.size(); ++i) {
            if(max[s[i-1]] < min[s[i]]) dp[i] = dp[i-1] + 1;
            else dp[i] = 1;
            if(ans < dp[i]) ans = dp[i];
        }
        ans = s.size() - ans;
        cout << ans << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp> // Common file
#include<ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define rep(i,a,b) for( ll i = a ; i < b ; i+=1 )
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld",&x)
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define ld long double
ll t = 1, tt;
const ll inf=2e18;
inline ll add(ll x,ll y){x+=y;return x%hell;}
inline ll mult(ll x,ll y){x%=hell;y%=hell;return (1LL*x*y)%hell;}
inline ll powMod(ll base,ll expo){
	ll ans=1;
	while(expo){
		if(expo&1)ans=mult(ans,base);
		base=mult(base,base);
		expo>>=1;
	}
	return ans;
}
void go(vector<ll>& pre,vector<ll>& suf,vector<ll>& v){
	map<ll,ll> first, rank, last;
	set<ll> seta;
	rep(i,0,sz(v))seta.insert(v[i]);
	ll mini=0, cur=1, maxi=hell;
	for(auto i : seta)rank[i]=cur++;
	rep(i,0,sz(v))v[i]=rank[v[i]];
	seta.clear();
	//cout<<"in go\n";
	rep(i,0,sz(v)){
		//cout<<v[i]<<" ";
		if(seta.find(v[i])==seta.end())
			first[v[i]]=i;
		last[v[i]]=i;
		seta.insert(v[i]);
	}
	//cout<<"\n";
	ll k = sz(seta);
	cur=0;
	rep(i,1,k+1){
		if(mini>first[i])
			cur=i;
		mini=max(mini,last[i]);
		pre[i]=cur;
	}
	cur=k;
	for(ll i=k;i>=1;i--){
		if(maxi<last[i])
			cur=i;
		maxi=min(maxi,first[i]);
		suf[i]=k-cur;
	}
}
void solve(){
	ll n, mini, maxi, f=1;
	cin>>n;
	vector<ll> v(n), v1, vn;
	set<ll> seta;
	rep(i,0,n){
		cin>>v[i];
		if(i>0 and v[i]<v[i-1])f=0;
		seta.insert(v[i]);
	}
	if(f){
		cout<<"0";return;
	}
	mini=*seta.begin();
	maxi=*prev(seta.end());
	rep(i,0,n){
		if(v[i]!=mini)v1.pb(v[i]);
		if(v[i]!=maxi)vn.pb(v[i]);
	}
	ll k = sz(seta)-1, ans=hell;
	vector<ll> pre(k+5,0), suf(k+5,0), pre1(k+5,0), suf1(k+5,0);
	go(pre,suf,v1);
	go(pre1,suf1,vn);
	/*rep(i,0,k+1)cout<<pre[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<suf[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<pre1[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<suf1[i]<<" ";
	cout<<endl;*/
	ans=min(ans,1+pre[k]);
	ans=min(ans,1+suf[1]);
	ans=min(ans,1+pre1[k]);
	ans=min(ans,1+suf1[1]);
	rep(i,1,k+1){
		ans=min(ans,1+pre[i]+suf[i]);
		ans=min(ans,1+pre1[i]+suf1[i]);
	}
	cout<<ans;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin>>t;
	tt=t;
    while(t--){
        solve();
        if(t!=0)cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp> // Common file
#include<ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define rep(i,a,b) for( ll i = a ; i < b ; i+=1 )
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld",&x)
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define ld long double
ll t = 1, tt;
const ll inf=2e18;
inline ll add(ll x,ll y){x+=y;return x%hell;}
inline ll mult(ll x,ll y){x%=hell;y%=hell;return (1LL*x*y)%hell;}
inline ll powMod(ll base,ll expo){
	ll ans=1;
	while(expo){
		if(expo&1)ans=mult(ans,base);
		base=mult(base,base);
		expo>>=1;
	}
	return ans;
}
void solve(){
	ll n;
	cin>>n;
	vector<ll> v(n), a, first(n+1,-1), last(n+1), dp(n+1,inf);
	rep(i,0,n){
		cin>>v[i];
		if(first[v[i]]==-1)first[v[i]]=i;
		last[v[i]]=i;
		a.pb(v[i]);
	}
	sort(all(a));
	a.erase(unique(all(a)),a.end());
	ll ans=sz(a)-1;
	dp[sz(a)-1]=1;
	for(ll i=sz(a)-2;i>=0;i--){
		if(last[a[i]]<first[a[i+1]])dp[i]=dp[i+1]+1;
		else
			dp[i]=1;
		ans=min(ans,sz(a)-dp[i]);
	}
	cout<<ans;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin>>t;
	tt=t;
    while(t--){
        solve();
        if(t!=0)cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp> // Common file
#include<ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define rep(i,a,b) for( ll i = a ; i < b ; i+=1 )
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld",&x)
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define ld long double
ll t = 1, tt, n;
const ll inf=2e18;
inline ll add(ll x,ll y){x+=y;return x%hell;}
inline ll mult(ll x,ll y){x%=hell;y%=hell;return (1LL*x*y)%hell;}
inline ll powMod(ll base,ll expo){
	ll ans=1;
	while(expo){
		if(expo&1)ans=mult(ans,base);
		base=mult(base,base);
		expo>>=1;
	}
	return ans;
}
void go(vector<ll>& pre,vector<ll>& suf,vector<ll>& v){
	map<ll,ll> first, rank, last;
	set<ll> seta;
	rep(i,0,sz(v))seta.insert(v[i]);
	ll mini=0, cur=1, maxi=hell;
	for(auto i : seta)rank[i]=cur++;
	rep(i,0,sz(v))v[i]=rank[v[i]];
	seta.clear();
	rep(i,0,sz(v)){
		//cout<<v[i]<<" ";
		if(seta.find(v[i])==seta.end())
			first[v[i]]=i;
		last[v[i]]=i;
		seta.insert(v[i]);
	}
	//cout<<"\n";
	ll k = sz(seta);
	cur=0;
	rep(i,1,k+1){
		if(mini>first[i])
			cur=i;
		mini=max(mini,last[i]);
		pre[i]=cur;
	}
	cur=k;
	for(ll i=k;i>=1;i--){
		if(maxi<last[i])
			cur=i;
		maxi=min(maxi,first[i]);
		suf[i]=k-cur;
	}
}
void solve(){
	ll n, mini, maxi, f=1;
	cin>>n;
	vector<ll> v(n), v1, vn;
	set<ll> seta;
	rep(i,0,n){
		cin>>v[i];
		if(i>0 and v[i]<v[i-1])f=0;
		seta.insert(v[i]);
	}
	if(f){
		cout<<"0";return;
	}
	mini=*seta.begin();
	maxi=*prev(seta.end());
	rep(i,0,n){
		if(v[i]!=mini)v1.pb(v[i]);
		if(v[i]!=maxi)vn.pb(v[i]);
	}
	ll k = sz(seta)-1, ans=hell;
	vector<ll> pre(k+5,0), suf(k+5,0), pre1(k+5,0), suf1(k+5,0);
	go(pre,suf,v1);
	go(pre1,suf1,vn);
	/*rep(i,0,k+1)cout<<pre[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<suf[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<pre1[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<suf1[i]<<" ";
	cout<<endl;*/
	ans=min(ans,1+pre[k]);
	ans=min(ans,1+suf[1]);
	ans=min(ans,1+pre1[k]);
	ans=min(ans,1+suf1[1]);
	rep(i,1,k){
		ans=min(ans,1+pre[i]+suf[k-i]);
		ans=min(ans,1+pre1[i]+suf1[k-i]);
	}
	cout<<ans;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin>>t;
	tt=t;
    while(t--){
        solve();
        if(t!=0)cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp> // Common file
#include<ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define rep(i,a,b) for( ll i = a ; i < b ; i+=1 )
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld",&x)
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define ld long double
ll t = 1, tt;
const ll inf=2e18;
inline ll add(ll x,ll y){x+=y;return x%hell;}
inline ll mult(ll x,ll y){x%=hell;y%=hell;return (1LL*x*y)%hell;}
inline ll powMod(ll base,ll expo){
	ll ans=1;
	while(expo){
		if(expo&1)ans=mult(ans,base);
		base=mult(base,base);
		expo>>=1;
	}
	return ans;
}
void go(vector<ll>& pre,vector<ll>& suf,vector<ll>& v){
	map<ll,ll> first, rank, last;
	set<ll> seta;
	rep(i,0,sz(v))seta.insert(v[i]);
	ll mini=0, cur=1, maxi=hell;
	for(auto i : seta)rank[i]=cur++;
	rep(i,0,sz(v))v[i]=rank[v[i]];
	seta.clear();
	//cout<<"in go\n";
	rep(i,0,sz(v)){
		//cout<<v[i]<<" ";
		if(seta.find(v[i])==seta.end())
			first[v[i]]=i;
		last[v[i]]=i;
		seta.insert(v[i]);
	}
	//cout<<"\n";
	ll k = sz(seta);
	cur=0;
	rep(i,1,k+1){
		if(mini>first[i])
			cur=i;
		mini=max(mini,last[i]);
		pre[i]=cur;
	}
	cur=k;
	for(ll i=k;i>=1;i--){
		if(maxi<last[i])
			cur=i;
		maxi=min(maxi,first[i]);
		suf[i]=k-cur;
	}
}
void solve(){
	ll n, mini, maxi, f=1;
	cin>>n;
	vector<ll> v(n), v1, vn;
	set<ll> seta;
	rep(i,0,n){
		cin>>v[i];
		if(i>0 and v[i]<v[i-1])f=0;
		seta.insert(v[i]);
	}
	if(f){
		cout<<"0";return;
	}
	mini=*seta.begin();
	maxi=*prev(seta.end());
	rep(i,0,n){
		if(v[i]!=mini)v1.pb(v[i]);
		if(v[i]!=maxi)vn.pb(v[i]);
	}
	ll k = sz(seta)-1, ans=hell;
	vector<ll> pre(k+5,0), suf(k+5,0), pre1(k+5,0), suf1(k+5,0);
	go(pre,suf,v1);
	go(pre1,suf1,vn);
	/*rep(i,0,k+1)cout<<pre[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<suf[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<pre1[i]<<" ";
	cout<<endl;
	rep(i,0,k+1)cout<<suf1[i]<<" ";
	cout<<endl;*/
	ans=k;
	ans=min(ans,1+pre[k]);
	ans=min(ans,1+suf[1]);
	ans=min(ans,1+pre1[k]);
	ans=min(ans,1+suf1[1]);
	rep(i,1,k+1){
		ans=min(ans,1+pre[i]+suf[i]);
		ans=min(ans,1+pre1[i]+suf1[i]);
	}
	cout<<ans;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin>>t;
	tt=t;
    while(t--){
        solve();
        if(t!=0)cout<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+50;
const int inf=0x3f3f3f3f;
int a[N],b[N],mi[N],ma[N],dp[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            mi[i]=inf,ma[i]=-1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
            mi[a[i]]=min(i,mi[a[i]]);
            ma[a[i]]=max(i,ma[a[i]]);
        }
        sort(b+1,b+1+n);
        int len=unique(b+1,b+1+n)-b-1;
        dp[1]=1;
        int ans=1;
        for(int i=2;i<=len;i++)
        {
            if(mi[b[i]]>ma[b[i-1]])
                dp[i]=dp[i-1]+1;
            else
                dp[i]=1;
            ans=max(ans,dp[i]);
        }
        printf("%d\n",len-ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;

int cas, n;
int a[maxn], b[maxn];
int mis[maxn], mas[maxn];
int dp[maxn];

int main() {
    cin >> cas;
    while(cas --) {
        cin >> n;
        memset(mas, -1, (n + 5) * sizeof(int));
        memset(mis, 0x3f, (n + 5) * sizeof(int));
        for(int i = 1; i <= n; i ++) {
            cin >> a[i];
            b[i] = a[i];
            mis[a[i]] = min(i, mis[a[i]]);
            mas[a[i]] = max(i, mas[a[i]]);
        }
        sort(b + 1, b + 1 + n);
        int len = unique(b + 1, b + 1 + n) - b - 1;
        dp[1] = 1;
        int ans = 1;
        for(int i = 2; i <= len; i ++) {
       //     cout << b[i] << " " << b[i - 1]  << endl;
        //    cout << mis[b[i]] << "  " << mas[b[i - 1]] << endl;
            if(mis[b[i]] > mas[b[i - 1]]) dp[i] = dp[i - 1] + 1;
            else dp[i] = 1;
            ans = max(dp[i], ans);
         //   cout << ans << endl;
        }
        cout << len - ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+50;
const int inf=0x3f3f3f3f;
int a[N],b[N],mi[N],ma[N],dp[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            mi[i]=inf,ma[i]=-1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
            mi[a[i]]=min(i,mi[a[i]]);
            ma[a[i]]=max(i,ma[a[i]]);
        }
        sort(b+1,b+1+n);
        int len=unique(b+1,b+1+n)-b-1;
        dp[1]=1;
        int ans=0;
        for(int i=2;i<=len;i++)
        {
            if(mi[b[i]]>ma[b[i-1]])
                dp[i]=dp[i-1]+1;
            else
                dp[i]=1;
            ans=max(ans,dp[i]);
        }
        printf("%d\n",len-ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+50;
const int inf=0x3f3f3f3f;
int a[N],b[N],mi[N],ma[N],dp[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(mi,inf,sizeof(mi));
        memset(ma,-1,sizeof(ma));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
            mi[a[i]]=min(i,mi[a[i]]);
            ma[a[i]]=max(i,ma[a[i]]);
        }
        sort(b+1,b+1+n);
        int len=unique(b+1,b+1+n)-b-1;
        dp[1]=1;
        int ans=0;
        for(int i=2;i<=len;i++)
        {
            if(mi[b[i]]>=ma[b[i-1]])
                dp[i]=dp[i-1]+1;
            else
                dp[i]=dp[i-1];
            ans=max(ans,dp[i]);
        }
        printf("%d\n",len-ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+50;
const int inf=0x3f3f3f3f;
int a[N],b[N],mi[N],ma[N],dp[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(mi,inf,sizeof(mi));
        memset(ma,-1,sizeof(ma));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
            mi[a[i]]=min(i,mi[a[i]]);
            ma[a[i]]=max(i,ma[a[i]]);
        }
        sort(b+1,b+1+n);
        int len=unique(b+1,b+1+n)-b-1;
        dp[1]=1;
        int ans=0;
        for(int i=2;i<=len;i++)
        {
            if(mi[b[i]]>ma[b[i-1]])
                dp[i]=dp[i-1]+1;
            else
                dp[i]=1;
            ans=max(ans,dp[i]);
        }
        printf("%d\n",len-ans);
    }
    return 0;
}