I am getting a MLE error on question Playing with cards (using stack)

#include <iostream>
#include <stack>

bool checkPrime(int s)
{
	for (int j = 2; j < s; j++)
	{
		if (s % j == 0) {
			return true;
		}
	}
	return false;
}

int main()
{
	int n;
	std::cin >> n;
	std::stack<int> a;
	std::stack<int> b;
	std::stack<int> s;
	int q;
	std::cin >> q;
	for (int i = 0; i < q; i++)
	{
		for (int j = 0; j < n; j++)
		{
			int t;
			std::cin >> t;
			s.push(t);
		}
		while(true)
		{
			if (s.empty())
			{
				break;
			}
			if (checkPrime(s.top()))
			{
				b.push(s.top());
				s.pop();
			}
			else
			{
				a.push(s.top());
				s.pop();
			}
		}
		while (true)
		{
			std::cout << b.top() << std::endl;
			b.pop();
			if (b.empty())
			{
				break;
			}
		}
		while(true)
		{
			std::cout << a.top() << std::endl;
			a.pop();
			if (a.empty())
			{
				break;
			}
		}
	}
}

// Not sure how I reduce memory here.