from collections import Counter

def main():
    n = int(input())
    a = Counter(map(int, input().split()))
    temp, a = listed(a)
    q = int(input())
    bc = [tuple(map(int, input().split())) for _ in range(q)]
    
    for b, c in bc:
        t = a[b]
        if t == 0:
            print(temp)
            continue
        a[b] = 0
        temp -= b*t
        a[c] += t
        temp += c*t
        print(temp)

def listed(a):
    x = [0] * 10**5
    kei = 0
    for i,v in a.items():
        x[i] = v
        kei += i*v
    return kei, x

if __name__ == '__main__':
    main()