
def sum_dict(d):
    num = 0
    for k, v in d.items():
        num += k * v
    return num


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    BC = []
    for i in range(Q):
        BC.append(tuple(map(int, input().split())))

    d = {i: 0 for i in range(10**5)}
    for a in A:
        d[a] += 1

    for b, c in BC:
        d[c] += d[b]
        d[b] = 0
        print(sum_dict(d))
