import collections

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

    a_dict = collections.Counter(a)
    a_dict = {key: key*value for key, value in a_dict.items()}
    for _ in range(q):
        b, c = map(int, input().split())
        if b in a_dict:
            if c in a_dict:
                a_dict[c] += (a_dict.pop(b)//b) * c
            else:
                a_dict[c] = (a_dict.pop(b)//b) * c
        print(sum(a_dict.values()))


if __name__ == '__main__':
    main()
