import sequtils
import strutils
import tables


proc calc(table: Table[int, int]): int64 =
    result = 0.toBiggestInt()
    for a, b in table:
        result += (a * b)


discard stdin.readLine.parseInt()
var A: seq[int] = stdin.readline.split.map(parseInt)

var aTable = initTable[int, int]()

for a in A:
    if aTable.hasKey(a):
        aTable[a] += 1
    else:
        aTable[a] = 1

var Q: int = stdin.readLine.parseInt()
for _ in 1..Q:
    var cmd: seq[int] = stdin.readline.split.map(parseInt)
    if not aTable.hasKey(cmd[0]):
        continue
    if aTable.hasKey(cmd[1]):
        aTable[cmd[1]] += aTable[cmd[0]]
    else:
        aTable[cmd[1]] = aTable[cmd[0]]
    aTable.del(cmd[0])
    echo(calc(aTable))