import streams, sequtils, strutils, algorithm, math, future, sets, tables, hashes
proc `ceilDiv`*[T](x, y: T): T = x div y + ord(x mod y != 0)
proc `//=`*(x: var SomeInteger; y: SomeInteger) = x = x div y
proc `%=`*(x: var SomeInteger; y: SomeInteger) = x = x mod y
var
    N = stdin.readLine.parseInt
    d = initTable[int, int]()
    A = stdin.readLine.split.map(parseInt)
    b, c:int
    ans = A.sum
    Q = stdin.readLine.parseInt
for a in A:
    if not d.hasKey(a):
        d[a] = 1
    else:
        d[a] += 1
for i in 0..Q-1:
    (b, c) = stdin.readLine.split.map(parseInt)
    if d.hasKey(b):
        ans -= b * d[b]
        ans += c * d[b]
        if not d.hasKey(c):
            d[c] = d[b]
        else:
            d[c] += d[b]
    echo ans
