F - インタプリタをつくろう 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

問題文

一文字の変数と一桁の整数および +- からなる式 S が与えられます。
例えば、1+2+33-a+2+c+01 は与えられる場合があります。
対して、12+34-aba/2+5 は与えられません。それぞれ、12 が一桁の整数ではない、ab が一文字の変数ではない(a\times b と解釈することもありません)、/ が含まれているためです。

変数の値が N 個の組 (c _ 1,v _ 1),(c _ 2,v _ 2),\ldots,(c _ N,v _ N) として与えられます。 i 番目の組 (c _ i,v _ i) は、変数 c _ i の値が v _ i であることを表します。

S を計算した結果の値を求めてください。

制約

  • S は長さ 1 以上 100 未満の文字列
  • S の長さは奇数
  • S の奇数番目の文字は英小文字か数字
  • S の偶数番目の文字は +- のどちらか
  • 1\leq N\leq 26
  • c _ i は英小文字 (1\leq i\leq N)
  • i\neq j\implies c _ i\neq c _ j\ (1\leq i,j\leq N)
  • 英小文字 cS に出現するなら、ある i\ (1\leq i\leq N) が存在して c=c _ i
  • -100\leq v _ i\leq100\ (1\leq i\leq N)
  • N および v _ 1,v _ 2,\ldots,v _ N はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

S
N
c _ 1 v _ 1
c _ 2 v _ 2
\vdots
c _ N v _ N

出力

答えを出力せよ。


入力例 1

2+a-7
1
a 18

出力例 1

13

a=18 のとき、2+a-7=2+18-7=13 です。 よって、13 と出力してください。


入力例 2

1
4
s 29
b -1
m -8
f -2

出力例 2

1

S に変数が含まれなかったり、S に含まれない変数の情報が与えられる場合もあります。


入力例 3

0-1+8-i+m-d-3-a-m+4+a+m-2+5+5-5
10
a 68
b -88
d -10
e -19
i -33
l -44
m -39
n 50
r -36
y 63

出力例 3

15

Problem Statement

You are given an expression S consisting of one-letter variables, one-digit integers, and + and - signs.
For example, 1+2+3, 3-a+2+c+0, and 1 can be given,
but 12+3, 4-ab, and a/2+5 cannot, because 12 is not a one-digit integer, ab is not a one-letter variable (nor is it interpreted as a\times b), and it contains /, respectively.

You are given the values of the variables as N pairs, (c _ 1,v _ 1),(c _ 2,v _ 2),\ldots, and (c _ N,v _ N). The i-th pair (c _ i,v _ i) means that the variable c _ i should evaluate to the value v _ i.

Evaluate the expression S.

Constraints

  • S is a string of length between 1 and 99, inclusive.
  • S has an odd length.
  • Each character at an odd-numbered position of S is a lowercase English letter or a digit.
  • Each character at an even-numbered position of S is + or -.
  • 1\leq N\leq 26
  • c_i is a lowercase English letter (1\leq i\leq N).
  • i\neq j\implies c _ i\neq c _ j\ (1\leq i,j\leq N)
  • If a lowercase English letter c occurs in S, then there exists i\ (1\leq i\leq N) such that c=c _ i.
  • -100\leq v _ i\leq100\ (1\leq i\leq N)
  • N, v _ 1,v _ 2,\ldots, and v _ N are all integers.

Input

The input is given from Standard Input in the following format:

S
N
c _ 1 v _ 1
c _ 2 v _ 2
\vdots
c _ N v _ N

Output

Print the answer.


Sample Input 1

2+a-7
1
a 18

Sample Output 1

13

When a=18, we have 2+a-7=2+18-7=13. Thus, 13 should be printed.


Sample Input 2

1
4
s 29
b -1
m -8
f -2

Sample Output 2

1

S may not contain a variable. Also, a value may be given for a variable that is not contained in S.


Sample Input 3

0-1+8-i+m-d-3-a-m+4+a+m-2+5+5-5
10
a 68
b -88
d -10
e -19
i -33
l -44
m -39
n 50
r -36
y 63

Sample Output 3

15