def main():
    from collections import Counter
    n, *i = map(int, open(0).read().split())
    a = i[:n]
    _ = i[n]
    d = Counter(a)
    s = sum(k * v for k, v in d.items())
    ans = []
    for b, c in zip(*[iter(i[n + 1:])] * 2):
        v, w = d.get(b), d.get(c)
        if v and w:
            d.pop(b)
            d[c] += v
            s += (c-b) * v
        elif v:
            d[c] = v
            s += (c-b) * v
        ans.append(s)

    print(*ans, sep="\n")


if __name__ == '__main__':
    main()
