import collections

n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = []
c = []
for i in range(q):
    tmpb,tmpc = map(int,input().split())
    b.append(tmpb)
    c.append(tmpc)

dic = collections.Counter(a)
val = 0
for i in range(q):
    if b[i] in dic:
        if c[i] not in dic:
            dic[c[i]] = 0
        dic[c[i]] += dic[b[i]]
        dic[b[i]] = 0
    for key, value in dic.items():
        val +=key*value
    print(val)
    val=0