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

def main():
    b_l = []
    c_l = []
    for i in range(0, q):
        b, c = map(str, input().split())
        b_l.append(b)
        c_l.append(c)

    new_a = []
    for j in range(0, q):
        if j==0:
            a_replace = [s.replace(b_l[j], c_l[j]) for s in a]
            new_a.append(a_replace)
            l_si_i = [int(s) for s in a_replace]
            print(sum(l_si_i))
        else:
            a_replace = [s.replace(b_l[j], c_l[j]) for s in new_a[j-1]]
            new_a.append(a_replace)
            l_si_i = [int(s) for s in a_replace]
            print(sum(l_si_i))
    return
main()