Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
S=raw_input()

L=len(S)

ans=L/2
for r in range(L/2,L):
	C=Counter(S[L-r-1:r+1])
	if len(C)==1:
		ans=r+1

print ans

Fixed submission:
S=raw_input()
N=len(S)
TS=N
for k in range(N-1):
	if S[k]!=S[k+1]:
		TS=min(TS, max(k+1, N-k-1))

print TS

Buggy python submission (verdict: Runtime Error):
S=raw_input()
N=len(S)
n=len(S)/2
if N%2==1: c=n	# c:center

cnt=0
for i in range(1,n+1):
	if N%2==1:
		if S[c-i]==S[c+i]==S[c]:
			cnt=1+2*i
			pass
		else:
			if i==n:
				cnt+=1
			break
	else:	
		if S[n-i]==S[n+i]:
			cnt=2*n
		else:
			break
if N%2==1:
	print max(n+1,cnt)
else:
	print max(n, cnt)



Fixed submission:
S=raw_input()
N=len(S)
TS=N
for k in range(N-1):
	if S[k]!=S[k+1]:
		TS=min(TS, max(k+1, N-k-1))

print TS

Buggy python submission (verdict: Runtime Error):
S=raw_input()
c=[]
cnt=0
for i in S:
	if i=="0":
		cnt+=1
	else:
		if cnt!=0:
			c.append(cnt)
			cnt=0
else:
	if cnt!=0:
		c.append(cnt)

if len(c)==1:
	print c[0]
else:
	print max(c)+1
			



Fixed submission:
S=raw_input()
N=len(S)
TS=N
for k in range(N-1):
	if S[k]!=S[k+1]:
		TS=min(TS, max(k+1, N-k-1))

print TS
Buggy python submission (verdict: Runtime Error):
import heapq
N=input()
#print N
a=[]
a=map(int,raw_input().split())
#print a

q1=[] #+
q2=[] #-
for ind,i in enumerate(a):
	if i>=0:
		heapq.heappush(q1, [-1*i,ind])
	else:
		heapq.heappush(q2, [i,ind])
#print "# q1"	
#print q1
#print "# q2"
#print q2

cnt=0
b=[]
for ind,i in enumerate(a):
	if ind==0:
		prev_i=i
		continue
	else:
		if prev_i<=i:
			prev_i=i
			continue
		else:
			#print ind,prev_i,i
			#while prev_i>i:
			if i<0 and prev_i<0:
				t_val=q2[0][0]
				#prev_i+=t_val
				cnt=cnt+(abs(i-prev_i)/t_val)+1
				#print (abs(i-prev_i)/t_val),cnt
				for i in range( (abs(i-prev_i)/t_val)+1 ):
					b.append((q2[0][1]+1,ind))
			else:
				t_val=q1[0][0]
				#i-=t_val
				cnt=cnt+(abs(i-prev_i)/t_val)+1
				#print cnt
				for i in range( (abs(i-prev_i)/t_val)+1 ):
					b.append((q1[0][1]+1,ind+1))

	

print cnt
#print b
for i,j in b:
	print i,j

	

Fixed submission:
import sys
N=input()
#print N
a=[]
a=map(int,raw_input().split())
#print a

if max(a)==min(a):
	print 0
	sys.exit()
elif abs(max(a))>abs(min(a)):
	num=(max(a),a.index(max(a)))
else:
	num=(min(a),a.index(min(a)))
#print num

ans=[]
for i in range(N):
	a[i]+=num[0]
	ans.append((num[1]+1,i+1))

if num[0]>=0:
	for i in range(N-1):
		ans.append((i+1,i+2))
else:
	for i in range(N,1,-1):
		ans.append((i,i-1))

print len(ans)
for i,j in ans:
	print i,j
Buggy python submission (verdict: Time Limit Exceeded):
import collections
N,C=map(int,raw_input().split())
#print N,C
d = collections.defaultdict(list)
start=float("inf")
end=0
for i in range(N):
	s,t,c=map(int,raw_input().split())
	d[c].append((s,t))
	start=min(start,s)
	end=max(end,t)

#print start,end
#print d
[d[i].sort() for i in d.keys()]
#print d

c = collections.defaultdict(list)

for i in range(start,end):
	#print "# i"
	#print i
	cnt=0
	for j in d:
		for k in d[j]:
			if i>=k[0] and i<=k[1]:
				#print j,"Yes"
				c[i].append(j)
				break;

#print c

max_recs=0
for i in c.values():
	max_recs=max(max_recs,len(i))

print max_recs

	
	

Fixed submission:
import collections
N,C=map(int,raw_input().split())
a=collections.defaultdict(list)

max_t=0
for i in range(N):
	s,t,c=map(int,raw_input().split())
	a[c].append((s,t))
	max_t=max(max_t,t)

[a[i].sort() for i in a.keys()]
#print a
#print max_t

b=collections.defaultdict(list)


for i,j in a.items():
	start=0
	end=0
	for k in j:
		if start==0 and end==0:
			start=k[0]-1
			end=k[1]
			continue
		elif k[0]<=end:
			end=k[1]
		else:
			b[i].append((start,end))
			start=k[0]-1
			end=k[1]
	else:
		b[i].append((start,end))		

#print "# b"
#print b

c=[0 for i in range(max_t+2)]

for i in b.values():
	for j,k in i:
		#print j,1,k+1,-1
		c[j]+=1
		c[k]-=1
#print c

ans=0
sum=0
for i in c:
	sum+=i
	ans=max(ans,sum)

print ans
Buggy python submission (verdict: Runtime Error):
import sys
S=list(raw_input())
if len(S)>10:
	print "NO"
	sys.exit()

if S.index("K")+1!=S.index("I") or S.index("I")+1!=S.index("H"):
	print "NO"
	sys.exit()

for i in S:
	if i=="A":
		S.remove("A")


if ''.join(S)=="KIHBR":
	print "YES"
else:
	print "NO"


Fixed submission:
import sys
S=list(raw_input())
if len(S)>10:
	print "NO"
	sys.exit()

if "K" in S and "I" in S and "H" in S:
	pass
else:
	print "NO"
	sys.exit()
	
if S.index("K")+1!=S.index("I") or S.index("I")+1!=S.index("H"):
	print "NO"
	sys.exit()

for i in S:
	if i=="A":
		S.remove("A")


if ''.join(S)=="KIHBR":
	print "YES"
else:
	print "NO"
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
import heapq

H,W=map(int,raw_input().split())

#隣接リスト
c=[[] for i in range(10)]
for i in range(10):
	a=map(int,raw_input().split())
	for j in range(10):
		c[i].append(a[j])

#print c

A=[]
for i in range(H):
	A.append(map(int, raw_input().split()))
#print A


def djk(n):
	#開始位置
	Start=n

	#初期化
	d=[-1 for i in range(10)]
	d[Start]=0


	#優先度付きキューに[距離,頂点の番号]を格納する
	#スタート地点の頂点1には距離0を入れる
	q=[]
	heapq.heappush(q, [0,Start])

	while len(q)!=0:
		#距離が最小の頂点を取り出し
		l,u=heapq.heappop(q)
		for i,val in enumerate(c[u]):
			v=i
			l_u_v=val
			#print v, l_u_v
			if (d[v]>d[u]+l_u_v) or d[v]==-1:
				d[v]=d[u]+l_u_v
				heapq.heappush(q, [d[v],v])
	return d[1]


sum=0
for i in range(H):
	for j in range(W):
		if A[i][j]==-1 or A[i][j]==1:
			continue
		else:
			sum+=djk(A[i][j])

print sum


Fixed submission:
# -*- coding: utf-8 -*-
import sys
import heapq

H,W=map(int,raw_input().split())

#隣接リスト
c=[[] for i in range(10)]
for i in range(10):
	a=map(int,raw_input().split())
	for j in range(10):
		c[i].append(a[j])

#print c

A=[]
for i in range(H):
	A.append(map(int, raw_input().split()))
#print A


def djk(n):
	#開始位置
	Start=n

	#初期化
	d=[-1 for i in range(10)]
	d[Start]=0


	#優先度付きキューに[距離,頂点の番号]を格納する
	#スタート地点の頂点1には距離0を入れる
	q=[]
	heapq.heappush(q, [0,Start])

	while len(q)!=0:
		#距離が最小の頂点を取り出し
		l,u=heapq.heappop(q)
		for i,val in enumerate(c[u]):
			v=i
			l_u_v=val
			#print v, l_u_v
			if (d[v]>d[u]+l_u_v) or d[v]==-1:
				d[v]=d[u]+l_u_v
				heapq.heappush(q, [d[v],v])
	return d[1]


sum=0
temp=[0 for i in range(10)]
for i in range(H):
	for j in range(W):
		if A[i][j]==-1 or A[i][j]==1:
			continue
		else:
			if temp[A[i][j]]==0:
				temp[A[i][j]]=djk(A[i][j])
			sum+=temp[A[i][j]]

print sum
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000000)

N,X,Y=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

memo={}
def func(n,last,turn):  #n:カードの残り枚数、last:前の手番が最後に引いたスコア、turn: 0先手の番 1後手の番
    #print "func(",n,",",last,",",turn,")"
    if (n,turn) in memo:    #カードの残り枚数と、どちらの手番かで最終スコアはいつも同じ
        return memo[(n,turn)]
    if n==1:
        ret=abs(last-A[N-n])
    elif turn==0:
        ret=max( func(n-1,last,0) , func(n-1,A[N-n],1) ) #手番がXのまま  手番がYに移動
    elif turn==1:
        ret=min( func(n-1,last,1) , func(n-1,A[N-n],0) ) #手番がYのまま  手番がXに移動
    memo[(n,turn)]=ret
    return ret


print func(N,Y,0)

Fixed submission:
X,Y=raw_input().split()
if X<Y:
	print "<"
elif X>Y:
	print ">"
elif X==Y:
	print "="
Buggy python submission (verdict: Time Limit Exceeded):
N=input()

A=list(map(int,raw_input().split()))
B=list(map(int,raw_input().split()))
C=list(map(int,raw_input().split()))
A.sort()
B.sort()
C.sort()
#print A
#print B
#print C

sum=0

for i in range(N):
	j_start=0
	for j in range(N):
		if B[j]<=A[i]:
			continue
		for k in range(j_start,N):
			#print "# loop C"
			#print A[i],B[j],C[k],N-k
			if C[k]>B[j]:
				#print A[i],B[j],C[k],N-k
				sum+=(N-k)
				j_start=k
				break

print sum
			



Fixed submission:
import bisect
N=input()

A=list(map(int,raw_input().split()))
B=list(map(int,raw_input().split()))
C=list(map(int,raw_input().split()))
A.sort()
B.sort()
C.sort()

sum=0
for val_b in B:
	sum+=bisect.bisect_left(A, val_b)*( N-bisect.bisect_right(C, val_b) )
			
print sum

Buggy python submission (verdict: Time Limit Exceeded):
N=input()

A=list(map(int,raw_input().split()))
B=list(map(int,raw_input().split()))
C=list(map(int,raw_input().split()))
A.sort()
B.sort()
C.sort()
#print A
#print B
#print C

sum=0
for i,val_i in enumerate(A):
	for j,val_j in enumerate(B):
		if val_j<=val_i:
			continue
		for k,val_k in enumerate(C):
			#print "# loop C"
			if val_k>val_j:
				#print val_i,val_j,val_k,N-k
				sum+=(N-k)
				break

print sum
			



Fixed submission:
import bisect
N=input()

A=list(map(int,raw_input().split()))
B=list(map(int,raw_input().split()))
C=list(map(int,raw_input().split()))
A.sort()
B.sort()
C.sort()

sum=0
for val_b in B:
	sum+=bisect.bisect_left(A, val_b)*( N-bisect.bisect_right(C, val_b) )
			
print sum
Buggy python submission (verdict: Runtime Error):
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		#print ans
		#print S_tmp
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if S0==''.join(S1) and S0.find("?")!=-1:
	print "UNRESTORABLE"
	sys.exit()
	

print ans



Fixed submission:
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)

if len(S0)<len(T):
	print "UNRESTORABLE"
	sys.exit()


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	#print S1
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		#print ans
		#print S_tmp
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if ans=="":
	print "UNRESTORABLE"
	sys.exit()
else:
	print ans

Buggy python submission (verdict: Runtime Error):
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		#print ans
		#print S_tmp
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if S0==''.join(S1):
	print "UNRESTORABLE"
	sys.exit()
	

print ans



Fixed submission:
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)

if len(S0)<len(T):
	print "UNRESTORABLE"
	sys.exit()


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	#print S1
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		#print ans
		#print S_tmp
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if ans=="":
	print "UNRESTORABLE"
	sys.exit()
else:
	print ans

Buggy python submission (verdict: Runtime Error):
import sys
S0=raw_input()
S1=list(S0)
T=list(raw_input())
#print S1
#print T

l=len(T)

S=[]

for i in range(3):
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
	
if S0==''.join(S1):
	print "UNRESTORABLE"
	sys.exit()
	

S2=''.join(S1)
print S2.replace('?','a')
	


Fixed submission:
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)

if len(S0)<len(T):
	print "UNRESTORABLE"
	sys.exit()


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	#print S1
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		#print ans
		#print S_tmp
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if ans=="":
	print "UNRESTORABLE"
	sys.exit()
else:
	print ans
Buggy python submission (verdict: Time Limit Exceeded):
import sys
S=list(raw_input())

cnt=0
while len(S)>1:
	#print S,cnt
	if S[0]==S[-1]:
		S.pop(0)
		S.pop()
	elif S[0]!=S[-1] and S[0]=="x":
		cnt+=1
		S.append("x")
	elif S[0]!=S[-1] and S[-1]=="x":
		cnt+=1
		S.insert(0,"x")
	else:
		print "-1"
		sys.exit()
print cnt

Fixed submission:
import sys
S=raw_input()

cnt=0
while len(S)>1:
	#print S,cnt
	if S[0]==S[-1]:
		S=S[1:-1]
	elif S[0]!=S[-1] and S[0]=="x":
		cnt+=1
		S=S+"x"
	elif S[0]!=S[-1] and S[-1]=="x":
		S="x"+S
		cnt+=1
	else:
		print "-1"
		sys.exit()
print cnt
Buggy python submission (verdict: Time Limit Exceeded):
import sys
N=input()
D=map(int,raw_input().split())
M=input()
T=map(int,raw_input().split())

D.sort()
T.sort()

#print N
#print D
#print M
#print T

no=0
for i in range(M):
	for j in range(no,N):
		#print i,j,T[i],D[j]
		if T[i]==D[j]:
			no=j+1
			break
		elif T[i]!=D[j] and j==N-1:
			print "NO"
			sys.exit()

print "YES"


Fixed submission:
import sys
N=input()
a={}
for i in raw_input().split():
	#print i
	if i in a:
		a[i]=a[i]+1
	else:
		a[i]=1
#print a


M=input()
for j in raw_input().split():
	#print j
	if j in a:
		a[j]=a[j]-1
		if a[j]<0:
			print "NO"
			sys.exit()
	else:
			print "NO"
			sys.exit()		

print "YES"
Buggy python submission (verdict: Runtime Error):
A,B,C,D,E,F=map(int, raw_input().split())
noudo=int(float(E)/(100+E)*100)

a=[]
for i in range(noudo):
	for j in range(noudo):
		#print i,j
		if i*C+D*j<=noudo:
			a.append(i*C+D*j)

b=[]
cnt=F/100
for i in range(cnt):
	for j in range(cnt):
		if i*100+j*100<=F and i*100+j*100!=0:
			b.append(i*100+j*100)


noudo=float(E)/(100+E)*100
max_noudo=0
for i in a:
	for j in b:
		t_noudo=float(i)/(i+j)*100
		if t_noudo>max_noudo:
			max_noudo=t_noudo
			ans=i,j

print ans[0]+ans[1],ans[0]




Fixed submission:
# -*- coding: utf-8 -*-
A,B,C,D,E,F=map(int, raw_input().split())
#print A,B,C,D,E,F

sugar_list=[0]*(F+1)
i=0
while i <= F :
	for j in range(F):
		val=C*i+D*j
		if val > F:
			break
		elif val <= F:
			sugar_list[val]=1
	i+=1

#print "# Sugar"
#print sugar_list

#Water
water_list=[0]*(F/100+1)
#print water_list
loop_cnt=F/100+1

i=0
while i <= loop_cnt:
	for j in range(loop_cnt):
		val=A*i+B*j
		if val > F/100:
			break
		else:
			#print i,j,val
			water_list[val]=1
	i+=1

#print "# Water"
#print water_list

max_noudo=float(E)/(100+E)
#print max_noudo


hwm=0
water_ans=A*100
sugar_ans=0
for i in range(1,len(water_list)):
	for j in range(len(sugar_list)):
		if water_list[i]*sugar_list[j]==1 and i*100+j<=F and j<=i*E:
			if float(j)/(i*100+j)>hwm:
				hwm=float(j)/(i*100+j)
				water_ans=i*100
				sugar_ans=j
				

print water_ans+ sugar_ans, sugar_ans

Buggy python submission (verdict: Runtime Error):
A,B,C,D,E,F=map(int, raw_input().split())
noudo=int(float(E)/(100+E)*100)

a=[]
for i in range(noudo):
	for j in range(noudo):
		#print i,j
		if i*C+D*j<=noudo:
			a.append(i*C+D*j)

b=[]
cnt=F/100
for i in range(cnt):
	for j in range(cnt):
		if i*100+j*100<=F and i*100+j*100!=0:
			b.append(i*100+j*100)


noudo=float(E)/(100+E)*100
max_noudo=0
for i in a:
	for j in b:
		t_noudo=float(i)/(i+j)*100
		if t_noudo>max_noudo:
			max_noudo=t_noudo
			ans=i,j

print ans[0],ans[1]




Fixed submission:
# -*- coding: utf-8 -*-
A,B,C,D,E,F=map(int, raw_input().split())
#print A,B,C,D,E,F

sugar_list=[0]*(F+1)
i=0
while i <= F :
	for j in range(F):
		val=C*i+D*j
		if val > F:
			break
		elif val <= F:
			sugar_list[val]=1
	i+=1

#print "# Sugar"
#print sugar_list

#Water
water_list=[0]*(F/100+1)
#print water_list
loop_cnt=F/100+1

i=0
while i <= loop_cnt:
	for j in range(loop_cnt):
		val=A*i+B*j
		if val > F/100:
			break
		else:
			#print i,j,val
			water_list[val]=1
	i+=1

#print "# Water"
#print water_list

max_noudo=float(E)/(100+E)
#print max_noudo


hwm=0
water_ans=A*100
sugar_ans=0
for i in range(1,len(water_list)):
	for j in range(len(sugar_list)):
		if water_list[i]*sugar_list[j]==1 and i*100+j<=F and j<=i*E:
			if float(j)/(i*100+j)>hwm:
				hwm=float(j)/(i*100+j)
				water_ans=i*100
				sugar_ans=j
				

print water_ans+ sugar_ans, sugar_ans
Buggy python submission (verdict: Runtime Error):
import sys

N=input()
a=map(int, raw_input().split())
#print a
#print max(a)

dict={}


for i in range(len(a)):
	if a[i] in dict:
		dict[ a[i] ]+=1 
	else:
		dict[ a[i] ]=1
		#print dict


for j in range(max(a)):
	if not j in dict:
		dict[j]=0

#print dict

max_sum=0
for key,value in dict.items():
	if key==0:
		sum=dict[key]+dict[key+1]
	elif key==max(a):
		sum=dict[key-1]+dict[key]
	else:
		sum=dict[key-1]+dict[key]+dict[key+1]
	#print key,sum

	if max_sum<sum:
		max_sum=sum

print max_sum




Fixed submission:
import sys

N=input()
a=map(int, raw_input().split())
#print a
#print max(a)

dict={}


for i in range(len(a)):
	if a[i] in dict:
		dict[ a[i] ]+=1 
	else:
		dict[ a[i] ]=1

#print dict

max_sum=0
for key,value in dict.items():
	#print key,value
	sum=dict[key]
	if key-1 in dict:
		sum+=dict[key-1]
	if key+1 in dict:
		sum+=dict[key+1]

	if max_sum<sum:
		max_sum=sum

print max_sum
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
#幅優先探索
#TLE
import sys

N=input()

a=[[] for i in range(N+1)]
#隣接リスト
for i in range(N-1):
	x,y,z=list(map(int,raw_input().split()))
	a[x].append((y,z))
	a[y].append((x,z))

#print a

Q,K=list(map(int,raw_input().split()))


b=[]
for i in range(Q):
	b.append(map(int, raw_input().split()))
#print b



#訪問判別フラグ
F=(N+1)*[0]

#Queue
queue=[]

#距離
dist=[0 for i in range(N+1)]

p=-1
v=-1
length=0
#幅優先探索
queue.append(K)
while len(queue) != 0:
	v=queue.pop(0)
	F[v]=1
	for u,length in a[v]:
		if F[u]==0:
			queue.append(u)
			dist[u]=dist[v]+length


#print dist


for x in range(len(b)):
	print dist[ b[x][0] ]+ dist[ b[x][1] ]




Fixed submission:
# -*- coding: utf-8 -*-
import sys


N=input()


a=[[] for i in range(N+1)]

#隣接リスト
for i in range(N-1):
	x,y,z=list(map(int,raw_input().split()))
	a[x].append((y,z))
	a[y].append((x,z))

#print a
	

Q,K=list(map(int,raw_input().split()))


b=[]
for i in range(Q):
	b.append(map(int, raw_input().split()))
#print b



#訪問判別フラグ
F=(N+1)*[0]

#Stack
S=[]

#距離
dist=[0 for i in range(N+1)]

#先頭から深さ優先探索
S.append(K)
former_v=0
v=0
while len(S) != 0:
	v=S.pop()
	
	if F[v]==0:
		F[v]=1

		for u,length in a[v]:
			if F[u]==0:
				S.append(u)
				dist[u]=dist[v]+length
				#print dist

#print dist



for x in range(len(b)):

	print dist[ b[x][0] ]+ dist[ b[x][1] ]

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
#幅優先探索
import sys

N=input()

a=[[] for i in range(N+1)]
#隣接リスト
for i in range(N-1):
	x,y,z=list(map(int,raw_input().split()))
	a[x].append((y,z))
	a[y].append((x,z))

#print a

Q,K=list(map(int,raw_input().split()))


b=[]
for i in range(Q):
	b.append(map(int, raw_input().split()))
#print b



#訪問判別フラグ
F=(N+1)*[0]

#Queue
queue=[]

#距離
dist=[0 for i in range(N+1)]

#先頭から幅優先探索
queue.append(K)
while len(queue) != 0:
	#print queue
	v= queue.pop(0)
	#print "v::"+str(v)
	if F[v]==0:
		F[v]=1

		for u,length in a[v]:
			if F[u]==0:
				queue.append(u)
				dist[u]=dist[v]+length
				#print dist

#print dist


for x in range(len(b)):
	print dist[ b[x][0] ]+ dist[ b[x][1] ]




Fixed submission:
# -*- coding: utf-8 -*-
import sys


N=input()


a=[[] for i in range(N+1)]

#隣接リスト
for i in range(N-1):
	x,y,z=list(map(int,raw_input().split()))
	a[x].append((y,z))
	a[y].append((x,z))

#print a
	

Q,K=list(map(int,raw_input().split()))


b=[]
for i in range(Q):
	b.append(map(int, raw_input().split()))
#print b



#訪問判別フラグ
F=(N+1)*[0]

#Stack
S=[]

#距離
dist=[0 for i in range(N+1)]

#先頭から深さ優先探索
S.append(K)
former_v=0
v=0
while len(S) != 0:
	v=S.pop()
	
	if F[v]==0:
		F[v]=1

		for u,length in a[v]:
			if F[u]==0:
				S.append(u)
				dist[u]=dist[v]+length
				#print dist

#print dist



for x in range(len(b)):

	print dist[ b[x][0] ]+ dist[ b[x][1] ]
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque

N=input()

nl=[ [] for i in range(N+1)]
D=[ 0 for i in range(N+1)]	# No.1からの距離
V=[ 0 for i in range(N+1)]	# 訪問済みフラグ

for i in range(N-1):
	a,b=map(int, sys.stdin.readline().split())
	nl[a].append(b)
	nl[b].append(a)


def bfs(v):	#1からの頂点Nまでの距離を算出
	q = deque([]) 
	V[v]=1
	q.append(v)
	while q:
		v=q.popleft()
		for i in nl[v]:
			if V[i]==0:
				D[i]=D[v]+1
				V[i]=1
				q.append(i)

bfs(1)


L=deque([]) 	#1-Nまでの最短経路を復元 
def bfs2(v):
	q = deque([]) 
	q.append(v)
	while q:
		v=q.popleft()
		for i in nl[v]:
			if D[i]==D[v]-1 and i!=1:
				q.append(i)
				L.appendleft(i)

bfs2(N)


color=[ None for i in range(N+1)]
color[1]="b"
color[N]="w"

l=len(L)
for i in range( (l+1)/2 ):
	color[ L[i] ]="b"

for i in range( (l+1)/2, l ):
	color[ L[i] ]="w"



def bfs3(v):
	q = deque([]) 
	q.append(v)
	while q:
		v=q.popleft()
		for i in nl[v]:
			if color[i] is None:
				color[i]=color[v]
				q.append(i)
for i in L:
	bfs3(i)
		


b_cnt=color.count("b")
w_cnt=color.count("w")


if N%2==1:
	if b_cnt>w_cnt:
		print "Fennec"
	else:
		print "Snuke"
else:
	if b_cnt==w_cnt:	
		print "Snuke"
	elif b_cnt>w_cnt:
		print "Fennec"
	else:
		print "Snuke"



Fixed submission:
#Python

l = map(int, raw_input().split())

A=l[0]
B=l[1]
#print A
#print B

if A%3==0 or B%3==0 or (A+B)%3==0:
	print 'Possible'
else:
	print 'Impossible'
Buggy python submission (verdict: Time Limit Exceeded):
#Python

N = input()
a = map(int, raw_input().split())


b=[]
for i in range(N-1):
	i=i+1
	#print "x::"+str(sum(a[:i]))
	#print "sum-2x::"+str( abs( sum(a) - sum(a[:i])*2 ) )
	b.append( abs( sum(a) - sum(a[:i])*2 ) )

#print b
print min(b)


Fixed submission:
#Python

N = input()
a = map(int, raw_input().split())

b=[]
ans=0
sum=sum(a)

x=0
for i in range(N-1):
	x+=a[i]
	#print "x::"+str(x)
	if i==0:
		ans= abs( sum - x*2 )
	if ans>abs( sum - x*2 ):
		ans= abs( sum - x*2 )
	#print "ans::"+str(ans)

print ans

Buggy python submission (verdict: Time Limit Exceeded):
#Python

N = input()

#print N

a = map(int, raw_input().split())
#print a
#print sum(a)

b=[]

for i in range(N-1):
	i=i+1
	#print "g1::"+str(sum(a[:i]))
	#print "g2::"+str(sum(a[i:]))
	#print "sa::"+str( abs(sum(a[:i]) - sum(a[i:])) )
	b.append( abs(sum(a[:i]) - sum(a[i:])) )

#print b
print min(b)

Fixed submission:
#Python

N = input()
a = map(int, raw_input().split())

b=[]
ans=0
sum=sum(a)

x=0
for i in range(N-1):
	x+=a[i]
	#print "x::"+str(x)
	if i==0:
		ans= abs( sum - x*2 )
	if ans>abs( sum - x*2 ):
		ans= abs( sum - x*2 )
	#print "ans::"+str(ans)

print ans