a = int(input())
b = input().rstrip(" ").split()
B = ''.join(b)
c = int(input())
e = []
f = ""
f = B
g = 0
h = 1
x = ""
y = ""
X = 0
for i in range(c):
    e = input().rstrip().split()
    f = f.replace(e[0],e[1])
    for i in range(a):
        x = x + " " + f[g:h]
        g = g + 1
        h = h + 1
    x = x.split()
    y = [int(s) for s in x]
    print(sum(y))
    x = ""
    g = 0
    h = 1