Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
//インクルード
#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//イテレータあるけど順序保持しないmap
#include<unordered_set>//イテレータあるけど順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列

using namespace std;
typedef long long ll;

//マクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

signed main(){
    ll n,k;cin >> n >> k;
    const ll nod=40;//桁数
    vector< ll > a(n);REP(i,n)cin >> a[i];
    vector< pair<ll,ll> > ans(nod);
    REP(i,nod){
        ll ans_sub1=0;ll ans_sub2=0;
        REP(j,n){if(a[j]>>i&1){ans_sub1+=1;}else{ans_sub2+=1;}}
        ans[i]=MP(ans_sub1,ans_sub2);
    }
    //REP(i,nod)cout << ans[i].F << " " << ans[i].S << endl;
    ll reans=0;
    ll x=0;
    REPD(i,nod){
        if(ans[i].F>=ans[i].S){
            reans+=ans[i].F*pow(2,i);
        }else{
            x+=pow(2,i);
            if(x>k){
                reans+=ans[i].F*pow(2,i);
                x-=pow(2,i);
            }else{
                reans+=ans[i].S*pow(2,i);
            }
        }
    }
    //cout << x << endl;
    cout << reans << endl;
}

Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
co=[0]*50
check=False
for i in range(49,-1,-1):
    for j in range(n):
        if (a[j]>>i)&1:
            co[i]+=1
    if (k>>i)&1:
        if co[i]>=n-co[i]:
            ans+=(co[i]*(2**i))
            check=True
        else:
            ans+=((n-co[i])*(2**i))
    else:
        if check:
            if co[i]>=n-co[i]:
                ans+=(co[i]*(2**i))
            else:
                ans+=((n-co[i])*(2**i))
        else:
            ans+=(co[i]*(2**i))
print(ans)
Buggy python submission (verdict: Runtime Error):
d=int(input())
if d=25:
    print("Christmas")
elif d=24:
    print("Christmas Eve")
elif d=23:
    print("Christmas Eve Eve")
else:
    print("Christmas Eve Eve Eve")


Fixed submission:
d=int(input())
if d==25:
    print("Christmas")
elif d==24:
    print("Christmas Eve")
elif d==23:
    print("Christmas Eve Eve")
else:
    print("Christmas Eve Eve Eve")
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())

for i in range(m//n,0,-1):
    if m%i==0:
        print(i)
        break


Fixed submission:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors
n,m=map(int,input().split())
ans=1
for i in make_divisors(m):
    if i>=n:
        ans=max(ans,m//i)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())

for i in range(m//n,0,-1):
    if m%i==0:
        print(i)
        break


Fixed submission:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors
n,m=map(int,input().split())
ans=1
for i in make_divisors(m):
    if i>=n:
        ans=max(ans,m//i)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())

for i in range(m//n,0,-1):
    if m%i==0 and m//i>=n:
        print(i)
        break


Fixed submission:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors
n,m=map(int,input().split())
ans=1
for i in make_divisors(m):
    if i>=n:
        ans=max(ans,m//i)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n,x=map(int,input().split())
y=sorted([abs(x-int(i)) for i in input().split()])

l=y[0]
for i in range(l):
    k=True
    for j in range(n):
        if y[j]%(l-i)!=0:
            k=False
            break
    if k:
        print(l-i)
        break


Fixed submission:
from fractions import gcd
n,X=map(int,input().split())
x=list(map(int,input().split()))

def hoge():
    global n,x,X
    ret=abs(X-x[0])
    for i in range(1,n):
        ret=gcd(abs(X-x[i]),ret)
    return ret

print(hoge())

Buggy python submission (verdict: Time Limit Exceeded):
n,x=map(int,input().split())
y=sorted([abs(x-int(i)) for i in input().split()])

def True_array(s,k):
    for i in s:
        if i%k!=0:
            return False
    return True

l=y[0]
for i in range(l):
    if True_array(y,l-i):
        print(l-i)
        break


Fixed submission:
from fractions import gcd
n,X=map(int,input().split())
x=list(map(int,input().split()))

def hoge():
    global n,x,X
    ret=abs(X-x[0])
    for i in range(1,n):
        ret=gcd(abs(X-x[i]),ret)
    return ret

print(hoge())
Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
print(n%k==0:
    print(0)
else:
    print(1)


Fixed submission:
n,k=map(int,input().split())
if n%k==0:
    print(0)
else:
    print(1)
Buggy python submission (verdict: Runtime Error):
n=int(input())
s=input().split()
a=sorted([int(s[i])-i-1 for i in range(n)])

if n==0:
    print(0)
else:
    #それぞれの間で考える
    mi=[]
    for i in range(n-1):
        c,d=a[i],a[i+1]
        m=0
        for k in range(n):
            if k<=i:
                m-=a[k]
            else:
                m+=a[k]
        l=(i+1)-(n-i-1)
        if l>0:
            m+=(l*a[i+1])
        else:
            m+=(l*a[i])
        mi.append(m)
    print(min(mi))


Fixed submission:
#あー統計のやつか、絶対値の和の最小値は平均の値
n=int(input())
s=input().split()
a=sorted([int(s[i])-i-1 for i in range(n)])
#ここまでの発想は流石にわかる
c=0
for i in range(n):
    if i<=n//2:
        c+=(a[n//2]-a[i])
    else:
        c+=(a[i]-a[n//2])
print(c)



'''
if n==1:
    print(0)
else:
    mi=[]
    h=sum(a)
    k=h
    for i in range(n-1):
        c,d=a[i],a[i+1]
        k-=2*a[i]
        m=k
        l=(i+1)-(n-i-1)
        if l>0:
            m+=(l*a[i+1])
        else:
            m+=(l*a[i])
        mi.append(m)
    mi.append(h-a[0]*n)
    mi.append(a[-1]*n-h)
    print(min(mi))
'''
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
print(max([sum(sorted([i*l[0]+j*l[1]+k*l[2] for l in [list(map(int,input().split())) for i in range(n)]],reverse=True)[:m]) for i in [-1,1] for j in [-1,1] for k in [-1,1]]))

Fixed submission:
n,m=map(int,input().split())
xyz=[list(map(int,input().split())) for i in range(n)]
ans=[]
for i in [-1,1]:
    for j in [-1,1]:
        for k in [-1,1]:
            ans.append(sum(sorted([i*l[0]+j*l[1]+k*l[2] for l in xyz],reverse=True)[:m]))
print(max(ans))
Buggy python submission (verdict: Time Limit Exceeded):
#しゃくとり法忘れてた
n=int(input())
a=list(map(int,input().split()))
ans=0
right=0
now=[0]*20#2^20なので
for i in range(n):
    left=i
    if i!=0:
        for j in range(20):
            if (a[left-1]>>j)&1:
                now[j]-=1
    else:
        for j in range(20):
            if (a[left]>>j)&1:
                now[j]+=1
    while all([now[j]<=1 for j in range(20)]):
        #print(100000000)
        right+=1
        if right==n:
            break
        for k in range(20):
            if (a[right]>>k)&1:
                now[k]+=1
    else:
        #print(right)
        for k in range(20):
            if (a[right]>>k)&1:
                now[k]-=1
    right-=1
    ans+=(right-left+1)
    #print(left,right)
print(ans)

Fixed submission:
#しゃくとり法忘れてた
n=int(input())
a=list(map(int,input().split()))
ans=0
right=0
now=[0]*20#2^20なので
for i in range(n):
    left=i
    if i!=0:
        for j in range(20):
            if (a[left-1]>>j)&1:
                now[j]-=1
    else:
        for j in range(20):
            if (a[left]>>j)&1:
                now[j]+=1
    while all([now[j]<=1 for j in range(20)]):
        #print(100000000)
        right+=1
        if right==n:
            break
        for k in range(20):
            if (a[right]>>k)&1:
                now[k]+=1
    else:
        #print(right)
        for k in range(20):
            if (a[right]>>k)&1:
                now[k]-=1
    right-=1
    ans+=(right-left+1)
    #print(left,right)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
    left,right=i,i
    now=[0]*20#2^20なので
    for j in range(20):
        if (a[left]>>j)&1:
            now[j]=1
    #print(now)
    while all([now[j]<=1 for j in range(20)]):
        #print(100000000)
        right+=1
        if right==n:
            break
        for k in range(20):
            if (a[right]>>k)&1:
                now[k]+=1
    ans+=(right-left)
    #print(left,right)
print(ans)

Fixed submission:
#しゃくとり法忘れてた
n=int(input())
a=list(map(int,input().split()))
ans=0
right=0
now=[0]*20#2^20なので
for i in range(n):
    left=i
    if i!=0:
        for j in range(20):
            if (a[left-1]>>j)&1:
                now[j]-=1
    else:
        for j in range(20):
            if (a[left]>>j)&1:
                now[j]+=1
    while all([now[j]<=1 for j in range(20)]):
        #print(100000000)
        right+=1
        if right==n:
            break
        for k in range(20):
            if (a[right]>>k)&1:
                now[k]+=1
    else:
        #print(right)
        for k in range(20):
            if (a[right]>>k)&1:
                now[k]-=1
    right-=1
    ans+=(right-left+1)
    #print(left,right)
print(ans)
Buggy python submission (verdict: Runtime Error):
import math
x=int(input())
ans=[1]
for i in range(2,x+1):
    #print(i)
    k=math.floor(math.log(x,i)))
    ans.append(i**(k+1))
print(max(ans))
#print(ans)


Fixed submission:
import math
x=int(input())
if x<4:
    print(1)
else:
    k=0
    for i in range(int(math.sqrt(x))-1):
        l=0
        for j in range(x):
            if (i+2)**j>x:
                break
            else:
                l=(i+2)**j
        if l>k:
            k=l
    print(k)

Buggy python submission (verdict: Runtime Error):
import math
x=int(input())
ans=[1]
for i in range(2,x+1):
    #print(i)
    k=math.floor(math.ceil(x,i))
    ans.append(i**k)
print(max(ans))
#print(ans)


Fixed submission:
import math
x=int(input())
if x<4:
    print(1)
else:
    k=0
    for i in range(int(math.sqrt(x))-1):
        l=0
        for j in range(x):
            if (i+2)**j>x:
                break
            else:
                l=(i+2)**j
        if l>k:
            k=l
    print(k)
Buggy python submission (verdict: Time Limit Exceeded):
s=input()
l=len(s)
k=int(input())
alp=[chr(i) for i in range(97, 97+26)]
ans=set()
for i in range(26):
    for j in range(l):
        if s[j]==alp[i]:
            for m in range(j,l):
                ans.add(s[j:m+1])
    if len(ans)>=k:
        break
print(sorted(list(ans))[k-1])

Fixed submission:
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,min(l,k)+1):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])

Buggy python submission (verdict: Runtime Error):
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,k):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])


Fixed submission:
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,min(l,k)+1):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])

Buggy python submission (verdict: Runtime Error):
#include<iostream>
#include<string>
#include<set>
using namespace std;

int main(){
  string s;cin >> s;
  int l=(int)s.size();
  int k;cin >> k;
  set<string> x;
  for(int i=1;i<=l;i++){
    for(int j=0;j<l;j++){
      x.insert(s.substr(j,i));
    }
  }
  int a=0;
  for(auto itr=x.begin();itr!=x.end();itr++){
    a++;
    if(a==k){cout << *itr << endl;break;}
  }

}


Fixed submission:
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,min(l,k)+1):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,l+1):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])


Fixed submission:
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,min(l,k)+1):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])
Buggy python submission (verdict: Runtime Error):
from itertools import accumulate
n,c=map(int,input().split())
sushi=[list(map(int,input().split())) for i in range(n)]

r=[sushi[0][1]-sushi[0][0]]
for i in range(1,n):
    r.append(r[-1]+sushi[i][1]-(sushi[i][0]-sushi[i-1][0]))
l=[sushi[n-1][1]-(c-sushi[n-1][0])]
for i in range(n-2,-1,-1):
    l.append(l[-1]+sushi[i][1]-((c-sushi[i][0])-(c-sushi[i+1][0])))
r2=[sushi[0][1]-2*sushi[0][0]]
for i in range(1,n):
    r2.append(r2[-1]+sushi[i][1]-2*(sushi[i][0]-sushi[i-1][0]))
l2=[sushi[n-1][1]-2*(c-sushi[n-1][0])]
for i in range(n-2,-1,-1):
    l2.append(l2[-1]+sushi[i][1]-2*((c-sushi[i][0])-(c-sushi[i+1][0])))
r=list(accumulate(r,max))
l=list(accumulate(l,max))

cand=[l[-1],r[-1]]
for i in range(n):
    if 0<=n-i-2<=n-1:
        cand.append(l2[i]+r[n-i-2])
        cand.append(r2[i]+l[n-i-2])

print(max(max(cand),0))


Fixed submission:
from itertools import accumulate
n,c=map(int,input().split())
sushi=[list(map(int,input().split())) for i in range(n)]

r=[sushi[0][1]-sushi[0][0]]
for i in range(1,n):
    r.append(r[-1]+sushi[i][1]-(sushi[i][0]-sushi[i-1][0]))
l=[sushi[n-1][1]-(c-sushi[n-1][0])]
for i in range(n-2,-1,-1):
    l.append(l[-1]+sushi[i][1]-((c-sushi[i][0])-(c-sushi[i+1][0])))
r2=[sushi[0][1]-2*sushi[0][0]]
for i in range(1,n):
    r2.append(r2[-1]+sushi[i][1]-2*(sushi[i][0]-sushi[i-1][0]))
l2=[sushi[n-1][1]-2*(c-sushi[n-1][0])]
for i in range(n-2,-1,-1):
    l2.append(l2[-1]+sushi[i][1]-2*((c-sushi[i][0])-(c-sushi[i+1][0])))
r=list(accumulate(r,max))
l=list(accumulate(l,max))

cand=[l[-1],r[-1]]
for i in range(n):
    if 0<=n-i-2<=n-1:
        cand.append(l2[i]+r[n-i-2])
        cand.append(r2[i]+l[n-i-2])

print(max(max(cand),0))
Buggy python submission (verdict: Time Limit Exceeded):
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n=int(input())
a=sorted(list(map(int,input().split())))
b=a[n-1]
a.pop(n-1)
ans=[0,comb(b,a[0])]
for i in range(n-1):
    #print(a[i])
    if ans[1]<comb(b,a[i]):
        ans=[i,comb(b,a[i])]
print(str(b)+" "+str(a[ans[0]]))


Fixed submission:
import math

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

k=int(input())
a=sorted([int(i) for i in input().split()])
ma=a[-1]/2
#print(a)
eps=10**(-7)

m=a[-1]-ma
for i in range(k):
    m=min(abs(a[i]-ma),m)
print(a[-1],end=" ")
for i in range(k):
    if abs(abs(a[i]-ma)-m)<eps:
        print(a[i])
        break
Buggy python submission (verdict: Time Limit Exceeded):
a,b,k=input().split()
a,b,k=int(a),int(b),int(k)

for i in range(b-a+1):
    if i<k:
        print(a+i)
    elif i>b-a-k:
        print(a+i)


Fixed submission:
#from collections import deque
a,b,k=input().split()
a,b,k=int(a),int(b),int(k)

#多分両端からアクセスしないとTLEになる
'''
for i in range(b-a+1):
    if i<k:
        print(a+i)
    elif i>b-a-k:
        print(a+i)
'''

d=[]
if a+(k-1)>b:
    k=b-a+1
for i in range(k):
        d.append(a+i)
        d.append(b-i)
#辞書もsort可能
for i in sorted(set(d)):
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k=input().split()
a,b,k=int(a),int(b),int(k)

for i in range(b-a+1):
    if i<k:
        print(a+i)
    elif i>b-a-k:
        print(a+i)


Fixed submission:
#from collections import deque
a,b,k=input().split()
a,b,k=int(a),int(b),int(k)

#多分両端からアクセスしないとTLEになる
'''
for i in range(b-a+1):
    if i<k:
        print(a+i)
    elif i>b-a-k:
        print(a+i)
'''

d=[]
if a+(k-1)>b:
    k=b-a+1
for i in range(k):
        d.append(a+i)
        d.append(b-i)
#辞書もsort可能
for i in sorted(set(d)):
    print(i)
Buggy python submission (verdict: Runtime Error):
a,b,c=map(int,input().split())
a,b,c=sorted([a+(a%2==b%2 xor a%2==c%2),b+(b%2==a%2 xor b%2==c%2),c+(c%2==a%2 xor c%2==b%2)])
print((c-a)//2+(c-b)//2+(a%2!=b%2 or b%2!=c%2))

Fixed submission:
a,b,c=map(int,input().split())
if a%2==b%2 and b%2==c%2:
    x=sorted([a,b,c])
    print((x[2]-x[1])//2+(x[2]-x[0])//2)
elif a%2==b%2:
    a+=1
    b+=1
    x=sorted([a,b,c])
    print((x[2]-x[1])//2+(x[2]-x[0])//2+1)
elif b%2==c%2:
    b+=1
    c+=1
    x=sorted([a,b,c])
    print((x[2]-x[1])//2+(x[2]-x[0])//2+1)
else:
    c+=1
    a+=1
    x=sorted([a,b,c])
    print((x[2]-x[1])//2+(x[2]-x[0])//2+1)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
b=[0]
b.extend(a)
b.append(0)
base=0
for i in range(1,n+2):
    base+=abs(a[i]-a[i-1])
ans=[]
for i in range(1,n+1):
    ans.append(base+abs(a[i+1]-a[i-1])-abs(a[i+1]-a[i])-abs(a[i]-a[i-1]))
for i in range(n):
    print(ans[i])

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
base=0
for i in range(n+1):
    if i==0:
        base+=abs(a[0])
    elif i==n:
        base+=abs(a[n-1])
    else:
        base+=abs(a[i]-a[i-1])
ans=[]
for i in range(n):
    if i==0:
        ans.append(base+abs(a[1])-abs(a[0])-abs(a[1]-a[0]))
    elif i==n-1:
        ans.append(base+abs(a[n-2])-abs(a[n-1])-abs(a[n-2]-a[n-1]))
    else:
        ans.append(base+abs(a[i+1]-a[i-1])-abs(a[i+1]-a[i])-abs(a[i]-a[i-1]))
for i in range(n):
    print(ans[i])

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
b=[0]
b.extend(a)
b.append(0)
base=0
for i in range(n+1):
    base+=abs(a[i]-a[i-1])
ans=[]
for i in range(1,n+1):
    ans.append(base+abs(a[i+1]-a[i-1])-abs(a[i+1]-a[i])-abs(a[i]-a[i-1]))
for i in range(n):
    print(ans[i])

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
base=0
for i in range(n+1):
    if i==0:
        base+=abs(a[0])
    elif i==n:
        base+=abs(a[n-1])
    else:
        base+=abs(a[i]-a[i-1])
ans=[]
for i in range(n):
    if i==0:
        ans.append(base+abs(a[1])-abs(a[0])-abs(a[1]-a[0]))
    elif i==n-1:
        ans.append(base+abs(a[n-2])-abs(a[n-1])-abs(a[n-2]-a[n-1]))
    else:
        ans.append(base+abs(a[i+1]-a[i-1])-abs(a[i+1]-a[i])-abs(a[i]-a[i-1]))
for i in range(n):
    print(ans[i])

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
b=[0]
b.extend(a)
b.append(0)
base=0
for i in range(n+1):
    base+=abs(a[i]-a[i-1])
ans=[]
for i in range(1,n+1):
    ans.append(base+abs(a[i+1]-a[i-1])-abs(a[i+1]-a[i])-abs(a[i]-a[i-1]))
for i in range(n):
    print(ans[i])

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
base=0
for i in range(n+1):
    if i==0:
        base+=abs(a[0])
    elif i==n:
        base+=abs(a[n-1])
    else:
        base+=abs(a[i]-a[i-1])
ans=[]
for i in range(n):
    if i==0:
        ans.append(base+abs(a[1])-abs(a[0])-abs(a[1]-a[0]))
    elif i==n-1:
        ans.append(base+abs(a[n-2])-abs(a[n-1])-abs(a[n-2]-a[n-1]))
    else:
        ans.append(base+abs(a[i+1]-a[i-1])-abs(a[i+1]-a[i])-abs(a[i]-a[i-1]))
for i in range(n):
    print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
x=[]

def find_element(k):
    global h,w
    for i in range(h):
        for j in range(w):
            if a[i][j]==k:
                return (i,j)

def make_distance(S,T):
    return abs(S[0]-T[0])+abs(S[1]-T[1])

for i in range(1,d+1):
    l=(h*w-i)//d
    x.append([0]*(l+1))
for i in range(1,d+1):
    l=len(x[i-1])
    for j in range(l-1):
        s=find_element(i+d*j)
        t=find_element(i+d*(j+1))
        x[i-1][j+1]=x[i-1][j]+make_distance(s,t)

q=int(input())
for i in range(q):
    l,r=map(int,input().split())
    p=l%d
    if p==0:
        v1=l//d-1
        v2=r//d-1
        print(x[d-1][v2]-x[d-1][v1])
    else:
        v1=l//d
        v2=r//d
        print(x[p-1][v2]-x[p-1][v1])
#print(x)


Fixed submission:
h,w,d=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]

def find_element(k):
    global h,w,a
    for i in range(h):
        for j in range(w):
            if a[i][j]==k:
                return (i,j)

def make_distance(S,T):
    return abs(S[0]-T[0])+abs(S[1]-T[1])

x1=[]
x2=[]
for i in range(1,d+1):
    l=(h*w-i)//d
    x1.append([0]*(l+1))
    x2.append([[0,0]for i in range(l+1)])

for i in range(h):
    for j in range(w):
        p=a[i][j]%d
        if p==0:
            p=d-1
        else:
            p-=1
        l=(a[i][j]-(p+1))//d
        x2[p][l]=[i,j]


for i in range(1,d+1):
    l=len(x1[i-1])
    for j in range(l-1):
        x1[i-1][j+1]=x1[i-1][j]+make_distance(x2[i-1][j],x2[i-1][j+1])

q=int(input())
for i in range(q):
    l,r=map(int,input().split())
    p=l%d
    if p==0:
        v1=l//d-1
        v2=r//d-1
        print(x1[d-1][v2]-x1[d-1][v1])
    else:
        v1=l//d
        v2=r//d
        print(x1[p-1][v2]-x1[p-1][v1])
#print(x)
Buggy python submission (verdict: Runtime Error):
s="".join(sorted(list(input())))
t="".join(sorted(list(input()),reverse=True))


if len(s)<len(t) and s==t[:len(s)]:
    print("Yes")
else:
    for i in range(len(s)):
        if s[i]!=t[i]:
            if s[i]<t[i]:
                print("Yes")
            else:
                print("No")
            break
        if i==len(s)-1:
            print("No")


Fixed submission:
s="".join(sorted(list(input())))
t="".join(sorted(list(input()),reverse=True))


if len(s)<len(t) and s==t[:len(s)]:
    print("Yes")
else:
    for i in range(min([len(s),len(t)])):
        if s[i]!=t[i]:
            if s[i]<t[i]:
                print("Yes")
            else:
                print("No")
            break
        if i==min([len(s),len(t)])-1:
            print("No")
Buggy python submission (verdict: Runtime Error):
from functools import reduce
from math import gcd
n=int(input())
a=list(map(int,input().split()))
x=reduce(gcd,a)
ans=0

while x%2==0:
    ans+=1
    x//=2
print(ans)


Fixed submission:
n=int(input())
c=32
for i in input().split():
    d=0
    z=int(i)
    while z%2==0:
        d+=1
        z=z//2
    if d<c:
        c=d

print(c)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ans=0

while all(i%2==0 for i in a):
    ans+=1
    for j in range(n):
        a[i]//=2
print(ans)


Fixed submission:
n=int(input())
c=32
for i in input().split():
    d=0
    z=int(i)
    while z%2==0:
        d+=1
        z=z//2
    if d<c:
        c=d

print(c)

Buggy python submission (verdict: Runtime Error):
from functools import reduce
from math import gcd
n=int(input())
a=list(map(int,input().split()))
x=reduce(gcd,a)
ans=0

while x%2==0:
    ans+=1
    x//=2
print(ans)


Fixed submission:
n=int(input())
c=32
for i in input().split():
    d=0
    z=int(i)
    while z%2==0:
        d+=1
        z=z//2
    if d<c:
        c=d

print(c)