#include
using namespace std;
struct Node {
int data;
Node *next;
};
Node* queue;
Node* head;
void push(int data) {
Node* new_node = new Node;
new_node->data = data;
new_node->next = nullptr;
if (queue == nullptr) {
queue = new_node;
head = new_node;
}
else {
queue->next = new_node;
queue = new_node;
}
}
int pop() {
int rtn;
if (queue == nullptr) return -1;
Node * tmp = head;
head = head->next;
rtn = tmp->data;
delete tmp;
return rtn;
}
void display() {
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << "\n";
}
int main() {
int input;
while (1) {
cin >> input;
if (input == -1) break;
if (input >= 1 &&input<=9) {
push(input);
}
else
{
int ans = pop();
if (ans==-1) {
cout << "에러\n";
}
else
cout << ans << " 뺐음\n";
}
display();
}
return 0;
}