#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING

#include <bits/stdc++.h>

using namespace std;
char line[1 << 10 | 1], buf[1 << 10 | 1], lastop[10];
int mp[1 << 8];

void init() {
    for (char ch = '0'; ch <= '9'; ch++)
        mp[ch] = ch - '0';
    for (char ch = 'A'; ch <= 'Z'; ch++)
        mp[ch] = ch - 'A' + 10;
    for (char ch = 'a'; ch <= 'z'; ch++)
        mp[ch] = ch - 'a' + 10;
    for (int i = 0; i <= 9; i++)
        mp[i] = i + '0';
    for (int i = 10; i <= 35; i++)
        mp[i] = i - 10 + 'A';
}

unsigned long long __atoi(const char *str, int radix) {
    unsigned long long ret = 0;
    while (*str)
        ret = ret * radix + mp[*str++];
    return ret;
}

void __itoa(unsigned long long val, char *str, int radix) {
    int len = 0;
    for (; val; val /= radix)
        str[len++] = mp[val % radix];
    str[len] = 0;
    if (len == 0) strcpy(str, "0");
    for (char *p1 = str, *p2 = str + len; p1 < p2;)
        swap(*p1++, *--p2);
}

int main() {
    init();
    int n, curBase = 10;
    unsigned long long curNum = 0;
    scanf("%d", &n);
    while (n--) {
        do
            fgets(line, 1 << 10 | 1, stdin);
        while (strlen(line) < 3);
        do {
            if (memcmp(line, "NUM", 3) == 0) {
                sscanf(line, "%*s%s", buf);
                unsigned long long val = __atoi(buf, curBase);
                do {
                    if (memcmp(lastop, "RST", 3) == 0) {
                        curNum = val;
                        break;
                    }
                    if (memcmp(lastop, "ADD", 3) == 0) {
                        curNum += val;
                        break;
                    }
                    if (memcmp(lastop, "SUB", 3) == 0) {
                        curNum -= val;
                        break;
                    }
                    if (memcmp(lastop, "MUL", 3) == 0) {
                        curNum *= val;
                        break;
                    }
                    if (memcmp(lastop, "DIV", 3) == 0) {
                        curNum /= val;
                        break;
                    }
                    if (memcmp(lastop, "MOD", 3) == 0) {
                        curNum %= val;
                        break;
                    }
                } while (false);
                break;
            }
            if (memcmp(line, "ADD", 3) == 0 ||
                memcmp(line, "SUB", 3) == 0 ||
                memcmp(line, "MUL", 3) == 0 ||
                memcmp(line, "DIV", 3) == 0 ||
                memcmp(line, "MOD", 3) == 0) {
                sscanf(line, "%s", lastop);
                break;
            }
            if (memcmp(line, "CHANGE", 6) == 0) {
                sscanf(line, "%*s%d", &curBase);
                break;
            }
            if (memcmp(line, "EQUAL", 5) == 0) {
                __itoa(curNum, buf, curBase);
                puts(buf);
                break;
            }
            if (memcmp(line, "CLEAR", 5) == 0) {
                strcpy(lastop, "RST");
                break;
            }
        } while (false);
    }
    return 0;
}
