n = int(input())
a = sorted(list(map(int, input().split())))
q = int(input())
l1 = [0]*(10**5+10)
l2 = [0]*(10**5+10)
index = 0
l1[0] = a[0]
l2[0] = 1
for i in a[1:]:
    if i == l1[index]:
        l2[index] += 1
    else:
        index += 1
        l1[index] = i
        l2[index] = 1
s = sum(a)
for _ in range(q):
    b, c = map(int, input().split())

    i = 0
    while i < index + 2:
        if (b == l1[i]):
            break
        i += 1
    if i > index:
        print(s)
        continue

    j = 0
    while j < index + 2:
        if(c == l1[j]):
            break
        j += 1
    if j > index:
        index = j
        l1[index] = c
    s += (c - b) * l2[i]
    l2[j] += l2[i]
    l2[i] = 0
    print(s)
