N = int(input())
asses_list = list(map(str, input().split()))
Q = int(input())

for i in range(Q):
    B, C = input().split()

    for k in range(N):
        if asses_list[k] == B:
            asses_list[k] = C
    total = 0
    for s in asses_list:
        s_int = int(s)
        total += s_int

    print(total)


