n=int(input())
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
    b,c=map(str,input().split())
    a = [str(s) for s in a]
    a=[s.replace(b,c) for s in a]
    a = [int(s) for s in a]
    print(sum(a))