"use strict";

var puts = console.log,
    p = require("util").print,
    cin = parseInput();

function expression() {
    var ret = term();
    while(true) {
        if(line[i] === "+") {
            i++;
            ret += term();
        } else {
            break;
        }
    }
    return ret;
}

function term() {
    var ret = factor();
    while(true) {
        if(line[i] === "*") {
            i++;
            ret *= factor();
        } else {
            break;
        }
    }
    return ret;
}

function factor() {
    var ret;
    if ( line[i] === "(" ) {
        i++;    // 開きカッコ
        ret = expression();
        i++;    // 閉じカッコ
    } else {
        ret = number();
    }
    return ret;
}

function number() {
    // 数字の場合？
    if(/^\d/.test(line[i])) {
        var ret = 0;
        while(line[i] && /^\d/.test(line[i])) ret = ret * 10 + +line[i++];
    // 英語の場合
    } else {
        var atm = line[i++], mag = 1, ret;

        if(line[i] && /[a-z]/.test(line[i])) atm += line[i++];
        if(/\d/.test(line[i])) mag = +line[i++];

        if(!map[atm]) throw new TypeError("UNKNOWN");
        ret = map[atm] * mag;
    }

    return ret;
}

function normalize(line) {
    var ret = "",
        idx = 0;

    while(idx < line.length) {
        var t = line[idx];
        if ( t == "(" || /[A-Z][a-z]?/.test(t) ) {
            ret += "+";
        } else if ( /\d/.test(t) ) {
            ret += "*";
            while(/\d/.test(line[idx+1])) {
                ret += line[idx++];
            }
        }
        ret += line[idx++];
    }

    return ret.replace(/(^|\()\+/g, "$1");
}

var map = {},
    line, i;

while(!cin.hasNext("END_OF_FIRST_PART")) {
    map[cin.next()] = cin.nextInt();
}
// puts(map);
cin.next(); // 区切りは読み飛ばし

while(!cin.hasNext("0")) {
    i = 0;
    line = cin.next();

    // puts("normalized: ", normalize(line));
    line = normalize(line);

    try {
        var result = expression();
        puts(result);
    } catch(e) {
        puts(e.message);
    }
}

function parseInput(useSplitSpace) {
    var index = 0,
        ret = [],
        input = require("fs").readFileSync("/dev/stdin", "utf8"),
        inputs = input.replace(/\r/g, '').split("\n");

    useSplitSpace = useSplitSpace || true;

    // 入力を改行/空白で区切り平坦な配列に変換
    inputs.forEach(function(val) {
        if ( useSplitSpace && val !== "" ) {
            val.split(" ").forEach(function(el) {
                if ( el !== "" ) ret.push(el);
            });
        } else {
            ret.push(val);
        }
    });

    return {
        hasNext: function(val) {
            if ( typeof val === "undefined" ) {
                return typeof ret[index] !== "undefined";
            } else {
                return ret[index] === val;
            }
        },
        next: function() {
            if ( typeof ret[index+1] === "undefined" ) throw new RangeError("Index out of bounds at '" + index+1 + "'");
            return ret[index++];
        },
        nextNumber: function () {
            if ( !/^\d+$/.test(this.top()) ) throw new TypeError("'" + this.top() + "' cannot convet to Number");
            return +this.next();
        },
        nextInt: function() {
            return parseInt(this.nextNumber());
        },
        top: function() {
            return ret[index];
        },
        rewind: function() {
            index = 0;
        }
    };
}

Measured memory_bytes: 9812