def main():
    n = int(input())
    d = dict()
    for a in list(map(int, input().split())):
        d[a] = d.get(a, 0) + 1

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:
            temp = d[b]
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

        print(sum(k*v for k, v in d.items()))

main()