from itertools import groupby
def main():
    n=int(input())
    aa=sorted(map(int,input().split()))
    d={}
    for k,g in groupby(aa):
        d[k] = len(list(g))
    q=int(input())
    bc=[]
    for _ in range(q):
        b,c=map(int,input().split())
        bc.append((b,c))
    for b,c in bc:
        if b in d:
            d[c] = d.setdefault(c, 0) + d[b]
            del d[b]
        print(sum(k*v for k,v in d.items()))
main()