CodeEff/ECCO huggingface dataset:
# Original solution
def main():
    N = int(input().strip())
    A = [int(_a) for _a in input().strip().split()]
    Q = int(input().strip())
    BCS = []
    for i in range(Q):
        BCS.append([int(_bc) for _bc in input().strip().split()])

    Amap = {}
    for a in A:
        if a not in Amap:
            Amap[a] = 0
        Amap[a] += 1


    for bc in BCS:
        b = bc[0]
        c = bc[1]
        if b in Amap:
            num = Amap[b]
            del Amap[b]
        else:
            num = 0

        if c not in Amap:
            Amap[c] = 0
        Amap[c] += num

        total = 0
        for k,v in list(Amap.items()):
            total += (k*v)
        print(total)

main()



# Optimized solution
def main():
    N = int(input().strip())
    A = [int(_a) for _a in input().strip().split()]
    Q = int(input().strip())
    BCS = []
    for i in range(Q):
        BCS.append([int(_bc) for _bc in input().strip().split()])

    Amap = {}
    for a in A:
        if a not in Amap:
            Amap[a] = 0
        Amap[a] += 1

    total = 0
    for k,v in list(Amap.items()):
        total += (k*v)

    for bc in BCS:
        b = bc[0]
        c = bc[1]
        if b in Amap:
            num = Amap[b]

            del Amap[b]
            if c not in Amap:
                Amap[c] = 0
            Amap[c] += num

            total = total + num*c - num*b

        print(total)

main()
# Original solution
import collections
b=[]
c=[]
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
d=collections.Counter(a)
for i in range(q):
    x,y=list(map(int,input().split()))
    b.append(x)
    c.append(y)
for i in range(q):
    tmp=d[b[i]]
    d[b[i]]=0
    d[c[i]]+=tmp
    sum=0
    for key,val in list(d.items()):
        sum+=key*val
  
    print(sum)


# Optimized solution
import collections
b=[]
c=[]
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
d=collections.Counter(a)
for i in range(q):
    x,y=list(map(int,input().split()))
    b.append(x)
    c.append(y)
sum=0
for i in range(n):
    sum+=a[i]
for i in range(q):
    tmp=d[b[i]]
    d[b[i]]=0
    d[c[i]]+=tmp
    sum=sum-b[i]*tmp+tmp*c[i]
    print(sum)
# Original solution
n = int(eval(input()))
n_list = list(map(int, input().split()))
q = int(eval(input()))

n_dic = {}
for i in n_list:
    n_dic.setdefault(i, 0)
    n_dic[i] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in n_dic:
        n_dic.setdefault(c, 0)
        n_dic[c] += n_dic[b]
        del n_dic[b]

    result = 0
    for k in n_dic:
        result += k * n_dic[k]
    print(result)


# Optimized solution
n = int(eval(input()))
n_list = list(map(int, input().split()))
q = int(eval(input()))

n_dic = {}
for i in n_list:
    n_dic.setdefault(i, 0)
    n_dic[i] += 1

result = 0
for k in n_dic:
    result += k * n_dic[k]

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in n_dic:
        n_dic.setdefault(c, 0)
        n_dic[c] += n_dic[b]
        result = result + (c-b)*n_dic[b]
        del n_dic[b]

    print(result)
# Original solution
import sys
input = sys.stdin.readline

# D - Replacing
def get_sum(dic):
	sum = 0
	
	for k, v in list(dic.items()):
		sum += k * v
	
	return sum


n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
dic = dict()

for ai in a:
	dic.setdefault(ai, 0)
	dic[ai] += 1

for i in range(q):
	b, c = list(map(int, input().split()))

	count = dic.pop(b, 0)
	
	if count > 0:
		dic.setdefault(c, 0)
		dic[c] += count
	
	print((get_sum(dic)))

# Optimized solution
import sys
input = sys.stdin.readline

# D - Replacing
def get_sum(dic):
	sum = 0
	
	for k, v in list(dic.items()):
		sum += k * v
	
	return sum


n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
dic = dict()

for ai in a:
	dic.setdefault(ai, 0)
	dic[ai] += 1

for i in range(q):
	b, c = list(map(int, input().split()))

	count = dic.pop(b, 0)
	
	if count > 0:
		dic.setdefault(c, 0)
		dic[c] += count

	if i == 0:	
		sum = get_sum(dic)
	else:
		sum += (c - b) * count
	
	print(sum)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
from collections import Counter
x = Counter(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    if b in x:
        m = x[b]
        x.pop(b)
        if c in x:
            x[c] += m
        else:
            x[c] = m
    print((sum(i*j for i, j in list(x.items()))))

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
from collections import Counter
x = Counter(a)
r = sum(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    m = 0
    if b in x:
        m = x[b]
        x.pop(b)
        if c in x:
            x[c] += m
        else:
            x[c] = m
    r += m*(c-b)
    print(r)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split(' ')))
Q = int(eval(input()))
Bs = []
for i in range(Q):
    Bs.append(list(map(int, input().split(' '))))
    
    
A_list = [0]*(10**5+1)
for i in A:
    A_list[i] += 1
    
for B in Bs:
    b, c = B
    A_list[c] += A_list[b]
    A_list[b] = 0
    print((sum([i*num for num, i in enumerate(A_list)])))

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split(' ')))
Q = int(eval(input()))
Bs = []
for i in range(Q):
    Bs.append(list(map(int, input().split(' '))))
    
    
A_list = [0]*(10**5+1)
for i in A:
    A_list[i] += 1
    
sum_value = sum(A)
for B in Bs:
    b, c = B
    sum_value += A_list[b]*c
    sum_value -= A_list[b]*b
    
    A_list[c] += A_list[b]
    A_list[b] = 0
    print(sum_value)
# Original solution
import sys
input = sys.stdin.readline
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
cnt = [0]*100005
s = sum(A)
for i in A:
    cnt[i] += 1
for i in range(Q):
    B, C = list(map(int,input().split()))
    s += (C-B)*cnt[B]
    print(s)
    cnt[C] += cnt[B]
    cnt[B] = 0

# Optimized solution
import sys
input = sys.stdin.readline
N = int(eval(input()))
A = tuple(map(int,input().split()))
Q = int(eval(input()))
cnt = [0]*100005
s = sum(A)
for i in A:
    cnt[i] += 1
for i in range(Q):
    B, C = list(map(int,input().split()))
    s += (C-B)*cnt[B]
    print(s)
    cnt[C] += cnt[B]
    cnt[B] = 0
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
n_data = {i: A.count(i) for i in range(1, 10 ** 5 + 1)}
ans = sum(A)
for _ in range(Q):
  b, c = list(map(int, input().split()))
  ans += (c - b) * n_data[b]
  n_data[c] += n_data[b]
  n_data[b] = 0
  print(ans)

# Optimized solution
from collections import Counter


N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
n_data = Counter(A)
ans = sum(A)
for _ in range(Q):
  b, c = list(map(int, input().split()))
  ans += (c - b) * n_data[b]
  n_data[c] += n_data[b]
  n_data[b] = 0
  print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))

g = []
t = sum(a)
s = []

for j in range(100001):
    h = a.count(j)
    g.append(h)

for i in range(q):
    b,c = list(map(int,input().split()))
    if g[b] != 0:
        t = t + c*g[b] - b*g[b]
        g[c] = g[c] + g[b]
        g[b] = 0
        s.append(t)
    else:
        s.append(t)

for m in range(q):
    print((s[m]))


# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))

g = [0]*(10**5+1)
t = sum(a)
s = []

for aj in a:
    g[aj]+=1

for i in range(q):
    b,c = list(map(int,input().split()))
    if g[b] != 0:
        t = t + c*g[b] - b*g[b]
        g[c] = g[c] + g[b]
        g[b] = 0
        s.append(t)
    else:
        s.append(t)

for m in range(q):
    print((s[m]))
# Original solution
import sys
input = sys.stdin.readline


N = int(eval(input()))
A_list = input().split()
Q = int(eval(input()))
num_list = [list(input().split()) for _ in range(Q)]

A_cha = ",".join(A_list)
for i in num_list:
    B = i[0]
    C = i[1]
    A_cha = A_cha.replace(B, C)
    A_result = sum([int(A) for A in A_cha.split(",")])
    print(A_result)

# Optimized solution
import sys
input = sys.stdin.readline


N = int(eval(input()))
A_list = input().split()
Q = int(eval(input()))
num_list = [input().split() for _ in range(Q)]

A_cha = ",".join(A_list)
for i in num_list:
    B = i[0]
    C = i[1]
    A_cha = A_cha.replace(B, C)
    A_result = sum([int(A) for A in A_cha.split(",")])
    print(A_result)
# Original solution
N = int(eval(input()))
l = [0]*(10**5+1)
A = list(map(int,input().split()))
Q = int(eval(input()))
for a in A:
  l[a] += a

for _ in range(Q):
  b,c = list(map(int,input().split()))
  l[c] += (l[b]//b)*c
  l[b] = 0
  print((sum(l)))

# Optimized solution
N = int(eval(input()))
l = [0]*(10**5+1)
A = list(map(int,input().split()))
Q = int(eval(input()))
for a in A:
  l[a] += a
suml = sum(l)
for _ in range(Q):
  b,c = list(map(int,input().split()))
  suml += ((l[b]//b)*c - l[b])
  l[c] += (l[b]//b)*c
  l[b] = 0
  print(suml)
# Original solution
N = int(eval(input()))
l = [0]*(10**5+1)
A = list(map(int,input().split()))
Q = int(eval(input()))
for a in A:
  l[a] += a
suml = sum(l)
for _ in range(Q):
  b,c = list(map(int,input().split()))
  suml += ((l[b]//b)*c - l[b])
  l[c] += (l[b]//b)*c
  l[b] = 0
  print(suml)

# Optimized solution
N = int(eval(input()))
l = [0]*(10**5+1)
A = list(map(int,input().split()))
Q = int(eval(input()))
for a in A:
  l[a] += a
suml = sum(l)
for _ in range(Q):
  b,c = list(map(int,input().split()))
  suml += (l[b]//b*c - l[b])
  l[c] += l[b]//b*c
  l[b] = 0
  print(suml)
# Original solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
oper = [list(map(int, input().split())) for _ in range(q)]
a.sort()
res = sum(a)

for i in range(q):
    b, c = oper[i]
    if b in a:
        bCount = a.count(b)
        idx = a.index(b)

        a = a[:idx] + a[idx+bCount:]
        a.extend([c]*bCount)
        dif = c-b

        res += dif*bCount

    print(res)

# Optimized solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
oper = [list(map(int, input().split())) for _ in range(q)]
dic = {}
for i in range(n):
    if a[i] in dic:
        dic[a[i]] += 1
    else:
        dic[a[i]] = 1
        
res = sum(a)

for i in range(q):
    b, c = oper[i]
    if b in dic:
        bCount = dic[b]
        del(dic[b])
        if c in dic:
            dic[c] += bCount
        else:
            dic[c] = bCount

        dif = c-b

        res += dif*bCount

    print(res)
# Original solution
from collections import defaultdict

d = defaultdict(int)

N = eval(input())
A = list(map(int, input().split())) 
for i in A:
    d[i] += 1

Q = int(eval(input()))
for _ in range(Q):
    B, C = list(map(int, input().split()))
    d[C] += d[B]
    d[B] = 0
    ans = 0
    for i,j in list(d.items()):
        ans += i*j
    print(ans)

# Optimized solution
from collections import defaultdict
 
d = defaultdict(int)
 
N = eval(input())
A = list(map(int, input().split())) 
for i in A:
    d[i] += 1

ans = sum(A)
 
Q = int(eval(input()))

for _ in range(Q):
    B, C = list(map(int, input().split()))
    ans = ans - B*d[B] + C*d[B]
    print(ans)
    d[C] += d[B]
    d[B] = 0
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
BC = [list(map(int, input().split())) for i in range(Q)]

def calc(A):
    ans = 0
    for i in range(len(A)):
        ans += A[i] * (i + 1)

    return ans

VALUE = [0 for i in range(100000)]

for i in range(N):
    VALUE[A[i] - 1] += 1

for i in range(Q):
    VALUE[BC[i][1] - 1] += VALUE[BC[i][0] - 1]
    VALUE[BC[i][0] - 1] = 0
    print((calc(VALUE)))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
BC = [list(map(int, input().split())) for i in range(Q)]

def calc(A):
    ans = 0
    for i in range(len(A)):
        ans += A[i] * (i + 1)

    return ans

VALUE = [0 for i in range(100000)]

for i in range(N):
    VALUE[A[i] - 1] += 1

ANS = []
ANS.append(calc(VALUE))

for i in range(Q):
    VALUE[BC[i][1] - 1] += VALUE[BC[i][0] - 1]
    ANS.append(ANS[i] + (BC[i][1] - BC[i][0]) * VALUE[BC[i][0] - 1])
    VALUE[BC[i][0] - 1] = 0

for i in range(1, Q + 1):
    print((ANS[i]))
# Original solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))

l = [0] * 100000
m = list(range(1,100001))

for i in range(n):
  l[a[i]-1] += 1
  
for j in range(q):
  b = list(map(int,input().split()))
  l[b[1]-1] += l[b[0]-1]
  l[b[0]-1] = 0
  print((sum(list(map(lambda x,y: x*y, l, m)))))


# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
ans = sum(a)

l = [0] * 100001

for i in a:
  l[i] += 1
  
for j in range(q):
  b, c = list(map(int,input().split()))
  l[c] += l[b]
  ans += l[b] * (c-b)
  l[b] = 0
  print(ans)
# Original solution
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    from collections import Counter
    N = int(eval(input()))
    A = list(map(int, input().split()))
    D = Counter(A)
    S = sum(A)
    Q = int(eval(input()))
    B = [0] * Q
    C = [0] * Q
    for i in range(Q):
        b, c = list(map(int, input().split()))
        x = D[b]
        y = D[c]
        temp = -x * b + x * c
        print((S+temp))
        S += temp
        D[b] = 0
        D[c] += x  

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

# Optimized solution
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    B = [0] * Q
    C = [0] * Q
    for i in range(Q):
        B[i], C[i] = list(map(int, input().split()))
        
    #output
    from collections import Counter
    D = Counter(A)
    S = sum(A)

    for i in range(Q):
        x = D[B[i]]
        temp = S - x*B[i] + x*C[i] 
        print(temp)
        D[B[i]] = 0
        D[C[i]] += x
        S = temp    

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

num_A = [A.count(i) for i in range(10**5+1)]
first = sum(A)

for i in range(Q):
    b, c = list(map(int, input().split()))
    first += c*num_A[b]
    first -= b*num_A[b]
    num_A[c] += num_A[b]
    num_A[b] = 0
    print(first)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

num_A = [0 for i in range(10**5+1)]
first = 0

for a in A:
    num_A[a] += 1
    first += a

for i in range(Q):
    b, c = list(map(int, input().split()))
    first += c*num_A[b]
    first -= b*num_A[b]
    num_A[c] += num_A[b]
    num_A[b] = 0
    print(first)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
bc = []
for i in range(q):
    bc.append(list(map(int, input().split())))

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

for i in range(q):
    ans = 0
    if str(bc[i][0]) in dic:
        buf = dic[str(bc[i][0])]
        dic[str(bc[i][0])] = 0
        if str(bc[i][1]) in dic:
            dic[str(bc[i][1])] += buf
        else:
            dic[str(bc[i][1])] = buf

    for j in dic:
        ans += int(j) * dic[j]
    print(ans)

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
bc = []
for i in range(q):
    bc.append(list(map(int, input().split())))

a_sum = sum(a)

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

for i in range(q):
    ans = 0
    if str(bc[i][0]) in list(dic.keys()):
        buf = dic[str(bc[i][0])]
        if str(bc[i][1]) in list(dic.keys()):
            dic[str(bc[i][1])] += buf
        else:
            dic[str(bc[i][1])] = buf

        a_sum += buf*(-bc[i][0] + bc[i][1])
        dic[str(bc[i][0])] = 0


    print(a_sum)
# Original solution
n0 = int(1e5)+1
x = [0]*n0

n = int(eval(input()))
a0 = list(map(int,input().split()))
q = int(eval(input()))
bcs = []
for i in range(q):
    bcs.append(list(map(int,input().split())))

for a in a0:
    x[a] += 1

for bc in bcs:
    x[bc[1]] += x[bc[0]]
    x[bc[0]] = 0
    sum0 = 0
    for i in range(n0):
        sum0 += i*x[i]
    print(sum0)

# Optimized solution
n0 = int(1e5)+1
x = [0]*n0

n = int(eval(input()))
a0 = list(map(int,input().split()))
q = int(eval(input()))
bcs = []
for i in range(q):
    bcs.append(list(map(int,input().split())))

for a in a0:
    x[a] += 1
sum0 = sum(a0)

for bc in bcs:
    sum0 = sum0 + bc[1]*x[bc[0]] - bc[0]*x[bc[0]]
    x[bc[1]] += x[bc[0]]
    x[bc[0]] = 0
    print(sum0)
# Original solution
from collections import Counter
n = int(eval(input()))
a_li = list(map(int,input().split()))
a_counter = Counter(a_li)
q = int(eval(input()))
for i in range(q):
    ans = 0
    a,b = list(map(int,input().split()))
    dum = a_counter[a]
    a_counter[a] = 0
    a_counter[b] += dum
    for j in a_counter:
        ans += j * a_counter[j]
    print(ans)


# Optimized solution
from collections import Counter
n = int(eval(input()))
a_li = list(map(int,input().split()))
ans = sum(a_li)
a_counter = Counter(a_li)
q = int(eval(input()))
for i in range(q):
    a,b = list(map(int,input().split()))
    dum = a_counter[a]
    ans -= dum*a
    ans += dum*b
    a_counter[a] = 0
    a_counter[b] += dum
    print(ans)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))

d=[0]*(10**5 +1 )

for i in a:
    d[i-1]+=i

for _ in range(q):
    b,c=list(map(int,input().split()))
    ans=0
    k=d[b-1]//b
    d[c-1]+=c*k
    d[b-1]=0

    print((sum(d)))


# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))

s=sum(a)
d=[0]*(10**5+1)
for i in a:
    d[i-1]+=1

for _ in range(q):
    b,c=list(map(int,input().split()))
    tmp=0
    k=d[b-1]
    d[c-1]+=k
    d[b-1]=0

    tmp = k*c-k*b
    s+=tmp
    print(s)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

dictA = {}

for z in A:
    dictA.setdefault(z, 0)
    dictA[z] += 1


BC = [list(map(int, input().split())) for i in range(Q)]
for x in BC:
    if x[0] in list(dictA.keys()):
        dictA.setdefault(x[1], 0)
        dictA[x[1]] += dictA[x[0]]
        dictA.pop(x[0])
        # print(dictA)

    sumA = 0
    for k, v in list(dictA.items()):
        sumA += k * v
    print(sumA)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

dictA = {}

for z in A:
    dictA.setdefault(z, 0)
    dictA[z] += 1

sumA = sum(A)

BC = [list(map(int, input().split())) for i in range(Q)]
for x in BC:
    if x[0] in list(dictA.keys()):
        sumA += (x[1] - x[0]) * dictA[x[0]]
        dictA.setdefault(x[1], 0)
        dictA[x[1]] += dictA[x[0]]
        dictA.pop(x[0])
    print(sumA)
# Original solution
from collections import Counter

N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
s = Counter(A)
t=[0]*(10**5+1)
for i in range(10**5+1):
    t[i] = s[i]*i

sumnum=sum(t)
for i in range(Q):
    B, C = list(map(int, input().split()))
    num = t[B]//(B)
    t[B] = 0
    t[C] += num*(C)
    sumnum=sumnum-num*B+num*C
    print((sum(t)))


# Optimized solution
from collections import Counter

N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
s = Counter(A)
t=[0]*(10**5+1)
for i in range(10**5+1):
    t[i] = s[i]*i

sumnum=sum(t)
for i in range(Q):
    B, C = list(map(int, input().split()))
    num = t[B]//(B)
    t[B] = 0
    t[C] += num*(C)
    sumnum=sumnum-num*B+num*C
    print(sumnum)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
ans=sum(a)
q=int(eval(input()))
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(q):
  b,c=list(map(int,input().split()))
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
ans=sum(a)
q=int(eval(input()))
dp=[0]*(10**5+1)
for i in a:
  dp[i]+=1

for j in range(q):
  b,c=list(map(int,input().split()))
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)
# Original solution
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    n = int(eval(input()))
    al = list(map(int, input().split()))
    q = int(eval(input()))
    cnt_dic = {}
    ans = 0
    for a in al:
        cnt_dic.setdefault(a,0)
        cnt_dic[a] += 1
        ans += a

    bcl = []
    for _ in range(q):
        b, c = list(map(int, input().split()))
        bcl.append((b,c))


    for bc in bcl:
        b,c = bc
        if b not in cnt_dic:
            print(ans)
            continue
        ans += cnt_dic[b]*(c-b)
        cnt_dic.setdefault(c,0)
        cnt_dic[c] += cnt_dic[b]
        cnt_dic[b] = 0
        print(ans)




if __name__ == "__main__":
    main()

# Optimized solution
n = int(eval(input()))
al = list(map(int, input().split()))

num_cnt = {}
c_sum = 0
for a in al:
    num_cnt.setdefault(a,0)
    num_cnt[a] += 1
    c_sum += a

ans = []
q = int(eval(input()))

for _ in range(q):
    b,c = list(map(int, input().split()))
    num_cnt.setdefault(b,0)
    num_cnt.setdefault(c,0)
    diff = num_cnt[b]*c - num_cnt[b]*b

    num_cnt[c] += num_cnt[b]
    num_cnt[b] = 0
    c_sum += diff
    ans.append(c_sum)

for a in ans:
    print(a)
# Original solution
import collections

N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
for i in range(Q):
  B,C=list(map(int,input().split()))
  A=[C if i==B else i for i in A]
  a=collections.Counter(A)
  check=list(a.items())
  ans=0
  for j in range(len(check)):
    ans+=check[j][0]*check[j][1]
  print(ans)


# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
ans=sum(A)
check=[0]*(10**5+1)
for key in A:
  check[key]+=1
Q=int(eval(input()))
for i in range(Q):
  B,C=list(map(int,input().split()))
  if check[B]==0:
    print(ans)
  else:
    x=C-B
    ans+=check[B]*x
    print(ans)
    check[C]+=check[B]
    check[B]=0