ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
a = li()
a.sort()

s = sum(a)

q = ii()

from bisect import bisect_left, bisect_right
def bisect_find(a, x):
    i = bisect_left(a, x)
    if i == len(a):
        return -1
    elif a[i] != x:
        return -1
    return i

def bisect_r_find(a, x):
    i = bisect_right(a, x)
    # if i == len(a):
    #     return -1
    return i

for i in range(q):
    b,c = mi()


    # 左を探す
    l = bisect_find(a,b)
    # 右を探す
    if l != -1:
        r = bisect_r_find(a[l:],b) + l
        # 個数をカウント
        count = r-l
        # 前回の合計との差を求める
        s = s + (c-b)*count

        # 配列を上書き
        # a[l:r] = [c]*count

        # print('a_p',a)

        if b > c:
            # j = bisect_left(a[:l],c)
            jr = bisect_right(a[:l],c)  
            jl = bisect_left(a[:jr],c)
            a = a[:jr] + [c]*count + a[jr:l] + a[r:]
        else:
            jl = bisect_left(a[r:],c) + r
            jr = bisect_right(a[jl:],c) + jl 
            # print('j',jl,jr,l,r)
            a = a[:l] + a[r:jl] + [c]*count  + a[jl:]
            # print(a[:l] , a[r:jl] , [c]*count  , a[jl:])

        # print('a',a)

        # a.sort()
    # print('-------------ans----------------',s)
    print(s)



    