Harryxun/stf_run huggingface dataset:
from sys import stdin

h,w,a,b = [int(x) for x in stdin.readline().rstrip().split()]

li = [[0 for _ in range(w)]for _ in range(h)]

if a > w//2 or b > h//2:
    print("No")
    exit()
elif h%a != 0 or w%b != 0:
    if a != 0 and b != 0:
        print("No")
        exit()
    else:
        if a == 0:
            a = 1
        elif b == 0:
            b = 1
        count = 0
        for i in range(h):
            for s in range(a):
                li[i][s+a*count] = 1
            count += 1
            count = count%(h//a)
        for p in range(h):
            print(''.join(map(str, li[p])))
else:
    count = 0
    for i in range(h):
        for s in range(a):
            li[i][s+a*count] = 1
        count += 1
        count = count%(h//a)
    li2 = [li[q][0] for q in range(h)]
    if li2.count(0) == b or li2.count(1) == b:
        for p in range(h):
            print(''.join(map(str, li[p])))
    else:
        print("No")

### FIXED CODE ###

from sys import stdin
h,w,b,a = map(int,stdin.readline().rstrip().split())
li = []
for i in range(a):
    s = "0"*b+"1"*(w-b)
    li.append(s)
for j in range(h-a):
    s = "1"*b+"0"*(w-b)
    li.append(s)
for i in li:
    print(i)
N = int(input())
 
search_max = int(sqrt(N))
 
min_number = 10**12
for x in range(1, search_max + 1):
    if N % x == 0:
        y = N // x
        if x + y < min_number:
            min_number = x + y
 print(min_number-2)

### FIXED CODE ###

N = int(input())
search_max = int(N**0.5)
 
min_number = 10**12
for x in range(1, search_max + 1):
    if N % x == 0:
        y = N // x
        if x + y < min_number:
         
            min_number = x + y
print(min_number-2)
# C - Monsters Battle Royale
# https://atcoder.jp/contests/abc118/tasks/abc118_c

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

while len(A) != 1:

    # minimum number of A
    minA = min(A)

    # next round A
    nA = set(), nA.add(minA)

    for Ai in A:
        if Ai % minA != 0 and Ai != minA:
            nA.add(Ai%minA)

    # update for next round
    A = nextA

print '{}'.format( list(A)[-1])


### FIXED CODE ###

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

while len(A) != 1:
    minA = min(A)

    nextA = set()
    for Ai in A:
        if Ai == minA:
            nextA.add(Ai)
            continue
        if Ai % minA != 0:
            nextA.add(Ai%minA)

    A = nextA

A = list(A)
print '{}'.format(A[0])
n =input()
if "7" in n:
  prnt("Yes")
else:
  print("No")

### FIXED CODE ###

a = input()
ans = a.count("7")
if ans==0:
  print("No")
else:
  print("Yes")
k, x = map(int, input().split())
i = max(−1000000, x - k + 1)
while i <= min(1000000, x + k - 1):
  print(i, end=" ")
  i += 1

### FIXED CODE ###

k, x = map(int, input().split())
i = max(-1000000, x - k + 1)
while i <= min(1000000, x + k - 1):
  print(i, end=" ")
  i += 1
import math
n = int(input())
h = list(map(int,input().split(' ')))

n-=1
dp = {}
def frog1(i):
    if i==n:
        return 0
    if i in dp:
        return dp[i]
    
    a = 0
    b = 0
    if i+1>n:
        a = math.inf
    else:
        a = frog1(i+1)+abs(h[i]-h[i+1])

    if i+2>n:
        b = math.inf
    else:
        b = frog1(i+2)+abs(h[i]-h[i+2])
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0))


### FIXED CODE ###

import math
n = int(input())
h = list(map(int,input().split(' ')))
'''
n-=1
inf = int(1e9+5)

dp = {}
def frog1(i):
    if i==n:
        return 0
    if i in dp:
        return dp[i]
    
    a = 0
    b = 0
    if i+1>n:
        a = inf
    else:
        a = frog1(i+1)+abs(h[i]-h[i+1])

    if i+2>n:
        b = inf
    else:
        b = frog1(i+2)+abs(h[i]-h[i+2])
    dp[i] = min(a,b)
    return dp[i]
'''
def solve(n,h):
    dp = [0]*n
    for i in reversed(range(0,n-1)):
        if i==n-1:
            dp[i] = 0
        else:
            if i==n-2:
                dp[i] = abs(h[n-1]-h[n-2])
                continue
            a = 0
            b = 0
            if i+1<=n-1:
                a = dp[i+1]+abs(h[i]-h[i+1])
            if i+2<=n-1:
                b = dp[i+2]+abs(h[i]-h[i+2])  
            dp[i] = min(a,b)
    return dp[0]
print(solve(n,h))
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    MOD = 10 ** 9 + 7
    ans = 1
    ok = True
    plus = []
    minus = []
    for a in A:
        if a >= 0:
            ok = False
            plus.append(a)
        else:
            minus.append(a)
    
    if ok and N == K and K % 2 == 0:
        ok = False
    
    if ok:
        A.sort(reverse=True)
        for i in range(K):
            ans *= A[i]
            ans %= MOD
    else:
        plus.sort(reverse=True)
        minus.sort()
        B = []
        if K % 2 == 0:
            for i in range(0,len(plus),2):
                if i + 1 < len(plus):
                    B.append(plus[i] * plus[i+1])
            for i in range(0,len(minus),2):
                if i + 1 < len(minus):
                    B.append(minus[i] * minus[i+1])
            B.sort(reverse=True)
            for i in range(K//2):
                ans *= B[i]
                ans %= MOD
        else:
            ans = plus.pop(0)
            for i in range(0,len(plus),2):
                if i + 1 < len(plus):
                    B.append(plus[i] * plus[i+1])
            for i in range(0,len(minus),2):
                if i + 1 < len(minus):
                    B.append(minus[i] * minus[i+1])
            B.sort(reverse=True)
            for i in range(K//2):
                ans *= B[i]
                ans %= MOD

    print(ans)
            



if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    plus = []
    minus = []
    for a in A:
        if a >= 0:
            plus.append(a)
        else:
            minus.append(a)
    # 答えが負になる場合をFalse
    flag = False
    if len(plus) > 0:
        if N == K:
            flag = (len(minus) % 2) == 0
        if N > K:
            flag = True
    elif len(plus) == 0:
        flag = (K % 2) == 0
    
    MOD = 10 ** 9 + 7
    ans = 1
    if flag:
        plus.sort(reverse=True)
        minus.sort()
        if K % 2 == 1:
            ans = plus.pop(0)
        B = []
        for i in range(0,len(plus),2):
            if i + 1 < len(plus):
                B.append(plus[i]*plus[i+1])
        for i in range(0,len(minus),2):
            if i + 1 < len(minus):
                B.append(minus[i]*minus[i+1])
        B.sort(reverse=True)
        for j in range(K//2):
            ans *= B[j]
            ans %= MOD
    else:
        A.sort(reverse=True)
        for i in range(K):
            ans *= A[i]
            ans %= MOD

    print(ans)
        


if __name__ == "__main__":
    main()
from sys import stdin

K = int(inpu())
S = input()

if len(S) <= K:
    print(S)
else:
    print(S[:K],'...',sep='')

### FIXED CODE ###

from sys import stdin

K = int(input())
S = input()

if len(S) <= K:
    print(S)
else:
    print(S[:K],'...',sep='')
N = int(input())
A = list(map(int,input().split()))
from bisect import bisect_left

for i in range(N):
    A[i] = A[i]-i
A.sort()

S = [0]*(N+1)
for i in range(N):
    S[i+1] = S[i]+A[i]

sumA = sum(A)
maxA,minA = max(A),min(A)

Ans = 10**9
for i in range(3z00):
    l = bisect_left(A,minA)
    low  = abs(minA*l - S[l]) + abs(S[N]-S[l] - minA*(N-l))
    h = bisect_left(A,maxA)
    high  = abs(maxA*h - S[h]) + abs(S[N]-S[h] - maxA*(N-h))
    if low==high:
        minA += 1
    elif low < high:
        maxA = (maxA+minA)//2
    else:
        minA = (maxA+minA)//2
    #print(Ans,low,high,minA,maxA)
    Ans = min(Ans,low,high)

print(Ans)

### FIXED CODE ###

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

for i in range(N):
    A[i] -= i

A.sort()

Ans = 0
mi = len(A)//2 # 中央値のindex
for i in range(N):
    Ans += abs(A[i]-A[mi])

print(Ans)
N=int(input())
ans = [0]*N
for i in range(1,100):
    for j in range(1,100):
        for k in range(1,100):
            ans[i**2+j**2+k**2+i*j+j*k+i*k] += 1
for i in range(N):
    print(ans[i])


### FIXED CODE ###

N=int(input())
ans = [0]*10000000
for i in range(1,100):
    for j in range(1,100):
        for k in range(1,100):
            ans[i**2+j**2+k**2+i*j+j*k+i*k] += 1
for i in range(1,N+1):
    print(ans[i])
v=[4,1,4,1,2,1,2,1]
while True:
    x=raw_input()
    x=x[0:len(x)-1]
    p=map(int,x.split(" "))
    min=[]
    min_value=0

    for i in range(8):
        count=0
        for j in range(8):
            if p[(j)%8]-v[(j+i)%8]>=0:
                count+=p[(j)%8]-v[(j+i)%8]
        if len(min)==0:
            min_value=count
            min.append(i)
        else:
            if count<min_value:
                min_value=count
                min=[]
                min.append(i)
            elif count==min_value:
                min.append(i)
    min1_value=41412122
    for i in range(len(min)):
        value=""
        for j in range(8):
            value+=str(v[(j+min[i])%8])
        if min1_value>int(value):
            min1_value=int(value)
            min1=min[i]
    solve=[]
    for i in range(8):
        solve.append(str(v[(i+min1)%8]))
    print " ".join(solve)

### FIXED CODE ###

v=[4,1,4,1,2,1,2,1]
while True:
    try:
        p=map(int,raw_input().split(" "))
        min=[]
        min_value=0
        
        for i in range(8):
            count=0
            for j in range(8):
                if p[(j)%8]-v[(j+i)%8]>=0:
                    count+=p[(j)%8]-v[(j+i)%8]
            if len(min)==0:
                min_value=count
                min.append(i)
            else:
                if count<min_value:
                    min_value=count
                    min=[]
                    min.append(i)
                elif count==min_value:
                    min.append(i)
        min1_value=41412122
        for i in range(len(min)):
            value=""
            for j in range(8):
                value+=str(v[(j+min[i])%8])
            if min1_value>int(value):
                min1_value=int(value)
                min1=min[i]
        solve=[]
        for i in range(8):
            solve.append(str(v[(i+min1)%8]))
        print " ".join(solve)
    except:
        break
k = int(input())
a,b = map(int, input().split())
for i in range(a, b+1):
  if i % k == 0:
    print('Yes')
  	exit()
print('No')

### FIXED CODE ###

k = int(input())
a,b = map(int, input().split())
for i in range(a, b+1):
  if i % k == 0:
    print('OK')
    exit()
print('NG')
a, b, c = map(int, input().split())
x = [sorted(a, b, c)]

print(x[2]*10 + x[0] + x[1])

### FIXED CODE ###

a, b, c = map(int, input().split())
x = sorted([a, b, c])

print(x[2]*10 + x[0] + x[1])
S = input()
l = len(S)
c = 0
for i in range(l // 2):
  if S[i] == S[l-i]:
    pass
  else:
    c +=1
print(c)


### FIXED CODE ###

S = input()
l = len(S)
c = 0
for i in range(l // 2):
  if S[i] == S[l-i-1]:
    pass
  else:
    c +=1
print(c)
N = int(input())
h = list(map(int,input().split()))
ans = True
for i in range(N-1,-1,-1):
    if h[i] > h[i+1]:
        h[i]-=1
for i in range(N-1):
    if h[i] <= h[i+1]:
        ans = True
    else:
        ans = False
        break
if ans:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

N = int(input())
h = list(map(int,input().split()))
ans = True
for i in range(N-2,-1,-1):
    if h[i] > h[i+1]:
        h[i]-=1
for i in range(N-1):
    if h[i] <= h[i+1]:
        ans = True
    else:
        ans = False
        break
if ans:
    print('Yes')
else:
    print('No')
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n+1):    
    c = a.index(i)
    c += 1
    print(c,end=' ')

### FIXED CODE ###

a = int(input())
if a%2==0:
    print("0.5")
else:
    b = a+1
    b = b/2
    print(b/a)
from collections import deque
r, c = map(int, input().split())
 
orig_maze = [[val for val in list(input())] for _ in range(r)]

maze = [[None for _ in range(c+2)] for _ in range(r+2)]
for r_index in range(r+2):
  for c_index in range(c+2):
    if r_index == 0 or r_index == r+1 or c_index == 0 or c_index == c+1:
      maze[r_index][c_index] = "#"
    else:
      maze[r_index][c_index] = orig_maze[r_index-1][c_index-1]
      
def wfs(queue, goal):
    finish_dist = None
    while len(queue) > 0:
        a = queue.popleft()
        y = a[0]
        x = a[1]
        count = a[2]
        if checked_maze[y][x]:
            continue
 
        if a[0] == goal[0] and a[1] == goal[1]:
            finish_dist = count
            break
 
        checked_maze[y][x] = True
        if not maze[y][x] == "#":
            count += 1
            queue.append((a[0]+1, a[1], count))
            queue.append((a[0]-1, a[1], count))
            queue.append((a[0], a[1]+1, count))
            queue.append((a[0], a[1]-1, count))

    return finish_dist 
 
 
if __name__ == "__main__":
    result = 0
    for s_y in range(1, r+1):
      for s_x in range(1, c+1):
        for g_y in range(1, r+1):
          for g_x in range(1, r+1):
            if maze[s_y][s_x] == "." and maze[g_y][g_x] == ".":
              if s_y == g_y and s_x == g_x:
                finish_dist = 0
              else:
                checked_maze = [[None for _ in range(c+2)] for _ in range(r+2)]
                que = deque()
                checked_maze[s_y][s_x]= 0
                que.append((s_y, s_x, 0))
                finish_dist = wfs(que, (g_y, g_x))
            result = max(result,finish_dist)
    print(result)

### FIXED CODE ###

from collections import deque
r, c = map(int, input().split())
 
orig_maze = [[val for val in list(input())] for _ in range(r)]

maze = [[None for _ in range(c+2)] for _ in range(r+2)]
for r_index in range(r+2):
  for c_index in range(c+2):
    if r_index == 0 or r_index == r+1 or c_index == 0 or c_index == c+1:
      maze[r_index][c_index] = "#"
    else:
      maze[r_index][c_index] = orig_maze[r_index-1][c_index-1]
      
def wfs(queue):
    finish_dist = None
    max_way = 0
    while len(queue) > 0:
        a = queue.popleft()
        y = a[0]
        x = a[1]
        count = a[2]
        if checked_maze[y][x]:
            continue
        
        checked_maze[y][x] = True
        if not maze[y][x] == "#":
            max_way = count
            count += 1
            queue.append((a[0]+1, a[1], count))
            queue.append((a[0]-1, a[1], count))
            queue.append((a[0], a[1]+1, count))
            queue.append((a[0], a[1]-1, count))

    return max_way 
 
if __name__ == "__main__":
    result = 0
    
    for s_y in range(1, r+1):
      for s_x in range(1, c+1):
        checked_maze = [[None for _ in range(c+2)] for _ in range(r+2)]
        que = deque()
        checked_maze[s_y][s_x]= 0
        que.append((s_y, s_x, 0))
        finish_dist = wfs(que)
        result = max(result,finish_dist)
    print(result)
def counter():#島の数をカウントする
    count=0
    #12*12個の値をひとつずつ調べる
    for x in range(12):
        for y in range(12):
            if is_data[x][y] == 1:#島が発見されたら
                dfs(x,y)
                count+=1
    return count
def dfs(x,y):
    if x<0 or x == len(is_data[x]) or y<0 or y == len(is_data):
        return
    if is_data[x][y] == 1:
        is_data[x][y] = 0
        dfs(x+1,y)
        dfs(x,y+1)
        dfs(x-1,y)
        dfs(x,y-1)
def get_map():
    is_data = []
    while True:
        try:
            tmp = list(int(input()))
            if len(tmp) != 12: break
            is_data.append(tmp)
        except:
            break
    return map
try:
    while True:
        is_data = get_map()
        if len(is_data) != 12: break
except EOFError:
    print(counter())


### FIXED CODE ###

def counter():#島の数をカウントする
    count=0
    #12*12個の値をひとつずつ調べる
    for x in range(len(is_data)):
        for y in range(len(is_data)):
            if is_data[x][y] == '1':#島が発見されたら
                dfs(x,y)
                count+=1
    return count
def dfs(x,y):
    if x<0 or x == len(is_data) or y<0 or y == len(is_data):
        return
    if is_data[x][y] == '1':
        is_data[x][y] = '0'
        dfs(x+1,y)
        dfs(x,y+1)
        dfs(x-1,y)
        dfs(x,y-1)

is_data=[]
while True:
    try:
        s = list(input())#入力
        if not s:#空入力のとき
            print(counter())
            is_data=[]
            s = []
        else:
            is_data.append(s)
    except EOFError:
        print(counter())
        break
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

from math import log2, ceil

def solve():
    n, k = MI()

    ans = 0
    m = ceil(log2(k))

    sm = 0
    v = 0
    for point in range(1, n+1):
        if 1 <= point < k:
            # さいころを振る
            # print(point)
            sm = sm + 1
            prob = 1
            for j in range(m+1):
                point = point * 2
                prob = prob / 2
                # print('  j+1', j+1, point)
                if point >= k:
                    # print(j)
                    ans = ans + prob
                    break
        else:
            # 無条件で勝利
            v = v + 1
    t = v / n
    print(ans * (1 - t) / sm + t)


if __name__ == '__main__':
    solve()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n, k = MI()

    ans = 0
    for point in range(1, n+1):
        prob = 1
        while point < k:
            prob = prob / 2
            point = point * 2
        # print(point, prob)
        ans = ans + prob / n

    print(ans)

if __name__ == '__main__':
    solve()
n = int(input())
record =  [ [ [0 for r in range(10)] for f in range(3) ] for b in range(4)]
for i in range(n):
    b,f,r,v = [int(a) in input().split(" ")]
    record[b - 1][f - 1][r - 1] += v
for b in range(4):
    for f in record[b]:
        print( " ".join( [str(x) for x in f] ) )
    if b != 3:
        print("#" * 20)
        

### FIXED CODE ###

n = int(input())
record = [ [ [0 for r in range(10)] for f in range(3)] for b in range(4)]
for _ in range(n):
    b,f,r,v = [int(i) for i in input().split(" ")]
    record[b - 1][f - 1][r - 1] += v
for b1 in range(4):
    for f in record[b1]:
        print(" "+" ".join([str(x) for x in f]))
    if b1 != 3:
        print("#" * 20)
N = input()
n = [int(N[i]) for i in range(4)]  


def addd(lista,op):
    cnt = lista[0]


    tmp = [lista[0]]
    for i in range(len(op)):
        if op[i]==0:
            cnt+=lista[i+1]
            tmp.append("+")
            tmp.append(lista[i+1])
        else:
            cnt -=lista[i+1]
            tmp.append("-")
            tmp.append(lista[i+1])
    if cnt ==7:
        tmp.append("=7")
        return tmp
    else:
        False

for i in range(2):
    for j in range(2):
        for k in range(2):
            ans = addd(n,[i,j,k])
            if ans != False:
                print("".join([str(n) for n in ans]))
                exit()
    




### FIXED CODE ###

N = input()
n = [int(N[i]) for i in range(4)]  

def addd(lista,op):
    cnt = lista[0]


    tmp = [lista[0]]
    for i in range(len(op)):
        if op[i]==0:
            cnt+=lista[i+1]
            tmp.append("+")
            tmp.append(lista[i+1])
        else:
            cnt -=lista[i+1]
            tmp.append("-")
            tmp.append(lista[i+1])
    if cnt ==7:
        tmp.append("=7")
        return tmp
    else:
        return False

for i in range(2):
    for j in range(2):
        for k in range(2):
            ans = addd(n,[i,j,k])
            if ans != False:
                print("".join([str(n) for n in ans]))
                exit()
N = int(input())
Y = int(input())

answer = []
comb = []
for j in range(N+1):
    for i in range(N+1):
        sum = 10000*j + 5000*i + 1000*(N-i-j)
        if(sum==Y and N-i-j>=0):
            print(j,i,N-i-j)
            exit()
            
print('-1 -1 -1')

### FIXED CODE ###

date = input()

date_list = list(date)
date_list[3] = '8'
output = ''.join(date_list)

print(output)
A,P = map(int,input().split())
print((A*3+B)//2)



### FIXED CODE ###

A,P = map(int,input().split())
print((A*3+P)//2)
import math
r = float(input())
print '%f %f' % (math.pi*r^2, 2*math.pi*r)

### FIXED CODE ###

import math
r = float(input())
print '%f %f' % (math.pi*r*r, 2*math.pi*r)
N, M = map(int, input().split())
ab = [[] for _ in range(M)]

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

count = 0

def dfs(i, now, done):
    global count
    if i == N-1:
        count += 1
        return

    for j in ab[now]:
        if j not in done:
            dfs(i+1, j, done + [j])
    return

dfs(0, 0, [0])
print(count)


### FIXED CODE ###

import itertools

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

path = [[False] * n for i in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    path[a][b] = True
    path[b][a] = True

ans = 0

for i in itertools.permutations(range(n), n):#N!
    if i[0] == 0:
        for j in range(n):
            if j == n - 1:
                ans += 1
                break
            if not path[i[j]][i[j + 1]]:
                break

print(ans)
# floor(N/m) = N % m

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

def solve(n):
    s = 0

    for d in divisors(n):
        m = d - 1
        if m and (m+1)*(n//m) == n:
            s += m
    return s


n = int(input())
print(solve(n))


### FIXED CODE ###

n,k=map(int,input().split())
print(n-k+1)
W, H, x, y, r = map(int, input().split()))
  if x - r > 0 and x + r < W:
    if y - r > 0 and y + r < H:
      print("Yes")
  else:
    print("No")

### FIXED CODE ###

W, H, x, y, r = map(int, input().split())
if r <= x <= W - r and r <= y <= H - r:
  print("Yes")
else:
  print("No")
a = list(map(int,input().split()))
if a[0] > 0:
    f = 1
else:
    f = -1
m = a[0]
cnt = 0
for i in range(1, n):
    f *= -1
    m += a[i]
    if f == 1:
        if m > 0:
            continue
        else:
            cnt += f-m
            m += f-m
    else:
        if m < 0:
            continue
        else:
            cnt += m-f
            m += f-m
print(cnt)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
m1 = 0
cnt1 = 0
f = 1
for i in range(n):
    f *= -1
    m1 += a[i]
    if f == 1:
        if m1 <= 0:
            cnt1 += f-m1
            m1 += f-m1
    else:
        if m1 >= 0:
            cnt1 += m1-f
            m1 -= m1-f
m2 = 0
cnt2 = 0
f = -1
for i in range(n):
    f *= -1
    m2 += a[i]
    if f == 1:
        if m2 <= 0:
            cnt2 += f-m2
            m2 += f-m2
    else:
        if m2 >= 0:
            cnt2 += m2-f
            m2 -= m2-f

print(min(cnt1, cnt2))
n=int(input())
a=list(map(int, input().split()))
q=int(input())
results=[]

S=sum(a)

table=[0]*(10**5)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))
        results.append(ans)
        table[b]=0
        S=ans
    else:
        results.appned(S)

for result in results:
    print(result)

### FIXED CODE ###

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

S=sum(a)

table=[0]*(10**6)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))

        print(ans)

        table[b]=0
        S=ans
    else:
        print(S)