n = int(input())
a = list(map(int,input().split()))
q = int(input())

while True:
    try:
        bc=list(input().split())
        b = bc[0]
        c = bc[1]

        # a = [c if i == b else i for i in a]
        a = map(str,a)
        a = "".join(a)
        a = a.replace(b,c)
        a.split()
        a = list(map(int, a))
        print(sum(a))
    except:
        break;