#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_QUEUE 40

int main() {
    char input[1000];
    fgets(input, sizeof(input), stdin);

    char *token = strtok(input, " \t\n"); // \s+ equivalent in C
    int queue[MAX_QUEUE];
    int front = 0, rear = 0;

    while (token != NULL) {
        if (strcmp(token, "B") == 0) {
            if (front < rear) {
                printf("%d\n", queue[front++]);
            } else {
                printf("Invalid input.\n");
            }
        } else {
            int valid = 1;
            for (int i = 0; token[i]; i++) {
                if (!isdigit(token[i])) {