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