from collections import Counter

from collections import defaultdict

def main():

    n = int(eval(input()))

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

    q = int(eval(input()))

    asum = sum(a)

    ac = Counter(a)

    acl = defaultdict(int)

    for ae in ac:

        acl[ae] = ac[ae]



    for _ in range(q):

        b, c = list(map(int, input().split()))

        rmed = acl[b]

        acl[c] += rmed

        acl[b] = 0

        asum -= b * rmed

        asum += c * rmed

        print(asum)



if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

from collections import Counter

def main():

    n = int(eval(input()))

    a = tuple(map(int, input().split()))

    suma = sum(a)

    aa = Counter(a)

    q = int(eval(input()))

    m = list(map(int, read().split()))

    for b, c in zip(m, m):

        kosu = aa[b]

        sa = c - b

        suma += sa * kosu

        print(suma)

        aa[b] = 0

        aa[c] += kosu



if __name__ == '__main__':

    main()

from collections import Counter
from collections import defaultdict


def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    q = int(eval(input()))
    asum = sum(a)
    ac = Counter(a)
    acl = defaultdict(int)
    for ae in ac:
        acl[ae] = ac[ae]
    for _ in range(q):
        b, c = list(map(int, input().split()))
        rmed = acl[b]
        acl[c] += rmed
        acl[b] = 0
        asum -= b * rmed
        asum += c * rmed
        print(asum)


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter


def main():
    n = int(eval(input()))
    a = tuple(map(int, input().split()))
    suma = sum(a)
    aa = Counter(a)
    q = int(eval(input()))
    m = list(map(int, read().split()))
    for b, c in zip(m, m):
        kosu = aa[b]
        sa = c - b
        suma += sa * kosu
        print(suma)
        aa[b] = 0
        aa[c] += kosu


if __name__ == "__main__":
    main()

+import sys + +read = sys.stdin.read +readlines = sys.stdin.readlines -from collections import defaultdict -    a = list(map(int, input().split())) +    a = tuple(map(int, input().split())) +    suma = sum(a) +    aa = Counter(a) -    asum = sum(a) -    ac = Counter(a) -    acl = defaultdict(int) -    for ae in ac: -        acl[ae] = ac[ae] -    for _ in range(q): -        b, c = list(map(int, input().split())) -        rmed = acl[b] -        acl[c] += rmed -        acl[b] = 0 -        asum -= b * rmed -        asum += c * rmed -        print(asum) +    m = list(map(int, read().split())) +    for b, c in zip(m, m): +        kosu = aa[b] +        sa = c - b +        suma += sa * kosu +        print(suma) +        aa[b] = 0 +        aa[c] += kosu