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

def comp(a,b):
  r=""
  for x,y in zip(a,b):
    if x==y:
      r += x
    elif y=="?":
      r += x
    elif x=="?":
      r += y
    else:
      return False
  return r

def merge(a,b):
  for i in range(len(a)-len(b)+1):
    m = comp(a[i:],b)
    if m:
      yield a[:i]+m+a[i+len(b):]
  
  for i in range(1,len(b)):
    m = comp(b[i:], a)
    if m:
      yield  b[:i]+m+a[len(b)-i:]
    m = comp(b, a[-len(b)+i:])
    if m:
      yield a[:-len(b)+i]+m+b[-i:]
  yield a+b
  yield b+a

a=input()
b=input()
c=input()
a,b,c=sorted([a,b,c],key=len,reverse=True)

abc = float("inf")
for d in merge(a,b):
  if len(d) > abc:
    break
  l = len(next(merge(d, c)))
  abc = min(abc, l)

for d in merge(a,c):
  if len(d) > abc:
    break
  l = len(next(merge(d, b)))
  abc = min(abc, l)

for d in merge(b,c):
  if len(d) > abc:
    break
  if len(a) < len(d):
    l = len(next(merge(d, a)))
  else:
    l = len(next(merge(a, d)))
  abc = min(abc, l)

print(abc)



### FIXED CODE ###

from itertools import *
from heapq import *

def comp(a,b):
  return all(x==y or "?" in (x,y) for x,y in zip(a, b))

def comp3(a,b,c):
  return 

def comp3(i,j,k,a,b,c):
  m = max(i,j,k)
  n = min(i+len(a),j+len(b),k+len(c))
  return all(len({x,y,z}-{"?"}) <= 1 for x,y,z in zip(a[m-i:n-i],b[m-j:n-j],c[m-k:n-k]))

def _merge_mid(a,b):
  la, lb = map(len,[a,b])
  for i in range(la-lb+1):
    if comp(a[i:],b):
      yield i

def merge_mid(a,b):
  return list(_merge_mid(a,b))

def _merge_right(a,b):
  la, lb = map(len,[a,b])
  for i in range(max(la-lb+1, 1),la):
    if comp(b, a[i-la:]):
      yield i
  yield la

def merge_right(a,b):
  return list(_merge_right(a,b))

class K(int):
  def __new__(cls, *args, **kwargs):
    cb = kwargs.pop("cb")
    i = int.__new__(cls, *args, **kwargs)
    i.cb = cb
    return i

def min_merge(a,b,c):
  keys = sorted([a,b,c],key=len,reverse=True)
  len_keys = list(map(len, keys))
  mid = {(i,j):merge_mid(keys[i],keys[j]) for i,j in permutations(range(3), 2)}
  right = {(i,j):merge_right(keys[i],keys[j]) for i,j in permutations(range(3), 2)}
  
  for i in mid[0,1]:
    for j in mid[0,2]:
      if j+len_keys[2] <= i or i+len_keys[2] <= j or (j-i in mid[1,2] or j-i in right[1,2] or i-j in right[2,1]):
        return len_keys[0]
  
  def candicates(a,b,c):
    return chain(
      (K(i+len_keys[b], cb=lambda:(
       (mid[a,c] and mid[a,c][0]+len_keys[c] <= i) or (mid[b,c] and len_keys[a] <= i+mid[b,c][-1]) or 
       any((i-j in right[c,b] if i>j else j-i in mid[b,c]) and comp3(0,i,j,keys[a],keys[b],keys[c]) for j in dropwhile((i-len_keys[c]).__gt__, mid[a,c]+right[a,c]))
      )) for i in dropwhile((len_keys[c]-len_keys[b]+1).__gt__, right[a,b])),
      (K(min(i+next(dropwhile((len_keys[a]-i+1).__gt__, y))+len_keys[b] for i in x), cb=lambda:True) for (x, y) in [sorted((right[a,c], right[c,b]), key=len)]),
    )
  
  can = merge(*(candicates(x,y,z) for x,y,z in permutations(range(3),3)))
  
  for k in can:
    if k.cb():
      return k
  
  return sum(len_keys)

a=input()
b=input()
c=input()

print(min_merge(a,b,c))
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
totals=[0]*n


def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]


for i in range(n-1,-1,-1):
    if totals[i]%2==a[i]:
        continue
    else:
        ans[i]+=1
        arr=make_divisors(i+1)

        for num in arr:
            totals[num-1]+=1

cnt=ans.count(1)

print(cnt)
if cnt!=0:
    b=[]
    for j in range(n):
        if ans[j]!=0:
            b.append(j+1)

    print(*b)
    


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
totals=[0]*n


def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]


for i in range(n-1,-1,-1):
    if totals[i]%2==a[i]:
        continue
    else:
        ans[i]+=1
        arr=make_divisors(i+1)

        for num in arr:
            totals[num-1]+=1

cnt=ans.count(1)

print(cnt)
if cnt!=0:
    b=[]
    for j in range(n):
        if ans[j]!=0:
            b.append(j+1)

    print(*b)
### BUGGY CODE ###

dhw = [(1,0),(0,-1),(0,1),(-1,0),
( 2,-2),( 2,-1),( 2, 0),( 2, 1),( 2, 2),
( 1,-2),( 1,-1),        ( 1, 1),( 1, 2),
( 0,-2),                        ( 0, 2),
(-1,-2),(-1,-1),        (-1, 1),(-1, 2),
(-2,-2),(-2,-1),(-2, 0),(-2, 1),(-2, 2)]

H, W = map(int, input().split())
sh, sw = map(lambda x:int(x)-1, input().split())
eh, ew = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(H)]

dst = [[-1]*W for _ in range(H)]
dst[sh][sw] = 0

from collections import deque
dq = deque([(sh, sw)])
while dq:
    h, w = dq.popleft()

    for i in range(4):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh : continue
        if nw < 0 or W <= nw : continue
        if S[nh][nw] == '#': continue
        if dst[nh][nw] == -1 or dst[h][w] < dst[nh][nw]:
            dst[nh][nw] = dst[h][w]
            dq.appendleft((nh, nw))

    for i in range(4, 24):
        nh = h + dhw[i][0]; nw = w + dhw[i][1]
        if nh < 0 or H <= nh : continue
        if nw < 0 or W <= nw : continue
        if S[nh][nw] == '#': continue
        if dst[nh][nw] >= 0: continue
        if dst[nh][nw] == -1:
            dst[nh][nw] = dst[h][w] + 1
        elif dst[h][w] + 1 < dst[nh][nw]:
            dst[nh][nw] = min(dst[nh][nw], dst[h][w] + 1)
        else: continue
        dq.append((nh, nw))

print(dst[eh][ew])


### FIXED CODE ###

dhw = [(1,0),(0,-1),(0,1),(-1,0),
( 2,-2),( 2,-1),( 2, 0),( 2, 1),( 2, 2),
( 1,-2),( 1,-1),        ( 1, 1),( 1, 2),
( 0,-2),                        ( 0, 2),
(-1,-2),(-1,-1),        (-1, 1),(-1, 2),
(-2,-2),(-2,-1),(-2, 0),(-2, 1),(-2, 2)]

H, W = map(int, input().split())
sh, sw = map(lambda x:int(x)-1, input().split())
eh, ew = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(H)]

dst = [[-1]*W for _ in range(H)]

from collections import deque
dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    if 0 <= h < H and 0 <= w < W and dst[h][w] < 0 and S[h][w] != '#':
        dst[h][w] = d
    else:
        continue
    for i in range(4): dq.appendleft((h + dhw[i][0], w + dhw[i][1], d))
    for i in range(4, 24): dq.append((h + dhw[i][0], w + dhw[i][1], d+1))

print(dst[eh][ew])
### BUGGY CODE ###

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

a = [0] * M
for i in range(M):
    a[i] = int(input())
    



def fib(n):
    memo = [0] * (int(n)+1)
    memo[0] = 1
    for i in range(1,n+1):
        if i in a:
          memo[i] = 0
        ## memo[i] i 番目の階段にいくまでのルート数
        else:
          memo[i] = memo[i-1] +memo[i-2]
    return memo[n]

print(fib(N) % 1000000007)


### FIXED CODE ###

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

a = [0] * M
for i in range(M):
    a[i] = int(input())
    



memo = [-1] * (N+1)
memo[0] = 1

for i in range(M):
    memo[a[i]] = 0


for i in range(1,N+1):
    if i == 1 and memo[i] == -1:
        memo[i] = memo[i-1]
    elif memo[i] >= 0:
       memo[i] = 0
        ## memo[i] i 番目の階段にいくまでのルート数
    else:
          memo[i] = memo[i-1] +memo[i-2]


print(memo[N] % 1000000007)
### BUGGY CODE ###

import sys
import math
from bisect import bisect_left as bl

def cul(l,r):
    return bl(b,r)-bl(b,l)

N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
q=max(a+[b[-1]])
if q==0: print(0),sys.exit()

p=math.ceil( math.log(q,2) )+1


ans=0
for i in range(p,-1,-1):
    T=2**i
    b=[b[i]%(2*T) for i in range(N)]
    b.sort()
    tmp=0
    for j in range(N):
        x=a[j]%(2*T)
        tmp+= -bl(b,T-x)+bl(b,2*T-x)-bl(b,3*T-x)+bl(b,4*T-x)
    ans+=(tmp%2)*T

print(ans)

### FIXED CODE ###

import sys
import math
import numpy as np
bl = np.searchsorted

N=int(input())
a=np.int32(input().split())
b=np.int32(input().split())
b.sort()
q=max(a+[b[-1]])
if q==0: print(0),sys.exit()

p=math.ceil( math.log(q,2) )+1


ans=0
for i in range(p,-1,-1):
    T=2**i
    ai=a % (2*T)
    bi=b % (2*T)
    bi.sort()

    tmp=0
    left1 = bl(bi, T - ai)
    right1 = bl(bi, 2*T - ai)
    left2 = bl(bi, 3*T - ai)
    right2 = bl(bi, 4*T - ai)    
    if np.sum(-left1 + right1 -left2 +right2) % 2: ans+=T

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

import copy
N = int(input())
A = list(map(int,input().split()))
B = copy.deepcopy(A)
B.sort()
ans = []
for i in B:
    ans.append(A.index(i) + 1)
for i in ans:
    print(i,end=" ")

### FIXED CODE ###

import copy
N = int(input())
A = list(map(int,input().split()))
B = copy.deepcopy(A)
B.sort()
num = [i for i in range(N)]
data = dict(zip(A,num))
ans = []
for i in B:
    ans.append(data[i]+1)
for i in ans:
    print(i,end=" ")
### BUGGY CODE ###

N = int(input())
a = [int(input()) for i in range(N)]
hako = []
count = 0
flag = True
i = 1

while True:
    hako.append(a[i-1])
    if hako.count(a[i-1]) == 2:
        flag = False
        break
    i = a[i-1]
    count += 1
    if i == 2:
        break

if flag:
    print(count)
else:
    print(-1)


### FIXED CODE ###

N = int(input())
a = [int(input()) for i in range(N)]
i = 1
count = 0

for x in range(N):
  i = a[i-1]
  count += 1
  if i == 2:
    break
    
if i == 2:
  print(count)
else:
  print(-1)
### BUGGY CODE ###

import math
import fractions
def lcm(x, y):
  return (x * y) // fractions.gcd(x, y)

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

MODp = 1000000007
K = 1
for i in range(N):
  K = lcm(K,l[i])

Ans = 0
for i in range(N):
  Ans += ((K // l[i])%MODp)

Ans = Ans % MODp
print(Ans)

### FIXED CODE ###

import math
import fractions
def lcm(x, y):
  return (x * y) // fractions.gcd(x, y)

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

MODp = 1000000007
K = 1
for i in range(N):
  K = lcm(K,l[i])

Ans = 0
for i in range(N):
  Ans += ((K // l[i]))

Ans = Ans % MODp
print(Ans)
### BUGGY CODE ###

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

list0 = []
list1 = []
set1 = set()

td = []
for i in range(N):
    t, d = map(int, input().split())
    td.append((t, d))

td.sort(key=lambda x: (x[0], -x[1]))

for t, d in td:
    if t in set1:
        list0.append(d)
    else:
        set1.add(t)
        list1.append(d)

list0.sort(reverse=True)
list1.sort(reverse=True)

various = min(K, len(list1))
selected0 = []
selected1 = []
for i in range(various):
    selected1.append(list1.pop(0))
if K > various:
    for i in range(K - various):
        selected0.append(list0.pop(0))


ans = sum(selected0) + sum(selected1) + various ** 2

mn = selected1[-1]
while list0 and list0[0] > mn:
    mn = selected1.pop(-1)
    selected0.append(list0.pop(0))
    various -= 1
    tmp = sum(selected0) + sum(selected1) + various ** 2
    if tmp <= ans:
        break
    ans = tmp

print(ans)


### FIXED CODE ###

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

list0 = []
list1 = []
set1 = set()

td = []
for i in range(N):
    t, d = map(int, input().split())
    td.append((t, d))

td.sort(key=lambda x: (x[0], -x[1]))

for t, d in td:
    if t in set1:
        list0.append(d)
    else:
        set1.add(t)
        list1.append(d)

list0.sort(reverse=True)
list1.sort(reverse=True)

various = min(K, len(list1))
selected0 = []
selected1 = []
for i in range(various):
    selected1.append(list1.pop(0))
if K > various:
    for i in range(K - various):
        selected0.append(list0.pop(0))


ans = sum(selected0) + sum(selected1) + various ** 2

mn = selected1[-1]
sum0 = sum(selected0)
sum1 = sum(selected1)
while list0 and list0[0] > mn:
    sum0 += list0.pop(0)
    sum1 -= selected1.pop(-1)
    various -= 1
    tmp = sum0 + sum1 + various ** 2
    if tmp <= ans:
        break
    ans = tmp

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

#!/usr/bin/env python3
import sys, math, fractions, itertools

YES = "POSSIBLE"  # type: str
NO = "IMPOSSIBLE"  # type: str

def solve(N: int, M: int, a: "List[int]", b: "List[int]"):
    G = [[] for _ in range(N)]
    for i in range(M):
        G[a[i]-1].append(b[i]-1)
        G[b[i]-1].append(a[i]-1)
    flag = False
    for g in G[0]:
        if g in G[-1]:
            flag = True
    if flag:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [int()] * (M)  # type: "List[int]" 
    b = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys, math, fractions, itertools, bisect

YES = "POSSIBLE"  # type: str
NO = "IMPOSSIBLE"  # type: str

def solve(N: int, M: int, a: "List[int]", b: "List[int]"):
    G = [[] for _ in range(N)]
    for i in range(M):
        G[a[i]-1].append(b[i]-1)
        G[b[i]-1].append(a[i]-1)
    G[0].sort()
    G[-1].sort()
    flag = False
    for g in G[0]:
        i = bisect.bisect_left(G[-1], g)
        if 0 <= i <= len(G[-1])-1:
            if G[-1][i] == g:
                flag = True
    if flag:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [int()] * (M)  # type: "List[int]" 
    b = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

INF = 1 << 64


def calc_cost(height1, height2):
    return abs(height1 - height2)


def main(n, k, heights):
    for i in range(k):
        heights.append(INF)
    costs = [INF for i in range(n + k)]
    costs[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            costs[i + j] = min(costs[i + j], calc_cost(heights[i], heights[i + j]) + costs[i])
    return costs[n - 1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))


### FIXED CODE ###

import numpy as np


def main(n, k, heights):
    heights = np.array(heights, dtype=int)
    costs = np.zeros(n, dtype=int)
    for i in range(1, n):
        start = max(0, i - k)
        costs[i] = np.min(costs[start:i] + np.abs(heights[i] - heights[start:i]))
    return costs[-1]


if __name__ == '__main__':
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    print(main(N, K, H))
### BUGGY CODE ###

import queue

def solve(maze, start):
    if maze[start[0]][start[1]] == '#':
        return 0

    q = queue.Queue()
    q.put((start, 0))

    h = len(maze)
    w = len(maze[0])

    memo = [[False] * w for _ in range(h)]

    ans = 0

    while not q.empty():
        p, i = q.get()

        if not memo[p[0]][p[1]]:
            memo[p[0]][p[1]] = True
            ans = i
        else:
            continue

        if p[0] < h-1 and maze[p[0] + 1][p[1]] == '.':
            q.put(((p[0] + 1, p[1]), i + 1))

        if p[0] > 0 and maze[p[0] - 1][p[1]] == '.':
            q.put(((p[0] - 1, p[1]), i + 1))

        if p[1] < w-1 and maze[p[0]][p[1] + 1] == '.':
            q.put(((p[0], p[1] + 1), i + 1))

        if p[1] > 0 and maze[p[0]][p[1] - 1] == '.':
            q.put(((p[0], p[1] - 1), i + 1))

    return ans


h, w = map(int, input().split())

maze = []
for i in range(h):
    s = list(input())
    maze.append(s)

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, solve(maze, (i, j)))

print(ans)


### FIXED CODE ###

import queue

def solve(maze, start):
    if maze[start[0]][start[1]] == '#':
        return 0

    q = queue.Queue()
    q.put((start, 0))

    h = len(maze)
    w = len(maze[0])

    memo = [[False] * w for _ in range(h)]
    memo[start[0]][start[1]] = True

    ans = 0

    while not q.empty():
        p, i = q.get()
        ans = i

        if p[0] < h-1 and not memo[p[0] + 1][p[1]] and maze[p[0] + 1][p[1]] == '.':
            q.put(((p[0] + 1, p[1]), i + 1))
            memo[p[0] + 1][p[1]] = True

        if p[0] > 0 and not memo[p[0] - 1][p[1]] and maze[p[0] - 1][p[1]] == '.':
            q.put(((p[0] - 1, p[1]), i + 1))
            memo[p[0] - 1][p[1]] = True

        if p[1] < w-1 and not memo[p[0]][p[1] + 1] and maze[p[0]][p[1] + 1] == '.':
            q.put(((p[0], p[1] + 1), i + 1))
            memo[p[0]][p[1] + 1] = True

        if p[1] > 0 and not memo[p[0]][p[1] - 1] and maze[p[0]][p[1] - 1] == '.':
            q.put(((p[0], p[1] - 1), i + 1))
            memo[p[0]][p[1] - 1] = True

    return ans


h, w = map(int, input().split())

maze = []
for i in range(h):
    s = list(input())
    maze.append(s)

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, solve(maze, (i, j)))

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

n=int(input())
x=2
while x%n!=0:
    x+=2

print(x)


### FIXED CODE ###

n=int(input())
if n%2==0:
    print(n)
else:
    print(n*2)
### BUGGY CODE ###

from itertools import product

N = int(input())
r = range(N)
A = [list(map(int, input().split())) for _ in r]

for i in r: A[i][i] = float('inf')
possible = True
ans = 0
for i,j in product(r, r) :
    if j <= i: continue
    for k in r:
        if A[i][j] < A[i][k] + A[k][j]:
            continue
        elif A[i][j] == A[i][k] + A[k][j]:
            break
        else:
            possible = False
        break
    else:
        ans += A[i][j]
print(ans if possible else -1)

### FIXED CODE ###

def main():
    inf=float("inf")
    n=int(input())
    alst=[list(map(int,input().split())) for _ in range(n)]
 
    for i in range(n):
        alst[i][i]=inf
 
    sm=0
    token=0
 
    for i in range(n):
        for j in range(i+1,n):
            for k in range(n):
                if alst[i][k]+alst[k][j]>alst[i][j]:
                    continue
                elif alst[i][k]+alst[k][j]==alst[i][j]:
                    break
                else:
                    token=1
                    break
            else :
                sm+=alst[i][j]
    
    if token : sm=-1
 
    print(sm)
 
main()
### BUGGY CODE ###

N,Q = map(int,input().split())
sons_of_root = []
edges = {}
for _ in range(N-1):
    a,b = map(int,input().split())
    if a==1:
    	sons_of_root.append(b)
    
    if a in edges.keys():
        edges[a] = edges[a]+[b]
    else:
        edges[a] = [b]
            
    if b in edges.keys():
        edges[b] = edges[b]+[a]
    else:
        edges[b] = [a]
        
counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
        
queue = [sons_of_root]
cqueue = [[counts[1]]*len(sons_of_root)]
ignores = [1]
results = [0]*(N+1)
results[1] = counts[1]
while queue != []:
    vals = queue.pop(0)
    cvals = cqueue.pop(0)
    for x,c in zip(vals,cvals):
        results[x] = counts[x]+c
        sons = [val for val in edges[x] if val not in ignores]
        queue.append(sons)
        cqueue.append([results[x]]*len(sons))
        ignores.append(x)
for i in range(1,N+1):
    print(results[i], end=' ')

### FIXED CODE ###

N,Q = map(int,input().split())
parents = {1:0}
for _ in range(N-1):
    a,b = map(int,input().split())
    key,val = max(a,b), min(a,b)
    parents[key] = val
counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
results = [0]*(N+2)
for i in range(1,N+1):
    results[i] = counts[i]+results[parents[i]]
    print(results[i], end=' ')
### BUGGY CODE ###

d = int(input())
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(input()) - 1 for i in range(d)]


def score(T):
    L = [-1 for j in range(26)]
    tmp = 0
    for i in range(d):
        tmp += S[i][T[i]]
        L[T[i]] = i
        tmp -= sum([C[j] * (i - L[j]) for j in range(26)])
    return tmp


m = int(input())
for k in range(m):
    p, q = map(int, input().split())
    T[p - 1] = q - 1
    print(score(T))


### FIXED CODE ###

d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [int(input()) - 1 for i in range(d)]
m = int(input())
PQ = [tuple(map(int, input().split())) for k in range(m)]
PQ = [(p - 1, q - 1) for p, q in PQ]

for p, q in PQ:
    T[p] = q
    L = [-1 for j in range(26)]
    X = [0 for j in range(26)]
    score = 0
    for i in range(d):
        score += S[i][T[i]]
        X[T[i]] += (d - i) * (i - L[T[i]])
        L[T[i]] = i
    for j in range(26):
        score -= C[j] * (dd - X[j])
    print(score)
### BUGGY CODE ###

N = int(input())
answer = []
for i in range(1,N+1):
  j = N / i
  if j == int(j):
    answer.append(max(len(str(i)),len(str(int(j)))))
print(min(answer))

### FIXED CODE ###

N = int(input())
answer = []
for i in range(1,int(N**0.5+1)):
  j = N / i
  if j == int(j):
    answer.append(max(len(str(i)),len(str(int(j)))))
print(min(answer))
### BUGGY CODE ###

n = int(input())
ans=0

import math
from functools import reduce

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

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

for i in range(n):
	for j in range(n):
		for k in range(n):
			ans+=gcd(i+1,j+1,k+1)
print(ans)

### FIXED CODE ###

n = int(input())
ans=0

import math
from functools import reduce

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

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

for i in range(n):
	for j in range(n):
		if math.gcd(i+1,j+1)==1:
			ans+=n
			continue
		else:
			for k in range(n):
				ans+=gcd(i+1,j+1,k+1)
print(ans)
### BUGGY CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    h,w=map(int,input().split())
    B=[[-1]*w for _ in range(h)]
    ans=0
    que=deque()
    for i in range(h):
        s=input()
        for j,ss in enumerate(s):
            if ss=="#":
                que.append((i,j))
                B[i][j]=0
    while que:
        qr,qc=que.popleft()
        for r,c in ((qr+1,qc),(qr-1,qc),(qr,qc+1),(qr,qc-1)):
            if r>=0 and r<=h-1 and c>=0 and c<=w-1 and B[r][c]==-1:
                B[r][c]=B[qr][qc]+1
                que.append((r,c))
    print(max([max(b) for b in B]))

if __name__=='__main__':
    main()

### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    h,w=map(int,input().split())
    B=[[-1]*w for _ in range(h)]
    ans=0
    que=deque()
    for i in range(h):
        s=input()
        for j,ss in enumerate(s):
            if ss=="#":
                que.append((i,j))
                B[i][j]=0
    while que:
        qr,qc=que.popleft()
        for r,c in ((qr+1,qc),(qr-1,qc),(qr,qc+1),(qr,qc-1)):
            if r>=0 and r<=h-1 and c>=0 and c<=w-1 and B[r][c]==-1:
                B[r][c]=B[qr][qc]+1
                que.append((r,c))
    print(max([max(b) for b in B]))

if __name__=='__main__':
    main()