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

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

result = 1
for a in A:
    result *= a
if result > 10 ** 18:
    result = '-1'
print(result)


### FIXED CODE ###

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

result = 1
if 0 in A:
    print("0")
else:
    for a in A:
        result *= a
        if result > 10**18:
            result = -1
            break
    print(result)
### BUGGY CODE ###

def resolve():
    s = input()
    q = int(input())
    for _ in range(q):
        inp = input()
        if inp == "1":
            s = s[::-1]
        else:
            z = inp.split()
            b = int(z[1])
            c = z[2]
            if b == 1:
                s = "".join([c, s])
            else:
                s = "".join([s, c])
    print(s)
resolve()

### FIXED CODE ###

def resolve():
    from collections import deque
    s = deque(input())
    q = int(input())
    qlist = [input().split() for i in range(q)]
    flip = False
    for j in range(q):
        if qlist[j][0] == "1":
            flip = not flip
        elif qlist[j][1] == "1":
            if flip:
                s.append(qlist[j][2])
            else:
                s.appendleft(qlist[j][2])
        elif qlist[j][1] == "2":
            if flip:
                s.appendleft(qlist[j][2])
            else:
                s.append(qlist[j][2])
    if not flip:
        print("".join(s))
    else:
        s.reverse()
        print("".join(s))
resolve()
### BUGGY CODE ###

import os, sys, re, math

(N, Q) = [int(n) for n in input().split()]
S = input()

oc = []
for i in range(len(S) - 1):
    if S[i:i+2] == 'AC':
        oc.append(i)

for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    print(len(list(filter(lambda x:x >= l - 1 and x < r - 1, oc))))


### FIXED CODE ###

import os, sys, re, math

(N, Q) = [int(n) for n in input().split()]
S = input()

oc = [0]
n = 0
for i in range(len(S) - 1):
    if S[i:i+2] == 'AC':
        n += 1
    oc.append(n)
oc.append(n)

for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    print(oc[r - 1] - oc[l - 1])
### BUGGY CODE ###

def get_pow():
    cache = {}

    def func(x):
        if x not in cache:
            cache[x] = pow(2, x, mod)
        return cache[x]

    return func


mod = 1000000007
n = int(input())
parents = list(map(int, input().split()))
children = [set() for _ in range(n + 1)]
for c, p in enumerate(parents):
    children[p].add(c + 1)

levels = [{0}]
while True:
    level = set()
    for p in levels[-1]:
        level.update(children[p])
    if not level:
        break
    levels.append(level)
levels.reverse()

level_node_count = []
balls = [None] * (n + 1)

for i, level in enumerate(levels):
    level_node_count.append(len(level))
    for node in level:
        balls[node] = [[1, 1, 0]]
        if children[node]:
            balls_from_children = [balls[c] for c in children[node]]
            balls_from_children.sort(key=len)
            for bs1, bs2 in zip(balls_from_children, balls_from_children[1:]):
                for (b10, b11, b12), b2 in zip(bs1, bs2):
                    b2[2] = ((b11 + b12) * b2[1] + b12 * b2[0]) % mod
                    b2[1] = (b10 * b2[1] + b11 * b2[0]) % mod
                    b2[0] *= b10
            for b in balls_from_children[-1]:
                b[0] += b[2]
                b[2] = 0
            balls[node].extend(balls_from_children[-1])

level_node_count.reverse()

pow2 = get_pow()
print(sum(b[1] * pow2(n - l + 1) % mod for l, b in zip(level_node_count, balls[0])) % mod)


### FIXED CODE ###

from collections import deque


def get_pow():
    cache = {}

    def func(x):
        if x not in cache:
            cache[x] = pow(2, x, mod)
        return cache[x]

    return func


mod = 1000000007
n = int(input())
parents = list(map(int, input().split()))
children = [set() for _ in range(n + 1)]
for c, p in enumerate(parents):
    children[p].add(c + 1)

levels = [{0}]
while True:
    level = set()
    for p in levels[-1]:
        level.update(children[p])
    if not level:
        break
    levels.append(level)
levels.reverse()

level_node_count = []
balls = [None] * (n + 1)

for i, level in enumerate(levels):
    level_node_count.append(len(level))
    for node in level:
        cn = children[node]
        if cn:
            if len(cn) == 1:
                bs = balls[cn.pop()]
                bs.appendleft([1, 1, 0])
                balls[node] = bs
                continue
            balls_from_children = [balls[c] for c in children[node]]
            balls_from_children.sort(key=len)
            bs1 = balls_from_children[0]
            for bs2 in balls_from_children[1:]:
                for (b10, b11, b12), b2 in zip(bs1, bs2):
                    b2[2] = ((b11 + b12) * b2[1] + b12 * b2[0]) % mod
                    b2[1] = (b10 * b2[1] + b11 * b2[0]) % mod
                    b2[0] = b2[0] * b10 % mod
                bs1 = bs2
            for b in bs1:
                b[0] = (b[0] + b[2]) % mod
                b[2] = 0
            bs1.appendleft([1, 1, 0])
            balls[node] = bs1
        else:
            balls[node] = deque([[1, 1, 0]])

level_node_count.reverse()

pow2 = get_pow()
print(sum(b[1] * pow2(n - l + 1) % mod for l, b in zip(level_node_count, balls[0])) % mod)
### BUGGY CODE ###

n = int(input())

string_list = [''.join(sorted(input())) for i in range(n)]

counter = 0

for i in range(n):
  str_i = string_list[i]
  for j in range(i + 1, n):
    str_j = string_list[j]
    if str_i == str_j:
      counter += 1

print(counter)

### FIXED CODE ###

n = int(input())

string_list = [''.join(sorted(input())) for i in range(n)]

word_dict = dict()

counter = 0

for i in range(n):
  word_dict.setdefault(string_list[i], 0)
  word_dict[string_list[i]] += 1
  if word_dict[string_list[i]] != 1:
    counter += word_dict[string_list[i]] - 1
  
print(counter)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
idx = 0
count = 0
for i in range(n):
  if idx == n:
    break
  for j in range(n, 0, -1):
    if a[idx:j] == sorted(a[idx:j]) or a[idx:j] == sorted(a[idx:j], reverse=True):
      idx += len(a[idx:j])
      count += 1
      break
print(count)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
# 0: 1:増加 2:減少
state = 0
count = 1
for i in range(1, n):
  if state == 0:
    if a[i-1] < a[i]:
      state = 1
    elif a[i-1] > a[i]:
      state = 2
  elif state == 1:
    if a[i-1] > a[i]:
      state = 0
      count += 1
  else:
    if a[i-1] < a[i]:
      state = 0
      count += 1
print(count)
### BUGGY CODE ###

N,X,M=map(int, input().split())
A=[X]
for i in range(1,M+1):
  d=pow(A[i-1],2,M)
  if d in A:
    break
  A.append(d)
c=A.index(d)
e=len(A)
cy=e-c
ans=0
if N<=e:
  for i in range(N):
    ans+=A[i]
  print(ans)
else:
  m=sum(A[:c])
  n=sum(A[c:e])
  #print(m,n)
  f=(N-c)//cy
  ans=m+n*f+sum(A[c:c+((N-c)%cy)])
  print(ans)

### FIXED CODE ###

N,X,M=map(int, input().split())
A=[X]
for i in range(1,M+1):
  d=pow(A[i-1],2,M)
  if d in A:
    break
  A.append(d)
c=A.index(d)
e=len(A)
cy=e-c
ans=0
if N<=e:
  for i in range(N):
    ans+=A[i]
  print(ans)
else:
  m=sum(A[:c])
  n=sum(A[c:e])
  #print(m,n)
  f=(N-c)//cy
  ans=m+n*f+sum(A[c:c+((N-c)%cy)])
  print(ans)
### BUGGY CODE ###

#E
from fractions import gcd
N=int(input())

mod=1000000007
ans=1
P=dict()
M=dict()
zero=0
zero1=0
zero2=0
for i in range(N):
    a,b=map(int,input().split())
    if a==0 and b==0:
        zero+=1
    elif a==0 and b!=0:
        zero1+=1
    elif a!=0 and b==0:
        zero2+=1
    elif (a>0 and b<0) or (a<0 and b>0):
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        s=str(a)+"/"+str(b)
        P[s]=P.get(s,0)+1
        
    else:
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        s=str(b)+"/"+str(a)
        M[s]=M.get(s,0)+1

P["0/0"]=zero1
M["0/0"]=zero2
for v,cnt_p in P.items():
    if v in M:
        cnt_m=M[v]
        ans*=(2**cnt_p+2**cnt_m-1)
        M.pop(v)
    else:
        ans*=2**cnt_p
    ans%=mod
    
for v,cnt_m in M.items():
    ans*=2**cnt_m
    ans%=mod

ans-=1
ans+=zero
ans%=mod
print(ans)

### FIXED CODE ###

#E
from math import gcd
N=int(input())

mod=1000000007
ans=1
P=dict()
M=dict()
zero=0
zero1=0
zero2=0
for i in range(N):
    a,b=map(int,input().split())
    if a==0 and b==0:
        zero+=1
    elif a==0 and b!=0:
        zero1+=1
    elif a!=0 and b==0:
        zero2+=1
    elif (a>0 and b<0) or (a<0 and b>0):
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        s=str(a)+"/"+str(b)
        P[s]=P.get(s,0)+1
        
    else:
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        s=str(b)+"/"+str(a)
        M[s]=M.get(s,0)+1

P["0/0"]=zero1
M["0/0"]=zero2
for v,cnt_p in P.items():
    if v in M:
        cnt_m=M[v]
        ans*=(2**cnt_p+2**cnt_m-1)
        M.pop(v)
    else:
        ans*=2**cnt_p
    ans%=mod
    
for v,cnt_m in M.items():
    ans*=2**cnt_m
    ans%=mod

ans-=1
ans+=zero
ans%=mod
print(ans)
### BUGGY CODE ###

import collections
N = int(input())
S = [input() for _ in range(N)]
c = collections.Counter(S)
n = len(c)
max = c.most_common()[0][1]
ans = [c.most_common()[i][0] for i in range(n) if c.most_common()[i][1] == max]
ans.sort()
m = len(ans)
for i in range(m):
  print(ans[i])

### FIXED CODE ###

from collections import defaultdict
N = int(input())
S = [input() for _ in range(N)]
dic = defaultdict(int)
for i in range(N):
        dic[S[i]] += 1
dic = dict(dic)
dic = sorted(dic.items(),key = lambda x:-x[1])
n = len(dic)
max = dic[0][1]
d = 0
for i in range(n):
  if dic[i][1] == max:
    d += 1
  else:
    d = i
    break
dic = sorted(dic[0:d],key = lambda x:x[0])
for i in range(len(dic)):
  print(dic[i][0])
### BUGGY CODE ###

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

ans = 0
for i in range(k - 1, len(p)):
    ans = max(ans, sum(p[i - k + 1:i + 1]))

print((ans + k) / 2)


### FIXED CODE ###

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

if n != k:
    memo = [0] * (n + 1)

    for i in range(n):
        memo[i] = memo[i - 1] + p[i]

    ans = 0
    for i in range(k, n):
        ans = max(ans, memo[i] - memo[i - k])

    ans = (ans + k) / 2
    print(ans)
else:
    ans = (sum(p) + k) / 2
    print(ans)
### BUGGY CODE ###

n,m=map(int,input().split())
a=list(map(int,input().split()))
a[0]=a[0]%m
for i in range(n-1):
    a[i+1]=(a[i]+a[i+1])%m
result=0
for i in range(n):
    if a[i]==0:
        result+=1
    for j in range(i+1,n):
        if a[i]==a[j]:
            result+=1
print(result)

### FIXED CODE ###

n,m=map(int,input().split())
a=list(map(int,input().split()))
a[0]=a[0]%m
for i in range(n-1):
    a[i+1]=(a[i]+a[i+1])%m
result=0
for i in range(n):
    if a[i]==0:
        result+=1

a.sort()
num=a[0]
b=0
for i in range(n):
    if a[i]==num:
        b+=1
    else:
        result+=b*(b-1)/2
        b=1
        num=a[i]
print(int(result+b*(b-1)/2))
### BUGGY CODE ###

from itertools import product

import numpy as np

N, A, B, C = (int(i) for i in input().split())
l = []

for i in range(N):
    l.append(int(input()))

l = np.array(l)

###########

'''
Aの竹の材料にする、Bの竹の材料にする、Cの竹の材料にする、使わない
'''

min_cost = 1e10

for i in product(np.arange(4), repeat=N):
    i = np.array(i)

    if(np.sum(np.unique(i)) != 6):
        continue

    for_a = l[i == 1]
    for_b = l[i == 2]
    for_c = l[i == 3]

    A_diff = np.abs(np.sum(for_a) - A) + (len(for_a) - 1) * 10
    B_diff = np.abs(np.sum(for_b) - B) + (len(for_b) - 1) * 10
    C_diff = np.abs(np.sum(for_c) - C) + (len(for_c) - 1) * 10

    cost = A_diff + B_diff + C_diff

    if(cost < min_cost):
        min_cost = cost

print(min_cost)


### FIXED CODE ###

N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9


def dfs(cur, a, b, c):
    if cur == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10

    return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))
### BUGGY CODE ###

A, B, N = map(int, input().split())

if B > N:
    ans = int(A*N/B)
    print(ans)
else:
    ans = 0    
    x = B - 1
    while True:
        temp = int(A*x/B) - A*int(x/B)
        if ans > temp:
            break
        ans = temp
        x += B
        if x > N:
            break
    print(ans)

### FIXED CODE ###

A, B, N = map(int, input().split())

if B > N:
    print(int((A*N)/B)-A*int(N/B))
else:
    print(int((A*(B-1))/B)-A*int((B-1)/B))

# if B > N:
#     ans = floor(A*N/B)
#     print(ans)
# else:
#     ans = floor(A*N/B) - A*floor(N/B)
#     ans_2nd = 0
#     x = (N//B)*B - 1
#     temp = floor((A*x)/B) - A*floor(x/B)
#     # while ans <= temp and x >= 0:
#     #     ans = temp
#     #     temp = floor(A*x/B) - A*floor(x/B)
#     #     x -= B
#     # while ans_2nd <= temp and x >= 0:
#     #     ans_2nd = temp
#     #     temp = floor(A*x/B) - A*floor(x/B)
#     #     x -= B
#     # prfloor(max(ans, ans_2nd))
#     print(max(temp, ans))
### BUGGY CODE ###

import numpy as np

h, w = map(int, input().split())
S = list(list(input()) for _ in range(h))
S = np.array(S)
data = np.zeros_like(S, dtype=int)
data[S=='.'] = 1

def get_dp(d, width):
  dp = d.copy()
  for i, row in enumerate(d):
    for j in range(1, width):
      val = row[j]
      if val > 0:
        n = dp[i][j-1]
        dp[i][j] += n
        dp[i, j-n:j] += 1
  return dp

dp_row = get_dp(data, w)
dp_col = get_dp(data.T, h)
print((dp_row+dp_col.T).max() - 1)

### FIXED CODE ###

import numpy as np

h, w = map(int, input().split())
S = list(list(input()) for _ in range(h))
S = np.array(S)
S_int = np.zeros_like(S, dtype=int)
S_int[S=='.'] = 1

def getLR(d, width, height):
  L = np.zeros_like(d)
  R = L.copy()
  L[:][0] = 0
  R[:][-1] = 0
  for i in range(1, width):
    ridx = width - 1 - i
    L[:,i] = d[:,i] * (L[:,i-1] + d[:,i-1])
    R[:,ridx] = d[:,ridx] * (R[:,ridx+1] + d[:,ridx+1])
  return L, R

L, R = getLR(S_int, w, h)
U, D = getLR(S_int.T, h, w)
print((L+R+U.T+D.T).max() + 1)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N, W = map(int, input().split())
vmax = 1000*100+1
dp = [100*10**9] * vmax
dp[0] = 0

for i in range(N):
    wi, vi = map(int, input().split())
    ndp = [100*10**9] * vmax

    for j in range(vmax):
        if j-vi >= 0:
            ndp[j] = min(dp[j], dp[j-vi] + wi)
        else:
            ndp[j] = dp[j]
    dp = ndp

v = 0
for i, w in enumerate(dp):    
    if w == 100*10**9:
        continue
    if w <= W:
        v = i

print(v)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N, W = map(int, input().split())
vmax = 1000*100+1
wmax = 100*10**9
dp = [wmax] * vmax
dp[0] = 0
 
for i in range(N):
    wi, vi = map(int, input().split())
    ndp = dp[:]
    for j in range(vi, vmax):
        ndp[j] = min(dp[j], dp[j-vi] + wi)
    dp = ndp

v = 0
for i, w in enumerate(dp):    
    if w == wmax:
        continue
    if w <= W:
        v = i
print(v)
### BUGGY CODE ###

A,B,C,K = map(int,input().split())
ls = []
ans = 0
for i in range(A):
    ls.append(1)
for i in range(B):
    ls.append(0)
for i in range(C):
    ls.append(-1)
for i in range(K):
    ans += ls[i]
print(ans)


### FIXED CODE ###

A,B,C,K = map(int,input().split())
ans = 0
if A >= K:
    ans = K
elif A < K and A+B >= K:
    ans = A
else:
    ans = A - (K-A-B)*(1)
print(ans)
### BUGGY CODE ###

#ABC142D
import math
import fractions
A,B = map(int,input().split())

g = fractions.gcd(A,B)

cnt = 0
first = True
for i in range(2,g+1):
    while g % i == 0:
        g //= i
        #print(g)
        if first:
            cnt += 1
            first = False

    first = True

print(cnt+1)




### FIXED CODE ###

#ABC142D
import math
import fractions
A,B = map(int,input().split())

g = fractions.gcd(A,B)

cnt = 0
first = True

for i in range(2,int(math.sqrt(g))+1):
    while g%i == 0:
        g //= i
        if first:
            cnt += 1
            first = False

    first = True

if g != 1:
    cnt += 1

print(cnt+1)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
s = 1
for a in A:
    s = s * a   
if s > 10**18 :
    print('-1')
else:
    print(s)
        

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
s = 1
over = False
zero = False

for i in range(N):
    if A[i] == 0:
        zero = True
        break

if zero:
    print(0)
else:
    for i in range(N):
        s *= A[i]
        if s > 10**18:
            over = True
            break
    if over :
        print('-1')
    else:
        print(s)
### BUGGY CODE ###

n = input()
a = list(map(int, input().split()))
cnt = 0
for i, j in enumerate(a):
    for j2 in a[i:]:
        if j > j2:
            cnt += 1
print(cnt)

### FIXED CODE ###

def merge(a, left, mid, right):
    global cnt
    l = a[left:mid] + [sentinel]
    r = a[mid:right] + [sentinel]
    i = j = 0
    for k in range(left, right):
        if l[i] <= r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1
            cnt += mid - left - i


def merge_sort(a, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        merge_sort(a, left, mid)
        merge_sort(a, mid, right)
        merge(a, left, mid, right)

n = input()
a = list(map(int, input().split()))
sentinel = 10 ** 9 + 1
cnt = 0
merge_sort(a, 0, len(a))
print(cnt)
### BUGGY CODE ###

def f2(cnt, n, ci):
    if n < ci:
        return 0
    elif n == ci and cnt > 1:
        return 0
    elif n == ci and cnt == 1:
        return 1
    else:
        v = f2(cnt - 1, n - ci, ci + 1)
        v += f2(cnt, n, ci + 1)
        return v


while 1:
    n, s = map(int, input().split())
    if n == 0 and s == 0:
        break
    print(f2(n, s, 0))

### FIXED CODE ###

def f2(cnt, n, ci):
    if ci >= 10 or n > 45:
        return 0
    elif n < ci:
        return 0
    elif n == ci and cnt > 1:
        return 0
    elif n == ci and cnt == 1:
        return 1
    else:
        v = f2(cnt - 1, n - ci, ci + 1)
        v += f2(cnt, n, ci + 1)
        return v


while 1:
    n, s = map(int, input().split())
    if n == 0 and s == 0:
        break
    print(f2(n, s, 0))
### BUGGY CODE ###

N = int(input())
S = [input() for x in range(N)]

S_L = []

for i in S:
    if i not in S_L:
        S_L.append(i)
# print(S_L)

S_N = [0] * len(S_L)

for j in S:
    ind = S_L.index(j)
    S_N[ind] += 1
# print(S_N)

S_max = [] # 最大の数を持つ文字列を入れるリスト
S_N_max = max(S_N) # 最大の数を追加

for k in range(len(S_N)):
    if S_N[k] == S_N_max:
        S_max.append(S_L[k])

ans_L = sorted(S_max)
ans = ''
cnt = 0
while cnt < len(ans_L):
    ans += ans_L[cnt]
    cnt += 1
    if cnt != len(ans_L):
        ans += ' '
print(ans)

### FIXED CODE ###

from collections import Counter
N = int(input())
S = [input() for _ in range(N)]

S_cnt = Counter(S)
max_cnt = max(S_cnt.values())
# print(max_cnt)

names = [name for name, v in S_cnt.items() if v == max_cnt]
names.sort()

print(*names, sep='\n')
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [1] * (N-K+1)
for i in range(N-K+1):
    for j in A[i:K+i]:
        B[i] *= j
for i in range(N-K):
    if B[i+1] > B[i]:
        print('Yes')
    else:
        print('No')


### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N-K):
    if A[i+K] > A[i]:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

n=int(input())
a=[int(i) for i in  input().split()]
sigma=0
for i in a:
    sigma+=i
sigma/=2
sigma=int(sigma)

x=[sigma for i in range(n)]


for i in range(n):
    g=i+1
    g=g%n
    for j in range(int((n-1)/2)):
        x[i]-=a[g]
        g+=2
        g=g%n
        
        
x2=[str(i*2) for i in x]
print(" ".join(x2))

### FIXED CODE ###

n=int(input())
a=[int(i) for i in  input().split()]
sigma=0
for i in a:
    sigma+=i
sigma/=2
sigma=int(sigma)

x=[sigma for i in range(n)]
g=1

for i in range(int((n-1)/2)):
    sigma-=a[g]
    g+=2
    
x[0]=sigma
for i in range(1,n):
    x[i]=a[i-1]-x[i-1]

        
        
x2=[str(i*2) for i in x]
print(" ".join(x2))
### BUGGY CODE ###

n = int(input())
seq = list(map(int, input().split()))
res = 0
i = 0
j = 1
for i in range(n - 1):
  for j in range(i + 1, n):
    res += (seq[i] * seq[j]) % (10 ** 9 + 7)
print(res % (10 ** 9 + 7))

### FIXED CODE ###

n = int(input())
seq = list(map(int, input().split()))
a = 10 ** 9 + 7
s = sum(seq)
res = 0
for i in range(n - 1):
  s -= seq[i]
  res += (seq[i] * s) % a
print(res % a)
### BUGGY CODE ###

import sys
import numpy as np
from collections import deque

h, w = map(int, input().split())
maze = [list(input()) for _ in range(h)]
direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]

max_step = 0
for i in range(h):
    for j in range(w):
        largest_step = 0
#        print("start: ", i, j)
        if maze[i][j] == "#":
            continue
        visited = [[False] * w for _ in range(h)]
        distance = [[-1] * w for _ in range(h)]
        plan = deque()
        step = 0
        plan.appendleft((i, j, step))
        distance[i][j] = step
        while True:
            if len(plan) == 0:
                break
            y, x, step = plan.pop()
#            print(" ", x, y, step)
            visited[y][x] = True
            distance[i][j] = step
            # if step > largest_step:
            #     largest_step = step
            for next_direction in direction:
                next_y = y + next_direction[0]
                next_x = x + next_direction[1]
                if -1 == next_x or next_x == w or -1 == next_y or next_y == h or maze[next_y][next_x] == "#" or visited[next_y][next_x]:
                    continue
                plan.appendleft((next_y, next_x, step + 1))
#        print(largest_step)
        # if largest_step > max_step:
        #     max_step = largest_step
        max_step = max(max_step, np.max(np.array(distance)))

print(max_step)


### FIXED CODE ###

import sys
import numpy as np
from collections import deque

h, w = map(int, input().split())
maze = [list(input()) for _ in range(h)]
direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]

max_step = 0
for i in range(h):
    for j in range(w):
        largest_step = 0
#        print("start: ", i, j)
        if maze[i][j] == "#":
            continue
        visited = [[False] * w for _ in range(h)]
        distance = [[-1] * w for _ in range(h)]
        plan = deque()
        step = 0
        plan.appendleft((i, j, step))
        distance[i][j] = step
        visited[i][j] = True
        while plan:
            y, x, step = plan.pop()
#            print(" ", x, y, step)
            # if step > largest_step:
            #     largest_step = step
            for next_direction in direction:
                next_y = y + next_direction[0]
                next_x = x + next_direction[1]
                if -1 == next_x or next_x == w or -1 == next_y or next_y == h or maze[next_y][next_x] == "#" or visited[next_y][next_x]:
                    continue
                distance[next_y][next_x] = distance[y][x] + 1
                visited[next_y][next_x] = True
                plan.appendleft((next_y, next_x, step + 1))
#        print(largest_step)
        # if largest_step > max_step:
        #     max_step = largest_step
        max_step = max(max_step, np.max(np.array(distance)))

print(max_step)
### BUGGY CODE ###

def calc(b, c):
  _sum = 0
  for i in range(n):
    a[i] = c if a[i] == b else a[i]
    _sum += a[i]
  return _sum

n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  print (calc(b, c))

### FIXED CODE ###

_counts = [0 for i in range(100001)]
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  _counts[a[i]] += 1
_sum = sum(a)
q = int(input())

for i in range(q):
  b, c = map(int, input().split())
  _sum += (c - b) * _counts[b]
  print (_sum)
  _counts[c] += _counts[b]
  _counts[b] = 0