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

#!/use/bin/env python3

(n, k) = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]

like = []

ans = n
while (1):

    c = True
    b = n
    while (b != 0):
        b = b // 10
        d = b % 10

        if d in a:
            c = False
            break

    if (c):
        ans = n
        break
    n += 1

print(ans)


### FIXED CODE ###

#!/use/bin/env python3

(n, k) = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]

like = []

ans = n
b = n
while (1):

    c = True
    b = n
    while (b != 0):
        d = b % 10
        b = b // 10

        if d in a:
            c = False
            break

    if (c):
        ans = n
        break
    n += 1

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

import sys
 
n,m = map(int,input().split())
a_arr = []
b_arr = []
c_arr = []
 
dic_arr = [{i:sys.maxsize for i in range(2 ** n)}]
dic_arr[0][0] = 0
 
for i in range(m):
  a,b = map(int,input().split())
  a_arr.append(a)
  b_arr.append(b)
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for j in range(1, n+1):
    if j in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c = int(''.join(arr),2)
  
  dic_arr.append(dic_arr[i].copy())
  
  for k,v in dic_arr[i].items():
    dic_arr[i+1][k|c] = min(dic_arr[i+1][k|c],a+v)  
 
if dic_arr[m][(2**n)-1] != sys.maxsize:
  print(dic_arr[m][(2**n) -1])
else:
  print(-1)

### FIXED CODE ###

n,m = map(int,input().split())
 
dic = {0:0}
 
for i in range(m):
  a,b = map(int,input().split())
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for j in range(1, n+1):
    if j in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c = int(''.join(arr),2)
  
  dic2 = dic.copy()
  
  for k,v in dic.items():
    if (k|c) in dic2.keys():
      dic2[k|c] = min(dic2[k|c],a+v)
    else:
      dic2[k|c] = a+v
 
  dic = dic2
 
if ((2**n)-1) in dic.keys():
  print(dic[(2**n) -1])
else:
  print(-1)
### BUGGY CODE ###

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

ans=1
thresh=10**18
for i in range(N):
    ans = ans * A[i]
if ans > thresh:
    ans=-1

print(ans)

### FIXED CODE ###

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

ans=1
thresh=10**18
A.sort()
for i in range(N):
    ans = ans * A[i]
    if ans > thresh:
        ans=-1
        break

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

# -*- coding: utf-8 -*-
from functools import reduce
import fractions


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


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

max_gcd = 0
for i in reversed(range(n)):
  wk_gcd = gcd(*(lst[:i] + lst[i + 1:len(lst)]))
  max_gcd = wk_gcd if wk_gcd > max_gcd else max_gcd

print(max_gcd)


### FIXED CODE ###

import fractions
from functools import reduce

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

# 累積GCD
left = [0] * (n + 1)
right = [0] * (n + 1)
for i in range(n):
  left[i + 1] = (fractions.gcd(left[i], a[i]))
  right[n - i - 1] = (fractions.gcd(right[n - i], a[n - 1 - i]))

ans = 0
for i in range(n):
  ans = max(ans, fractions.gcd(left[i], right[i + 1]))
print(ans)
### BUGGY CODE ###

import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines


def dfs(N, E, s):
    stack = [(s, 0)]
    seen = [0]*(N+1)
    seen[s] = 1
    
    while stack:
        v, par = stack.pop()
        for i in E[v]:
            if i == par:
                continue
            elif seen[i] == 0:
                seen[i] = 1
                stack.append((i, v))
    
    return seen

def main():
    N,M,K = map(int, readline().split())

    F = [[] for _ in range(N+1)]
    for _ in range(M):
        a,b = map(int, readline().split())
        F[a].append(b)
        F[b].append(a)
    
    B = [[] for _ in range(N+1)]
    for _ in range(K):
        c,d = map(int, readline().split())
        B[c].append(d)
        B[d].append(c)


    ans = []
    for i in range(1,N+1):
        tmp = 0
        seen = dfs(N,F,i)
        tmp += sum(seen) - 1
        tmp -= len(F[i])
        for j in B[i]:
            if seen[j] == 1:
                tmp -= 1
        ans.append(tmp)
    
    print(" ".join(map(str, ans)))


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

class UnionFind_size:
    def __init__(self, n):
        # 初期状態は全要素が根なので、親は自分自身
        self.par = [i for i in range(n+1)]
        # 集団の要素数(size)を格納する(初期は全て1)
        self.size = [1] * (n+1)
    
    # 根を検索する関数
    def find(self, x):
        # 根までたどったらその番号を返す
        if self.par[x] == x:
            return x
        # 根でないなら親の要素で再検索
        else:
            # 検索過程で親を書き換える(圧縮経路)
            self.par[x] = self.find(self.par[x])
            return self.find(self.par[x])
  
    # 結合(unite)する関数
    def unite(self, x, y):
        # 根を探す
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        # サイズを比較し、小さい方を大きい方に繋げる
        if self.size[x] < self.size[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
           
    # 同じグループに属するかを判定する関数
    def same_check(self, x, y):
        return self.find(x) == self.find(y)
    
    # 要素が属する木のサイズを返す関数
    def get_size(self, x):
        return self.size[self.find(x)]
    
    # グループ数を返す関数
    def group_sum(self):
        c = 0
        for i in range(len(self.par)):
            if self.find(i) == i:
                c += 1
        return c


def main():
    N,M,K = map(int, readline().split())

    uf = UnionFind_size(N+1)
    F = [[] for _ in range(N+1)]
    for _ in range(M):
        a,b = map(int, readline().split())
        F[a].append(b)
        F[b].append(a)
        uf.unite(a, b)
    
    B = [[] for _ in range(N+1)]
    for _ in range(K):
        c,d = map(int, readline().split())
        B[c].append(d)
        B[d].append(c)


    ans = []
    for i in range(1,N+1):
        tmp = 0
        tmp += uf.get_size(i) - 1
        tmp -= len(F[i])
        for j in B[i]:
            if uf.same_check(i, j):
                tmp -= 1
        ans.append(tmp)
    
    print(" ".join(map(str, ans)))


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

import sys
input = sys.stdin.readline
from heapq import heappush, heappop
def min_cost_flow_dijkstra(E, s, t, f):
    INF = 1 << 100
    NN = 2 * N + 6
    LN = NN.bit_length()
    G = [[] for _ in range(NN)]
    for a, b, cap, c in E:
        G[a].append([b, cap, c, len(G[b])])
        G[b].append([a, 0, -c, len(G[a])-1])
    prevv = [-1] * NN
    preve = [-1] * NN
    res = 0
    h = [0] * NN
    while f > 0:
        dist = [INF] * NN
        dist[s] = 0
        Q = []
        heappush(Q, s)
        while len(Q):
            x = heappop(Q)
            d, v = (x>>LN), x % (1<<LN)
            if dist[v] < d: continue
            for i, (w, _cap, c, r) in enumerate(G[v]):
                if _cap > 0 and dist[w] > dist[v] + c + h[v] - h[w]:
                    dist[w] = dist[v] + c + h[v] - h[w]
                    prevv[w] = v
                    preve[w] = i
                    heappush(Q, (dist[w] << LN) + w)
        if dist[t] == INF:
            return -1
        
        for v in range(N):
            h[v] += dist[v]
        
        d = f
        v = t
        while v != s:
            d = min(d, G[prevv[v]][preve[v]][1])
            v = prevv[v]
        f -= d
        res += d * dist[t]
        v = t
        while v != s:
            G[prevv[v]][preve[v]][1] -= d
            G[v][G[prevv[v]][preve[v]][3]][1] += d
            v = prevv[v]
    return res

E = []
N = int(input())
su = 0
inf = 1 << 31
for i in range(N):
    x, y, cnt = map(int, input().split())
    E.append((2*N+4, i, cnt, 0))
    E.append((i, 2*N, cnt, x + y + inf))
    E.append((i, 2*N+1, cnt, x - y + inf))
    E.append((i, 2*N+2, cnt, - x + y + inf))
    E.append((i, 2*N+3, cnt, - x - y + inf))
    su += cnt

for i in range(N):
    x, y, cnt = map(int, input().split())
    E.append((i+N, 2*N+5, cnt, 0))
    E.append((2*N, i+N, cnt, - x - y + inf))
    E.append((2*N+1, i+N, cnt, - x + y + inf))
    E.append((2*N+2, i+N, cnt, x - y + inf))
    E.append((2*N+3, i+N, cnt, x + y + inf))

print(su * inf * 2 - min_cost_flow_dijkstra(E, 2*N+4, 2*N+5, su))

### FIXED CODE ###

from networkx import*
N = int(input())
G = MultiDiGraph()
for i in range(N):
    x, y, c = map(int, input().split())
    G.add_node(i, demand = -c)
    G.add_weighted_edges_from([(i, -1, x + y)], capacity = c)
    G.add_weighted_edges_from([(i, -2, x - y)], capacity = c)
    G.add_weighted_edges_from([(i, -3, -x + y)], capacity = c)
    G.add_weighted_edges_from([(i, -4, -x - y)], capacity = c)
for i in range(N):
    x, y, c = map(int, input().split())
    G.add_node(N + i, demand = c)
    G.add_weighted_edges_from([(-1, N + i, -x - y)], capacity = c)
    G.add_weighted_edges_from([(-2, N + i, -x + y)], capacity = c)
    G.add_weighted_edges_from([(-3, N + i, x - y)], capacity = c)
    G.add_weighted_edges_from([(-4, N + i, x + y)], capacity = c)

print(-min_cost_flow_cost(G))
### BUGGY CODE ###

### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest
import numpy as np

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,k=map(int, readline().rstrip().split())
    arr=list(map(int, readline().rstrip().split()))

    for _ in range(k):
        tmp=[0]*n
        for i in range(n):
            x = i - arr[i]
            y = i + arr[i] + 1
            if x<0:
                x=0
            if y<n:
                tmp[y]-=1
            tmp[x]+=1
        arr[0]=tmp[0]
        for i in range(1,n):
            arr[i]=arr[i-1]+tmp[i]
    print(" ".join(map(str,arr)))
    return

    xxx="2 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0"
    arr=list(map(int, xxx.rstrip().split()))
    n=len(arr)
    k=10

    for _ in range(k):
        tmp=[0]*n
        for i in range(n):
            x = i - arr[i]
            y = i + arr[i] + 1
            if x<0:
                x=0
            if y>n:
                y=n
            for j in range(x,y):
                tmp[j]+=1
        arr=tmp
        print(arr)

    arr=list(map(int, xxx.rstrip().split()))
    n=len(arr)
    print("---")
    tmp=[0]*n
    for i in range(n):
        x = i - arr[i]
        y = i + arr[i] + 1
        if x<0:
            x=0
        if y>n:
            y=n
        for j in range(x,y):
            tmp[j]+=1
    arr=tmp
    print(arr)
    #n=int(readline())
    #ss=readline().rstrip()
    #yn(1)

    arr=list(map(int, xxx.rstrip().split()))
    n=len(arr)
    print("---")
    tmp=[0]*n
    for i in range(n):
        x = i - arr[i]
        y = i + arr[i] + 1
        if x<0:
            x=0
        if y<n:
            tmp[y]-=1
        tmp[x]+=1
    arr=list(np.cumsum(tmp))
    print(arr)
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

### FIXED CODE ###

### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,k=map(int, readline().rstrip().split())
    arr=list(map(int, readline().rstrip().split()))
    for _ in range(k):
        tmp=[0]*n
        for i in range(n):
            x = i - arr[i]
            y = i + arr[i] + 1
            if x<0:
                x=0
            if y<n:
                tmp[y]-=1
            tmp[x]+=1
        sm=tmp[0]
        arr[0]=tmp[0]
        for i in range(1,n):
            arr[i]=arr[i-1]+tmp[i]
            sm+=abs(tmp[i])
        if tmp[0]==n and sm==n:
            break
    print(" ".join(map(str,arr)))
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------
### BUGGY CODE ###

s = input()
k = int(input())
dic = {}
for i in range(len(s)):
    for j in range(i+1,len(s)+1):
        if s[i:j] in dic:
            continue
        dic[s[i:j]] = 1

print(sorted(dic.keys())[k-1])

### FIXED CODE ###

#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    s = input()
    k = I()
    l = []
    n = len(s)
    alp = list("abcdefghijklmnopqrstuvwxyz")
    for i in alp:
        for j in range(n):
            if s[j] == i:
                l.append(s[j])
                for m in range(j+1,min(j+k,n)):
                    l.append(s[j:m+1])
        l = sorted(list(set(l)))
        if len(l) >= k:break
    print(l[k-1])
#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    C()
### BUGGY CODE ###

N = int(input())
c=list(map(int, input().split()))
c.sort()

count = 0
while count<len(c):
  d = c[count]
  if d in c[count+1:len(c)]:
    s = count
    while d in c:
      c.pop(s)
    continue
  f = 1
  while f<len(c):
    if f == count:
      f+=1
      continue
    if c[f]%c[count]==0:
      c.pop(f)
      continue
    f+=1
  count+=1
print(len(c))


### FIXED CODE ###

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

c.sort()
a = c[N-1]
list_c = [0]*(a+1)
for i in c:
  mal = 2
  b = i
  while i<=a:
    list_c[i]+=1
    i = b*mal
    mal+=1
count = 0
for i in c:
  if list_c[i]==1:
    count+=1
print(count)
### BUGGY CODE ###

N=int(input())
S=input()
R=0

def judge(i,j,k):
  if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
    if j-i != k-j:
      # print(i,j,k, "True")
      return True
  # print(i,j,k, "False")
  return False

for i in range(N):
  for j in range(i,N):
    for k in range(j,N):
      if judge(i,j,k):
        R+=1

print(R)

### FIXED CODE ###

n = int(input())
s = input()
result = 0

r = s.count('R')
g = s.count('G')
b = s.count('B')

result = r * g * b
count = 0

for i in range(n):
    for j in range(i, n):
        k = 2 * j - i
        if j < k < n:
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                count += 1

print(result-count)
### BUGGY CODE ###

from collections import deque
H, W=map(int, input().split())
S=['#'*(W+2)]+['#'+input()+'#' for i in range(H)]+['#'*(W+2)]

def f(data):
  m[data[0]][data[1]]=data[2]
  if m[data[0]-1][data[1]]<0 and S[data[0]-1][data[1]]=='.':
    q.append([data[0]-1, data[1], data[2]+1])
  if m[data[0]+1][data[1]]<0 and S[data[0]+1][data[1]]=='.':
    q.append([data[0]+1, data[1], data[2]+1])
  if m[data[0]][data[1]-1]<0 and S[data[0]][data[1]-1]=='.':
    q.append([data[0], data[1]-1, data[2]+1])
  if m[data[0]][data[1]+1]<0 and S[data[0]][data[1]+1]=='.':
    q.append([data[0], data[1]+1, data[2]+1])

out=0
for x in range(1, H+1):
  for y in range(1, W+1):
    if S[x][y]=='#':
      continue
    q=deque()
    q.append([x, y, 0])
    m=[[-1]*(W+2) for i in range(H+2)]
    while len(q)>0:
      data=q.popleft()
      f(data)
    tmp=max([max(m[i]) for i in range(1, H+1)])
    if tmp>out:
      out=tmp
      
print(out)

### FIXED CODE ###

from collections import deque
import sys
def input():
  return sys.stdin.readline()[:-1]

H, W=map(int, input().split())
S=['#'*(W+2)]+['#'+input()+'#' for i in range(H)]+['#'*(W+2)]

def f(data):
  m[data[0]][data[1]]=data[2]
  if m[data[0]-1][data[1]]==-2 and S[data[0]-1][data[1]]=='.':
    m[data[0]-1][data[1]]=-1
    q.append([data[0]-1, data[1], data[2]+1])
  if m[data[0]+1][data[1]]==-2 and S[data[0]+1][data[1]]=='.':
    m[data[0]+1][data[1]]=-1
    q.append([data[0]+1, data[1], data[2]+1])
  if m[data[0]][data[1]-1]==-2 and S[data[0]][data[1]-1]=='.':
    m[data[0]][data[1]-1]=-1
    q.append([data[0], data[1]-1, data[2]+1])
  if m[data[0]][data[1]+1]==-2 and S[data[0]][data[1]+1]=='.':
    m[data[0]][data[1]+1]=-1
    q.append([data[0], data[1]+1, data[2]+1])

out=0
for x in range(1, H+1):
  for y in range(1, W+1):
    if S[x][y]=='#':
      continue
    q=deque()
    q.append([x, y, 0])
    m=[[-2]*(W+2) for i in range(H+2)]
    while len(q)>0:
      data=q.popleft()
      f(data)
      if data[2]>out:
        out=data[2]
      
print(out)
### BUGGY CODE ###

from copy import deepcopy
H, W = map(int, input().split())
A = [list(input()) for h in range(H)]
ans = 2019
for i in range(2000):
    newA = deepcopy(A)
    count = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] == '#':
                if h > 0:
                    newA[h-1][w] = '#'
                if h < H-1:
                    newA[h+1][w] = '#'
                if w > 0:
                    newA[h][w-1] = '#'
                if w < W-1:
                    newA[h][w+1] = '#'
                count += 1
    A = deepcopy(newA)
    if count == H*W:
        ans = i
        break
print(ans)


### FIXED CODE ###

INF = 10**9
H, W = map(int, input().split())
A = [list(input()) for h in range(H)]
# (0, 0), (0, W-1), (H-1, 0), (H-1, W-1)
ans = [[INF for w in range(W)] for h in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w] == '#':
            ans[h][w] = 0
        tmp = ans[h][w]+1
        if h < H-1:
            ans[h+1][w] = min(tmp, ans[h+1][w])
        if w < W-1:
            ans[h][w+1] = min(tmp, ans[h][w+1])
for h in range(H-1, -1, -1):
    for w in range(W-1, -1, -1):
        tmp = ans[h][w]+1
        if h > 0:
            ans[h-1][w] = min(tmp, ans[h-1][w])
        if w > 0:
            ans[h][w-1] = min(tmp, ans[h][w-1])
# print(ans)
print(max([w for h in ans for w in h]))
### BUGGY CODE ###

"""
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
"""
k = int(input())

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

cnt=0

for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            cnt += gcd(gcd(a,b),c)

print(cnt)


### FIXED CODE ###

"""
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
"""
"""
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a % b)
"""
import math

k = int(input())


cnt=0

for a in range(1, k+1):
    for b in range(1, k+1):
        tmp=math.gcd(a,b)       
        for c in range(1, k+1):
            cnt += math.gcd(tmp,c)

print(cnt)
### BUGGY CODE ###

k = int(input())
cnt = 0
if k % 2 == 0:
    print("-1")
else:
    n = ""
    while True:
        cnt += 1
        n = n + "7"
        if int(n) % k == 0:
            break
        else:
            pass
    print(cnt)

### FIXED CODE ###

K = int(input())
a = [7%K]
for i in range(1, K):
  a.append((a[i-1]*10+7)%K)

isCorrect = False
for i in range(K):
  if a[i] == 0:
    print(i+1)
    isCorrect = True
    break
if not isCorrect:
  print("-1")
### BUGGY CODE ###

from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

for x in range(len(A_s)):
  i=A.count(A_s[x])
  A_count.append(i)

  if 2<=i:
    cnt+=(i*(i-1))//2


for i in range(N):
  x=A_count[A_s.index(A[i])]
  if x==1:
    print(cnt)
  else:
    print(cnt-x+1)

### FIXED CODE ###

import collections
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_c=collections.Counter(A)
cnt=0

for i in A_c.values():
  cnt+=(i*(i-1))//2


for i in A:
  x=A_c[i]
  if x==1:
    print(cnt)
  else:
    print(cnt-x+1)
### BUGGY CODE ###

N = int(input()) 
A=list(map(int,input().split())) 
s=[]
sumall=0
for i in range(N):
    s.append(A.count(i+1))
    sumall=sumall+s[i]
    print(s[i])

### FIXED CODE ###

import collections
n = int(input())
base = map(int,input().split())
c = collections.Counter(base)
for i in range(1,n):
    print(c[i])
print(0)
### BUGGY CODE ###

if __name__ == "__main__":
    H, W, D = list(map(int, (input().split(' '))))
    A = []
    for i in range(H):
        A.append(list(map(int, (input().split(' ')))))
    Q = int(input())
    LR = []
    for i in range(Q):
        LR.append(list(map(int, (input().split(' ')))))

    def check_val(H, W, A, target):
        for i in range(H):
            for j in range(W):
                if target == A[i][j]:
                    x = i
                    y = j
                    return x, y
        return -1, -1

    buf = H * W
    dict_buf = {}
    for i in range(1, buf+1):
        x, y = check_val(H, W, A, i)
        if x != -1 and y != -1:
            dict_buf[str(i)] = [x, y]

    for i in range(Q):
        mp = 0
        stat_LR = LR[i]
        if stat_LR[0] == stat_LR[1]:
            print(mp)
            continue
#        stat_x, stat_y = check_val(H, W, A, stat_LR[0])
        stat_x, stat_y = dict_buf[str(stat_LR[0])]
        next = stat_LR[0]
        while next != stat_LR[1]:
            next += D
#            x, y = check_val(H, W, A, next)
            x, y = dict_buf[str(next)]
            mp += abs(x - stat_x) + abs(y - stat_y)
            stat_x = x
            stat_y = y
        print(mp)

### FIXED CODE ###

if __name__ == "__main__":
    H, W, D = list(map(int, (input().split(' '))))
    A = []
    for i in range(H):
        A.append(list(map(int, (input().split(' ')))))
    Q = int(input())
    LR = []
    for i in range(Q):
        LR.append(list(map(int, (input().split(' ')))))

#    def check_val(H, W, A, target):
#        for i in range(H):
#            for j in range(W):
#                if target == A[i][j]:
#                    x = i
#                    y = j
#                    return x, y
#        return -1, -1

#    check_list = []
#    for i in range(Q):
#        stat_LR = LR[i]
#        for j in range(stat_LR[0], stat_LR[1]+D, D):
#            check_list.append(j)
#    check_list = list(set(check_list))

#    buf = H * W
    dict_buf = {}
#    for i in check_list:
#        x, y = check_val(H, W, A, i)
#        if x != -1 and y != -1:
#            dict_buf[str(i)] = [x, y]
    for i in range(H):
        for j in range(W):
            dict_buf[str(A[i][j])] = [i, j]

    d = {}
    for i in range(1, D+1):
        d[str(i)] = 0
    for i in range(D+1, H*W+1):
        d[str(i)] = d[str(i-D)] + \
                    abs(dict_buf[str(i)][0] - dict_buf[str(i-D)][0]) + \
                    abs(dict_buf[str(i)][1] - dict_buf[str(i-D)][1])

    for i in range(Q):
#        mp = 0
#        stat_LR = LR[i]
#        if stat_LR[0] == stat_LR[1]:
#            print(mp)
#            continue
#        stat_x, stat_y = dict_buf[str(stat_LR[0])]
#        next = stat_LR[0]
#        while next != stat_LR[1]:
#            next += D
#            x, y = dict_buf[str(next)]
#            mp += abs(x - stat_x) + abs(y - stat_y)
#            stat_x = x
#            stat_y = y
#        print(mp)
        print(d[str(LR[i][1])] - d[str(LR[i][0])])
### BUGGY CODE ###

#ABC120C-Unification.py

from sys import stdin

input = stdin.readline

S = input().rstrip()

Nblock = 0

while '0' in S and '1' in S:
    s = S.split('10')
    Nblock += (len(s)-1)*2
    S = ''.join(s)
    t = S.split('01')
    Nblock += (len(t)-1)*2
    S = ''.join(t)


print(Nblock)


### FIXED CODE ###

#ABC120C-Unification.py

from sys import stdin

input = stdin.readline

S = input().rstrip()

Nblock = min(S.count('1'), S.count('0'))

print(Nblock*2)