import sys 

def main(n, a_n, q, bc_n):
    m = {}
    for i in range(n):
        if a_n[i] not in m:
            m[a_n[i]] = 0 
        m[a_n[i]] += 1

    s = []
    for j in range(q):
        b, c = bc_n[j]

        if b in m:
            if c not in m:
                m[c] = m[b]
            else:
                m[c] += m[b]
            del m[b]

        ts = 0 
        for k, v in m.items():
            ts += k * v 
        s.append(ts)
    return s


n = int(sys.stdin.readline().strip())
a_n = [int(x) for x in sys.stdin.readline().strip().split(" ")]
q = int(sys.stdin.readline().strip())
bc_n = []
for _ in range(q):
    b, c = [int(x) for x in sys.stdin.readline().strip().split(" ")]
    bc_n.append((b, c)) 
for i in main(n, a_n, q, bc_n):
    print(i)
