import bisect
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b=[]
c=[]
aa=[]
sum=0
for i in range(n):
    sum+= a[i]
for i in range(q):
    bb,cc = map(int,input().split())
    b.append(bb)
    c.append(cc)
a.sort()

for i in range(q):
    bb,cc=b[i],c[i]
    l= bisect.bisect_left(a,bb)
    h= bisect.bisect_right(a,bb)
    ll=bisect.bisect_left(a,cc)
    hh= bisect.bisect_right(a,cc)
    if bb>cc:
        a[ll+h-l:h],a[ll:ll+h-l] = a[ll:l],[cc]*(h-l)
    else:
        a[l+hh-h:hh],a[l:l+hh-h] = [cc]*(h-l),a[h:hh]
    sum=sum + (cc-bb)*(h-l)
    print(sum)