import java.util.LinkedList;
import java.util.Queue;
class MyStack {
Queue<Integer> q1=new LinkedList<Integer>();
Queue<Integer> q2=new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
if(q1.isEmpty()&&q2.isEmpty())
{
q1.add(x);
}
else
{
if(!q1.isEmpty())
q1.add(x);
else
q2.add(x);
}
}
// Removes the element on top of the stack.
public void pop() {
if(!q1.isEmpty())
{
while(q1.size()!=1)
{
q2.add(q1.poll());
}
q1.poll();
}
else
{
while(q2.size()!=1)
{
q1.add(q2.poll());
}
q2.poll();
}
}
// Get the top element.
public int top() {
int res=0;
if(!q1.isEmpty())
{
while(!q1.isEmpty())
{
res=q1.poll();
q2.add(res);
}
}
else
{
while(!q2.isEmpty())
{
res=q2.poll();
q1.add(res);
}
}
return res;
}
// Return whether the stack is empty.
public boolean empty() {
if(q1.isEmpty()&&q2.isEmpty())
return true;
else
return false;
}
}