_,a=input(),input().split()
for _ in range(int(input())):
    org,rplc=input().split()
    sum=0
    for i,n in enumerate(a):
        if n ==org:
            a[i]=rplc
        sum=sum+int(a[i])
    print(sum)
    sum=0