
a = int(input())
s = list(map(int, input().split()))
q = int(input())

val = {}

for i in s:
    if i not in val:
        val[i] = 1
    else:
        val[i] += 1

# print(val)

while q:
    c, d = map(int, input().split())
    # print(c, d)
    if c in val:
        if d in val:
            val[d] += val[c]
        else:
            val[d] = val[c]
        del val[c]
    # print(val)
    suss = 0
    for k, v in val.items():
        # print(k, v)
        suss += k *v

    print(suss)

    q -= 1

