def main():
    N = int(input())
    A = input()
    Q = input()

    answer = sum(list(map(int, A.split())))
    A = A.replace(" ", "")

    operateList = (open(0).read().split("\n"))[:-1]

    for operate in operateList:
        B, C = operate.split()
        numB = A.count(B)

        answer = answer + (int(C) - int(B)) * numB
        print(answer)

        A = A.replace(B, C)

if __name__ == "__main__":
    main()