Playing Cards Stack -Only 1 test case is passing, 2 failed

I am not able to figure out whats wrong when , since on manual input its giving correct ans

import java.util.*;
import java.util.Stack;
public class Main {
	static int ret=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int q = sc.nextInt();
		
		Stack<Integer> stack = new Stack<Integer>();
		Stack<Integer> stack1 = new Stack<Integer>();
		Stack<Integer> stack2 = new Stack<Integer>();
		for(int i=0;i<n;i++) {
			stack.push(sc.nextInt());
		}
		int num = prime(q);
		while(!stack.isEmpty()) {

			if(stack.peek()%num == 0) {
				stack1.push(stack.pop());
			}
			else {
				stack2.push(stack.pop());
			}
		}
		
		while(!stack1.isEmpty())
		System.out.println(stack1.pop()+" ");
		
		while(!stack2.isEmpty())
		System.out.println(stack2.pop()+" ");
		

	}
	
	public static int prime(int x)
	{	

        int [] prime = new int [100000];
        int index = 2;
        prime[1] = 2;
        for( int i = 3 ; i<100000 ; i++)
        {
            int count = 0;
            for( int j = 2 ; j*j<=i ; j++)
            {
                if( i % j == 0)
                {
                    count = 1;
                    break;
                }
            }
			if( count == 0)
                {
                    prime[index] = i;
                    index++;
                }
        }
        return prime[x];
	}

}

hey @anubhav_mishra
try for this
5 2
3 4 7 6 5
correct code :
4
6
3
5
7

Hi I made changes in my code to give the desired output as mentioned in ques.But I dont know why my complete output is not coming, there is some thing wrong with my code and i am not able to spot it

import java.util.*;
import java.util.Stack;
public class PlayingCardsStack {
	//static int ret=0;
	static int [] prime = new int [100000];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int q = sc.nextInt();
		
		Stack<Integer> stack = new Stack<Integer>();
		Stack<Integer> stack1 = new Stack<Integer>();
		Stack<Integer> stack2 = new Stack<Integer>();
		for(int i=0;i<n;i++) {
			stack.push(sc.nextInt());
		}
		prime(q);

		for(int k=1;k<=q;k++)
		{	
			if(stack.isEmpty())
				break;
			
			int num = prime[k];
		while(!stack.isEmpty()) {

			if(stack.peek()%num == 0) {
				stack1.push(stack.pop());
			}
			else {
				stack2.push(stack.pop());
			}
			
		  }
		 while(!stack1.isEmpty())
	        {
	           System.out.println(stack1.peek());
	            stack1.pop();
	        }
	        stack = stack2;
	        while(!stack2.isEmpty())
	            stack2.pop();
		}
	while(!stack.empty())
		  {
		      System.out.println(stack.peek());
		      stack.pop();
          }
	}
	
	
	
	
	public static void prime(int x)
	{	
        int index = 2;
        prime[1] = 2;
        for( int i = 3 ; i<100000 ; i++)
        {
            int count = 0;
            for( int j = 2 ; j*j<=i ; j++)
            {
                if( i % j == 0)
                {
                    count = 1;
                    break;
                }
            }
			if( count == 0)
                {
                    prime[index] = i;
                    index++;
                }
        }

	}

}

please see to it and point out the error the prime func works fine there is something wrong in the main func of my code only