First 2 test cases failed

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

int main()
{
//generate prime sieve
vector primes;
primes.push_back(2);
primes.push_back(3);
for(int i = 5; i <= 10000; i++)
{
int no = 0;
for(int j = 2; j*j <= i; j++)
{
if(i%j == 0)
no = 1;
}
if(!no)
primes.push_back(i);
}

int n,q;
cin>>n>>q;
stack<int> a1,b1;
stack<int> s;
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    s.push(x);
}
for(int i=0;i<q;i++){
    if(s.empty()) break;

    int ithprime=primes[i];
    while(s.empty()==false){
        int ce=s.top();
        s.pop();

        if(ce%ithprime == 0){
            b1.push(ce);
        }
        else{
            a1.push(ce);
        }
    }
}

while(!b1.empty())
    {
        cout<<b1.top()<<endl;
        b1.pop();
    }
    s = a1;
while(!a1.empty()){
        a1.pop();
}
while(!s.empty())
{
    cout<<s.top()<<endl;
    s.pop();
}


return 0; 

}

Hi, refer this https://ide.codingblocks.com/s/623617
and pls when u share ur code save it on IDE and then send link…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.