CodeEff/ECCO huggingface dataset:
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
ans=[0]*(10**5+1)
for i in a:
  ans[i]+=1
for _ in range(q):
  b,c=list(map(int,input().split()))
  ans[c]+=ans[b]
  ans[b]=0
  s=0
  for i,x in enumerate(ans):
    s+=i*x
  print(s)


# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
ans=[0]*(10**5+1)
for i in a:
  ans[i]+=1
res=sum(a)
for _ in range(q):
  b,c=list(map(int,input().split()))
  ans[c]+=ans[b]
  res-=ans[b]*b
  res+=ans[b]*c
  ans[b]=0
  print(res)
# Original solution
n = int(eval(input()))
a = list(map(int,input().strip().split()))
q = int(eval(input()))

import collections

counter = collections.Counter(a)

result = 0
for j in range(q):
  b,c = list(map(int,input().strip().split()))
  if c in counter:
    counter[c] += counter[b]
  else:
    counter[c] = counter[b]
  counter[b] = 0
  result = 0
  for i in counter:
    result += i * counter[i]
  print(result)

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

import collections

result = sum(a)
counter = collections.Counter(a)

for j in range(q):
  b,c = list(map(int,input().strip().split()))
  if c in counter:
    counter[c] += counter[b]
  else:
    counter[c] = counter[b]
  result += (c-b) * counter[b]
  counter[b] = 0
  print(result)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
bc = list(list(map(int, input().split())) for _ in range(q))

num = [0]*(10**5 + 1)
for i in range(10**5 + 1):
    num[i] = a.count(i)

s = sum(a)

for b, c in bc:
    s += (c - b)*num[b]
    num[c] += num[b]
    num[b] = 0

    print(s)

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
bc = list(list(map(int, input().split())) for _ in range(q))

s = sum(a)

num = [0]*(10**5 + 1)
for x in a:
    num[x] += 1

for b, c in bc:
    s += (c - b)*num[b]
    num[c] += num[b]
    num[b] = 0

    print(s)
# Original solution
import collections
N=int(eval(input()))
A=list(map(int,input().split()))
A=sorted(A)
Q=int(eval(input()))
B=[0]*Q;C=[0]*Q
for i in range(Q):
  B[i],C[i]=list(map(int,input().split()))

#c = collections.Counter(A)
#print(c)

for i in range(Q):
  A = [C[i] if B[i]==A[j] else A[j] for j in range(N)]
#  print(A)
  print((sum(A)))

# Optimized solution
import collections
N=int(eval(input()))
A=list(map(int,input().split()))
A=sorted(A)
Q=int(eval(input()))
B=[0]*Q;C=[0]*Q
for i in range(Q):
  B[i],C[i]=list(map(int,input().split()))

D = collections.Counter(A)
s=sum(A)
for i in range(Q):
  s=s-B[i]*D[B[i]]
  D[C[i]]+=D[B[i]]
  s=s+C[i]*D[B[i]]
  D[B[i]]=0
#  A = [C[i] if B[i]==A[j] else A[j] for j in range(N)]
#  print(A)
  print(s)
# Original solution
import collections
N = int(eval(input()))
a = list(map(int, input().split()))
a_c = collections.Counter(a)
Q = int(eval(input()))
BC = [list(map(int, input().split())) for _ in range(Q)]

for bc in BC:
  b = bc[0]
  c = bc[1]
  s = 0
  if b in a_c:
    a_c[c]+=a_c.pop(b)
  for k, v in list(a_c.items()):
    s += k*v
  print(s)

# Optimized solution
import collections
N = int(eval(input()))
a = list(map(int, input().split()))
a_c = collections.Counter(a)
Q = int(eval(input()))
BC = [list(map(int, input().split())) for _ in range(Q)]
s_old = 0
for k, v in list(a_c.items()):
    s_old += k*v
for bc in BC:
  b = bc[0]
  c = bc[1]
  s = s_old
  if b in a_c:
    n = a_c.pop(b)
    a_c[c]+=n
    s-=n*b
    s+=n*c
  print(s)
  s_old = s
# Original solution
N = int(eval(input()))
A = [int(n) for n in input().split()]
Q = int(eval(input()))

D = dict()
for a in A:
    if a not in list(D.keys()):
        D[a] = 0
    D[a] += 1

for _ in range(Q):
    B, C = [int(n) for n in input().split()]
    
    if B in list(D.keys()):
        if C not in list(D.keys()):
            D[C] = 0
        D[C] += D[B]
        del D[B]

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



# Optimized solution
N = int(eval(input()))
A = [int(n) for n in input().split()]
Q = int(eval(input()))

D = dict()
for a in A:
    if a not in list(D.keys()):
        D[a] = 0
    D[a] += 1

ans = 0
for k, v in list(D.items()):
    ans += k * v
    
for _ in range(Q):
    B, C = [int(n) for n in input().split()]
    
    if B in list(D.keys()):
        count = D[B]
        
        if C not in list(D.keys()):
            D[C] = 0
        D[C] += count
        del D[B]
        
        ans += (C - B) * count

    print(ans)
# Original solution
from collections import defaultdict

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

dd = defaultdict(int)

for a in arr:
    dd[a]+=1

q = int(eval(input()))
for _ in range(q):
    b,c = list(map(int,input().split()))
    if dd[b]>0:
        dd[c]+=dd[b]
        dd[b]=0
    sum = 0
    for key in list(dd.keys()):
        sum+=dd[key]*key
    print(sum)

# Optimized solution
from collections import defaultdict

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

dd = defaultdict(int)

for a in arr:
    dd[a]+=1

sum = sum(arr)

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


    if dd[b]>0:
        sum += dd[b]*(c-b)
        dd[c]+=dd[b]
        dd[b]=0

    print(sum)
# Original solution
n = int(eval(input()))

a = list(map(int,input().split()))
#print(a)
q = int(eval(input()))
z = [0 for i in range(100001)]
for i in range(len(a)):
    z[a[i]] += 1

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


for i in range(q):
    z[b[i][1]] += z[b[i][0]]
    z[b[i][0]] = 0
    #print(z[:5])
    s = 0
    for j in range(len(z)):
        s += j * z[j]
    print(s)

# Optimized solution
n = int(eval(input()))

a = list(map(int,input().split()))
#print(a)
q = int(eval(input()))
z = [0 for i in range(100001)]
for i in range(len(a)):
    z[a[i]] += 1
x = sum(a)
b = []
for _ in range(q):
    b.append(list(map(int,input().split())))
#print(b)


for i in range(q):
    x += (b[i][1] - b[i][0]) * z[b[i][0]]
    z[b[i][1]] += z[b[i][0]]
    z[b[i][0]] = 0
    print(x)
# Original solution
n = int(eval(input()))

a_list = [int(i) for i in input().split()]
q = int(eval(input()))

ary = [0 for _ in range((10 ** 5)+1)]

for i in a_list:
    ary[i] += i

ans = []
for _ in range(q):
    b, c = list(map(int, input().split()))
    ary[c] += (ary[b]/b)*c
    ary[b] = 0
    ans.append(int(sum(ary)))

for a in ans:
    print(a)


# Optimized solution
n = int(eval(input()))

a_list = [int(i) for i in input().split()]
q = int(eval(input()))

ary = [0 for _ in range((10**5) + 1)]

for i in a_list:
    ary[i] += 1

ans = []

f_ans = sum(a_list)
for _ in range(q):
    b, c = list(map(int, input().split()))
    f_ans += (c-b)*ary[b]
    ary[c] += ary[b]
    ary[b] = 0

    ans.append(f_ans)

for a in ans:
    print(a)
# Original solution
import sys
import collections
def main():
    n = int(eval(input()))
    a = collections.Counter(list(map(int, input().split())))
    s = [0 for i in range(10**5+1)]
    sa = 0
    for i in a:
        s[i] += a[i]
        sa += i*a[i]
    q = int(eval(input()))
    for x in sys.stdin.readlines():
        b, c = list(map(int, x.split()))
        s[c] += s[b]
        sa = sa + c*s[b] - b*s[b]
        s[b] = 0
        print(sa)

main()

# Optimized solution
import sys
def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    s = [0 for i in range(10**5+1)]
    sa = sum(a)
    for i in a:
        s[i] += 1
    q = int(eval(input()))
    for x in sys.stdin.readlines():
        b, c = list(map(int, x.split()))
        s[c] += s[b]
        sa = sa + (c-b)*s[b]
        s[b] = 0
        print(sa)

main()
# Original solution
import sys
readline = sys.stdin.readline
 
 
def st(line):
    tmp = ''
    for l in line:
        if l == '\n':
            yield tmp
        elif l == ' ':
            yield tmp
            tmp = ''
        else:
            tmp += l



N = int(eval(input()))
As = st(readline())

d = dict()
total = 0
for a in As:
    a = int(a)

    if a in d:
        d[a] += 1
    else:
        d[a] = 1
    total += a

Q = int(eval(input()))
queries = (list(map(int, readline().strip().split())) for _ in range(Q))

for que in queries:
    b, c = que

    if b not in d:
        print(total)
        continue

    n_b = d[b]
    d[b] = 0

    if c not in d:
        d[c] = 0

    d[c] += n_b
    total += (c - b) * n_b

    print(total)



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


def st(line):
    tmp = ''
    for l in line:
        if l == '\n':
            yield tmp
        elif l == ' ':
            yield tmp
            tmp = ''
        else:
            tmp += l


def solve():
    N = int(eval(input()))
    As = st(readline())

    d = dict()
    total = 0
    for a in As:
        a = int(a)

        if a in d:
            d[a] += 1
        else:
            d[a] = 1
        total += a

    Q = int(eval(input()))
    queries = (list(map(int, readline().strip().split())) for _ in range(Q))

    for que in queries:
        b, c = que

        if b not in d:
            print(total)
            continue

        n_b = d[b]
        d[b] = 0

        if c not in d:
            d[c] = 0

        d[c] += n_b
        total += (c - b) * n_b

        print(total)


def main():
    solve()


if __name__ == "__main__":
    main()
# Original solution
import sys
import math
from collections import deque
from collections import Counter

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(eval(input()))
NMI = lambda: list(map(int, input().split()))
NLI = lambda: list(NMI())
SI = lambda: eval(input())


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


#隣接リスト 1-order
def make_adjlist_d(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
        res[edge[1]].append(edge[0])
    return res


def make_adjlist_nond(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
    return res


#nCr
def cmb(n, r):
    return math.factorial(n) // math.factorial(r) // math.factorial(n - r)


def main():
    N = NI()
    A = NLI()
    Q = NI()
    querys = [NLI() for _ in range(Q)]

    Dic = dict(Counter(A))
    for b, c in querys:
        Dic.setdefault(c, 0)
        Dic.setdefault(b, 0)
        Dic[c] += Dic[b]
        del Dic[b]
        res = 0
        for n, x in list(Dic.items()):
            res += n * x
        print(res)


if __name__ == "__main__":
    main()

# Optimized solution
import sys
import math
from collections import deque
from collections import Counter

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(eval(input()))
NMI = lambda: list(map(int, input().split()))
NLI = lambda: list(NMI())
SI = lambda: eval(input())


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


#隣接リスト 1-order
def make_adjlist_d(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
        res[edge[1]].append(edge[0])
    return res


def make_adjlist_nond(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
    return res


#nCr
def cmb(n, r):
    return math.factorial(n) // math.factorial(r) // math.factorial(n - r)


def main():
    N = NI()
    A = NLI()
    Q = NI()
    querys = [NLI() for _ in range(Q)]
    res = sum(A)
    Dic = dict(Counter(A))
    for b, c in querys:
        Dic.setdefault(c, 0)
        Dic.setdefault(b, 0)
        gap = (c-b) * Dic[b]
        Dic[c] += Dic[b]
        del Dic[b]
        res += gap
        print(res)


if __name__ == "__main__":
    main()
# Original solution
from collections import Counter

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

# bc = [list(map(int,input().split())) for _ in range(q)]

c = Counter(a)

ans = []

for i in range(q):
    bi,ci = list(map(int,input().split()))
    c[ci] = c[bi]+c[ci]
    c[bi] = 0
    csum = 0
    for k,v in list(c.items()):
        csum += k*v
    ans.append(csum)
    
for i in ans:
    print(i)

# Optimized solution
from collections import Counter

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

# bc = [list(map(int,input().split())) for _ in range(q)]

c = Counter(a)

allsum = 0
for k,v in list(c.items()):
    allsum += k*v
    
ans = []

for i in range(q):
    bi,ci = list(map(int,input().split()))
    allsum += ci*c[bi] - bi*c[bi]
    c[ci] = c[ci] + c[bi]
    c[bi] = 0
    ans.append(allsum)
    
for i in ans:
    print(i)
# Original solution
N=int(eval(input()))
A=list(map(int,input().strip().split()))
Q=int(eval(input()))

dp=[0 for n in range(100000)]
for n in range(len(A)):
    dp[A[n]-1]+=1

for n in range(Q):
    B,C=list(map(int,input().strip().split()))
    dp[C-1]+=dp[B-1]
    dp[B-1]=0

    ans=0
    for n in range(1,len(dp)):
        ans+=n*dp[n-1]
    print(ans)

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

dp=[0 for n in range(100000+1)]
for n in range(len(A)):
    dp[A[n]-1]+=1

init=0
for n in range(1,len(dp)):
    init+=n*dp[n-1]

ans=init
for n in range(Q):
    B,C=list(map(int,input().strip().split()))
    ans=ans-dp[B-1]*B+dp[B-1]*C
    dp[C-1]+=dp[B-1]
    dp[B-1]=0
    print(ans)
# Original solution
#D
from collections import defaultdict
n = int(eval(input()))
a = list(map(int,input().split()))
mp = defaultdict(int)
for i in a:
    mp[i] += 1
q = int(eval(input()))
for _ in range(q):
    #print(mp)
    s = 0   
    b,c=list(map(int,input().split()))
    if c in mp:
        mp[c] = mp[c]+mp[b]
        mp.pop(b)
    elif b in mp:
        mp[c] = mp.pop(b)
        
    for i in mp:
        s += (mp[i])*i
    print(s)
    

# Optimized solution
#D                 more faster method than above               see it ..........
l = lambda : list(map(int,input().strip().split()))
n = int(eval(input()))
a = l()
t = [0]*(10**5+1)
s = 0
for i in a:
    s += i
    t[i] += 1
q = int(eval(input()))
for _ in range(q):
    b,c = l()
    s += (c-b)*t[b]
    t[c] += t[b]
    t[b] = 0
    print(s)
# Original solution
N = int(eval(input()))

A = list(map(int,input().split()))

Q = int(eval(input()))

BC = [list(map(int, input().split())) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]

list = [0] * 100001
sum = 0
for i in range(100001):
    list[i] = A.count(i)

for j in range(N):
    sum += A[j] 

for k in range(Q):
    sum += list[B[k]] * (C[k] -B[k])
    list[C[k]] += list[B[k]]
    list[B[k]] = 0
    print(sum)

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

for _ in range(Q):
    bi, ci = list(map(int, input().split()))
    B.append(bi)
    C.append(ci)

dic = {}
for i in A:
    dic[i] = dic.get(i,0) + 1

sum = sum(A)

for i in range(Q):
    sum += dic.get(B[i], 0) * (C[i] -B[i])
    dic[C[i]] = dic.get(C[i], 0) + dic.get(B[i], 0)
    dic[B[i]] = dic.get(B[i], 0)
    dic[B[i]] = 0
    print(sum)
# Original solution
from collections import Counter
def change_dict_key(d, old_key, new_key, default_value=None):
    d[new_key] = d.pop(old_key, default_value)

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

q=int(eval(input()))
for _ in range(q):
    b,c=list(map(int,input().split()))
    if b in list(a_cnt.keys()):
        if c in list(a_cnt.keys()):
            a_cnt[c]+=a_cnt[b]
            a_cnt.pop(b)
        else:
            change_dict_key(a_cnt,b,c)
    ans=0
    for key,value in list(a_cnt.items()):
        ans+=key*value
    print(ans)


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

sum=0
num=[0]*(10**5+1)
for i in range(n):
    sum+=a[i]
    num[a[i]]+=1

q=int(eval(input()))
for i in range(q):
    b,c=list(map(int,input().split()))
    sum+=(c-b)*num[b]
    print(sum)
    num[c]+=num[b]
    num[b]=0
# Original solution
from collections import Counter


n = int(eval(input()))
a = list(map(int, input().split()))
counter = Counter(a)
ini_sum = sum(a)
q = int(eval(input()))
s = [0] * q

for i in range(q):
    b, c = list(map(int, input().split()))
    counter[c] += counter[b]
    counter[b] = 0
    print((sum(key * value for key, value in list(counter.items()))))


# Optimized solution
from collections import Counter


n = int(eval(input()))
a = list(map(int, input().split()))
counter = Counter(a)
ini_sum = sum(a)
q = int(eval(input()))
s = [ini_sum] + [0] * q

for i in range(1, q+1):
    b, c = list(map(int, input().split()))
    x = c - b
    s[i] = s[i-1] + x*counter[b]
    counter[c] += counter[b]
    counter[b] = 0

for i in range(1, q+1):
    print((s[i]))
# Original solution
from collections import Counter
n = int(input())
A = list(input().split())
q = int(input())
ansList = [0]*q
d = Counter(A)
Sum = sum(map(int,A))

for i in range(q):
    b,c = input().split()
    Sum = Sum - int(b)*d[b] + int(c)*d[b]
    if b in d:
        if c in d:
            d[c] += d.pop(b)
        else:
            d[c] = d.pop(b)
    ansList[i]  +=  Sum

[print(ans) for ans in ansList]


# Optimized solution
from collections import Counter
n = int(input())
A = list(input().split())
q = int(input())
ansList = [0]*q
d = Counter(A)
Sum = sum(map(int,A))

for i in range(q):
    b,c = input().split()
    Sum -= (int(b)-int(c))*d[b]
    if b in d:
        if c in d:
            d[c] += d.pop(b)
        else:
            d[c] = d.pop(b)
    ansList[i]  +=  Sum

[print(ans) for ans in ansList]
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    B, C = [0]*Q, [0]*Q
    for i in range(Q):
        B[i], C[i] = list(map(int, input().split()))

    ans = sum(A)
    d = dict()
    for i, a in enumerate(A):
        if a not in d:
            d[a] = ()
        d[a] += (i,)

    for b, c in zip(B, C):
        if b not in d:
            print(ans)
            continue
        ans += len(d[b]) * (c-b)
        if c not in d:
            d[c] = ()
        d[c] = d[c] + d[b]
        d[b] = ()
        print(ans)

main()

# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    B, C = [0]*Q, [0]*Q
    for i in range(Q):
        B[i], C[i] = list(map(int, input().split()))

    ans = sum(A)
    d = dict()
    for i, a in enumerate(A):
        d[a] = d.get(a, 0) + 1

    for b, c in zip(B, C):
        if b not in d:
            print(ans)
            continue
        ans += d[b] * (c-b)
        if c not in d:
            d[c] = 0
        d[c] = d[c] + d[b]
        d[b] = 0
        print(ans)

main()
# Original solution
import collections

def calc_sum(c):
    sum_val = 0
    for key, val in list(c.items()):
        sum_val += key * val
    return sum_val

N = int(eval(input()))
arr = list(map(int, input().split()))
Q = int(eval(input()))

actions = []

for i in range(Q):
    B, C = list(map(int, input().split()))
    actions.append({'B':B, 'C':C})

c = collections.Counter(arr)

for i in range(Q):
    B = actions[i]['B']
    C = actions[i]['C']
    if B in c:
        c[C] += c[B]
        c.pop(B)
    print((calc_sum(c)))

# Optimized solution
import collections

def calc_sum(c):
    sum_val = 0
    for key, val in list(c.items()):
        sum_val += key * val
    return sum_val

N = int(eval(input()))
arr = list(map(int, input().split()))
Q = int(eval(input()))

actions = []

for i in range(Q):
    B, C = list(map(int, input().split()))
    actions.append({'B':B, 'C':C})

c = collections.Counter(arr)
sum_val = calc_sum(c)

for i in range(Q):
    B = actions[i]['B']
    C = actions[i]['C']
    if B in c:
        c[C] += c[B]
        sum_val += c[B] * C - c[B] * B
        c.pop(B)

    print(sum_val)
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
bcs = []
for i in range(Q):
  j,k = list(map(int,input().split()))
  bcs.append([j,k])
cnts = [0]*100000
for a in A:
  cnts[a-1]+=1
for b,c in bcs:
  t = cnts[b-1]
  cnts[b-1]=0
  cnts[c-1]+=t
  s = 0
  for i in range(1,100001):
    s += cnts[i-1]*i
  print(s)

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
bcs = []
for i in range(Q):
  j,k = list(map(int,input().split()))
  bcs.append([j,k])
s = sum(A)
cnts = [0]*100000
for a in A:
  cnts[a-1]+=1
for b,c in bcs:
  t = cnts[b-1]
  s -= t * b
  cnts[b-1]=0
  cnts[c-1]+=t
  s += t*c 
  print(s)
# Original solution
from collections import defaultdict

N = int(eval(input()))
d = defaultdict(int)
A = list(map(int, input().split()))
for a in A:
    d[a] += 1
Q = int(eval(input()))
for _ in range(Q):
    B, C = list(map(int, input().split()))
    d[C] += d[B]
    del d[B]
    ans = 0
    for k, v in list(d.items()):
        ans += k * v
    print(ans)

# Optimized solution
from collections import defaultdict

N = int(eval(input()))
d = defaultdict(int)
A = list(map(int, input().split()))
for a in A:
    d[a] += 1
Q = int(eval(input()))
ans = 0
for k, v in list(d.items()):
    ans += k * v
for _ in range(Q):
    B, C = list(map(int, input().split()))
    d[C] += d[B]
    ans -= B * d[B]
    ans += C * d[B]
    del d[B]
    print(ans)
# Original solution
N = int(eval(input()))
given_list = [str(i) for i in input().split()]
Q = int(eval(input()))

for _ in range(Q):
    query = input().split()
    query = [str(i) for i in query]

    B = query[0]
    C = query[1]

    given_list_replased = [a.replace(B, C) for a in given_list]
    given_list_replased = [int(i) for i in given_list_replased]
    print((sum(given_list_replased)))
    
    given_list = [str(i) for i in given_list_replased]

# Optimized solution
N = int(eval(input()))
given_list = [str(i) for i in input().split()]
Q = int(eval(input()))

for _ in range(Q):
    query = input().split()
    query = [str(i) for i in query]

    B = query[0]
    C = query[1]
    
    if B in given_list:
        given_list = sorted(given_list)
        for i in range(N):
          if given_list[i] == B:
              given_list[i] = C
          elif int(given_list[i]) > int(C):
              break
        given_list = [int(i) for i in given_list]  
        print((sum(given_list))) #####          
    else:
        given_list = [int(i) for i in given_list]  
        print((sum(given_list))) #####
    
    given_list = [str(i) for i in given_list]
# Original solution
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    l = [0]*10**5
    for i in a:
        l[i-1] += i
    q = int(eval(input()))
    for i in range(q):
        b,c = list(map(int, input().split()))
        l[c-1] += l[b-1]//b*c
        l[b-1] = 0
        print((sum(l)))



if __name__=="__main__":
    main()


# Optimized solution
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    ans = sum(a)
    l = [0]*10**5
    for i in a:
        l[i-1] += 1
    q = int(eval(input()))
    for i in range(q):
        b,c = list(map(int, input().split()))
        ans += (c-b)*l[b-1]
        l[c-1] += l[b-1]
        l[b-1] = 0
        print(ans)

if __name__=="__main__":
    main()