Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
r = []
for _ in range(q):
  r.append(list(map(int, input().split())))

s = {}

for v in a:
  if v in s:
    s[v] += v
  else:
    s[v] = v

for t in r:
  if t[0] in s:
    n = s[t[0]] // t[0] * t[1]
    if t[1] in s:
      s[t[1]] += n
    else:
      s[t[1]] = n
    del s[t[0]]
  print(sum(s.values()))


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
r = []
for _ in range(q):
  r.append(list(map(int, input().split())))

s = {}

for v in a:
  if v in s:
    s[v] += v
  else:
    s[v] = v

p = sum(s.values())

for t in r:
  if t[0] in s:
    n = s[t[0]] // t[0] * t[1]
    p = p - s[t[0]] + n
    if t[1] in s:
      s[t[1]] += n
    else:
      s[t[1]] = n
    del s[t[0]]
  print(p)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
r = []
for _ in range(q):
  r.append(list(map(int, input().split())))

s = {}

for v in a:
  if v in s:
    s[v] += 1
  else:
    s[v] = 1

for t in r:
  if t[0] in s:
    if t[1] in s:
      s[t[1]] += s[t[0]]
    else:
      s[t[1]] = s[t[0]]
    del s[t[0]]
  print(sum([i * s[i] for i in s]))


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
r = []
for _ in range(q):
  r.append(list(map(int, input().split())))

s = {}

for v in a:
  if v in s:
    s[v] += v
  else:
    s[v] = v

p = sum(s.values())

for t in r:
  if t[0] in s:
    n = s[t[0]] // t[0] * t[1]
    p = p - s[t[0]] + n
    if t[1] in s:
      s[t[1]] += n
    else:
      s[t[1]] = n
    del s[t[0]]
  print(p)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
a_sum = []
for _ in range(q):
    b,c = map(int, input().split())
    for i in sorted(a_list, reverse=True):
        if i == b:
            a_list.remove(b)
            a_list.append(c)
    a_sum.append(sum(a_list))


for i in a_sum:
    print(i)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
num = [0] * 100010
b = [0] * q
c = [0] * q
for i in range(q):
    b[i], c[i] = map(int, input().split())

for i in a:
    num[i] += 1

s = sum(a)

#print(num)
#print(b)
#print(c)


for i in range(q):
    s += (c[i]-b[i])*num[b[i]]
    num[c[i]] += num[b[i]]
    #print(num)
    num[b[i]] = 0
    #print(num,"\n")
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
a_sum = []
for _ in range(q):
    b,c = map(int, input().split())
    for i in sorted(a_list, reverse=True): #ここのインデックスを逆にするのがヤマ
        if i == b:
            a_list.remove(b)
            a_list.append(c) #ここで追加するとインデックスがずれるため
        #print(a_list)
    a_sum.append(sum(a_list))


for i in a_sum:
    print(i)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
num = [0] * 100010
b = [0] * q
c = [0] * q
for i in range(q):
    b[i], c[i] = map(int, input().split())

for i in a:
    num[i] += 1

s = sum(a)

#print(num)
#print(b)
#print(c)


for i in range(q):
    s += (c[i]-b[i])*num[b[i]]
    num[c[i]] += num[b[i]]
    #print(num)
    num[b[i]] = 0
    #print(num,"\n")
    print(s)