N = int(input())
A = list(map(int, input().split()))
A.sort()
Q = int(input())

for i in range(Q):
    b, c = map(int, input().split())
    s = 0
    e = N
    h = int((s+e)/2)
    while A[h] != b and s != h :
        if A[h] < b:
            s = h
            h = int((s+e)/2)
        elif A[h] > b:
            e = h
            h = int((s+e)/2)
    X = []
    for j in range(s, e):
        if A[j] == b:
            X.append(j)
            A[j] = c
    print(sum(A))
    if len(X) > 0:
        A = A[:X[0]] + A[X[-1]:]
        s = 0
        e = len(A)
        h = int((s+e)/2)
        while A[h] != c and s != h :
            if A[h] < c:
                s = h
                h = int((s+e)/2)
            elif A[h] > c:
                e = h
                h = int((s+e)/2)
        A = A[:h] + [c]*len(X) + A[h+1:]