import collections
s=str(input())
s = list(map(int, input().split()))
q = int(input())
x = [list(map(int,input().split())) for i in range(q)]
c = collections.Counter(s)
ans=s
for i in range(q):
    if x[i][0] not in c.keys():
        print(ans)
        continue
    tmp = c[x[i][0]]
    del c[x[i][0]]
    c[x[i][1]] = c[x[i][1]]+tmp
    ans=0
    for j in c.keys():
        ans+=j*c[j]
    print(ans)