Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,q = LI()
    s = "$" + S()

    for _ in range(q):
        l,r = LI()
        ans = s[l:r+1].count("AC")
        print(ans)
main()

Fixed submission:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,q = LI()
    s = "$" + S()

    lst = [0 for _ in range(n+1)]
    for i in range(n):
        before = s[i]
        after = s[i+1]
        if before == "A" and after == "C":
            lst[i+1] = lst[i] + 1
        else:
            lst[i+1] = lst[i]
    for _ in range(q):
        l,r = LI()
        ans = lst[r] - lst[l]
        print(ans)
main()
Buggy python submission (verdict: Time Limit Exceeded):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():


    s = "#" + S()
    t = "#" + S()

    dp = [[0 for _ in range(len(s) + 1)] for _ in range(len(t) + 1)]

    for i in range(len(t)-1):
        for j in range(len(s)-1):
            if t[i+1] == s[j+1]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])


    ans = ""
    now = dp[len(t)-1][len(s)-1]
    zahyo = [len(t)-1, len(s)-1]
    while now != 0:
        while dp[zahyo[0]][zahyo[1]-1]==now:
            zahyo[1]-=1
        while dp[zahyo[0]-1][zahyo[1]]==now:
            zahyo[0]-=1

        ans += t[zahyo[0]]

        now -= 1
        zahyo = [zahyo[0]-1, zahyo[1]-1]

    ans = ans[::-1]

    print(ans)

main()


Fixed submission:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():


    s = "#" + S()
    t = "#" + S()

    dp = [[0 for _ in range(len(s) + 1)] for _ in range(len(t) + 1)]

    for i in range(len(t)-1):
        for j in range(len(s)-1):
            if t[i+1] == s[j+1]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])


    ans = ""
    now = dp[len(t)-1][len(s)-1]
    zahyo = [len(t)-1, len(s)-1]
    while now != 0:
        while dp[zahyo[0]][zahyo[1]-1]==now:
            zahyo[1]-=1
        while dp[zahyo[0]-1][zahyo[1]]==now:
            zahyo[0]-=1

        ans += t[zahyo[0]]

        now -= 1
        zahyo = [zahyo[0]-1, zahyo[1]-1]

    ans = ans[::-1]

    print(ans)

main()
Buggy python submission (verdict: Time Limit Exceeded):
from math import gcd
from sys import setrecursionlimit
setrecursionlimit(10**6)


def gcds(lst):
    if len(lst) == 2:
        return gcd(*lst)
    return gcd(lst[0], gcds(lst[1:]))

n,x = map(int,input().split())
xl = list(map(int,input().split()))
xl = list(map(lambda y: abs(y-x), xl))

ans = -1
if len(xl) == 1:
    ans = xl[0]
else:
    ans = gcds(xl)

print(ans)

Fixed submission:
import math
from functools import reduce
from sys import setrecursionlimit
setrecursionlimit(10**6)

def gcd(*numbers):
  return reduce(math.gcd, numbers)

n,x = map(int,input().split())
xl = list(map(int,input().split()))
xl = list(map(lambda y: abs(y-x), xl))

ans = -1
if len(xl) == 1:
    ans = xl[0]
else:
    ans = gcd(*xl)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import gcd
from sys import setrecursionlimit
setrecursionlimit(10**6)


def gcds(lst):
    if len(lst) == 2:
        return gcd(*lst)
    return gcd(lst[0], gcds(lst[1:]))

n,x = map(int,input().split())
xl = list(map(int,input().split()))
xl = list(map(lambda y: abs(y-x), xl))

ans = -1
if len(xl) == 1:
    ans = xl[0]
else:
    ans = gcds(xl)

print(ans)

Fixed submission:
import math
from functools import reduce
from sys import setrecursionlimit
setrecursionlimit(10**6)

def gcd(*numbers):
  return reduce(math.gcd, numbers)

n,x = map(int,input().split())
xl = list(map(int,input().split()))
xl = list(map(lambda y: abs(y-x), xl))

ans = -1
if len(xl) == 1:
    ans = xl[0]
else:
    ans = gcd(*xl)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
c_num = set(a)
c_num = list(c_num)
c_cnt = [0]*(len(c_num))

for i in a:
    index = c_num.index(i)
    c_cnt[index] += 1

for i, x in enumerate(c_num):
    c_cnt[i] -= x

ans = 0

for i, x in zip(c_num, c_cnt):
    if x > 0:
        ans += x
    elif x < 0:
        ans += i + x

print(ans)

Fixed submission:
def count(list):
    dict = {}
    for i in range(len(list)):
        dict[list[i]] = dict.get(list[i], 0) + 1
    return dict

n = int(input())
a = list(map(int,input().split()))
dic = count(a)
ans = 0

for i, x in dic.items():
    differ = x - i
    if differ>0:
        ans += differ
    elif differ<0:
        ans += x

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,m = LI()
    route = [[] for _ in range(n+1)]
    for _ in range(m):
        a,b = LI()
        route[a].append(b)
        route[b].append(a)
    queue = collections.deque([1])
    explored = set()
    cnt = 1
    level = 0

    while len(queue)!=0 and not (level==2 and cnt==0):
        if cnt==0:
            level+=1
            cnt+=len(queue)
        ex = queue.popleft()
        explored.add(ex)
        for i in route[ex]:
            if i not in queue and i not in explored:
                queue.append(i)
        cnt -= 1
    
    ans = n in explored
    print("POSSIBLE" if ans else "IMPOSSIBLE")
        

main()            


Fixed submission:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,m = LI()
    route = [[] for _ in range(n+1)]
    for _ in range(m):
        a,b = LI()
        route[a].append(b)
        route[b].append(a)
    ans = False

    for i in route[n]:
        if 1 in route[i]:
            ans = True
    print("POSSIBLE" if ans else "IMPOSSIBLE")
        

main()
Buggy python submission (verdict: Runtime Error):
1 print(15 - sum(list(map(int, input().split()))))

Fixed submission:
A = list(map(int, input().split()))

key = 0
for i,a in enumerate(A):
    if a == 0:
        key = i+1
        break
print(key)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    k = kwargs["k"]
    res = 0
    p = 7
    rs = 0

    while True:
        res += 1
        r = p % k
        rs += r
        if rs % k == 0:
            break
        if r == 0:
            res = -1
            break
        p = r * 10


    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["k"] = [int(i) for i in cin][0]

    cout = main(kwargs)
    print(cout)

Fixed submission:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    k = kwargs["k"]
    res = 0
    p = 7
    rs = 0

    while True:
        res += 1
        r = p % k
        rs += r
        if rs % k == 0:
            break
        if r == 0 or res > 1000000:
            res = -1
            break
        p = r * 10


    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["k"] = [int(i) for i in cin][0]

    cout = main(kwargs)
    print(cout)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    C = kwargs["C"]
    res = n

    for i in range(n + 1):
        left = C[:i+1].count("W")
        right = C[i+1:].count("R")
        tmp = max([left, right])
        res = min(res, tmp)
    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]
    cin = input().split()[0]
    kwargs["C"] = [i for i in cin]

    cout = main(kwargs)
    print(cout)

Fixed submission:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    C = kwargs["C"]
    left = 0
    right = C.count("R")
    res = max([left, right])

    for i in range(n):
        if C[i] == "W":
            left += 1
        if C[i] == "R":
            right -= 1
        tmp = max([left, right])
        res = min(res, tmp)
    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]
    cin = input().split()[0]
    kwargs["C"] = [i for i in cin]

    cout = main(kwargs)
    print(cout)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    A = kwargs["A"]
    res = 1

    for i in range(n):
        res *= A[i]

    if res > 10 ** 18:
        res = -1

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]

    cin = input().split()
    kwargs["A"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)
    

Fixed submission:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    A = kwargs["A"]
    res = 1

    for i in range(n):
        res *= A[i]

        if res > 10 ** 18:
            res = -1

            if min(A) == 0:
                res = 0

            break

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]

    cin = input().split()
    kwargs["A"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp

def trial_division(n):
    a = []
    f = 2
    while n > 1:
        if n % f == 0:
            a.append(f)
            n /= f
        else:
            f += 1
    return a

INF = inf
MOD = 1000000007

n = int(input())

tmp = 0
res = 0

cnt = Counter(trial_division(n))
for i in cnt.values():
    res += floor((-1 + sqrt(1 + 8 * i)) / 2)

print(res)


Fixed submission:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    arr = factorization(n)

    res = 0

    for i in arr:
        res += sankaku(i[1])

    if n == 1:
        res = 0

    return res

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def sankaku(n):
    res = 1

    while True:
        if res * (res + 1) // 2 <= n < (res + 1) * (res + 2) // 2:
            break

        res += 1

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]

    cout = main(kwargs)
    print(cout)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    a = kwargs["a"]
    b = kwargs["b"]
    n = kwargs["n"]
    res = 0

    for i in range(min(b, n) + 1):
        res = max(res, a * i // b - a * (i // b))

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    #kwargs["n"] = cin[0]
    #kwargs["n"], kwargs["k"] = cin
    #kwargs["n"] = [int(i) for i in cin][0]
    kwargs["a"], kwargs["b"], kwargs["n"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)

Fixed submission:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    a = kwargs["a"]
    b = kwargs["b"]
    n = kwargs["n"]

    for i in range(min(b, n), -1, -1):
        res = a * i // b - a * (i // b)

        if res > 0:
            break

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    #kwargs["n"] = cin[0]
    #kwargs["n"], kwargs["k"] = cin
    #kwargs["n"] = [int(i) for i in cin][0]
    kwargs["a"], kwargs["b"], kwargs["n"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)

Buggy python submission (verdict: Runtime Error):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    a = kwargs["a"]
    b = kwargs["b"]
    n = kwargs["n"]

    R = [0 for i in range(b)]

    for i in range(min(b, n) + 1):
        R[i] = a * i // b - a * (i // b)

    res = max(R)
    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    #kwargs["n"] = cin[0]
    #kwargs["n"], kwargs["k"] = cin
    #kwargs["n"] = [int(i) for i in cin][0]
    kwargs["a"], kwargs["b"], kwargs["n"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)

Fixed submission:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    a = kwargs["a"]
    b = kwargs["b"]
    n = kwargs["n"]

    for i in range(min(b, n), -1, -1):
        res = a * i // b - a * (i // b)

        if res > 0:
            break

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    #kwargs["n"] = cin[0]
    #kwargs["n"], kwargs["k"] = cin
    #kwargs["n"] = [int(i) for i in cin][0]
    kwargs["a"], kwargs["b"], kwargs["n"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)
Buggy python submission (verdict: Runtime Error):
import numpy as np
import math as m

def main(**kwargs):
    r = -1

    for i in range(10 ** n - 1, 10 ** (n - 1) - 1, -1):
        stri = str(i)
        flg = True

        for s, c in zip(S, C):
            if stri[s-1] != str(c):
                flg = False
    
        if flg:
            r = i

    if n == 1 and S == np.array([1]) and C == np.array([0]):
        r = 0

    return r

if __name__ == "__main__":
    cin = np.array(input().split(" ")).astype("int")
    n, m, *_ = cin

    S = np.zeros(m, dtype="int")
    C = np.zeros(m, dtype="int")

    for i in range(m):
        cin = np.array(input().split(" ")).astype("int")
        S[i], C[i] = np.array(cin)
    
    cout = main(n=n, m=m, S=S, C=C)
    print(cout)

Fixed submission:
import numpy as np
import math as m

def main(**kwargs):
    r = -1

    if n > 1:
        for i in range(10 ** n - 1, 10 ** (n - 1) - 1, -1):
            stri = str(i)
            flg = True

            for s, c in zip(S, C):
                if stri[s-1] != str(c):
                    flg = False
        
            if flg:
                r = i

    else:
        for i in range(9, -1, -1):
            stri = str(i)
            flg = True

            for s, c in zip(S, C):
                if stri[s-1] != str(c):
                    flg = False
        
            if flg:
                r = i
    
    return r

if __name__ == "__main__":
    cin = np.array(input().split(" ")).astype("int")
    n, m, *_ = cin

    S = np.zeros(m, dtype="int")
    C = np.zeros(m, dtype="int")

    for i in range(m):
        cin = np.array(input().split(" ")).astype("int")
        S[i], C[i] = np.array(cin)
    
    cout = main(n=n, m=m, S=S, C=C)
    print(cout)
Buggy python submission (verdict: Runtime Error):
import numpy as np

def main(n, xs):
    minx = min(xs)
    maxx = max(xs)
    ress = []

    for x in range(minx, maxx+1):
        res = sum((xs - x) ** 2)
        ress.append(res)

    res = min(ress)
    return res

if __name__ == "__main__":
    cin = iter(input, "")
    n = int(cin[0].split(" ")[0])
    xs = np.array(cin[1].split(" ")).astype("int")
    cout = main(n, xs)
    print(cout)

Fixed submission:
import numpy as np

def main(n, xs):
    minx = min(xs)
    maxx = max(xs)
    ress = []

    for x in range(minx, maxx+1):
        res = sum((xs - x) ** 2)
        ress.append(res)

    res = min(ress)
    return res

if __name__ == "__main__":
    cin = input()
    n = int(cin.split(" ")[0])
    cin = input()
    xs = np.array([], dtype="int")

    for i in range(n):
        xs = np.append(xs, int(cin.split(" ")[i]))
    cout = main(n, xs)
    print(cout)

Buggy python submission (verdict: Runtime Error):
import numpy as np

def main(n, xs):
    minx = min(xs)
    maxx = max(xs)
    ress = []

    for x in range(minx, maxx+1):
        res = sum((xs - x) ** 2)
        ress.append(res)

    res = min(ress)
    return res

if __name__ == "__main__":
    cin = list(iter(input, ""))
    n = int(cin[0].split(" ")[0])
    xs = np.array(cin[1].split(" "))
    xs = xs.astype("int")
    cout = main(n, xs)
    print(cout)

Fixed submission:
import numpy as np

def main(n, xs):
    minx = min(xs)
    maxx = max(xs)
    ress = []

    for x in range(minx, maxx+1):
        res = sum((xs - x) ** 2)
        ress.append(res)

    res = min(ress)
    return res

if __name__ == "__main__":
    cin = input()
    n = int(cin.split(" ")[0])
    cin = input()
    xs = np.array([], dtype="int")

    for i in range(n):
        xs = np.append(xs, int(cin.split(" ")[i]))
    cout = main(n, xs)
    print(cout)
Buggy python submission (verdict: Runtime Error):
n = int(input())
k = int(input())
if n >= k:
  n = n % k

print(min(n, -(n-k)))

Fixed submission:
n, k = map(int, input().split())

if n >= k:
  n %= k
  
print(min(n, -(n-k)))

Buggy python submission (verdict: Runtime Error):
n = int(input())
k = int(input())

if n >= k:
  n %= k
  
print(n)

Fixed submission:
n, k = map(int, input().split())

if n >= k:
  n %= k
  
print(min(n, -(n-k)))
Buggy python submission (verdict: Runtime Error):
N, X, T = map(int, input().split())

print(floor(N/X) * T + 1)

Fixed submission:
import math
N, X, T = map(int, input().split())
 
if N % X == 0:
  print(int(N / X * T))
else:
  print((math.floor(N/X) + 1) * T)
Buggy python submission (verdict: Runtime Error):
using namespace std;
#include <iostream>
int main(){
  int N;
  cin >> N;

  if (N % 1000 == 0) cout << 0;
  else cout << 1000 - N % 1000;

  return 0;
}

Fixed submission:
a = int(input())

if a % 1000 == 0:
  out = 0
else:
  out = 1000 - a % 1000

print(out)

Buggy python submission (verdict: Runtime Error):
n = int(input())
 
nodes = []
nodes_append = nodes.append
components = 0
for i in range(n - 1):
  edge = tuple(map(int, input().split()))
  node_max = max(edge)
  node_min = min(edge)
  if node_min not in nodes:
      nodes_append(node_min)
      components += node_min * (n - node_min + 1)
  if node_max not in nodes:
      nodes_append(node_max)
      components += node_max * (n - node_max + 1)
  num_edge = node_min * (n - node_max + 1)
  components -= num_edge
  
print(components)

Fixed submission:
a = int(input())

if a % 1000 == 0:
  out = 0
else:
  out = 1000 - a % 1000

print(out)

Buggy python submission (verdict: Runtime Error):
import numpy as np

n = int(input())
edges = [list(map(int, input().split())) for _ in range(n - 1)]

grid = np.zeros([n, n])

for i in range(n - 1):
  edge = sorted(edges[i])
  grid[edge[0] - 1][edge[1] - 1] = 1

components = 0
for i in range(n):
  for j in range(i, n):
    loss = grid[i:j + 1, i:j + 1].sum()
    components += j - i + 1 - loss
  
print(int(components))

Fixed submission:
a = int(input())

if a % 1000 == 0:
  out = 0
else:
  out = 1000 - a % 1000

print(out)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

types = ['AC', 'WA', 'TLE', 'RE']
num = int(input())
categories = defaultdict(int)

for i in range(num):
    categories[input()] += 1

for i in types:
    print("{} x {}".format(i, categories[i]))

Fixed submission:
a = int(input())

if a % 1000 == 0:
  out = 0
else:
  out = 1000 - a % 1000

print(out)

Buggy python submission (verdict: Runtime Error):
a = input()
print(a % 1000)

Fixed submission:
a = int(input())

if a % 1000 == 0:
  out = 0
else:
  out = 1000 - a % 1000

print(out)
Buggy python submission (verdict: Runtime Error):
N = int(input())
As = list(map(int, input().split()))
Q = int(input())
 
sumAs = 0
dictAs = [0] * 1E5
for i in range(len(As)):
  sumAs += As[i]
  dictAs[As[i] - 1] = 1


for _ in range(Q):
  b, c = map(int, input().split())
  sumAs += dictAs[b - 1] * (c - b)
  dictAs[c - 1] += dictAs[b - 1]
  dictAs[b - 1] = 0
  print(sumAs)
  

Fixed submission:
N = int(input())
As = list(map(int, input().split()))
Q = int(input())
 
sumAs = 0
dictAs = [0] * 100000
for i in range(len(As)):
  sumAs += As[i]
  dictAs[As[i] - 1] += 1


for _ in range(Q):
  b, c = map(int, input().split())
  sumAs += dictAs[b - 1] * (c - b)
  dictAs[c - 1] += dictAs[b - 1]
  dictAs[b - 1] = 0
  print(sumAs)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))
Q = int(input())

sumAs = sum(As)

for _ in range(Q):
  b, c = map(int, input().split())
  for i in range(len(As)):
    if As[i] == b:
      As[i] = c
      sumAs = sumAs - b + c
  print(sumAs)
  


Fixed submission:
N = int(input())
As = list(map(int, input().split()))
Q = int(input())
 
sumAs = 0
dictAs = [0] * 100000
for i in range(len(As)):
  sumAs += As[i]
  dictAs[As[i] - 1] += 1


for _ in range(Q):
  b, c = map(int, input().split())
  sumAs += dictAs[b - 1] * (c - b)
  dictAs[c - 1] += dictAs[b - 1]
  dictAs[b - 1] = 0
  print(sumAs)