def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    import numpy as np
    A.sort()
    ans = sum(A)
    from bisect import bisect_left,bisect_right

    for i in range(q):
        b,c = map(int, input().split())
        insert = bisect_left(A,c)
        start = bisect_left(A,b)
        end = bisect_right(A,b)
        A = A[:insert] + [c]*(end-start) + A[insert:]
        if b > c:
            A = A[:end] + A[2*end-start:]
        else:
            A = A[:start] + A[end:]
        ans += (c-b)* (end-start)
        print(ans)


if __name__ == '__main__':
    main()