Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from fractions import gcd
from functools import reduce

N = int(input())
A = [int(i) for i in input().split()]

def gcd_list(numbers):
    return reduce(gcd, numbers)

_max=1
for i in range(N):
    _max = max(gcd_list(A[0:i]+A[i+1:N]), _max)
print(_max)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
a.sort()
d={}

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors
for i in make_divisors(a[0]):
    d[i]=1


for g in range(1,n):
    if g==1:
        new={}
        for i,m in d.items():
            if a[g]%i==0:
                d[i]+=1
                if not a[g]//i in d.keys():
                    new[a[g]//i]=1
        d.update(new)
    else:
        for i,m in d.items():
            if a[g]%i==0:
                d[i]+=1

ans=1
for i,m in d.items():
    if m==n-1 or m==n:
        ans=max(ans,i)
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(pow(10, 8))
N = int(input())
As = list(map(int, input().split()))
d = dict()
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(3)]
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for x in range((N+1)**3):
    E = 0
    i,j,k = decode(x)
    s0 = N-i-j-k
    if i:
        E += (i*N)/((N-s0)**2) + d[x-1]*(i/(N-s0))
    if j:
        E += (j*N)/((N-s0)**2) + d[x-N]*(j/(N-s0))
    if k:
        E += (k*N)/((N-s0)**2) + d[x-N**2-N]*(k/(N-s0))
    d[x] = E
print(d[B])


### FIXED CODE ###

N = int(input())
As = list(map(int, input().split()))
d = dict()
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(3)]
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for k in range(N+1):
    for j in range(N+1-k):
        for i in range(N+1-k-j):
            E = 0
            x = encode(i,j,k)
            s0 = N-i-j-k
            if i:
                E += (i*N)/((N-s0)**2) + d[x-1]*(i/(N-s0))
            if j:
                E += (j*N)/((N-s0)**2) + d[x-N]*(j/(N-s0))
            if k:
                E += (k*N)/((N-s0)**2) + d[x-N**2-N]*(k/(N-s0))
            d[x] = E
print(d[B])
### BUGGY CODE ###

from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
path=[[] for i in range(n)]
for i in range(m):
  a,b=map(int,input().split())
  path[a-1].append(b-1)
  path[b-1].append(a-1)
l=deque()
l.append(1)
while True:
  flag=True
  for i in path[l[0]]:
    if not i in l:
      l.appendleft(i)
      flag=False
      break
  for i in path[l[-1]]:
    if not i in l:
      l.append(i)
      flag=False
      break
  if flag or len(l)==n:
    break
print(len(l))
for i in range(len(l)):
  l[i]+=1
print(*l)

### FIXED CODE ###

from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
path=[[] for i in range(n)]
for i in range(m):
  a,b=map(int,input().split())
  path[a-1].append(b-1)
  path[b-1].append(a-1)
l=deque()
s=set()
l.append(0)
s.add(0)
while True:
  for i in path[l[0]]:
    if not i in s:
      l.appendleft(i)
      s.add(i)
      break
  else:break
while True:
  for i in path[l[-1]]:
    if not i in s:
      l.append(i)
      s.add(i)
      break
  else:break
print(len(l))
l=list(map(lambda x:x+1,l))
print(*l)
### BUGGY CODE ###

K = int(input())
S = '7'
i = 1
if (1 <= K) and (K <= (10 ** 6)):
    while True:

        if int(S) % K != 0:
            S += '7'
            i += 1
        elif i > K:
            break
        else:
            break
    print(i)
else:
    print('"K" is out of range!')

### FIXED CODE ###

K = int(input())
if K % 2 == 0:
    print(-1)
elif K % 5 == 0:
    print(-1)
else:
    S = 7
    for i in range(K+1):
        if S % K == 0:
            print(i+1)
            break
        else:
            S = S % K
            S = S * 10 + 7
    else:
        print(-1)
### BUGGY CODE ###


 
n=int(input())
a=[]
i=1
while n!=1:
    i+=1
    b=0
    c=0
    while n%i==0:
        n=n//i
        b+=1
    while not c*(c+1)<=2*b<(c+1)*(c+2):
        c+=1
    a.append(c)
print(sum(a))

### FIXED CODE ###

n=int(input())
def f(x):
    z=1
    while not(z*(z+1)<=2*x<(z+1)*(z+2)):
        z+=1
    return z

x=[-1]*(10**6+1) #2以上の自然数に対して最小の素因数を表す
x[0]=0
x[1]=1
i=2
prime=[]
while i<=10**6:
    if x[i]==-1:
        x[i]=i
        prime.append(i)
    for j in prime:
        if i*j>10**6 or j>x[i]:break
        x[j*i]=j
    i+=1
if n==1:
    print(0)
    exit()
y=0
j=2
while j*j<=n:
    if n%j==0:
        y=1
    j+=1
if y==0:
    print(1)
    exit()
a=[]
q=0
for i in range(len(prime)):
    p=prime[i]
    while n%p==0:
        q+=1
        n=n//p
    if q>0:a.append(q)
    q=0

ans=0
for i in range(len(a)):
    ans+=f(a[i])
print(ans if n==1 else ans+1)
### BUGGY CODE ###

N,K = map(int, input().split())
A_list = list(map(int, input().split()))



for i in range(N-K+1):
  i = i+K
  sum_list = A_list[i-K:i]
  after = sum(sum_list)
  if(i!=K):
    if(before<after):
      print("Yes")
    else:
      print("No")
  before = after

### FIXED CODE ###

N,K = map(int, input().split())
A_list = list(map(int, input().split()))

for i in range(N-K):
  #print(i,K+i)
  #print(A_list[i],A_list[K+i])
  if(A_list[i]<A_list[K+i]):
    print("Yes")
  else:
    print("No")
### BUGGY CODE ###

n, k = map(int, input().split())
X = list(map(int, input().split()))

ans = float('inf')
for i in range(n-k+1):
    l, r = X[i:i+k][0], X[i:i+k][-1]
    if r < 0:
        ans = min(abs(l), ans)
    elif 0 < l:
        ans = min(abs(r), ans)
    else:
        ans = min(min(abs(l), abs(r))*2+max(abs(l), abs(r)), ans)
print(ans)

### FIXED CODE ###

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

ans = float('inf')
for L in range(n-k+1):
    R = L + k
    if A[L] < 0 and A[R-1] < 0:
        ans = min(ans, abs(A[L]))
    elif A[L] < 0 and 0 <= A[R-1]:
        ans = min(ans, min(abs(A[L]), abs(A[R-1]))*2 + max(abs(A[L]), abs(A[R-1])))
    else:
        ans = min(ans, abs(A[R-1]))
print(ans)
### BUGGY CODE ###

n = int(input())
li = list(map(int,input().split()))
for i in range(1,n+1):
    if i in li:
        print(li.count(i))
        li.remove(i)
    else:
        print(0)

### FIXED CODE ###

n = int(input())
li = list(map(int,input().split()))
li.sort(reverse = True)
count = 0
for i in range(1,n+1):
    count = 0
    while True:
        if li:
            if li[-1]==i:
                count+=1
                li.pop()
            else:
                break
        else:
            break
    print(count)
### BUGGY CODE ###

def gcd(a,b):
	if b==0:
		return a
	else:
		return gcd(b,a%b)

def lcm(a,b):
	return a*b//gcd(a,b)

n=int(input())
a=[int(i) for i in input().split()]
tmp=1
for i in a:
	tmp=lcm(i,tmp)
mod=1000000007
ans=0
for i in a:
	ans=(ans+tmp//i)%mod
print(ans)

### FIXED CODE ###

def gcd(a,b):
	if b==0:
		return a
	else:
		return gcd(b,a%b)

def lcm(a,b):
	return a*b//gcd(a,b)

n=int(input())
a=[int(i) for i in input().split()]
tmp=1
for i in a:
	tmp=lcm(i,tmp)
mod=1000000007
print(sum(tmp//i for i in a)%mod)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
MOD = 10 ** 9 + 7
digit = 60

ans = 0
for j in range(digit):
  o = 0
  for i in a:
    if (i >> j) & 1:
      o += 1
  ans += (o*(n-o)*2**j)
  ans %= MOD
print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
MOD = 10 ** 9 + 7
digit = 60
o=[0]*digit
z=[0]*digit

for i in a:
  for j in range(digit):
    if (i >> j) & 1:
      o[j] += 1
    else:
      z[j] += 1
ans = 0    
for j in range(digit):
  ans += (o[j]*z[j]*pow(2,j,MOD))
print(ans % MOD)
### BUGGY CODE ###

from fractions import Fraction
n = int(input())
candidate = [sorted(list(input())) for _ in range(n)]
ans = 0
candidate.sort()

# 有理数型をインポート
# 組合せの総数


def comb(n, k):

    if n < 0 or k < 0:
        pdt = 0

    else:
        pdt = 1
        for s in range(1, k + 1):
            pdt *= Fraction(n - s + 1, s)

    # 戻り値をint型にするために分子を取り出す
    return pdt.numerator


cnt = 1
for i in range(1, n):
    if candidate[i] != candidate[i - 1]:
        ans += comb(cnt, 2)
        cnt = 1
    else:
        cnt += 1
        if i == n - 1:
            ans += comb(cnt, 2)
print(ans)


### FIXED CODE ###

from fractions import Fraction
import math
n = int(input())
candidate = [sorted(list(input())) for _ in range(n)]
ans = 0
candidate.sort()

# 有理数型をインポート
# 組合せの総数


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
# def comb(n, k):

#     if n < 0 or k < 0:
#         pdt = 0

#     else:
#         pdt = 1
#         for s in range(1, k + 1):
#             pdt *= Fraction(n - s + 1, s)

#     # 戻り値をint型にするために分子を取り出す
#     return pdt.numerator


cnt = []
num = 1
for i in range(1, n):
    if candidate[i] == candidate[i - 1]:
        num += 1
        if i == n - 1 and num > 1:
            cnt.append(num)
    else:
        if num > 1:
            cnt.append(num)
        num = 1
for i in cnt:
    if i > 1:
        ans += combinations_count(i, 2)
print(ans)
### BUGGY CODE ###

k = int(input())

if (k % 2 == 0 or k % 5 == 0):
    print('-1')
    exit()

sept = 7

count = 1
for i in range(1000000):
    if sept % k == 0:
        print(count)
        exit()

    else:
        sept = int(str(sept) + '7')
        count += 1

else:
    print('-1')


### FIXED CODE ###

k = int(input())

if ((k % 2 == 0) or (k % 5 == 0)):
    print(-1)

else:
    if k % 7 == 0:
        d = (9 * k) // 7
    else:
        d = 9 * k
    
    res = 10

    for i in range(1, 100000000):
        if res % d == 1:
            print(i)
            exit()
        else:
            res = (res % d) * 10
    print(-1)
### BUGGY CODE ###

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

ans=0
MOD=10**9+7

for k in range(60):
    x0=0
    x1=0
    for i in range(n):
        cnt=(a[i]>>k)&1
        if cnt==0:
            x0+=1
        else:
            x1+=1

    tmp=1
    for j in range(k):
        tmp*=2
        tmp%=MOD

    su=(x0*x1)*tmp%MOD
    ans+=su
    ans%=MOD

print(ans)

### FIXED CODE ###

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

ans=0
MOD=10**9+7

for k in range(60):
    x0=0
    x1=0
    for i in range(n):
        cnt=(a[i]>>k)&1
        if cnt==0:
            x0+=1
        else:
            x1+=1

    tmp=1
    for j in range(k):
        tmp*=2
        tmp%=MOD

    su=(x0*x1)*tmp%MOD
    ans+=su
    ans%=MOD

print(ans)
### BUGGY CODE ###

N=int(input())
L=list(map(int,input().split()))
counter=0
for t in range(1,N-1):
  for i in range(0,t):
    for p in range(t+1,N):
      if L[i]<L[t]+L[p] and L[p]<L[i]+L[t] and L[t]<L[i]+L[p]:
        counter+=1
print(counter)

### FIXED CODE ###

import bisect
counter=0
N=int(input())
L=sorted(list(map(int,input().split())))
for a in range(N-2):
  for b in range(a+1,N-1):
    k=bisect.bisect_left(L,L[a]+L[b])
    counter+=k-b-1
print(counter)
### BUGGY CODE ###

import itertools
n = int(input())
a = list(map(int,input().split()))

mod = 1000000007

sum = 0
#普通にとく
for v in itertools.combinations(a, 2):
    sum += v[0]*v[1]


print(sum%mod)

### FIXED CODE ###

import itertools
n = int(input())
a = list(map(int,input().split()))
mod = 1000000007

total = 0
for i in range(len(a)-1):
    if(i == 0):
        before_sum = sum(a) - a[i]
    else:
        before_sum -= a[i]
    total += a[i] * (before_sum) 


print(total%mod)
### BUGGY CODE ###

from collections import deque

h, w = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1
t = [[i for i in range(w)] for j in range(h)]
ans = 0
for i in range(h):
  temp = input()
  for j in range(w):
    t[i][j] = temp[j]

d = deque()
d.append((Ch, Cw))
ans = 0
s = set() # いったところ

while True:
  flag = True
  # 行けるところすべて追加
  while flag:
    flag = False
    for _ in range(len(d)):
      Th, Tw = d.popleft()
      for i,j in [(1,0),(-1,0),(0,1),(0,-1),(0,0)]:
        _h = Th + i
        _w = Tw + j
        _p = (_h, _w)
        if (not (_p) in d) and _h >= 0 and _h < h and _w >= 0 and _w < w and t[_h][_w] == '.':
          d.append(_p)
          if not(i == 0 and j == 0):
            flag = True
  # クリアしてるか確認
  temps = set(d)
  if (Dh,Dw) in temps:
    print(ans)
    exit()
  else:
    s |= temps
    d = deque()
    for Th,Tw in temps:
      for i in [-2,-1,0,1,2]:
        for j in [-2,-1,0,1,2]:
          _h = Th + i
          _w = Tw + j
          _p = (_h, _w)
          if (not (_p) in s) and (not (_p) in d) and _h >= 0 and _h < h and _w >= 0 and _w < w and t[_h][_w] == '.':
            d.append(_p)
    if len(d) == 0:
      print('-1')
      exit()
    ans += 1


### FIXED CODE ###

from collections import deque

h, w = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1
t = [[i for i in range(w)] for j in range(h)]
visited = [[0 for i in range(w)] for j in range(h)] # いったところ
ans = 0
for i in range(h):
  temp = input()
  for j in range(w):
    t[i][j] = temp[j]

d = deque([(Ch, Cw)])
ans = 0
a = [(1,0),(-1,0),(0,1),(0,-1)]

while True:
  # 移動A
  stemp = set()
  while len(d) != 0:
    Th, Tw = d.popleft()
    # s.add((Th, Tw))
    visited[Th][Tw] = 1
    stemp.add((Th, Tw))
    if (Th,Tw) == (Dh, Dw):
      print(ans)
      exit()
    for i, j in a:
      _h,_w = Th + i, Tw + j
      _p = (_h, _w)
      if 0 <= _h < h and 0 <= _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:
        d.append(_p)
        visited[_h][_w] = 1
  # 移動B
  ans += 1
  for Th,Tw in stemp:
    for i in range(-2, 3) :
      for j in range(-2, 3) :
        _h,_w = Th + i, Tw + j
        _p = (_h, _w)
        if 0 <= _h < h and 0 <=  _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:
          d.append(_p)
  if len(d) == 0:
    print('-1')
    exit()
### BUGGY CODE ###

S = input()
Q = int(input())
Query = [[] for _ in range(Q)]
for i in range(Q):
    Query[i] = list(map(str,input().split()))

for q in Query:
    if q[0]=='1':
        S = S[::-1]
    else:#q[0]=='2'
        if q[1]=='1':
            S = q[2] + S
        else:#q[1]=='2'
            S = S + q[2]
print(S)

### FIXED CODE ###

from collections import deque
S = deque(input())
Q = int(input())
Query = [[] for _ in range(Q)]
for i in range(Q):
    Query[i] = list(map(str,input().split()))

fl = False
for q in Query:
    if q[0]=='1':
        fl = not fl
    else:#q[0]=='2'
        if (q[1]=='1') ^ fl:
            S.appendleft(q[2])
        else:#q[1]=='2'
            S.append(q[2])
T = ''.join(S)
if fl:
    T = T[::-1]
print(T)
### BUGGY CODE ###

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

number_dict = {}
for h in range(H):
    for w in range(W):
        number_dict[A[h][w]] = (w+1, h+1)
        
       
for i in range(Q):
    tmp = 0
    now = LR[i][0]
    
    while True:
        if now == LR[i][1]:
            break
        
        tmp += abs(number_dict[now+D][0] - number_dict[now][0]) + abs(number_dict[now+D][1] - number_dict[now][1])
        now = now + D
        

            
    print(tmp)
        



### FIXED CODE ###

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

number_dict = {}
for h in range(H):
    for w in range(W):
        number_dict[A[h][w]] = (w+1, h+1)
        
        
number = [0 for _ in range(H*W+1)]
for i in range(H*W):
    now = H * W - i
    
    if now + D > H * W:
        pass
    
    else:
        number[now] = number[now + D] + abs(number_dict[now+D][0] - number_dict[now][0]) + abs(number_dict[now+D][1] - number_dict[now][1])
       

for i in range(Q):
    res = number[LR[i][0]] - number[LR[i][1]]
            
    print(res)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
A.sort()
L=[]
for i in range(N):
    if len(A)>=3:
        a=A.pop(0)
        b=A.pop(-1)
        if (b-a)-A[0] >= A[-1]-(a-b):
            A.append(b-a)
            L.append([b,a])
        else:
            A.insert(0,a-b)
            L.append([a,b])
    elif len(A)==2:
        a = A.pop(0)
        b = A.pop(-1)
        A.append(b-a)
        L.append([b,a])
        break
    else:
        break
print(A[0])
for j in L:
    print(j[0],j[1],sep=' ')


### FIXED CODE ###

from collections import deque
N=int(input())
A=list(map(int,input().split()))
A.sort()
A=deque(A)
L=deque([])
for i in range(N):
    if len(A)>=3:
        a=A.popleft()
        b=A.pop()
        if (b-a)-A[0] >= A[-1]-(a-b):
            A.append(b-a)
            L.append([b,a])
        else:
            A.appendleft(a-b)
            L.append([a,b])
    elif len(A)==2:
        a = A.popleft()
        b = A.pop()
        A.append(b-a)
        L.append([b,a])
        break
    else:
        break
print(A[0])
for j in L:
    print(j[0],j[1],sep=' ')
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = 0
for n in range(1,N):
    for k in range(n,N):
        ans += A[n-1] * A[k]
print(ans % 1000000007)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
MOD = 1000000007
SUM = sum(A) % MOD
ans = 0

for n in A:
    SUM -= n
    SUM %= MOD
    ans += SUM * n
    ans %= MOD

print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
 
n = int(input())
a = list(map(int,input().split()))
l = [0]*n
 
for i in range(1,n+1):
  z = a.index(i)
  l[i-1] = z+1
  
print(*l)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
List = [0]*N
 
 
for i, a in enumerate(A):
  List[int(a)-1] = str(i+1)
 
  
print(" ".join(List))
### BUGGY CODE ###

inputNumN = list(map(int, input().split()))
N = inputNumN[0]
Q = inputNumN[1]

AB = {}
for i in range(N - 1):
    inputNumAB = list(map(int, input().split()))
    if not AB.get(inputNumAB[0]):
        AB[inputNumAB[0]] = []
    AB[inputNumAB[0]].append(inputNumAB[1])

PQ = {}
for i in range(Q):
    inputNumPQ = list(map(int, input().split()))
    if not PQ.get(inputNumPQ[0]):
        PQ[inputNumPQ[0]] = 0
    PQ[inputNumPQ[0]] += inputNumPQ[1]
PQ = sorted(PQ.items())

result = [0] * N

for pq in PQ:
    result[pq[0] - 1] += pq[1]

for A, B in AB.items():
    for b in B:
        result[b - 1] += result[A - 1]

for r in result:
    print(r, end=' ')


### FIXED CODE ###

inputNumN = list(map(int, input().split()))
N = inputNumN[0]
Q = inputNumN[1]

AB = {}
for i in range(N - 1):
    inputNumAB = list(map(int, input().split()))
    if not AB.get(inputNumAB[0]):
        AB[inputNumAB[0]] = []
    AB[inputNumAB[0]].append(inputNumAB[1])

result = [0] * N
for i in range(Q):
    inputNumPQ = list(map(int, input().split()))
    result[inputNumPQ[0] - 1] += inputNumPQ[1]

for A, B in AB.items():
    for b in B:
        result[b - 1] += result[A - 1]

for r in result:
    print(r, end=' ')
### BUGGY CODE ###

n,k = map(int,input().split())
l = []
for i in range(n):
    a,b = map(int,input().split())
    l1 = [a]*b
    l = l + l1
l.sort()
print(l[k-1])

### FIXED CODE ###

n,k = map(int,input().split())
l = []
for i in range(n):
    a = list(map(int,input().split()))
    l.append(a)
l.sort(key=lambda x:x[0])
count = 0
i = 0
while count < k:
    count = count + l[i][1]
    i = i + 1
print(l[i-1][0])
### BUGGY CODE ###

def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 1
    for i in range(N):
        ans *= A[i]
    if ans > 10**18:
        ans = -1
    print(ans)

if __name__ == "__main__":
    solve()

### FIXED CODE ###

def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 1
    for i in range(N):
        if ans == -1 and A[i] != 0:
            continue
        ans *= A[i]
        if ans > 10**18:
            ans = -1
    print(ans)

if __name__ == "__main__":
    solve()
### BUGGY CODE ###

n=int(input())
a=input()
q=int(input())
BC=[[] for _ in range(q)]
for i in range(q):
    b,c=input().split()
    BC[i].append(b)
    BC[i].append(c)
for i in range(q):
    if BC[i][0] in a:
        
        a=a.replace(BC[i][0],BC[i][1])
    e=list(map(int,a.split()))
    print(sum(e))

### FIXED CODE ###

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

LIST=""
LIST=list(LIST)
c=0

def func(S):
    global LIST
    global c

    if(len(S)==0):
        c=1
    if(c==1):
        for i in range(len(LIST)-1,-1,-1):
            print(LIST[i])
        exit()

    So=S[::]
    for i in range(len(S)):
        S=So[::]
        if(S[i]==(i+1)):
            LIST.append(S.pop(i))
            func(S)
    if(len(LIST)==0):
        print("-1")
        exit()
    LIST.pop(-1)

N=int(input())
S=list(map(int,input().split()))

for i in range(N):
    if((S[i] - i)>1):
        print("-1")
        exit()  

func(S)
print("-1")

### FIXED CODE ###

N=int(input())
S=list(map(int,input().split()))
LIST=list("")

for i in range(N):
    if((S[i] - i)>1):
        print("-1")
        exit()  

while(len(S)>0):
    c=10**10
    for i in range (len(S)):
        if(S[i]==i+1):
            c=i
    if(c==(10**10)):
        print("-1")
        exit()  
    LIST.append(S.pop(c))

for i in range(len(LIST)-1,-1,-1):
    print(LIST[i])
### BUGGY CODE ###

import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import Counter
def rem2019(d):
    return d%2019
def resolve():
    S = [s for s in sr()]
    N = len(S)
    a = ['']
    for i in range(N):
        s = S.pop()
        a.append(int(s+str(a[i])))
    a[0] = 0
    a = list(map(rem2019, a))
    A = Counter(a)
    ans = 0
    for n in A.values():
        ans += n*(n-1)//2
    print(ans)
resolve()

### FIXED CODE ###

import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    S = sr()
    N = len(S)
    a = [0]*2019
    a[0] += 1
    t = 0
    r = 1
    for s in reversed(S):
        t += int(s)*r
        t %= 2019
        r *= 10
        r %= 2019
        a[t] += 1
    ans = 0
    for i in range(2019):
        ans += a[i]*(a[i]-1)//2
    print(ans)
resolve()
### BUGGY CODE ###

R,G,B,N= map(int,input().split())

x_max= N//R
y_max= N//G
ans=0

for x in range(x_max+1):
    for y in range(y_max+1):

        z= (N-R*x-G*y)//B

        if z >=0 and (N-R*x-G*y)% B ==0:
            ans += 1

print(ans)

### FIXED CODE ###

R,G,B,N= map(int,input().split())

x_max= N//R
y_max= N//G
ans=0

for x in range(x_max+1):
    for y in range(y_max+1):

        z= (N-R*x-G*y)//B

        if z >=0 and (N-R*x-G*y)% B ==0:
            ans += 1

print(ans)
### BUGGY CODE ###

N = int(input())
A_List = list(map(int,input().split()))

for i in range(N):
    for j in range(i+1,N):
        if A_List[i]==A_List[j]:
            print("NO")
            exit()

print("YES")

### FIXED CODE ###

N = int(input())
A_List = list(map(int,input().split()))

print("YES" if len(A_List)==len(set(A_List)) else "NO")
### BUGGY CODE ###

n, q = map(int, input().split())
s = input()
len_s = len(s)
ans = []
for i in range(q):
    l, r = map(int, input().split())
    tmp_len = len(s[l-1:r].replace("AC", ""))
    res = ((r-l+1) - tmp_len) / 2
    ans.append(int(res))
for num in ans:
    print(num)


### FIXED CODE ###

n, q = map(int, input().split())
s = input()
ans = [0] * (n + 1)
for i in range(n):
    ans[i + 1] = ans[i] + (1 if s[i:i+2] == "AC" else 0)
for j in range(q):
    l,r = map(int, input().split())
    res = ans[r-1] - ans[l-1]
    print(res)