@staticmethod
def d():
    n = int(sys.stdin.readline().rstrip())
    a = [int(x) for x in sys.stdin.readline().split()]
    s = sum(a)
    cnt = Counter(a)
    q = int(sys.stdin.readline().rstrip())
    for _ in range(q):
        (b, c) = map(int, sys.stdin.readline().split())
        s += ((c - b) * cnt[b])
        print(s)
        cnt[c] += cnt[b]
        cnt[b] = 0
@staticmethod
def d():
    n = int(sys.stdin.readline().rstrip())
    a = [int(x) for x in sys.stdin.readline().split()]
    s = sum(a)
    cnt = Counter(a)
    q = int(sys.stdin.readline().rstrip())
    for _ in range(q):
        (b, c) = map(int, sys.stdin.readline().split())
        s += ((c - b) * cnt[b])
        print(s)
        cnt[c] += cnt[b]
        cnt[b] = 0