#include <iostream>
#include <string>
using namespace std;

#define in cin

int a, b;

int sumar(const string& num) {
    int i = atoi(num.c_str()), costo = 0;
    while (i != 0) {
        costo += (i % 2 == 0) ? a : b;
        i /= 2;
    }
    return costo;
}

int main() {
    string opcion;
    int set = 1;
    while (in >> opcion && opcion != "#") {
        if (opcion == "COST") {
            in >> a >> b;
            cout << "Set " << set++ << endl;
        } else
            cout << sumar(opcion) << endl;
    }
    return 0;
}

