def myfunc(chars):
    global result
    if len(chars) == 1 or len(chars) == 3:
        result += 0
    else:
        for i in range(len(chars)-2):
            if chars[i] == '(' and chars[i+1].isalpha() and chars[i+2] == ')':
                chars = chars[:i] + chars[i+1] + chars[i+3:]
                break
        for j in range(len(chars)-1):
            if chars[j].isalpha() and chars[j+1].isalpha():
                result += c[chars[j]][0]*c[chars[j]][1]*c[chars[j+1]][1]
                comb = chars[:j]+chars[j+2:]
                s = sum([comb.count(k) for k in d])
                if s >= 1:
                    c[d[s]] = [c[chars[j]][0],c[chars[j+1]][1]]
                    chars = chars[:j] + d[s] + chars[j+2:]
                else:
                    c['a'] = [c[chars[j]][0],c[chars[j+1]][1]]
                    chars = chars[:j] + 'a' + chars[j+2:]
                myfunc(chars)
                break
#            return temp

while True:
    try:
        a = int(input())
        b = [chr(i) for i in range(ord('A'),ord('Z')+1)]
        d = [chr(i) for i in range(ord('a'),ord('z')+1)]
        c = {}
        result = 0
        for i in range(a):
            c[b[i]] = list(map(int,input().split()))
        order = input().upper()
        myfunc(order)
        print(result)
    except:
        break