peterverebics/CodeNet_Python_428 huggingface dataset:
# Judge status: Accepted

import math
 
def f(A, B, X):
	return math.floor((A*X)/B) - A*math.floor(X/B)
 
A,B,N = map(int,input().split())
 
if N < B:
	print(f(A,B,N))
else:
	print(f(A,B,B-1))
# Judge status: Wrong Answer
A, B, C = map(int, input().split())

for i in range(10**6):
    if A * i % B == C:
        print('Yes')
        break
else:
    print('No')
# Judge status: Wrong Answer
from operator import mul
from functools import reduce

N, P = map(int, input().split())
A = list(map(int, input().split()))
cE, cO = 0, 0

for a in A:
    if a % 2 == 0:
        cE += 1
    else:
        cO += 1


# 全て偶数
if cO == 0:
    ans = 2 ** (N - 1) if P == 0 else 0
    print(ans)
    exit()
else:
    print(2 ** (N - 1))
# Judge status: Accepted
A=list(map(int,input().split()))
B=sorted(A)
print(B[0]*B[1]//2)
# Judge status: Accepted
a,b=map(int,input().split())
print(a-1+(b>=a))
# Judge status: Accepted
N,M=map(int,input().split())

Pre=[[] for _ in range(N)]
for i in range(M):
    p,y=map(int,input().split())
    Pre[p-1].append([y,i+1])

ID=[0 for _ in range(M)]
for j in range(N):
    Pre[j].sort()
    for k in range(len(Pre[j])):
        ID[Pre[j][k][1]-1]=str(j+1).zfill(6)+str(k+1).zfill(6)

for d in ID:
    print(d)
# Judge status: Accepted
a,b = map(int,input().split())
s = input()
sw = 0
for i in range(len(s)):
    if i == a and s[i] != '-':
        sw = 1
    elif i != a and s[i] == '-':
        sw = 1
    else:
        pass
if sw == 1:
    print('No')
else:
    print('Yes')
# Judge status: Accepted
N=int(input())
S=input()
for i in range(0, len(S)):
    print(chr((ord(S[i])-ord('A')+N)%26+ord('A')), end="")
# Judge status: Accepted
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

s = input().strip()

from collections import Counter
c = Counter(s)
if set(list(c.values())) == {1}:
    print('yes')
else:
    print('no')
# Judge status: Wrong Answer
N = int(input())
S = [set(input())for _ in range(N)]
m = 0
for i in S:
    m += S.count(i)
    
print(m)
# Judge status: Accepted
k, x = list(map(int, input().split()))
print('Yes' if k * 500 >= x else 'No')
# Judge status: Wrong Answer
s = list(map(int,input().split()))

for i in s:
    if i == 0:
        print(s[i]+1)
# Judge status: Accepted
N = int(input())

X = int(N / 1.08)

ans = ':('
for x in range(X-5, X+6):
    n = int(x * 1.08)
    if n == N:
        ans = x
        break
print(ans)
# Judge status: Accepted
N = int(input())
A = []
B = []
for i in range(N):
  a, b = map(int, input().split())
  A.append(a)
  B.append(b)
A = list(reversed(A))
B = list(reversed(B))

ans = 0
for i in range(N):
  d, m = divmod(A[i] + ans, B[i])
  if m > 0:
    add = B[i] - m
    ans += add

print(ans)
# Judge status: Accepted
X = int(input())

years = 0
money = 100
while(True) :
    years += 1
    money += int(money * 0.01)
    if (X <= money) :
        break

print(years)
# Judge status: Accepted
A, B = map(int, input().split())

if 9<A or 9<B:
    print('-1')
else: print(A*B)
# Judge status: Accepted
K = int(input())
answer = 0
if K % 2 == 0:
    answer = (K // 2) ** 2
else:
    answer = (K // 2) * (K // 2 + 1)
print(answer)
# Judge status: Accepted
# Assumed solution

from collections import deque


def main():
    k = int(input())

    queue = deque()
    for i in range(1, 10):
        queue.append(i)

    l = 0
    for _ in range(k):
        l = queue.popleft()
        r = l % 10
        if r != 0:
            queue.append(l * 10 + r - 1)
        queue.append(l * 10 + r)
        if r != 9:
            queue.append(l * 10 + r + 1)
    print(l)


if __name__ == "__main__":
    main()
# Judge status: Wrong Answer
n=int(input())
s=input()
ans=n+1
l=[0]*(n+1)
for i in range(n):
  if l[i]=='E':
    l[i+1]=l[i]+1
ans=n+1
for i in range(n):
  ans=min(ans,i-l[i-1]+l[n]-l[i])
print(ans)
# Judge status: Wrong Answer
x, y= map(int, input().split())

import math
a=(2*y-x)//3
b=(2*x-y)//3


if x%3==0 and y%3==0:
	s= math.factorial(a+b)
	d=math.factorial(a)*math.factorial(b)
	print(s//d)

else:
	print(0)
# Judge status: Wrong Answer
N=int(input())
flag='Yes'
p,q,r=0,0,0
for _ in range(N):
  t,x,y=map(int,input().split())
  a=x+y-p-q
  t=t-r
  if a<=t:
    if (t-a)%2!=0:
      flag='No'
      break
  else:
    flag='No'
    break
  p=x
  q=y
  r=t
print(flag)
# Judge status: Accepted
s=input()
a=s.count("R")
if a==0:
    print(0)
elif a==1:
    print(1)
elif a==2:
    if s[1]=="S":
        print(1)
    else:
        print(2)
else:
    print(3)
# Judge status: Accepted
import sys
input = sys.stdin.readline
from collections import deque
n, m = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
T = [-1]*n
que = deque()
que.append(0)
while que:
    v = que.popleft()
    for nv in G[v]:
        if T[nv] == -1:
            T[nv] = v+1
            que.append(nv)
print("Yes")
print(*T[1:], sep="\n")
# Judge status: Accepted
n = int(input())

l = []
for i in range(1, n):
    a = i
    b = n - i
    ra = 0
    rb = 0

    flg = True
    while flg:
        aa = a % 10
        a //= 10
        ra += aa
        if a == 0:
            flg = False
    flg = True
    while flg:
        bb = b % 10
        b //= 10
        rb += bb
        if b == 0:
            flg = False
    l.append(ra+rb)
print(min(l))
# Judge status: Wrong Answer
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
l = ab[(n+1)//2-1][0]
ab.sort(key=lambda x:x[1], reverse=True)
r = ab[(n+1)//2-1][1]

if n % 2 == 0:
    print((r-l)*2-1)
else:
    print(r-l+1)
# Judge status: Accepted
import sys

input = sys.stdin.readline

def main():
    n = int(input())
    a = list(map(int, input().split()))
    m = 10**5
    l = [0]*m
    for i in range(n):
        if a[i] >= 1:
            l[a[i]-1] += 1
        l[a[i]] += 1
        if a[i] < m-1:
            l[a[i]+1] += 1
    print(max(l))

if __name__ == '__main__':
    main()
# Judge status: Wrong Answer
s=input()
if s.count('N')==s.count('S') and s.count('E')==s.count('W'):
  print('Yes')
else:
  print('No')
# Judge status: Accepted
n = int(input())
A = list(map(int, input().split()))

sum_A = [0 for _ in range(n + 1)]
for i in range(1, n + 1):
    sum_A[i] = sum_A[i - 1] + A[i - 1]

ans = float('inf')
for i in range(1, n):
    ans = min(abs(sum_A[i] - (sum_A[-1] - sum_A[i])), ans)
print(ans)
# Judge status: Accepted
#ABC113

n = int(input())
t,a = list(map(int,input().split()))
h = list(map(int,input().split()))
l = [abs(t - h[i]*0.006-a) for i in range(n)]

print(l.index(min(l))+1)
# Judge status: Accepted
n = input()
a = list(map(int,input().split()))

s = set()
b = 0
for i in a:
    if i < 3200:
        s.add(i//400)
    else:
        b += 1
else:
    min = len(s)
    print(max(min, 1), min + b)
# Judge status: Accepted
N = int(input())
res = 1
while True:
    val = 2 * res
    if val <= N:
        res = val
    else:
        break
print(res)
# Judge status: Accepted
x = int(input())

a=b=0

n=200

for a in range(-n,n):
	for b in range(-n,n):
		if a**5 - b**5 == x:
			print(a,b)
			exit(0)
# Judge status: Accepted
# coding: utf-8
N=int(input())
A=list(map(int,input().split()))

A.sort()
ans=max(A)

for i in range(N-2):
    ans+=A[-(2+i//2)]

print(ans)
# Judge status: Accepted
n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)

ans = 10 ** 20
s_sum = 0
for i in range(n - 1):
    s_sum += a[i]
    ans = min(ans, abs((sum_a - s_sum) - s_sum))
print(ans)
# Judge status: Accepted
N = int(input())
S = input()

ans = 0
for i in range(N):
    left = set(S[:i])
    right = set(S[i:])
    ans = max(ans, len(left&right))
print(ans)
# Judge status: Wrong Answer
S=raw_input()
month=int(S[5:7])
if month>=5:
  print "TBD"
else:
  print "HeiSei"
# Judge status: Accepted
A, B, N = map(int, input().split())

if N >= B:
    x = B - 1
else:
    x = N

ans = (A * x) // B - A * (x // B)
print(ans)
# Judge status: Accepted
a,b,n = map(int, input().split())

x =  min(b-1, n)
print(int(a*x/b) - a*int(x/b))
# Judge status: Accepted

nim = list(map(int,input().split()))
nim.sort(reverse=True)
print(nim[0]*10 + nim[1] + nim[2])
# Judge status: Accepted
x,y = map(int,input().split())
li = list(map(int,input().split()))	
cnt = 0
for i in range(len(li)):
    if li[i] >= sum(li) / (4*y):
        cnt += 1
else:
    pass

if cnt >= y:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
n = int(input())
print((10**n-8**n-(9**n-8**n)*2)%(10**9+7))
# Judge status: Accepted
n,k = map(int,input().split())
s = input()
ans = s.count("RL") * 2
ans1 = 0
if s[0]=="L":
	ans1 += 1
if s[-1]=="R":
	ans1 += 1
while k > 0:
	if ans > 0:
		if ans == 2 and ans1 == 0:ans1 += 1;ans -= 2;break
		ans -= 2
	elif ans1 == 2:
		ans1 -= 1
	k -= 1
print(n - ans - ans1)
# Judge status: Accepted
n, k = map(int, input().split())
l = list(input().split())

ans = n
while 1:
    for i in str(ans):
        if i in l:
            break
    else:
        break
    ans += 1

print(ans)
# Judge status: Accepted
import sys
input = sys.stdin.readline

H,W=list(map(int,input().split()))

num_ac = H*W
if H>=2 and W>=2:
    num_hd = 2*W+2*(H-2)
elif (H == 1 and W>= 2) or ( H>=2 and W==1):
    num_hd = 2

else:
    num_hd = 0

print(num_ac-num_hd)
# Judge status: Wrong Answer
N,K=map(int,input().split())
h=[]
for i in range(N):
    h.append(int(input()))
    
    
h.sort(reverse=True)
Q=abs(h[0]-h[K-1])
h.sort()
QQ=abs(h[0]-h[K-1])
print(min(Q,QQ))
# Judge status: Accepted
N = int(input())
arr = list(map(int, input().split()))

ans = arr[0]+arr[N-2]
for i in range(1,N-1,1):
   ans += min(arr[i],arr[i-1])
   
print(ans)
# Judge status: Accepted
def main():
    n = int(input())

    for i in range(1, 50000):
        if i * 108 // 100 == n:
            print(i)
            break
    else:
        print(":(")


if __name__ == "__main__":
    main()
# Judge status: Accepted
import math
a,b = map(int,input().split())
x = (a+b) / 2
print(math.ceil(x))
# Judge status: Wrong Answer
N = int(input())


def f(x1, y1, x2, y2):
    return abs(x1 - x2) + abs(y1 - y2)


pt = 0
cx, cy = 0, 0
for _ in range(N):
    t, x, y = map(int, input().split())
    d = f(cx, cy, x, y)
    if t - pt < d:
        print('No')
        exit()
    if (t - pt - d) % 2 != 0:
        print('No')
        exit()
print('Yes')

# 最短距離にかかる time + 2 * i ならいける
# Judge status: Wrong Answer
n = int(input())
arr = list(map(int, input().split()))
arrr = arr.sort(reverse=True)
ans = sum(arr[n:n + n])
print(ans)
# Judge status: Wrong Answer
a, b = map(int, input().split())

if(a >= b):
    print(str(a) * b)
else:
    print(str(b) * a)
# Judge status: Accepted
s=input()
t=input()

s_d = ''.join(sorted(s))
t_d = ''.join(sorted(t,reverse=True))

arr = [t_d,s_d]
arr=sorted(arr)

if s==t:
    print('No')
    exit(0)

if arr[0]==s_d:
    print('Yes')
else:
    print('No')
# Judge status: Accepted
x,a,b = map(int,input().split())
A = abs(a-x)
B = abs(b-x)
if A > B:
  print("B")
else:
  print("A")
# Judge status: Wrong Answer

a, b, c = map(int, input().split())

for i in range(1, b + 1):
    print((a * i) % b, a * i)
    if (a * i) % b == c:
        print("YES")
        exit()
print("NO")
# Judge status: Wrong Answer
N = int(input())
a = list(map(int, input().split()))
a.sort()
right = N // 2
left = right - 1
for i in range(N):
    if i < N / 2:
        print(a[right])
    else:
        print(a[left])
# Judge status: Accepted
#ABC166-A
X = input()
print(X == "ABC" and "ARC" or "ABC")
# Judge status: Wrong Answer
arr = list(map(int,input().split()))
arr.sort()
print(int(arr[0]) + int(arr[2]+arr[1]))
# Judge status: Wrong Answer
n = int(input())
l, l1, l2 = list(""), list(""), list("")
for i in range(n):
    x, y = map(int, input().split())
    l1.append(x)
    l2.append(y)
for i in range(n - 1):
    for j in range(i + 1, n):
        l.append(((l1[i] -l1[j]) ** 2 + (l2[i] - l2[j]) ** 2) ** 0.5)
print(l)
print('{:.10f}'.format(2 * sum(l) / len(l)))
# Judge status: Wrong Answer
N=int(input())
k=0
s=0
m=0
L=list(map(int,input().split()))
for i in range(N):
  if L[i]==i+1:
    m+=1
  else:
    s+=m//2+m%2
    m=0
print(s)
# Judge status: Wrong Answer
s = input()
l = list(s)
count = 0
ans = 0
for i in range(len(l)):
    if l[i] == "A" or l[i] == "T" or l[i] == "G" or l[i] == "C":
        count += 1
        continue
    ans = max(ans,count)
    count = 0
print(ans)
# Judge status: Accepted
N = int(input())
out=0

if N%2 ==1:
    out=0

else:
    k=5
    while k<10**18:
        out+= (N//k)//2
        k*=5

print(out)
# Judge status: Accepted
import itertools

N, M, Q = map(int, input().split())
A = [int(i + 1) for i in range(M)]
abcd = [[int(j) for j in input().split()] for i in range(Q)]
ans = 0
for i in itertools.combinations_with_replacement(A, N):
	a = 0
	for j in abcd:
		if i[j[1] - 1] - i[j[0] - 1] == j[2]:
			a += j[3]
	ans = max(a, ans)
print(ans)
# Judge status: Accepted
import collections

n,m=map(int, input().split())
g=[[] for _ in range(n+1)]
for i in range(m):
	a,b=map(int,input().split())
	g[a].append(b)
	g[b].append(a)

q=collections.deque()
q.append(1)
visit=[-1]*(n+1)
visit[1] = 1
ans=[0]*(n+1)
while q:
	v=q.popleft()
	for i in g[v]:
		if visit[i] == -1:
			ans[i] = v
			visit[i] = 1
			q.append(i)
print('Yes')
for i in range(2,n+1):
	print(ans[i])
# Judge status: Accepted
from collections import Counter

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

c = {k: v for k, v in Counter(A).items() if v >= 2}

if len(c) < 2:
    print(0)
    exit()

x = max(c)

if c[x] >= 4:
    print(x * x)
else:
    del c[x]
    y = max(c)
    print(x * y)
# Judge status: Accepted
n,m,x = list(map(int,input().split()))
ca = []
ans = -1

for i in range(n):
  ca.append(list(map(int,input().split())))

for i in range(2 ** n):
  A_sum = [0] * m
  C_sum = 0
  for j in range(n):
    if (i >> j) & 1:
      C_sum += ca[j][0]
      for k in range(1,m+1):
        A_sum[k-1] += ca[j][k]
  if min(A_sum) >= x:
    if ans == -1:
      ans = C_sum
    elif C_sum < ans:
      ans = C_sum

print(ans)
# Judge status: Accepted
s = list(map(int, input().split()))

N = s[0]
K = s[1]

print(min(N % K, K - N % K))
# Judge status: Wrong Answer
n,k = map(int, input().split())
x = list(map(int, input().split()))

ans=10**12
for i in range(n):
  if i+k <= n:
    t=x[i:i+k]
    if min(t)*max(t) > 0:
      ans=min(ans, abs(max(t)))
    else:
      ans=min(ans,min(abs(min(t)),max(t))*2+max(abs(min(t)),max(t)))
  else:
    break
    
print(ans)
# Judge status: Accepted
#043_B
s=input()
t=''
for i in range(len(s)):
    if s[i]=='B':
        if len(t)!=0:
            t=t[:-1]
    else:
        t+=s[i]
print(t)
# Judge status: Accepted
n,m = map(int,input().split())
ans = [0]*2
tmp = [0]*n
false = [0]*n
for _ in range(m):
    p,s = map(str,input().split())
    p = int(p)
    if tmp[p-1] == 0:
        if s == 'AC':
            ans[0] += 1
            tmp[p-1] += 1
            ans[1] += false[p-1]
        elif s == 'WA':
            false[p-1] += 1
print(*ans)
# Judge status: Accepted
N,L = map(int,input().split())
R = L+N-1
if L <= 0 <= R:
    print(sum(range(L,R+1)))
elif L > 0:
    print(sum(range(L+1,R+1)))
else:
    print(sum(range(L,R)))
# Judge status: Accepted
n = int(input())
ans = 10000000000

for i in range(n//2):
    x = i+1
    y = str(n-x)
    x = str(x)
    z = 0

    for c in x:
        z += int(c)
    for c in y:
        z += int(c)
    
    if z < ans:
        ans = z

print(ans)
# Judge status: Accepted
(N, _), *edges = [list(map(int, s.split())) for s in open(0)]
graph = {i: [] for i in range(N)}
for a, b in edges:
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
print("\n".join(str(len(graph[i])) for i in range(N)))
# Judge status: Accepted
s = input()
a,b = int(s[:2]),int(s[2:])

c = 0
if 1 <= a <= 12:
    c += 1
if 1 <= b <= 12:
    c += 2

print(['NA', 'MMYY', 'YYMM', 'AMBIGUOUS'][c])
# Judge status: Accepted
n,Y = map(int,(input().split()))
x = -1
y = -1
z = -1
for i in range(n+1):
    for j in range(n+1):
        k=Y-i*10000-j*5000
        if k==(n-i-j)*1000 and k>=0:
            x=i
            y=j
            z=int(k/1000)
print(x,y,z)
# Judge status: Wrong Answer
n, m, k = map(int, input().strip().split())
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
i=j=0

time=0
while j<m:
    time+=B[j]
    if time >k:
        time-=B[j]
        break
    j += 1
j-=1
rem=k-time
ans=j+1
cnt=ans
while j>=0:
    while i<n and rem-A[i]>=0:
        rem-=A[i]
        i+=1
        cnt+=1
    ans=max(ans,cnt)
    
    rem+=B[j]
    j-=1
    cnt-=1

print(ans)
# Judge status: Wrong Answer
n, m = map(int, input().split())
X = sorted(map(int, input().split()))
diff = []
for x, y in zip(X, X[1:]):
    diff.append(y-x)
diff.sort()
print(sum(diff[:m-n]))
# Judge status: Accepted
n = int(input())
A = list(map(int, input().split()))
L = [i+x for i, x in enumerate(A)]
R = [i-x for i, x in enumerate(A) if i-x > 0]

from collections import Counter
P = Counter(L)
Q = Counter(R)
 
ans = 0
for k, v in P.items():
  if k in Q:
    ans += v*Q[k]
print(ans)
# Judge status: Wrong Answer
S=input()
print(S if S!="ABC" else "ABC")
# Judge status: Accepted
import queue
N, M = map(int,input().split())
D = [[] for i in range(N)]

for i in range(M):
  a,b = map(int,input().split())
  D[a-1].append(b)
  D[b-1].append(a)

print("Yes")

dist = [0 for i in range(N)]
prev = [0 for i in range(N)]
prev[0] == 1

q = queue.Queue()
q.put(1)

while not q.empty():
  t = q.get()
  for i in D[t-1]:
    if prev[i-1] == 0:
      prev[i-1] = t
      q.put(i)

for i in prev[1:]:
  print(i)
# Judge status: Wrong Answer
from collections import deque
from operator import itemgetter

N, M = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(N)]
jobs = sorted(jobs, key=itemgetter(1,0), reverse=True)

q = deque(jobs)
res = 0
for i in range(M):
    while q:
        job = q.popleft()
        if M - job[0] >= i:
            res += job[1]
            break

print(res)
# Judge status: Accepted
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
p = tuple([int(i) for i in readline().split()])
q = tuple([int(i) for i in readline().split()])

if p == q:
    print(0)
    exit()

from itertools import permutations
L = list(range(1,n+1))
for i,c in enumerate(permutations(L)):
    if c == p:
        a = i
    elif c == q:
        b = i

print(abs(a-b))
# Judge status: Accepted
import math

h = int(input())

x = int(math.log2(h))
res = 2**(x+1)-1

print(res)
# Judge status: Accepted
n=int(input())
a,b=2,1
for i in range(n):
  a,b=b,a+b
print(a)
# Judge status: Wrong Answer
a, b = sorted(map(int, input().split()))

if (b-a)%2==0:
    print(int((b-a)/2))
else:
    print('IMPOSSIBLE')
# Judge status: Wrong Answer
K,X = input().split()

def main(K,X):
  if K * 500 >= X:
    print('Yes')
  else:
    print('No')
 
if __name__ == '__main__':
  main(K,X)
# Judge status: Wrong Answer
S = input()

S = 'BAB'

if S == 'AAA' or S== 'BBB':
    print("No")
    
else:
    print("Yes")
# Judge status: Accepted
s = input()

n = len(s)
ans = "Yes"

if s != s[::-1]:
    ans = "No"
elif s[:(n-1)//2] != s[:(n-1)//2][::-1]:
    ans = "No"
elif s[(n+2)//2:] != s[(n+2)//2:][::-1]:
    ans = "No"
    
print(ans)
# Judge status: Accepted
def solve(string):
    _, c = string.split()
    r = c.count("R")
    return str(c[:r].count("W"))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))
# Judge status: Accepted
n = int(input())
w = list(map(int, input().split()))

# wをなめていって、w[:i]とw[i:]の差の絶対値が小さいものを選べば良い。
# 累積和で高速化できるが、制約が弱いため不要
ans = float('inf')
for i in range(n):
    temp = abs(sum(w[:i]) - sum(w[i:]))
    if ans > temp:
        ans = temp
print(ans)
# Judge status: Wrong Answer
s = input()
a = s.count('a')
b = s.count('b')
c = s.count('c')

ans = ''
if a == b == c - 1:
  ans = 'YES'
elif a == b - 1 == c:
  ans = 'YES'
elif a - 1 == b == c:
  ans = 'YES'
elif a == b == c + 1:
  ans = 'YES'
elif a == b + 1 == c:
  ans = 'YES'
elif a + 1 == b == c:
  ans = 'YES'
else:
  ans = 'NO'
print(ans)
# Judge status: Accepted
from collections import defaultdict
import sys
input = sys.stdin.readline

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

D = defaultdict(int)

for a in A:
    D[a] += 1

ans = sum(A)

for _ in range(Q):
    b, c = list(map(int, input().split()))
    D[c] += D[b]
    ans += c * D[b] - b * D[b]
    D[b] = 0
    print(ans)
# Judge status: Accepted
n, m= map(int, input().split())
got=0
sell={}
buy=0
for i in range(n):
  a,b=map(int,input().split())
  if a not in sell:
    sell[a]=0
  sell[a]+=b
sell=sorted(sell.items())
if m<=sell[0][1]:
  print(m*(sell[0][0]))
  exit()
for lis in sell:
  got+=lis[1]
  if got>m:
    buy+=(m-got+lis[1])*lis[0]
    print(buy)
    exit()
  else:
    buy+=(lis[0])*(lis[1])
# Judge status: Accepted
from sys import stdin
S = stdin.readline().rstrip()
if ('N' in S and 'S' not in S) or ('S' in S and 'N' not in S) or ('W' in S and 'E' not in S)or ('E' in S and 'W' not in S):
    print("No")
else:
    print('Yes')
# Judge status: Accepted
import itertools

n,m,q = input().split()
n = int(n)
m = int(m)
q = int(q)
num_list = []
a = list(range(1, n+1))
for i in range(q):
    num_list.append(list(map(int,input().split())))
ff = list(itertools.combinations_with_replacement(range(1, m + 1), n))
max = 0
for i in range(len(ff)):
  s = 0
  for j in range(q):
    a,b,c,d = num_list[j]
    if ff[i][b-1] - ff[i][a-1] == c:
      s = s + d
  if max < s:
        max = s
print(max)
# Judge status: Accepted
n,m=map(int,input().split())
memo=[0]*n
from itertools import accumulate
for i in range(m):
    l,r=map(lambda x:int(x)-1,input().split())
    memo[l]+=1
    if r+1!=n:
        memo[r+1]-=1
ac=list(accumulate(memo))
print(ac.count(m))
# Judge status: Wrong Answer
x = int(input())

num_500 = x / 500

amari = x - num_500

num_5 = amari / 5

print(1000 * num_500 + 5 * num_5)
# Judge status: Wrong Answer
N=int(input())
if N==1:
    print(0)
else:
    sup=int(N**0.5)
    ans=0
    div2=[N]
    kari=N
    for i in range(2,sup+1):
        if N%i==0:
            div2.append(N//i)
            if kari%i==0:
                ans+=1
                kari=kari//i
    for x in div2[::-1]:
        if kari%x==0:
            ans+=1
            kari=kari//x
    print(ans)
# Judge status: Accepted
x,y,z=map(int,input().split())
print(int((x-z)/(y+z)))
# Judge status: Wrong Answer
from sys import stdin,stdout

def main():
    line=stdin.readline()
    parts=line.split()
    a=int(parts[0])
    stdout.write(str(int(a*800-round(a/15,0)*200)))
main()
# Judge status: Accepted
##A
s = input()
if s[2] == s[3] and s[4] == s[5]:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
n, a, b, c, d = map(int, input().split())
s = input()
a -= 1
b -= 1
c -= 1
d -= 1
if '##' in s[a:c+1] or "##" in s[b:d+1]:
    print('No')
    exit()

if c > d:
    if '...' in s[b-1:d+2]:
        print('Yes')
    else:
        print('No')
else:
    print('Yes')
# Judge status: Accepted
s = input()
if len(s) == len(set(s)):
  print('yes')
else:
  print('no')
# Judge status: Wrong Answer
n,k = list(map(int,input().strip().split()))
temp = []
ans = int(0)
for i in range(k):
    d = int(input())
    li = list(map(int,input().split()))
    for j in range(d):
        temp += str(li[j])
for m in range(n):
    b  = str(m + 1)
    a = temp.count(b)
    if a == 0:
        ans += 1
print(ans)
# Judge status: Accepted
C = input()

tmp = ord(C)
print(chr(tmp+1))
# Judge status: Accepted
N = int(input())
a = list(map(int, input().split()))

x, y, z = 0, 0, 0
for v in a:
    if v % 4 == 0:
        x += 1
    elif v % 2 == 0:
        y += 1
    else:
        z += 1

if x < z:
    if (z - x == 1) & (y == 0):
        print('Yes')
    else:
        print('No')
else:
    print('Yes')
# Judge status: Accepted
n = int(input())
a = list(map(int,input().split()))
for i in range(n-1):
    if a[i] > 0:
        pass
    else:
        a[i] *= -1
        a[i+1] *= -1
if a[-1] < 0:
    a[-1] *= -1
    a = sorted(a)
    ans = sum(a) - 2*a[0]
else:
    ans = sum(a)
print(ans)