def solve():
    dict_A = {}
    for a in A:
        dict_A[a] = dict_A.get(a, 0) + 1
    for i in range(Q):
        tmp = dict_A.get(B[i], 0)
        dict_A[B[i]] = 0
        dict_A[C[i]] = dict_A.get(C[i], 0) + tmp
    ret = 0
    for k in dict_A:
        ret += k * dict_A[k]
    print(ret)

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B, C = [], []
    for _ in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
    solve()