gets
s=0
a=0
for x in gets.chomp.split.map(&:to_i)
	s += x;
	a += -s if s < 0
	s -= s if s < 0
end

puts a

# -*- encoding: utf-8 -*-
n = gets.chomp.to_i
events = gets.chomp.split(" ").map{ |e| e.to_i }

ans = 0
p = 0

events.each { |event|
  if event == -1
    if p > 0
      p -= 1
    else
      ans += 1
    end
  else
    p += event
  end
}

puts ans

n = gets.to_i
a = gets.split.map(&:to_i)

x = 0
y = 0

for i in a
  if i == -1
    if x > 0
      x -= 1
    else
      y += 1
    end
  else
    x += i
  end
end

print y

n = gets.to_i
arr = gets.split.map(&:to_i)
soliders = 0
crimes = 0

arr.each do |x|
	soliders += x
	(crimes += 1; soliders = 0) if soliders < 0
end

puts crimes

n,v=gets.to_i,gets.split.map(&:to_i)
s,c=0,0
v.each {|x| (s+=x) == -1 ? (c,s=c+1,0) : s}
p c

n = gets.to_i
p = gets.split.map{|r| r.to_i}

counters = 0; xounterz = 0
for i in 0..n-1
  counters+=p[i]
  if counters<0
      xounterz+=1
      counters = 0
  end
end
puts xounterz

n = $stdin.gets.to_i
f = $stdin.readline.split.map(&:to_i)

k = 0
f.inject(0) do |cops_count, x|
    #puts "cop_count=#{cops_count}, x=#{x}, k=#{k}"
    if x == -1
        if cops_count == 0
            k+=1
        else
            cops_count -= 1
        end
    else
        cops_count += x
    end
    cops_count
end

puts k

n= gets.to_i
list = gets.split(" ").map(&:to_i)

i= 0
available_police = 0
crime =0
n.times do
  if list[i] > 0
    available_police+=list[i]

  elsif list[i]<0 and available_police>0
    available_police+=list[i]
  else
    crime +=1
  end
  i+=1

end
puts crime

n = gets.to_i
arr = gets.split.map &:to_i
cops = 0
crimes = 0
arr.each do |x|
  if x == -1 && cops == 0
    crimes += 1
  elsif x == -1 && cops != 0
    cops -= 1
  else
    cops += x
  end
end
puts crimes

n = gets.to_i
a = gets.split.map(&:to_i)
cur = 0
ans = 0
for x in a
  if x>0
    cur += x
  else
    if cur == 0
      ans += 1
    else
      cur -= 1
    end
  end
end
print ans

n = gets.to_i
s = 0
ans = 0
gets.split.map(&:to_i).each do |e|
  if e < 0
    if s > 0
      s += e
    else
      ans += 1
    end
  else
    s += e
  end
end
puts ans

gets
f=a=0
gets.split.map{|i| i.to_i}.each{|s|
	if s<0
		if f<=0
			a+=1
		else
			f-=1
		end
	else
		f+=s
	end
}
p a

gets
x=gets.split.map{|e| e.to_i}
sum=0
ans=0
x.each{|e|
	if e<0 then
		sum+=e
		ans+=1 if sum<0
	else
		sum=([sum,0].max)+e
	end
}
puts ans

t = gets.chomp.to_i
arr = gets.chomp.split.map(&:to_i)

co = 0
t = 0
arr.each do |el|
  # p [t+el]
  if t + el < 0
    co += 1
    t = 0
  else
    t += el
  end
end
puts co

n = gets.to_i
a = gets.split.map(&:to_i)
c,r = 0,0
n.times do |i|
	if a[i] > 0
		c += a[i]
	elsif c > 0
		c -= 1
	else
		r += 1
	end
end
p r

police = 0
criminal = 0
out = 0
k = gets.to_i
gets.chomp.split.map do |e|
  e = e.to_i
  police += e if e > 0
  out += 1 if e == -1 and police == 0
  police -= 1 if  e == -1 and police > 0

end

puts out

n = gets.chomp.to_i

ans,pol = 0,0

gets.chomp.split(" ").each { |e|
  if e.to_i == -1
    if pol>0
      pol -= 1
    else
      ans += 1
    end
  else
    pol += e.to_i
  end
}
puts ans

crimes = gets.chomp.to_i
n = gets.chomp
events = n.split

count = 0
untreated = 0
for i in (0..crimes-1)
	if count == 0 && events[i].to_i == -1
		untreated += 1
	elsif events[i].to_i >= 1
		count += events[i].to_i
	elsif count > 0 && events[i].to_i == -1
		count -= 1
	end
end

puts untreated

n = gets.chomp
events = gets.chomp.split(' ').map(&:to_i)
ans = f = 0
events.reverse.each do |e|
  next if f == 0 && e != -1
  f = 1
  if e == -1
    ans = ans + e
  else
    ans = ans + (ans < 0 ? e : 0)
    ans = 0 if ans > 0
  end
end
puts ans.abs

gets
now = 0
ans = 0
gets.split.map(&:to_i).each { |c|
  if c == -1
    if now >= 1
      now -= 1
    else
      ans += 1
    end
  else
    now += c
  end
}

puts ans

cops = 0
result = 0

gets
gets.split(' ').map(&:to_i).each do |x|
  cops += x
  if cops < 0
    result += 1
    cops = 0
  end
end

puts result

eventNumber = gets.chomp.to_i
input = gets.chomp.split(' ').map {|item| item.to_i}

police = 0
ans = 0
(0...eventNumber).each do |event|
    police += input[event] if input[event] >= 1

    if input[event] == -1 && police <= 0
        ans += 1
    elsif input[event] == -1
        police -= 1
    end
end

puts ans

n = gets.to_i
plc, crm = 0, 0
gets.split(' ',n).map(&:to_i).each do |x|
  plc += x
  if plc < 0
    crm += 1
    plc = 0
  end
end
puts crm

#!/usr/bin/ruby
gets
r=0
n=0
gets.split.each{|e|
	n+=e.to_i
	if n<0
		r+=-n
		n=0
	end
}
p r

police = 0
ans = 0
gets.chomp
gets.chomp.split(" ").each { |p|
  if p.to_i > -1
    police += p.to_i
  elsif police > 0
    police -= 1
  else
    ans += 1
  end
}

puts ans

n = gets.to_i
events = gets.chomp.split(" ").map{ |e| e.to_i }
s = 0
p = 0
events.each{ |event|
	if event < 0
		if p > 0
			p -= 1
		else
			s += 1
		end
	else
		p += event
	end
}
puts(s)

police = 0
ans = 0
gets.chomp
gets.chomp.split(" ").each { |p|
  if p.to_i == 1
    police += 1
  elsif police > 0
    police -= 1
  else
    ans += 1
  end
}

puts ans

n = gets.to_i
events = gets.chomp.split(" ").map{ |e| e.to_i }
s = 0
p = 0
events.each{ |event|
	if event == -1
		if p >= 0
			p -= 1
		else
			s += 1
		end
	else
		p+=1
	end
}
puts(s)

n = gets.to_i
events = gets.chomp.split(" ").map{ |e| e.to_i }
s = 0
p = 0
events.each{ |event|
	if event == -1
		if p > 0
			p -= 1
		else
			s += 1
		end
	else
		p+=1
	end
}
puts(s)

n = gets.to_i
a = gets.split.map(&:to_i)

x = 0
y = 0

for i in a
  if i == -1
    if x > 0
      x -= 1
    else
      y += 1
    end
  else
    x += 1
  end
end

print y

n,v=gets.to_i,gets.split.map(&:to_i)
s=0
p v.map {|x| s+=x}.min.abs

gets
f=a=0
gets.split.each{|s|
	if s[1]
		if f<0
			a+=1
		else
			f-=1
		end
	else
		f+=1
	end
}
p a

n = gets.chomp
events = gets.chomp.split(' ').map(&:to_i)
ans = f = 0
events.reverse.each { |e| f == 0 && e == 1 ? next : (f, ans = 1, ans + e) }
puts ans.abs


k,d,t = gets.split.map(&:to_i)
if k > d
  d = (k/d.to_f).ceil * d
end

per = k + (d-k)/2.to_f
n = (t/per).to_i
ans = n * d
if (t-per*n) <= k
  ans += t - per*n
elsif
  ans += k + (t-k-per*n)*2
end
puts ans

k, d, t = gets().split().map{|x| 2*(x.to_i)}
r = (d - k%d) % d
n = k/d
n += 1 if r != 0

ans = 0
if t <= k
	ans += t
else
	cycle = k + r/2
	ans += t/cycle * n * d
	t %= cycle
	if t <= k
		ans += t
	else
		ans += k + 2*(t-k)
	end
end

puts "%.10f" % [ans/2.0]

K,D,T=gets.split.map(&:to_f)
S=(K/D).ceil*D
def f(t)
  (t/S).floor*(K+(S-K)*0.5)+[t%S,K].min+[0,t%S-K].max*0.5
end
l=T
r=2*T
100.times{
  m=(l+r)/2.0
  if f(m)>T
    r=m
  else
    l=m
  end
}
p l

K,D,T=gets.split.map(&:to_f)
S=(K/D).ceil*D
# def f(t)
#   (t/S).floor*(K+(S-K)*0.5)+[t%S,K].min+[0,t%S-K].max*0.5
# end
# l=T
# r=2*T
# 100.times{
#   m=(l+r)/2.0
#   if f(m)>T
#     r=m
#   else
#     l=m
#   end
# }
# p l

ans = 0

a = (S + K) * 0.5
ans += (T/a).floor * S
T -= (T/a).floor * a

ans += [T, K].min
T -= [T, K].min

if T > 0
  ans += 2 * T
end

p ans

k,d,t = gets.split.map(&:to_i)
if k > d
  d = (k/d.to_f).ceil * d
end

trial = k + (d-k)/2.to_f
n = (t/trial).to_i
a = n * d
if (t-trial*n) <= k
  ans = n * d + t-trial*n
elsif
  ans = n * d + k + (t-trial*n-k)/2.to_f
end
sprintf("%.0f", ans.to_f)

k,d,t = gets.split.map(&:to_i)
if k > d
  d = (k/d.to_f).ceil * d
end

trial = k + (d-k)/2.to_f
n = (t/trial).to_i
a = n * d
if (t-trial*n) <= k
  ans = n * d + t-trial*n
elsif
  ans = n * d + k + (t-trial*n-k)/2.to_f
end
printf("%.0f", ans)

k,d,t = gets.split.map(&:to_i)
if k > d
  d = (k/d.to_f).ceil * d
end

trial = k + (d-k)/2.to_f
n = (t/trial).to_i
a = n * d
if (t-trial*n) <= k
  ans = n * d + t-trial*n
elsif
  ans = n * d + k + (t-trial*n-k)/2.to_f
end
puts ans

k,d,t = gets.split.map(&:to_i)
if k > d
  d = (k/d.to_f).ceil * d
end

per = k + (d-k)/2.to_f
n = (t/per).to_i
ans = n * d
p ans
p n*per
if (t-per*n) <= k
  ans += t - per*n
elsif
  ans += k + (t-k-per*n)*2
end
puts sprintf("%.9f", ans)

k,d,t = gets.split.map(&:to_i)
if k > d
  d = (k/d.to_f).ceil * d
end

per = k + (d-k)/2.to_f
n = (t/per).to_i
ans = n * d
p ans
p n*per
if (t-per*n) <= k
  ans += t - per*n
elsif
  ans += k + (t-k-per*n)*2
end
puts ans


n = gets.to_i
f = gets.split.map { |x| x.to_i }

acc = 0
hash = Hash.new(0)
f.each do |x|
	acc += x
	hash[acc] += 1
end

ans = 0
hash.each do |key, val|
	ans = [ans, val].max
end

puts n - ans

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
mp = Hash.new(0)
best = 0
sum = 0
arr.each do |num|
  sum += num
  mp[sum] += 1
  best = [best, mp[sum]].max
end
puts n - best

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
mp = Hash.new
mp[0] = true
sum = 0
ct = 0
found = false
arr.each do |num|
  sum += num
  if found
    if sum == 0
      ct += 1
    end
  else
    if mp.has_key?(sum)
      found = true
      sum = 0
      ct += 1
    else
      mp[sum] = true
    end
  end
end
if found && sum != 0
  ct += 1
end
puts n - ct

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
mp = Hash.new
mp[0] = true
sum = 0
ct = 0
found = false
arr.each do |num|
  sum += num
  if found
    if sum == 0
      ct += 1
    end
  else
    if mp.has_key?(sum)
      found = true
      ct += 1
    else
      mp[sum] = true
    end
  end
end
puts n - ct

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
mp = Hash.new
mp[0] = true
sum = 0
ct = 0
found = false
arr.each do |num|
  sum += num
  if found
    if sum == 0
      ct += 1
    end
  else
    if mp.has_key?(sum)
      found = true
      ct += 1
    end
  end
end
puts n - ct


    t = gets.to_i
    t.times{|w|
    n = gets.to_i
    board = [gets.chomp.to_s,gets.chomp.to_s]
    path = [0,0]
    find = false
    visited = []
    while path.size != 0
    node = path.shift
    if node[0] == n-1 && node[1] == 1
    find = true
    break
    end
    if (!visited.include?(node))
    visited<<node
    dir = [[node[0]+1,node[1]],[node[0],node[1]+1],[node[0]+1,node[1]+1],[node[0]+1,node[1]-1]].filter{|x,y| x<n && y>=0 && y < 2}
    dir.map{|w|
    if (board[w[1]][w[0]] == "0")
    path<<w
    end
    }
    end
    end
    puts find ? "YES" : "NO"
    }

t= gets.chomp.to_i
t.times do
    n=gets.chomp.to_i
    s1=gets.chomp.chars
    s2=gets.chomp.chars
    n.times do |i|
      if s1[i].to_i+s2[i].to_i==2
        puts "NO"
        break
    elsif i == n-1
        puts "YES"
    end
    end
end

gets.to_i.times do
  n=gets.to_i
  s=gets.chomp.chars.map(&:to_i)
  t=gets.chomp.chars.map(&:to_i)
  f=true
  n.times do |i|
    if s[i]+t[i]==2
      f=false
      break
    end
  end

  if f
    puts "YES"
  else
    puts "NO"
  end
end

# 4
# 3
# 000
# 000
# 4
# 0011
# 1100
# 4
# 0111
# 1110
# 6
# 010101
# 101010

n = gets.to_i

def result
  n = gets.to_i
  arr = 2.times.map { gets.split('').map(&:to_i) }
  if n.times.all? do |i|
    arr[0][i] == 0 || arr[1][i] == 0
  end
    'YES'
  else
    'NO'
  end
end

n.times do
  puts result
end

def dfs(grid, x, y)
  if y == 1 && x == grid.first.length
    return true
  elsif grid[y].nil? || grid[y][x].nil? || grid[y][x] == "1"
    return false
  else
    grid[y][x] = "1"
    return dfs(grid, x, (y + 1) % 2) ||
      dfs(grid, x + 1, y) ||
      dfs(grid, x + 1, (y + 1) % 2)
  end
end

STDIN.read
  .split("\n")
  .drop(1)
  .each_slice(3) do |arr|
    puts(dfs([arr[1].split(""), arr[2].split("")], 0, 0) ? "YES" : "NO")
  end


n, m = gets.chomp.split().map { |e| e.to_i }
pebble = gets.chomp.split().map { |e| e.to_i }
K = m - 1
answer = 0
for i in pebble
    answer += (i + K) / m
end
puts ((answer+1)/2)

number=gets.chomp.split.map{ |s| s.to_i}
w=gets.chomp.split.map{|s| s.to_i}
n=number[0]
k=number[1]
sum=0
count=0
w.each { |x| sum+=x/k}
w.each { |x| count+=1 if x%k!=0 }
if sum%2==1
	count+=1
end
result=sum/2+count/2
if count%2==1
	result+=1
end
puts result

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 0
a.each{|x| ans += (x + k - 1) / k}
p (ans + 1) / 2


n,k = gets.split.map(&:to_i)
b = Hash.new(0)
n.times do
    a = gets.to_i
    b[a] += 1
end
c = b.values.map{|i|i%2}.count(1)
ans = n-c/2
puts ans

a = gets.chomp.split.collect{|var| var.to_i}
n = a[0]
k = a[1]
array_k = Array.new(k,0)
for var in (0...n) do
    value = gets.chomp.to_i
    array_k[value - 1] += 1
end
students = 0
count = 0
for var in (0...k) do
    if(array_k[var] % 2 == 0)
        students += array_k[var]
    else
        students += (array_k[var] - 1)
        count += 1
    end
end
puts (students + (count.to_f/2).ceil)


x=y=1
gets.to_i.times{
	s=gets.chomp.to_sym
	if (s==:UL)||(s==:DR)
		x+=1
	elsif (s==:UR)||(s==:DL)
		y+=1
	else
		x+=1
		y+=1
	end
}
p x*y


gets.to_i.times{n, m = gets.split.map(&:to_i); p m * (m- 1  + n * n + n) / 2}

gets.to_i.times{n, m = gets.split.map(&:to_i);p (m * m + m * n * n + m * n - m) / 2}

gets.to_i.times{
    n, m = gets.split.map(&:to_i)
    p (m * m + m * n * n + m * n - m) / 2
}

gets.to_i.times do
  x, y = gets.split.map(&:to_i)
  puts((1 + y) * y / 2 + y * ((1 + x) * x / 2 - 1))
end

n = gets.chomp.to_i
for i in 1..n
    x, y = gets.split.map(&:to_i)
    ans = (1 + y) * y / 2 + y * ((1 + x) * x / 2 - 1)
    puts(ans)
end


gets.strip.to_i.times do
  a = gets.strip.split(' ').map(&:to_i)
  a.sort!
  res = if a[2] <= a[1] + a[0]
    (a[0] + a[1] + a[2]) / 2
  else
    a[0] + a[1]
  end
  if res * 2 == a.inject(:+) or  res * 2 == [a.inject(:+) - 1, 0].max
    puts "Yes"
  else
    puts "No"
  end
end

q = gets.chomp.to_i
(0..q-1).each do |i|
  a = gets.strip.split(' ').map(&:to_i)
  a.sort!
  res = if a[2] <= a[1] + a[0]
          (a[0] + a[1] + a[2]) / 2
        else
          a[0] + a[1]
        end
  if res * 2 == a.inject(:+) or  res * 2 == [a.inject(:+) - 1, 0].max
    puts "Yes"
  else
    puts "No"
  end
end

gets.strip.to_i.times {
    x, y, k = gets.strip.split(' ').map(&:to_i)
    mx = [x, y, k].max
    mn = [x, y, k].min
    if ((x + y + k) - (mx + mn) + mn) + 1 >= mx
        puts "YES"
    else
        puts "NO"
    end
}

a = gets.chomp.to_i
(0..a-1).each do |i|
  s = gets.chomp.split(' ')
  r = s[0].to_i; g = s[1].to_i; b = s[2].to_i;
  if ((r/g)>2 || (r/b)>2 || (g/b)>2 || (g/r)>2 || (b/r)>2 || (b/g)>2)
    puts 'No'
  else
    puts 'Yes'
  end
end

a = gets.chomp.to_i
(0..a-1).each do |i|
  s = gets.chomp.split(' ')
  r = s[0].to_i; g = s[1].to_i; b = s[2].to_i;
  if ((r-g).abs>2 || (r-b).abs>2 || (g-b).abs>2)
    puts 'No'
  else
    puts 'Yes'
  end
end

gets.strip.to_i.times {
    x, y, k = gets.strip.split(' ').map(&:to_i)
    if [x, y, k].max - [x, y, k].min <= 3
        puts "YES"
    else
        puts "NO"
    end
}


k,d=gets.chomp.split.map(&:to_i)
if d==0
	if k==1
		puts "0"
	else
		puts "No solution"
	end
elsif d>0
	puts d*(10**(k-1))
else
	puts "No solution"
end

k, d = gets.chomp.split(/ /).map(&:to_i)
if k > 1 && d == 0
    puts "No solution"
else
    if k == 1 && d == 0
        puts 0
    else
        puts "#{d * (10 ** (k - 1))}"
    end
end

k, d = gets.split.map(&:to_i)
if d == 0 && k > 1
  print "No solution"
else
  print d
  for i in 1..k-1
    print 0
  end
end

# def digital_root(n)
# 	return @dp[n] if @dp[n]
# 	s = n.to_s.split("").map(&:to_i).inject(&:+)
# 	if s < 10
# 		@dp[n] = s
# 		return s
# 	end
# 	@dp[n] = digital_root(s)
# 	return @dp[n]
# end

# digits,d = gets.split(" ").map(&:to_i)
# @dp = {}
# start = 10 ** (digits - 1)
# start = 0 if start == 1
# finish = (10 ** digits) - 1
# ans = nil
# if d == 0
# 	if digits == 1
# 		ans = 0
# 	end
# else
# 	(start..finish).each do |a|
# 		if digital_root(a) == d
# 			ans = a
# 			break
# 		end
# 	end
# end
# if ans
# 	puts ans
# else
# 	puts "No solution"
# end

def digital_root(n)
	s = n.to_s.split("").map(&:to_i).inject(&:+)
end

digits,d = gets.split(" ").map(&:to_i)
@dp = {}
start = 10 ** (digits - 1)
start = 0 if start == 1
finish = start + 10
ans = nil

(start..finish).each do |a|
	if digital_root(a) == d
		ans = a
		break
	end
end

if ans
	puts ans
else
	puts "No solution"
end

def digital_root(n)
	return @dp[n] if @dp[n]
	s = n.to_s.split("").map(&:to_i).inject(&:+)
	if s < 10
		@dp[n] = s
		return s
	end
	@dp[n] = digital_root(s)
	return @dp[n]
end

digits,d = gets.split(" ").map(&:to_i)
@dp = {}
start = 10 ** (digits - 1)
start = 0 if start == 1
finish = (10 ** digits) - 1
ans = nil
if d == 0
	if digits == 1
		ans = 0
	end
else
	(start..finish).each do |a|
		if digital_root(a) == d
			ans = a
			break
		end
	end
end
if ans
	puts ans
else
	puts "No solution"
end

k, d = gets.split.collect{|i| i.to_i}
if d == 0
    if k == 1
        puts 0
    else
        puts 'No solution'
    end
    exit
end
puts (Array.new([d])+Array.new(k-1,0)).join

def solution(k, n)
  return 0 if n == 0 && k == 1
  return "No solution" if n == 0 && k > 1

  num = num_k(k)

  until dr(num) == n || num.to_s.size != k do
    num += 1
  end

  return "No solution" if num.to_s.size != k
  num
end

def num_k(k)
  min_num = "1"

  (k-1).times { |i| min_num << "0" }
  min_num.to_i
end

def dr(n)
  return n if n < 10
  num = n.to_s.split('').map(&:to_i).inject(:+)
  dr(num)
end

k, n = STDIN.gets.chomp.split(" ").map(&:to_i)
puts solution(k, n)

k, d = gets.split

if k == '1'
  puts d
else
  if d == '0'
    puts 'No solution'
  else
    puts d + ('0' * k.to_i.pred)
  end
end

k, d = gets.chomp.split.map{ |i| i.to_i}

if d == 0 and k > 1
	puts "No solution"
else
	puts (d.to_s + ("0" * (k - 1)))
end

line = gets
line = line.split(" ");
k = line[0].to_i
d = line[1].to_i

ans = d * 10**(k-1)

if d == 0 && k > 1
    ans = "No solution"
end

ans = ans.to_s << "\n"
print(ans)

k, d = gets.split.map(&:to_i)

if d == 0 && k > 1
  print "No solution"
else
  print d
  for i in 1...k
    print 0
  end
end

k,d=gets.chomp.split.map(&:to_i)
if d==0 && k>1
  puts "No solution"
else
  print d
  (k-1).times { print 0}
end

k,d=gets.chomp.split.map &:to_i
puts d==0&&k>1?"No solution":d.to_s+'0'*(k-1)

dcnt, droot = gets.split.map{|s| s.to_i}

if droot == 0 && dcnt > 1
	puts "No solution"
else
	print droot
	(dcnt-1).times {print 0}
end

class A
  def initialize
    k, n = gets.chomp.split(' ').map(&:to_i)

    start = 0
    limit = 9 * k

    if k > 1 && n == 0
      puts "No solution"
      exit
    end

    (0..limit).each do |num|
      res = dfs(num)

      if res == n
        puts answer(num, k)
        exit
      end
    end

    puts "No solution"
  end

  def answer(num, k)
    list = [ 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
    index = 0
    answer = ""
    while num != 0
      count = num / list[index]
      num %= list[index]
      answer += list[index].to_s * count
      index += 1
    end

    if answer.to_s.size != k
      diff = k - answer.to_s.size
      answer += "0" * diff
    end

    answer
  end

  def dfs(num)
    if num < 10
      return num
    else
      return dfs(sum_digit(num))
    end
  end

  def sum_digit(num)
    str = num.to_s

    str.chars.map(&:to_i).inject(:+)
  end
end

a = A.new

line = gets
line = line.split(" ");
k = line[0].to_i
d = line[1].to_i
max = 10 ** (d-1)

while 1 && d != 0
    ans = rand((10**d) - 1) + max
    if ans % 9 == k
        break
    end
end

if d == 0
    ans = 0
end

p ans

line = gets
line = line.split(" ");
k = line[0].to_i
d = line[1].to_i
max = 10 ** (k-1)

if d != 0
    d = 0 if d == 9
    ans = rand((10**k) - 1) + max
    while ans % 9 != d
        ans += 1
    end
else
    if k == 1
        ans = 0
    else
        ans = "No solution"
    end
end

ans = ans.to_s << "\n"
print(ans)

line = gets
line = line.split(" ");
k = line[0].to_i
d = line[1].to_i
max = 10 ** (k-1)

while 1 && d != 0
    ans = rand((10**k) - 1) + max
    if ans % 9 == d
        break
    end
end

if d == 0
    ans = 0
end

p ans

line = gets
line = line.split(" ");
k = line[0].to_i
d = line[1].to_i
max = 10 ** d

while 1
    ans = rand(max-1) + max
    if ans % 9 == k
        break
    end
end

p ans

line = gets
line = line.split(" ");
k = line[0].to_i
d = line[1].to_i
max = 10 ** d

while 1 && d != 0
    ans = rand(max-1) + max
    if ans % 9 == k
        break
    end
end

if d == 0
    ans = 0
end

p ans

class A
  def initialize
    k, n = gets.chomp.split(' ').map(&:to_i)

    start = 0
    limit = 9 * k

    (0..limit).each do |num|
      res = dfs(num)

      if res == n
        puts answer(num, k)
        exit
      end
    end

    puts "No solution"
  end

  def answer(num, k)
    list = [ 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
    index = 0
    answer = ""
    while num != 0
      count = num / list[index]
      num %= list[index]
      answer += list[index].to_s * count
      index += 1
    end

    if answer.to_s.size != k
      diff = k - answer.to_s.size
      answer += "0" * diff
    end

    answer
  end

  def dfs(num)
    if num < 10
      return num
    else
      return dfs(sum_digit(num))
    end
  end

  def sum_digit(num)
    str = num.to_s

    str.chars.map(&:to_i).inject(:+)
  end
end

a = A.new

k, d = gets.chomp.split(/ /).map(&:to_i)
if k > 1 && d == 0
    puts "No solution"
else
    if k == 1 && d == 0
        puts 0
    else
        puts "#{k * (10 ** (d - 1))}"
    end
end

def digital_root(n)
	return @dp[n] if @dp[n]
	s = n.to_s.split("").map(&:to_i).inject(&:+)
	if s < 10
		@dp[n] = s
		return s
	end
	@dp[n] = digital_root(s)
	return @dp[n]
end

digits,d = gets.split(" ").map(&:to_i)
@dp = {}
start = 10 ** (digits - 1)
start = 0 if start == 1
finish = (10 ** digits) - 1
ans = nil
if d == 0
	if digits == 0
		ans = 0
	end
else
	(start..finish).each do |a|
		if digital_root(a) == d
			ans = a
			break
		end
	end
end
if ans
	puts ans
else
	puts "No Solution"
end

def digital_root(n)
	return @dp[n] if @dp[n]
	s = n.to_s.split("").map(&:to_i).inject(&:+)
	if s < 10
		@dp[n] = s
		return s
	end
	@dp[n] = digital_root(s)
	return @dp[n]
end

digits,d = gets.split(" ").map(&:to_i)
@dp = {}
start = 10 ** (digits - 1)
start = 0 if start == 1
finish = (10 ** digits) - 1
ans = nil
if d == 0
	if digits == 1
		ans = 0
	end
else
	(start..finish).each do |a|
		if digital_root(a) == d
			ans = a
			break
		end
	end
end
if ans
	puts ans
else
	puts "No Solution"
end

def solution(k, n)
  return 0 if n == 0

  num = num_k(k)

  until dr(num) == n || num.to_s.size != k do
    num += 1
  end

  return "No solution" if num.to_s.size != k
  num
end

def num_k(k)
  num = "1"
  (k-1).times { |i| num << "0" }
  num.to_i
end

def dr(n)
  num = n.to_s.split('').map(&:to_i).inject(:+)
  return dr(num) if num >= 10
  num
end

k, n = STDIN.gets.chomp.split(" ").map(&:to_i)
puts solution(k, n)

def solution(k, n)
  return 0 if n == 0

  num = num_k(k)

  until dr(num) == n || num.to_s.size != k do
    num += 1
  end

  return "No solution" if num.to_s.size != k
  num
end

def num_k(k)
  min_num = "1"

  (k-1).times { |i| min_num << "0" }
  min_num.to_i
end

def dr(n)
  return n if n < 10
  num = n.to_s.split('').map(&:to_i).inject(:+)
  dr(num)
end

k, n = STDIN.gets.chomp.split(" ").map(&:to_i)
puts solution(k, n)

def solution(k, n)
  return 0 if n == 0

  min_num, max_num = num_k(k)

  num = rand(min_num..max_num)

  until dr(num) == n || num.to_s.size != k do
    num += 1
  end

  return "No solution" if num.to_s.size != k
  num
end

def num_k(k)
  min_num = "1"
  max_num = "1"

  (k-1).times { |i| min_num << "0" }
  k.times { |i| max_num << "0" }
  [min_num.to_i, max_num.to_i]
end

def dr(n)
  num = n.to_s.split('').map(&:to_i).inject(:+)
  return dr(num) if num >= 10
  num
end

k, n = STDIN.gets.chomp.split(" ").map(&:to_i)
puts solution(k, n)

def solution(k, n)
  return 0 if n == 0

  num = num_k(k)
  until dr(num) == n && num.to_s.size == k do
    num += 1
  end

  return "No solution" if num.to_s.size != k
  num
end

def num_k(k)
  num = "1"
  (k-1).times { |i| num << "0" }
  num.to_i
end

def dr(n)
  num = n.to_s.split('').map(&:to_i).inject(:+)
  return dr(num) if num >= 10
  num
end

k, n = STDIN.gets.chomp.split(" ").map(&:to_i)
puts solution(k, n)

def solution(k, n)
  return 0 if n == 0

  min_num, max_num = num_k(k)

  num = rand(min_num..max_num)

  until dr(num) == n || num.to_s.size != k do
    num += 1
  end

  return "No solution" if num.to_s.size != k
  num
end

def num_k(k)
  min_num = "1"
  max_num = "1"

  (k-1).times { |i| min_num << "0" }
  k.times { |i| max_num << "0" }
  [min_num.to_i, max_num.to_i]
end

def dr(n)
  return n if n < 10
  num = n.to_s.split('').map(&:to_i).inject(:+)
  return dr(num) if num >= 10
  num
end

k, n = STDIN.gets.chomp.split(" ").map(&:to_i)
puts solution(k, n)

k, d = gets.split.map(&:to_i)

if k.zero?
  if d.zero?
    puts 0
  else
    puts 'No solution'
  end
else
  puts d.to_s + ('0' * k.pred)
end

k, d = gets.split.map(&:to_i)

if d.zero?
  if k.zero?
    puts 0
  else
    puts 'No solution'
  end
else
  puts d.to_s + ('0' * k.pred)
end


gets.to_i.times do
  n = gets.to_i

  if n.odd?
    puts '-1'
    next
  end

  puts "0 0 #{n / 2}"
end

a = gets.chomp.to_i
until a == 0 do
    x = gets.chomp.to_i
    if x % 2 == 0
        puts "#{x/2} 0 #{x/2}"
    else
        puts -1
    end
    a -= 1
end

num_cases = gets.to_i
for i in (1..num_cases)
  n = gets.to_i
  if n % 2 == 0
    puts "0 0 #{n/2}"
  else
    puts "-1"
  end
end

gets.to_i.times do
  n = gets.to_i
  if n % 2 == 0
    a, b, c = n / 2 ^ 1, n / 2 ^ 1, 1
    puts [a, b, c] * " "
  else
    p -1
  end
end

num_cases = gets.to_i
for i in (1..num_cases)
  n = gets.to_i
  if n % 2 == 0
    puts "0 #{n/2} #{n/2+1}"
  else
    puts "-1"
  end
end


#!/usr/bin/ruby
a=b=0
gets;$<.each{|_|
	x,y=_.split.map &:to_i
	a+=1 if x>y
	b+=1 if x<y
}
puts ['Friendship is magic!^^','Chris','Mishka'][b<=>a]

n = gets.to_i

result = (1..n).map do
  a, b = gets.split(' ').map(&:to_i)
  (a == b) ? 0 : (a - b) / (a - b).abs
end.inject(&:+)

if result > 0
  puts 'Mishka'
elsif result < 0
  puts 'Chris'
else
  puts 'Friendship is magic!^^'
end

n = gets.to_i
s = 0
for i in 1..n do
    a, b = gets.split.map(&:to_i)
    if a > b then
        s += 1
    elsif a < b then
        s -= 1
    end
end
if s > 0 then
    puts 'Mishka'
elsif s < 0 then
    puts 'Chris'
else
    puts 'Friendship is magic!^^'
end

gameNum = gets.chomp.to_i

mWin = 0
cWin = 0

gameNum.times do
	m, c = gets.chomp.split(' ').map(&:to_i)

	if m > c
		mWin += 1
	elsif c > m
		cWin += 1
	end
end

if mWin > cWin
	puts 'Mishka'
elsif cWin > mWin
	puts 'Chris'
else
	puts 'Friendship is magic!^^'
end

n = Integer(gets)
a, b = 0, 0
n.times do
  aa, bb = gets.split.map(&:to_i)
  if aa > bb
    a += 1
  end
  if aa < bb
    b += 1
  end
end

if a > b
  puts "Mishka"
end

if a < b
  puts "Chris"
end

if a == b
  puts "Friendship is magic!^^"
end

n = gets.strip.to_i
m_count = c_count = 0
n.times do
    m, c = gets.strip.split.map(&:to_i)
    m_count += 1 if m > c
    c_count += 1 if c > m
end
ans = "Friendship is magic!^^"
if m_count > c_count
    ans = "Mishka"
elsif m_count < c_count
    ans = "Chris"
end
puts ans

n=gets.to_i
ans=0
n.times{
	a,b=gets.split.map{|e| e.to_i}
	ans=ans+(a<=>b)
}
puts ["Chris","Friendship is magic!^^","Mishka"][(ans<=>0)+1]

n = gets.to_i
r = 0
n.times do |i|
	m,c = gets.split.map(&:to_i)
	if m > c
		r += 1
	elsif c > m
		r -= 1
	end
end
if r > 0
	puts 'Mishka'
elsif r < 0
	puts 'Chris'
else
	puts 'Friendship is magic!^^'
end

n = gets.to_i
r = 0

(1..n).each do
  a, b = gets.split.map(&:to_i)
  r += a <=> b
end

puts case
       when r > 0
         'Mishka'
       when r < 0
         'Chris'
       else
         'Friendship is magic!^^'
     end

gets;r=0;$<.map{|e|r+=eval(e.split*"<=>")};puts r>0?'Mishka':r<0?'Chris':'Friendship is magic!^^'

n = gets.to_i

m = 0
c = 0

n.times do
  a,b = gets.split.map(&:to_i)
  m+=1 if a > b
  c+=1 if b > a
end

if m > c
  puts "Mishka"
elsif c > m
  puts "Chris"
else
  puts "Friendship is magic!^^"
end

n = gets.chomp.to_i
a, b = 0, 0

n.times do
  c, d = gets.chomp.split.map(&:to_i)
  next if c == d
  c > d ? a += 1 : b += 1
end

case
when a == b
  puts "Friendship is magic!^^"
when a > b
  puts "Mishka"
else
  puts "Chris"
end

n = gets.to_i
c = 0
m = 0
(0...n).each do
    a, b = gets.split.map &:to_i
    if (a < b)
        c += 1
    else
        if (a > b)
            m += 1
        end
    end
end

if (c == m)
    puts "Friendship is magic!^^"
else
    if (m > c)
        puts "Mishka"
    else
        puts "Chris"
    end
end

n = gets.to_i()
mishka = 0
chris = 0
for i in (1..n)
  mc = gets.split(' ')
  if mc[0].to_i() > mc[1].to_i()
    mishka += 1
  elsif mc[0].to_i() < mc[1].to_i()
    chris += 1
  end
end
if mishka > chris
  puts "Mishka"
elsif mishka == chris
  puts "Friendship is magic!^^"
else
  puts "Chris"
end

# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.chomp end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

N = gi
MC = ngis N

v = MC.map{|m, c| m <=> c }.inject(:+)

case
when v < 0
    puts 'Chris'
when v > 0
    puts 'Mishka'
else
    puts 'Friendship is magic!^^'
end

# coding: utf-8

# if __FILE__ == $0
# end

rounds = gets.to_i
mishka_wins = 0
chris_wins = 0
draws = 0
rounds.times {
    mishka, chris = gets.split.map(&:to_i)

    if mishka > chris
        mishka_wins += 1
    elsif mishka < chris
        chris_wins += 1
    else
        draws += 0
    end
}

if mishka_wins > chris_wins
    puts "Mishka"
elsif mishka_wins < chris_wins
    puts "Chris"
else
    puts "Friendship is magic!^^"
end

n = gets.to_i
ar = [0,0]
n.times do
  s = gets.split.map(&:to_i)
  ar[0] += 1 if s[0] > s[1]
  ar[1] += 1 if s[1] > s[0]
end
puts "Mishka" if ar[0] > ar[1]
puts "Chris" if ar[1] > ar[0]
puts "Friendship is magic!^^" if ar[0] == ar[1]

mc = 0
cc = 0
gets.to_i.times do
  m, c = gets.split.map(&:to_i)
  if m > c
    mc += 1
  elsif m < c
    cc += 1
  end
end

if mc > cc
  puts 'Mishka'
elsif mc < cc
  puts 'Chris'
else
  puts 'Friendship is magic!^^'
end

n = gets.to_i
wins = Hash.new(0)
n.times.map do
  a, b = gets.split.map(&:to_i)
  if a - b > 0
    wins['Mishka'] += 1
  elsif a - b < 0
    wins['Chris'] += 1
  end
end
puts wins['Mishka'] == wins['Chris'] ? 'Friendship is magic!^^' : wins.max_by{|k, v| v}[0]

games = gets.chomp.to_i
results = []
for i in (0..games-1)
	results[i] = gets.chomp
end

count = 0
for i in (0..games-1)
	split = results[i].split
	if split[0].to_i > split[1].to_i
		count += 1
	elsif split[0].to_i < split[1].to_i
		count -= 1
	else
		count += 0
	end
end

if count > 0
	print "Mishka"
elsif count == 0
	print "Friendship is magic!^^"
else
	print "Chris"
end

a=[0,0,0]
g=["Friendship is magic!^^","Mishka","Chris"]
gets.to_i.times {|x| s=gets.split.map(&:to_i); a[s[0]<=>s[1]]+=1; }
puts g[a[1]<=>a[-1]]

n = gets.to_i
m = c = 0
for i in 1..n
  mi, ch = gets.split.map(&:to_i)
  if mi > ch
    m += 1
  elsif ch > mi
    c += 1
  end
end

if m > c
  puts 'Mishka'
elsif m < c
  puts 'Chris'
else
  puts 'Friendship is magic!^^'
end

a=b=0
gets.to_i.times do
	x = gets.split.map(&:to_i)
	a+=1 if x[0]>x[1]
	b+=1 if x[0]<x[1]
end

puts ["Friendship is magic!^^","Mishka","Chris"] [a<=>b]

n = gets.to_i
m=0
c=0
n.times do
	x,y = gets.split.map(&:to_i)
	if x>y
		m+=1
	elsif x<y
		c+=1
	end
end
if m>c
	puts "Mishka"
elsif c>m
	puts "Chris"
else
	puts "Friendship is magic!^^"
end

jj = 0
gets.to_i.times do
  aa, bb = gets.split.map(&:to_i)
  jj += aa <=> bb
end

if jj > 0
  puts "Mishka"
elsif jj < 0
  puts "Chris"
else
  puts "Friendship is magic!^^"
end

n = gets.to_i
ar = [0,0]
n.times do
  s = gets.split.map(&:to_i)
  ar[0] += s[0]
  ar[1] += s[1]
end
puts "Mishka" if ar[0] > ar[1]
puts "Chris" if ar[1] > ar[0]
puts "Friendship is magic!^^" if ar[0] == ar[1]

n = gets.to_i
r = 0
n.times do |i|
	m,c = gets.split.map(&:to_i)
	if m > c
		r += 1
	elsif c > m
		r -= 1
	end
end
if r > 0
	p 'Mishka'
elsif r < 0
	p 'Chris'
else
	p 'Friendship is magic!^^'
end

gets;r=0;$<.map{|e|r+=eval(e.split*"<=>")};puts r>0?'Mishka':r<0?'Chris':'Friendship is magic^^'

games = gets.chomp.to_i
results = []
for i in (0..games-1)
	results[i] = gets.chomp
end

count = 0
for i in (0..games-1)
	split = results[i].split
	for x in (0..1)
		if split[0].to_i > split[1].to_i
			count += 1
		else
			count -= 1
		end
	end
end

if count > 0
	print "Mishka"
elsif count == 0
	print "Friendship is magic!^^"
else
	print "Chris"
end

games = gets.chomp.to_i
results = []
for i in (0..games-1)
	results[i] = gets.chomp
end

mishka = 0
chris = 0
for i in (0..games-1)
	split = results[i].split
	mishka += split[0].to_i
	chris += split[1].to_i
end

if mishka > chris
	print "Mishka"
elsif mishka == chris
	print "Friendship is magic!^^"
else
	print "Chris"
end

a=[0,0,0]
g=["Friendship is magic!^^","Mishka","Chris"]
gets.to_i.times {|x| s=gets.split.map(&:to_i); a[s[1]<=>s[0]]+=1; }
puts g[a[1]<=>a[-1]]

n = gets.to_i
wins = Hash.new(0)
n.times.map do
  a, b = gets.split.map(&:to_i)
  if a - b > 0
    wins['Mishka'] += 1
  elsif a - b < 0
    wins['Chris'] += 1
  end
end
puts wins['Mishka'] == wins['Chris'] ? 'Friendship is magic!^^' : wins.max[0]

n = gets.to_i
wins = Hash.new(0)
n.times.map do
  a, b = gets.split.map(&:to_i)
  if a - b > 0
    wins['Misha'] += 1
  elsif a - b < 0
    wins['Chris'] += 1
  end
end
puts wins['Misha'] == wins['Chris'] ? 'Friendship is magic!^^' : wins.max[0]


t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  ok = true
  for i in 0...(n-1) do
    if (a[i+1] - a[i]) % 2 != 0
      ok = false
    end
  end
  puts ok ? "YES" : "NO"
end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  l = gets.chomp.split.map(&:to_i)
  num_of_odd, num_of_even = l.partition{|c| c.odd? }
  puts (!num_of_odd.size.zero? && !num_of_even.size.zero? ? "NO" : "YES")
end

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|x| x.to_i}
    ev = od = 0
    arr.each do |x|
        if x % 2 == 0
            ev += 1
        else
            od += 1
        end
    end
    if(ev == 0 || od == 0)
        puts "YES"
    else
        puts "NO"
    end
end

1.upto(gets.to_i) do
  gets
  a = gets.split(' ').map(&:to_i)

  puts a.all? { |e| e.even? == a.first.even? } ? 'YES' : 'NO'
end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  l = gets.chomp.split.map(&:to_i)
  has_odd, has_even = false, false
  l.each{|c| c.odd? ? (has_odd = true) : (has_even = true); (break) if has_odd && has_even }
  puts (has_odd && has_even ? "NO" : "YES")
end


n=gets.to_i
a=Array.new(n)
k=0
for i in 0...n do
    q=gets
    a[i]=q.to_f
    k+=q.to_i
end
    for i in 0...n do
        if k>0 then
            if a[i].to_i != a[i].floor or a[i].to_i != a[i].ceil then
                if a[i]>0 then
                    puts a[i].floor
                else
                    puts a[i].floor
                    k-=1
                end
                else
                    puts a[i].to_i
            end
        elsif k<0 then
            if a[i].to_i != a[i].floor or a[i].to_i != a[i].ceil then
                if a[i]>0 then
                    puts a[i].ceil
                    k+=1
                else
                    puts a[i].ceil
                end
                    else
                    puts a[i].to_i
            end
            else
            puts a[i].to_i
        end
    end

n = gets.to_i
a = n.times.map{gets.chomp}
b = Array.new(n,0)
sum = 0
a.each_with_index do |s,i|
    b[i] = s.to_i
    if s[0] == "-" && s[-5..-1] != "00000"
        b[i] -= 1
    end
    sum += b[i]
end
a.each_with_index do |s,i|
    break if sum == 0
    next if s[-5..-1] == "00000"
    sum += 1
    b[i] += 1
end
puts b

N = gets.to_i
sd = 0
incremantables = {}
cs = []
(0...N).map{ |i|
  a = gets.to_f
  c = a.floor
  cs << c
  d = a - c
  sd += d
  incremantables[i] = 1 if d > 0
}
sd = (sd + 0.5).floor

puts (0...N).map{ |i|
  if sd > 0 && incremantables[i]
    sd -= 1
    cs[i] + 1
  else
    cs[i]
  end
}

N = gets.to_i
sd = 0
incremantables = {}
cs = []
(0...N).map{ |i|
  a = gets.to_f
  c = a.floor
  cs << c
  d = a - c
  sd += d
  incremantables[i] = 1 if d > 0
}

puts (0...N).map{ |i|
  if sd > 0 && incremantables[i]
    sd -= 1
    cs[i] + 1
  else
    cs[i]
  end
} * " "

n = gets.to_i
a = n.times.map{gets.chomp}
b = a.map(&:to_i)
sum = b.inject(:+)
a.each_with_index do |s,i|
    break if sum == 0
    next if s[-5..-1] == "00000"
    sum += 1
    b[i] += 1
end
puts b

n=gets.to_i
a=Array.new(n)
k=0
for i in 0...n do
    q=gets
    a[i]=q.to_f
    k+=q.to_i
end
    for i in 0...n do
        if k>0 then
            if a[i]>0 and a[i].to_i!=a[i].ceil.to_i then
                puts a[i].ceil
            else
                puts a[i].floor
                k-=1
            end
        elsif k<0 then
            if a[i]>0 and a[i].to_i!=a[i].ceil.to_i then
                puts a[i].ceil
                k+=1
            else
                puts a[i].floor
            end
        else
                puts a[i].to_i
        end
    end

n=gets.to_i
a=Array.new(n)
k=0
for i in 0...n do
    q=gets
    a[i]=q.to_f
    k+=q.to_i
end
    for i in 0...n do
        if k>0 then
            if a[i]>0 then
                puts a[i].ceil
            else
                puts a[i].floor
                k-=1
            end
        elsif k<0 then
            if a[i]>0 then
                puts a[i].ceil
            else
                puts a[i].floor
                k+=1
            end
        else
                puts a[i].to_i
        end
    end

n=gets.to_i
a=Array.new(n)
k=0
for i in 0...n do
    q=gets
    a[i]=q.to_f
    k+=q.to_i
end
    for i in 0...n do
        if k>0 then
            if a[i]>0 then
                puts a[i].floor
                k-=1
            else
                puts a[i].ceil
            end
        elsif k<0 then
            if a[i]>0 then
                puts a[i].ceil
            else
                puts a[i].floor
            end
        else
                puts a[i].to_i
        end
    end

n=gets.to_i
a=Array.new(n)
k=0
for i in 0...n do
    q=gets
    a[i]=q.to_f
    k+=q.to_i
end
    for i in 0...n do
        if k>0 then
            if a[i].to_i != a[i].floor then
                if a[i]>0 then
                    puts a[i].floor
                else
                    puts a[i].floor
                    k-=1
                end
                else
                    puts a[i].to_i
            end
        elsif k<0 then
            if a[i].to_i != a[i].floor then
                if a[i]>0 then
                    puts a[i].ceil
                    k+=1
                else
                    puts a[i].ceil
                end
                    else
                    puts a[i].to_i
            end
            else
            puts a[i].to_i
        end
    end


n=gets.to_i
a=[]
n.times{a<<gets.to_i}
a.sort!.reverse!
i=c=0
j=(n+1)/2
while j<n
  (c+=1;i+=1)if a[i]/2>=a[j]
  j+=1
end
p n-c

n = gets.to_i
a = []
dem = []
dem1 = 0

(0..n-1).each do |i|
    a[i] = gets.to_i
end

a.sort!

i = 0

while (a[i]<= (a[n-1]/2)) && (i<=(n/2-1)) do
    if i == 0
        j = i+1
    else
        j = n - dem[i-1]
    end
    while ((2*a[i])>a[j]) && (j<n) do
        j += 1
    end
    dem[i] = n - j
    i += 1
end

i -= 1
while i>=0 do
    if dem[i] > dem1
        dem1 += 1
    end
    i -= 1
end

puts n-dem1

n = gets.to_i
a = []
dem = []
dem1 = 0

(0..n-1).each do |i|
    a[i] = gets.to_i
end

a.sort!

i = 0

while (a[i]<= (a[n-1]/2)) && (i<=(n/2-1)) do
    j = i+1
    while ((2*a[i])>a[j]) && (j<n) do
        j += 1
    end
    dem[i] = n - j
    i += 1
end

puts dem

i -= 1
while i>=0 do
    if dem[i] > dem1
        dem1 += 1
    end
    i -= 1
end

puts n-dem1

n=gets.to_i
a=[]
n.times{a<<gets.to_i}
a.sort!.reverse!
i=c=0
j=n/2
while j<n
  (c+=1;i+=1)if a[i]/2>=a[j]
  j+=1
end
p n-c

n=gets.to_i
a=[]
n.times{a<<gets.to_i}
a.sort!.reverse!
i=j=c=0
while j<n
  (c+=1;i+=1)if a[i]/2>=a[j]
  j+=1
end
p n-c


h = {}
q = 1
for i in 'a'..'z'
  for j in 'a'..'z'
    next if i == j
    h[i + j] = q
    q += 1
  end
end

gets.to_i.times do
  puts h[gets.chomp]
end

a=" "*27
for i in 0..25
	a[i]=('a'.ord+i).chr
end
r=[]
a.split('').each do |e|
	a.split('').each do |u|
		if e!=u && e!=" " && u!=" "
			r.append(e+u)
		end
	end
end
for t in 1..gets[..-2].to_i
	puts r.index(gets[..-2])+1
end

m, az = {}, "abcdefghijklmnopqrstuvwxyz".chars
az.product(az).filter {|a| a[0] !=  a[1]}.map{|a| a[0] + a[1]}.each_with_index {|a,i| m[a] = i+1}
gets.to_i.times  { puts m[gets.chomp] }

words = {}
cnt = 1
('a'..'z').each do |a|
  ('a'..'z').each do |b|
    next if a == b
    words[a + b] = cnt
    cnt += 1
  end
end

gets.to_i.times do
  s = gets.strip
  p words[s]
end

map = [*"a".."z"].flat_map do |a|
  ([*"a".."z"] - [a]).map do |b|
    a + b
  end
end.zip(1..).to_h

gets.to_i.times do
  p map[gets.chomp]
end

t=gets.chomp.to_i
arr = ('a'..'z').to_a
arrAZ =[]
kol=0
for i in 0..25
    for j in 0..25
        if (arr[i].to_s!=arr[j].to_s)
            kol+=1
            arrAZ[kol]=arr[i]+arr[j]
        end
    end
end

#puts arrAZ.compact
#puts arrAZ.length

t.times do
    puts arrAZ.index(gets.chomp.to_s)
end

t = gets.to_i
a = ?a.ord
t.times do
    x, y = gets.chomp.each_byte.map { |b| b - a }
    i = x * 25 + y + 1
    i -= 1 if y > x
    puts i
end


input = gets.strip.split(' ').map(&:to_i)
n = input[0]
a = input[1]
checkps = gets.strip.split(' ').map(&:to_i).sort
left_dists = []
right_dists = []
checkps.each do |cp|
  if cp < a
    left_dists << a - cp
  elsif cp > a
    right_dists << cp - a
  end
end
checkps.delete(a)

if checkps.length > 0
  if left_dists.length > 0 and right_dists.length > 0
    mindist1 = 0
    mindist2 = 0

    old_left = left_dists.dup
    left_dists.slice! left_dists.index(left_dists.max)
    ldm = 0
    rdm = 0
    if not left_dists.max.nil?
      ldm = left_dists.max
    end
    if not right_dists.max.nil?
      rdm = right_dists.max
    end
    if (ldm > rdm)
      mindist1 = 2 * rdm + ldm
    else
      mindist1 = rdm + 2 * ldm
    end

    left_dists = old_left
    right_dists.slice! right_dists.index(right_dists.max)
    ldm = 0
    rdm = 0
    if not left_dists.max.nil?
      ldm = left_dists.max
    end
    if not right_dists.max.nil?
      rdm = right_dists.max
    end
    if (ldm > rdm)
      mindist2 = 2 * rdm + ldm
    else
      mindist2 = rdm + 2 * ldm
    end

    puts [mindist1, mindist2].min

  elsif left_dists.length > 0
    left_dists.slice! left_dists.index(left_dists.max)
    ldm = 0
    if not left_dists.max.nil?
      ldm = left_dists.max
    end
    puts ldm
  else
    right_dists.slice! right_dists.index(right_dists.max)
    rdm = 0
    if not right_dists.max.nil?
      rdm = right_dists.max
    end
    puts rdm
  end
else
  puts 0
end

#!/usr/bin/ruby
n,s,*a=$<.read.split.map &:to_i
a.sort!
if n<2
	p 0
else
	p [[0,-2],[1,-1]].map{|i,j|
		x,y=[[s-a[i],0].max,[a[j]-s,0].max].minmax
		x*2+y
	}.min
end

#!/usr/bin/ruby
n,s,*a=$<.read.split.map &:to_i
a.sort!
if n<2
	p 0
elsif s<a[0]
	p a[-2]-s
elsif a[-1]<s
	p s-a[1]
else
	p [[0,-2],[1,-1]].map{|i,j|
		x,y=[[s-a[i],0].max,[a[j]-s,0].max].minmax
		x*2+y
	}.min
end

input = gets.strip.split(' ').map(&:to_i)
n = input[0]
a = input[1]
checkps = gets.strip.split(' ').map(&:to_i).sort
dists = []
checkps.each do |cp|
  dists << (cp-a).abs
end
tbr = dists.index(dists.max)
checkps.slice! tbr
dists.slice! tbr
left_exists = false
right_exists = false
checkps.each do |cp|
  if cp < a
    left_exists = true
  elsif cp > a
    right_exists = true
  end
end
if left_exists and right_exists
  tbds = dists.index(dists.max)
  mindist = 0
  if checkps[tbds] - a > 0
    mindist = dists[0] * 2 + dists[dists.length-1]
  else
    mindist = dists[dists.length-1] * 2 + dists[0]
  end
  puts mindist
else
  if left_exists
    puts dists[0]
  elsif right_exists
    puts dists[dists.length-1]
  else
    puts 0
  end
end

input = gets.strip.split(' ').map(&:to_i)
n = input[0]
a = input[1]
checkps = gets.strip.split(' ').map(&:to_i).sort
dists = []
left_exists = false
right_exists = false
checkps.each do |cp|
  dists << (cp-a).abs
  if cp < a
    left_exists = true
  elsif cp > a
    right_exists = true
  end
end
tbr = dists.index(dists.max)
checkps.slice! tbr
dists.slice! tbr
if left_exists and right_exists
  tbds = dists.index(dists.max)
  mindist = 0
  if checkps[tbds] - a > 0
    mindist = dists[0] * 2 + dists[dists.length-1]
  else
    mindist = dists[dists.length-1] * 2 + dists[0]
  end
  puts mindist
else
  if left_exists
    puts dists[0]
  elsif right_exists
    puts dists[dists.length-1]
  else
    puts 0
  end
end

input = gets.strip.split(' ').map(&:to_i)
n = input[0]
a = input[1]
checkps = gets.strip.split(' ').map(&:to_i).sort
dists = []
checkps.each do |cp|
  dists << (cp-a).abs
end
tbr = dists.index(dists.max)
checkps.slice! tbr
dists.slice! tbr
tbds = dists.index(dists.max)
mindist = 0
if checkps[tbds] - a > 0
  mindist = dists[0] * 2 + dists[dists.length-1]
else
  mindist = dists[dists.length-1] * 2 + dists[0]
end
puts mindist


data_lines = gets.chomp

def prodess_arr arr
	# p '-----'
	# p arr
	# p '-----'
	new_arr = []
	arr = arr.sort
	middle = arr.length / 2
	(0..arr.length-1).each do |index|
		if index < middle
			new_arr[index * 2] = arr[index]
		else
			new_index = index - middle
			new_arr[new_index*2+1] = arr[index]
		end
	end
	new_arr

	# success = false
	# while success == false
	# 	flag = true
	# 	(0..arr.length-1).each do |index|
	# 		# p "number = #{arr[index]} index = #{index}"
	# 		neigh_l_index = index-1
	# 		neigh_l_index = arr.length-1 if neigh_l_index < 0
	# 		neigh_r_index = index+1
	# 		neigh_r_index = 0 if neigh_r_index > arr.length-1
	# 		sr_ar = (arr[neigh_l_index].to_f + arr[neigh_r_index].to_f) / 2.0
	# 		# p "sr_ar - arr[index] = #{sr_ar - arr[index]}"
	# 		if sr_ar - arr[index] == 0.0
	# 			# p arr
	# 			flag = false
	# 			neigh_rr_index = index+2
	# 			if neigh_rr_index > arr.length-1
	# 				if neigh_rr_index == arr.length-1
	# 					neigh_rr_index = 0
	# 				else
	# 					neigh_rr_index = 1
	# 				end
	# 			end
	# 			buffer = arr[neigh_r_index]
	# 			arr[neigh_r_index] = arr[neigh_rr_index]
	# 			arr[neigh_rr_index] = buffer
	# 			# p arr
	# 		end
	# 	end
	# 	# p success
	# 	success = true if flag
	# 	# p success
	# end
	# arr
end
arrs = []
(1..data_lines.to_i).each do
	b = gets.chomp
	arrs.push prodess_arr(gets.chomp.split(' ').map{|_e| _e.to_i}).join(' ')
end
arrs.each do |a|
	puts a
end

results = []
t = gets.chomp
(1..t.to_i).each do |iteration|
	n = gets.chomp.to_i
	a = gets.chomp.split(' ').map{|e| e.to_i}.sort
	b = []

	(0..n-1).each do |idx|
		b[2*idx] = a[idx]
		b[2*idx+1] = a[n+idx]
	end

	results.push(b)
end
results.each do |res|
	puts res
end

def solution(n, arr)
	i = 0

	while i < 2 * n
		prev_index = prev_index(arr, 2 * n, i)
		next_index = next_index(arr, 2 * n, i)

		if (arr[prev_index] + arr[next_index]) / 2.0 == 1.0 * arr[i]
			arr[i], arr[prev_index] = arr[prev_index], arr[i]
			i = 0
		else
			i += 1
		end
	end

	arr
end

def prev_index(arr, n,  i)
	if i == 0
		return n - 1
	else
		return i - 1
	end
end

def next_index(arr, n,  i)
	if i == n - 1
		return 0
	else
		return i + 1
	end
end

test_cases = gets.to_i

test_cases.times do |test_case|
  n = gets.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  puts solution(n, arr).join(' ')
end

def check_valid arr
	arr.each_with_index do |el, idx|
		mid = (arr[idx+1].to_f + arr[idx-1].to_f) / 2
		if mid == el.to_f
			return false
		end
	end
	true
end

results = []
t = gets.chomp
(1..t.to_i).each do |iteration|
	n = gets.chomp
	a = gets.chomp.split(' ').map{|e| e.to_i}

	a.permutation.each do |perm|
		if check_valid perm
			results.push perm
			break
		end
	end
end
results.each do |res|
	p res.join("\s")
end

def check_valid arr
	arr.each_with_index do |el, idx|
		mid = (arr[idx+1].to_f + arr[idx-1].to_f) / 2
		if mid == el.to_f
			return false
		end
	end
	true
end

results = []
t = gets.chomp
(1..t.to_i).each do |iteration|
	n = gets.chomp
	a = gets.chomp.split(' ').map{|e| e.to_i}

	a.permutation.each do |perm|
		if check_valid perm
			results.push perm
			break
		end
	end
end
p results

data_lines = gets.chomp

def prodess_arr arr
	# p '-----'
	# p arr
	# p '-----'
	new_arr = []
	arr = arr.sort
	(0..arr.length-1).each do |index|
		if index % 2 == 0
			new_arr[index / 2] = arr[index]
		else
			middle = arr.length / 2
			new_arr[(index / 2) + middle] = arr[index]
		end
	end
	new_arr

	# success = false
	# while success == false
	# 	flag = true
	# 	(0..arr.length-1).each do |index|
	# 		# p "number = #{arr[index]} index = #{index}"
	# 		neigh_l_index = index-1
	# 		neigh_l_index = arr.length-1 if neigh_l_index < 0
	# 		neigh_r_index = index+1
	# 		neigh_r_index = 0 if neigh_r_index > arr.length-1
	# 		sr_ar = (arr[neigh_l_index].to_f + arr[neigh_r_index].to_f) / 2.0
	# 		# p "sr_ar - arr[index] = #{sr_ar - arr[index]}"
	# 		if sr_ar - arr[index] == 0.0
	# 			# p arr
	# 			flag = false
	# 			neigh_rr_index = index+2
	# 			if neigh_rr_index > arr.length-1
	# 				if neigh_rr_index == arr.length-1
	# 					neigh_rr_index = 0
	# 				else
	# 					neigh_rr_index = 1
	# 				end
	# 			end
	# 			buffer = arr[neigh_r_index]
	# 			arr[neigh_r_index] = arr[neigh_rr_index]
	# 			arr[neigh_rr_index] = buffer
	# 			# p arr
	# 		end
	# 	end
	# 	# p success
	# 	success = true if flag
	# 	# p success
	# end
	# arr
end
arrs = []
(1..data_lines.to_i).each do
	b = gets.chomp
	arrs.push prodess_arr(gets.chomp.split(' ').map{|_e| _e.to_i}).join(' ')
end
arrs.each do |a|
	puts a
end

def solution(n, arr)
	(0..(2 * n - 1)).each do |i|
		prev_index = prev_index(arr, 2 * n, i)
		next_index = next_index(arr, 2 * n, i)

		# puts "i: #{i}"
		# puts "prev_index: #{prev_index}"
		# puts "next_index: #{next_index}"
		# puts "------------"
		if (arr[prev_index] + arr[next_index]) / 2.0 == 1.0 * arr[i]
			# prev_index = prev_index(arr, n, i - 1)
			arr[i], arr[prev_index] = arr[prev_index], arr[i]
		end
	end

	arr
end

def prev_index(arr, n,  i)
	if i == 0
		return n - 1
	else
		return i - 1
	end
end

def next_index(arr, n,  i)
	if i == n - 1
		return 0
	else
		return i + 1
	end
end

test_cases = gets.to_i

test_cases.times do |test_case|
  n = gets.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  puts solution(n, arr).join(' ')
end


#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

n = inp[0]
n.times do
  a,b,c = inp.sort
  d =  (a+b) - c
  puts  (d <= 0)? a+b : c + d/2
end

class SweetProblem

  def initialize(debug=false)
    @debug = debug
  end

  def get_num_days_can_eat(input_arr)
    sum = 0
    input_arr.each do |val|
      sum = sum + val
    end

    ans = sum / 2

    sum_red_green = input_arr[0] + input_arr[1]
    sum_green_blue = input_arr[1] + input_arr[2]
    sum_red_blue = input_arr[0] + input_arr[2]

    if sum_red_green < ans
      ans = sum_red_green
    end
    if sum_green_blue < ans
      ans = sum_green_blue
    end
    if sum_red_blue < ans
      ans = sum_red_blue
    end

    ans
  end

  def main
    i = 0
    num_lines = 0
    while input = gets
      if i == 0
        num_lines = input.to_i
        i = i + 1
        next
      else
        input_arr = input.split(' ').map!(&:to_i)

        num_days_can_eat = get_num_days_can_eat(input_arr)

        puts num_days_can_eat
        i = i + 1
        break if i > num_lines
      end
    end
  end

end

sweet_problem = SweetProblem.new
sweet_problem.main

class SweetProblem

  def initialize(debug=false)
    @debug = debug
  end
  def to_hash(array)
    red = array[0]
    green = array[1]
    blue = array[2]
    result = Hash.new
    result['R'] = red
    result['G'] = green
    result['B'] = blue
    result
  end

  def min_color(hash)
    min = (10**8) + 1
    min_color_key = nil
    hash.each do |key,val|
      next if val == 0
      if val < min
        min = val
        min_color_key = key
      end
    end
    return min_color_key
  end

  def max_color(hash, color_key)
    two_colors_same_max = false
    max = 0
    max_color_key = nil
    hash.each do |key,val|
     if val == max and key != color_key
       two_colors_same_max = true
     end
     if val > max and key != color_key
        max = val
        max_color_key = key
      end
    end
    return max_color_key, two_colors_same_max
  end

  def num_colors_left(hash)
    num_colors = 0
    hash.each do |key,val|
      if val > 0
        num_colors = num_colors + 1
      end
    end
    num_colors
  end

  def get_num_days_can_eat(input_arr)
    hash = to_hash(input_arr)
    puts hash if @debug

    min_color = min_color(hash)
    puts "min_color: #{min_color}" if @debug
    max_color, two_colors_same_max = max_color(hash, min_color)
    puts "max_color: #{max_color}" if @debug
    puts "two_colors_same_max: #{two_colors_same_max}" if @debug

    num_days_can_eat = 0
    while num_colors_left(hash) >= 2
      min_color_count = hash[min_color]
      puts "min_color_count: #{min_color_count}" if @debug

      if two_colors_same_max and (num_colors_left(hash) == 3)
        max_color_count = hash[max_color] - 1
        hash[max_color] = max_color_count
      else
        max_color_count = hash[max_color] - min_color_count
        hash[max_color] = max_color_count
      end

      puts "max_color_count is now: #{hash[max_color]}" if @debug

      if two_colors_same_max and (num_colors_left(hash) == 3)
        min_color_count = hash[min_color] - 1
        hash[min_color] = min_color_count
      else
        hash[min_color] = 0
      end
      puts "min_color_count is now: #{hash[min_color]}" if @debug

      num_days_can_eat = num_days_can_eat + min_color_count
      puts "num_days_can_eat: #{num_days_can_eat}" if @debug

      min_color = min_color(hash)
      max_color, two_colors_same_max = max_color(hash, min_color)
      puts hash if @debug
      puts "min_color: #{min_color}" if @debug
      puts "max_color: #{max_color}" if @debug
    end
    num_days_can_eat
  end

  def main
    i = 0
    num_lines = 0
    while input = gets
      if i == 0
        num_lines = input.to_i
        i = i + 1
        next
      else
        input_arr = input.split(' ').map!(&:to_i)

        num_days_can_eat = get_num_days_can_eat(input_arr)

        puts num_days_can_eat
        i = i + 1
        break if i > num_lines
      end
    end
  end

end

sweet_problem = SweetProblem.new
sweet_problem.main

def to_hash(array)
  red = array[0]
  green = array[1]
  blue = array[2]
  result = Hash.new
  result['R'] = red
  result['G'] = green
  result['B'] = blue
  result
end

def min_color(hash)
  min = (10**8) + 1
  min_color_key = nil
  hash.each do |key,val|
    next if val == 0
    if val < min
      min = val
      min_color_key = key
    end
  end
  return min_color_key
end

def max_color(hash, color_key)
  max = 0
  max_color_key = nil
  hash.each do |key,val|
   if val > max and key != color_key
      max = val
      max_color_key = key
    end
  end
  return max_color_key
end

def num_colors_left(hash)
  num_colors = 0
  hash.each do |key,val|
    if val > 0
      num_colors = num_colors + 1
    end
  end
  num_colors
end

i = 0
num_lines = 0
while input = gets
  if i == 0
    num_lines = input.to_i
    i = i + 1
    next
  else
    input_arr = input.split(' ').map!(&:to_i)

    hash = to_hash(input_arr)

    min_color = min_color(hash)
    max_color = max_color(hash, min_color)

    num_days_can_eat = 0
    while num_colors_left(hash) >= 2
      min_color_count = hash[min_color]

      max_color_count = hash[max_color] - min_color_count
      hash[max_color] = max_color_count

      hash[min_color] = 0

      min_color = min_color(hash)
      max_color = max_color(hash, min_color)
      num_days_can_eat = num_days_can_eat + min_color_count
    end

    puts num_days_can_eat
    i = i + 1
    break if i > num_lines
  end
end


t = gets.chomp.to_i

def calc(pos, a, b)
    first = pos[b][-1]
    second = pos[a].reverse_each.find { |v| v > first }
    return Float::INFINITY if !second
    return first + second - first - 1
end

t.times do
    n = gets.chomp

    pos = Hash.new { |h, k| h[k] = [] }
    n.split('').each_with_index do |c, idx|
        if c == '0' || c == '2' || c == '5' || c == '7'
            pos[c] << n.length - idx - 1
        end
    end

    possibilities = []
    if pos['0'].length >= 2
        possibilities << calc(pos, '0', '0')
    end

    if pos['0'].length >= 1 && pos['5'].length >= 1
        possibilities << calc(pos, '5', '0')
    end

    if pos['2'].length >= 1 && pos['5'].length >= 1
        possibilities << calc(pos, '2', '5')
    end

    if pos['7'].length >= 1 && pos['5'].length >= 1
        possibilities << calc(pos, '7', '5')
    end

    puts possibilities.min
end

def f(dgts)
  n0 = dgts.length
  i0 = dgts.rindex("0") || 0
  j0 = i0 > 0 && dgts.rindex(/[05]/, i0 - 1) || -10000

  n5 = dgts.length
  i5 = dgts.rindex("5") || 0
  j5 = i5 > 0 && dgts.rindex(/[27]/, i5 - 1) || -10000

  # p [i0, j0, i5, j5]
  [n0 - 2 - j0, n5 - 2 - j5].min
end

gets.to_i.times do

n = gets.chomp
puts f(n)

end

t = gets.chomp.to_i

def solve
	n = gets.chomp.to_i
	d = n.digits
	d = d.reverse
	ans = 1e9
	inf = -1e9
	#25 75 50 00
	index = {2=>inf, 7=>inf, 5=>inf, 0=>inf}

	(0..(d.length - 1)).each { |i|
		if (d[i] == 5)
			ans = [ans, (i - index[2] - 1) + d.length - i - 1].min
			ans = [ans, (i - index[7] - 1) + d.length - i - 1].min
		end
		if (d[i] == 0)
			ans = [ans, (i - index[0] - 1) + d.length - i - 1].min
			ans = [ans, (i - index[5] - 1) + d.length - i - 1].min
		end
		if d[i] == 2 || d[i] == 7 || d[i] == 5 || d[i] == 0
			index[d[i]] = i
		end
	}
	puts ans
end

t.times {
	solve
}

t = gets.chomp.to_i

def solve
	n = gets.chomp.to_i
	d = n.digits
	d = d.reverse
	ans = 1e9
	inf = -1e9
	#25 75 50 00
	index = {2=>inf, 7=>inf, 5=>inf, 0=>inf}

	(0..(d.length - 1)).each { |i|
		if (d[i] == 5)
			ans = [ans, (i - index[2] - 1) + d.length - i - 1].min
			ans = [ans, (i - index[7] - 1) + d.length - i - 1].min
		end
		if (d[i] == 0)
			ans = [ans, (i - index[0] - 1) + d.length - i - 1].min
			ans = [ans, (i - index[5] - 1) + d.length - i - 1].min
		end
		index[d[i]] = i
	}
	puts ans
end

t.times {
	solve
}

def k(n)
    c1 = 0
    c2 = 0
    c3 = 0
    c4 = 0
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    while n > 0
        dig = n % 10
        if dig == 0
            c1 += 1
        else
            d1 += 1
        end

        if c1 == 2
            return d1
        end

        if dig == 5 and c2 == 0
            c2+=1
        elsif dig == 2 and c2 == 1
            return d2
        else
            d2+=1
        end

        if(dig == 0 and c3 == 0)
            c3+=1
        elsif(dig == 5 and c3 == 1)
            return d3
        else
            d3+=1
        end

        if(dig == 5 and c4 == 0)
            c4+=1
        elsif(dig == 7 and c4 == 1)
            return d4
        else
            d4+=1
        end

        n = n / 10
    end

    return 1000

end

gets.to_i.times do
    n = gets.to_i
    puts k(n)
end

gets.to_i.times do
	n = gets.chomp
	if n.to_i % 25 == 0
		puts 0
		next
	end
	res = 1000000000
	for i in 0..n.length - 1 do
		for j in i+1..n.length - 1 do
			if (n[i].to_i * 10 + n[j].to_i) % 25 == 0
				res = [res, j - i - 1 + (n.length - 1 - j)].min
			end
		end
	end
	puts res
end

def f(dgts)
  n0 = dgts.length
  i0 = dgts.rindex("0") || 0
  j0 = i0 > 0 && dgts.rindex(/[05]/, i0 - 1) || -1

  dgts = dgts.gsub(/0+(?!0)/, "")
  n5 = dgts.length
  i5 = dgts.rindex("5") || 0
  j5 = i5 > 0 && dgts.rindex(/[27]/, i5 - 1) || -1

  [n0 - 2 - j0, n5 - 2 - j5].min
end

gets.to_i.times do

n = gets.chomp
puts f(n)

end

def f(dgts)
  n = dgts.length

  i0 = dgts.rindex("0") || 1
  i5 = dgts.rindex("5") || 1

  j0 = dgts.rindex(/[05]/, i0 - 1) || 0
  j5 = dgts.rindex(/[27]/, i5 - 1) || 0

  [n - 2 - j0, n - 2 - j5].min
end

gets.to_i.times do

n = gets.chomp
puts f(n)

end


END { load __FILE__ unless $stdin.eof? }

module AtCoder
  class DSU
    def initialize(n = 0)
      @n = n
      @parent_or_size = Array.new(n, -1)
    end

    def merge(a, b)
      x, y = leader(a), leader(b)
      return x if x == y

      x, y = y, x if -@parent_or_size[x] < -@parent_or_size[y]
      @parent_or_size[x] += @parent_or_size[y]
      @parent_or_size[y] = x
    end

    def same(a, b)
      leader(a) == leader(b)
    end

    def leader(a)
      return a if @parent_or_size[a] < 0

      @parent_or_size[a] = leader(@parent_or_size[a])
    end

    def size(a)
      -@parent_or_size[leader(a)]
    end

    def groups
      @n.times.group_by { |i| leader(i) }.values
    end
  end
end

n, m1, m2 = gets.split.map!(&:to_i)
e1 = Array.new(m1) { gets.split.map!(&:to_i) }
e2 = Array.new(m2) { gets.split.map!(&:to_i) }

dsu1 = AtCoder::DSU.new(n + 1)
dsu2 = AtCoder::DSU.new(n + 1)

e1.each { |u, v| dsu1.merge(u, v) }
e2.each { |u, v| dsu2.merge(u, v) }

e3 = [*1..n].combination(2).select do |u, v|
  next false if dsu1.same(u, v) || dsu2.same(u, v)

  dsu1.merge(u, v)
  dsu2.merge(u, v)

  true
end

puts e3.size
e3.each { |uv| puts uv * " " }

# Disjoint Set Union
class DSU
  def initialize(n)
    @n = n
    @parent_or_size = Array.new(n, -1)
    # root node: -1 * component size
    # otherwise: parent
  end

  attr_reader :parent_or_size, :n

  def merge(a, b)
    x = leader(a)
    y = leader(b)
    return x if x == y

    x, y = y, x if -@parent_or_size[x] < -@parent_or_size[y]
    @parent_or_size[x] += @parent_or_size[y]
    @parent_or_size[y] = x
  end
  alias unite merge

  def same?(a, b)
    leader(a) == leader(b)
  end
  alias same same?

  def leader(a)
    unless 0 <= a && a < @n
      raise ArgumentError.new, "#{a} is out of range (0...#{@n})"
    end

    @parent_or_size[a] < 0 ? a : (@parent_or_size[a] = leader(@parent_or_size[a]))
  end
  alias root leader
  alias find leader

  def size(a)
    -@parent_or_size[leader(a)]
  end

  def groups
    (0 ... @parent_or_size.size).group_by{ |i| leader(i) }.values
  end
end

UnionFind = DSU

n,m1,m2=gets.split.map(&:to_i)
f1=DSU.new(n)
f2=DSU.new(n)

m1.times do
  u,v=gets.split.map(&:to_i).map{|x|x-1}
  f1.merge(u,v)
end
m2.times do
  u,v=gets.split.map(&:to_i).map{|x|x-1}
  f2.merge(u,v)
end

az=0
ans=[]
n.times do |j|
  j.times do |i|
    if !f1.same?(i,j)&&!f2.same?(i,j)
      f1.merge(i,j)
      f2.merge(i,j)
      ans << [i+1,j+1]
      az+=1
    end
  end
end

puts az
ans.each do |aa|
  puts aa.join(" ")
end


a,b,c,d = gets.chomp.split.map(&:to_i)

n=[a,b,c,d].max
arr=[a,b,c,d]-[n]
puts [n-arr[0],n-arr[1],n-arr[2]].join(' ')

def uwu(inte)
	max = 0
	output = ''
	for number in inte
		max = number.to_i if number.to_i >= max
	end
	inte.delete(max.to_s)
	for number in inte
		resutlat = max - number.to_i
		output << resutlat.to_s
		output << ' '
	end
	output[-1] = '' if output[-1] == ' '
	return output
end

inte = gets.chomp.split(" ")
puts(uwu(inte))

a,b,c,d = gets.split.map(&:to_i).sort
puts "#{d-a} #{d-b} #{d-c}"

# coding: utf-8

# if __FILE__ == $0
# end

inputs = gets.split.map { |i| i.to_i }

# inputs[?] = a + b
# inputs[?] = a + c
# inputs[?] = b + c
# inputs[?] = a + b + c
ans = []
# 모두 양수이므로, 가장 큰 수가 a + b + c
abc = inputs.max
inputs.delete abc
inputs.each { |i| ans.push abc - i }

puts ans.join ' '

arr = gets.split.map(&:to_i)
arr.sort!
arr.reverse!
answer = []
answer << arr[0]-arr[1]
answer << arr[0]-arr[2]
answer << arr[0]-arr[3]
puts answer.join(' ')

$n = gets
$a, $b, $c, $d = $n.split(' ').to_a
arr = [$a.to_i, $b.to_i, $c.to_i, $d.to_i]
$k = arr.max
arr.delete($k)
print $k.to_i-arr[0].to_i,' ',$k.to_i-arr[1].to_i,' ',$k.to_i-arr[2].to_i

require "matrix"
ngo = gets.chomp.split(" ").map(&:to_i).sort
puts Matrix[[1,1,0],[1,0,1],[0,1,1]].lup.solve(ngo.slice(0,3)).to_a.map(&:to_i).join(" ")

sums = gets.chomp.split(" ").map(&:to_i)

max = sums[0]
max_idx = 0
sums.each_with_index do |sum, idx|
    if sum > max
        max = sum
        max_idx = idx
    end
end

numbers = []

sums.each_with_index do |sum, idx|
    if idx != max_idx
        n = max - sum
        numbers.push(n)
    end
end

ans = numbers.join(" ")
puts ans

line = gets
n = line.split.map { |x| x.to_i }
sum = n.max
n.delete_at(n.index(sum))
n.map!{|x| sum - x}
puts(n.join ' ')

a = gets.split(' ').map(&:to_i).sort.reverse

m = a.shift
puts a.map { |x| m-x }.join(' ')

x = gets.split(' ').map(&:to_i).sort
v = []
x.each do |xx|
  v.push(x[3]-xx)
end
puts "#{v[0]} #{v[1]} #{v[2]}"

def read_tokens
  return gets.chomp.to_s.strip.split(' ')
end

def read_ints
  data = []
  read_tokens.each { |s|
    data.push(s.to_i)
  }
  return data
end

arr = read_ints()

def solve(arr)
  maximum = arr.max
  arr.each { |el|
    if el != maximum
      print "#{maximum - el} "
    end
  }
end

solve(arr)

#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/1154/A

numbers = gets.split(' ').map(&:to_i)

max = numbers.max
answer = []

numbers.each do |n|
  if (temp = max - n) != 0
    answer << temp
  end
end

puts answer.join(' ')

a,b,c,d=gets.split.map(&:to_i).sort
puts"#{d-a} #{d-b} #{d-c}"

a,b,c,d=gets.split.map(&:to_i).sort
p"#{d-a} #{d-b} #{d-c}"


gets
v = gets.split.map(&:to_i)

d = Array.new(v.length,-1)
d[0] = v[0]

0.upto(v.length-2) do |i|
	next if d[i] == -1
	if d[i+1] == -1 or d[i+1] < [d[i],v[i+1]].min
		d[i+1] = [d[i],v[i+1]].min
	end
	if i + 2 < v.length and (d[i+2] == -1 or d[i+2] < [d[i],v[i+2]].min)
		d[i+2] = [d[i],v[i+2]].min
	end
end

puts d[v.length-1]

n = gets.to_i
a = gets.split.map{|e| e.to_i}
min=[a[0],a[n-1]].min
for i in (0..n-2) do
  c = [a[i],a[i+1]].max
  if c<min then min=c end
end
puts min

def check(a)
  a.each_cons(2) do |ar|
    if ar[0] == false && ar[1] == false
      return false
    end
  end
  true
end

gets
a = gets.split.map(&:to_i)

m = [a.first,a.last].min

(a.min).upto m do |i|
  unless check a.map{|e| e<=i ? false : true}
    p i
    exit
  end
end

p m

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
l = arr.min
r = [arr[0],arr[-1]].min

avail = Array.new(n,true)
ans = nil
finish = false
(l..r).each do |i|
	# puts "i #{i} #{avail}"
	ans = i
	arr.each_with_index do |a,j|
		# puts "a #{a}"
		avail[j] = false if a == i
		if avail[j] == false && avail[j] == avail[j-1] && j > 0
			finish = true
			break
		end
	end
	break if finish
end
puts ans

gets
ns = gets.split.map(&:to_i)
sorted = ns.map.with_index{|n, i| [n, i]}.sort_by(&:first)
hash = {}
answer = ns.first
sorted.each do |n, i|
  hash[i] = true
  if hash[i-1] || hash[i+1] || hash[0] || hash[ns.length - 1]
    answer = n
    break
  end
end

puts answer

gets
v = gets.split.collect { |x| x.to_i}

d = Array.new(v.length,-1)
d[0] = v[0]

begin
	ch = false
	0.upto(v.length-2) do |i|
		if d[i] != -1
			if d[i+1] == -1 or d[i+1] < [d[i],v[i+1]].min
				d[i+1] = [d[i],v[i+1]].min
				ch = true
			end
			if i + 2 < v.length and (d[i+2] == -1 or d[i+2] < [d[i],v[i+2]].min)
				d[i+2] = [d[i],v[i+2]].min
				ch = true
			end
		end
	end
end while ch

puts d[v.length-1]

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|e| e.to_i}

days = 0
loop do
  break if a[0] == 0 or a[-1] == 0

  bang = false
  for i in 1 .. (n - 2) do
    bang = true if a[i] == 0 and a[i + 1] == 0
  end
  break if bang

  days += 1

  a.map!{|e|
    e > 0 ? e -= 1 : e
  }
end

puts days

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|e| e.to_i}

a << 0
a << 0

days = 0
loop do
  bang = false
  for i in 0 .. (n - 2) do
    bang = true if ! ((a[i] > 0 and (a[i + 1] > 0 or a[i + 2] > 0)) or (a[i] == 0 and a[i + 1] > 0))
  end

  break if bang

  days += 1

  a.map!{|e|
    e > 0 ? e -= 1 : e
  }
end

puts days

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|e| e.to_i}

days = 0
loop do
  bang = false
  for i in 0 .. (n - 3) do
    bang = true if !((a[i] > 0 and (a[i + 1] > 0 or a[i + 2] > 0)) or (a[i] <= 0 and (a[i + 1] > 0)))
  end

  break if bang

  days += 1

  a.map!{|e| e -= 1}
end

puts days

n = gets.to_i
a = gets.split.map{|e| e.to_i}
min=[a[0],a[n-1]].min
for i in (0..n-2) do
  c = [a[i],a[i+1]].max
  puts c
  if c<min then min=c end
end
puts min

n = gets.to_i
a = gets.split.map{|e| e.to_i}
min=100000
for i in (0..n-2) do
  c = [a[i],a[i+1]].max
  if c<min then min=c end
end
puts min

gets
ns = gets.split.map(&:to_i)
if ns.length == 1
  puts ns[0]
  exit
end
sorted = ns.map.with_index{|n, i| [n, i]}.sort_by(&:first)
hash = {}
answer = 0
sorted.each do |n, i|
  hash[i] = true
  if hash[i-1] || hash[i+1]
    answer = n
    break
  end
end

puts answer

gets
ns = gets.split.map(&:to_i)
sorted = ns.map.with_index{|n, i| [n, i]}.sort_by(&:first)
hash = {}
answer = 0
sorted.each do |n, i|
  hash[i] = true
  if hash[i-1] || hash[i+1]
    answer = n
    break
  end
end

puts answer


def run
  n, m = $stdin.gets.split.map(&:to_i)
  w = $stdin.gets.split.map(&:to_i)
  b = $stdin.gets.split.map(&:to_i)

  ordered_list = []
  ordered_hash = {}
  b.each_with_index do |book, i|
    if ordered_hash[book] == nil
      ordered_list << book
      ordered_hash[book] = true
    end
  end

  books_by_weight = (1..n).to_a.sort_by { |i| w[i - 1] }
  books_by_weight.each do |book|
    if ordered_hash[book] == nil
      ordered_list << book
    end
  end

  costs = 0
  b.each do |i|
    location = ordered_list.index(i)
    if location > 0
      lift_targets = ordered_list[0..(location - 1)]
      remaining    = ordered_list[(location + 1)..-1]
      costs += lift_targets.reduce(0) {|memo, item| memo + w[item - 1] }
      ordered_list = [ordered_list[location]] + lift_targets + remaining
    end
  end

  puts costs
end

run if $0 == __FILE__

def g;gets.split.map(&:to_i)end
n,m=g
w=g
b=g
#[*1..n].permutation{|st|
#  sum=0
#  b.each{|i|
#    x=st.index(i)
#    st.delete_at(x)
#    sum+=st[x..-1].inject(0){|s,r|s+w[r-1]}
#    st<< i
#  }
#  p sum
#}
st=(1..n).sort_by{|i|-(b.index(i)||m)}
sum=0
b.each{|i|
  x=st.index(i)
  st.delete_at(x)
  sum+=st[x..-1].inject(0){|s,r|s+w[r-1]}
  st<< i
}
p sum

n, m = gets.split.map(&:to_i)
ws = gets.split.map(&:to_i)
bs = gets.split.map(&:to_i)

score = Array.new(n){ 0 }
ord = Array.new(n){ n }
ret = 0
wini = 0
bs.each do |x|
  if ord[x-1] == n
    ret += wini
    wini += ws[x-1]
  else
    ret += score[x-1]
  end
  n.times do |i|
    if ord[i] < ord[x-1]
      score[i] += ws[x-1]
      ord[i] += 1
    end
  end
  score[x-1] = 0
  ord[x-1] = 0
  #print "(#{ret})"
end
puts ret

# 500C

require 'set'

def read_line
  gets.chomp.split.map &:to_i
end

n, _ = read_line
w = read_line
s = Set.new(1..n)
a = []
b = read_line
b.each { |x|
  if s.include? x
    a << x
    s.delete x
  end
}
a += s.to_a

s = 0
b.each { |v|
  m = a.index v
  s += (0...m).map { |i| w[a[i] - 1] }.inject(&:+) || 0
  a.delete_at m
  a.unshift v
}
puts s

def g;gets.split.map(&:to_i)end
n,_=g
w=g
b=g
#[*1..n].permutation{|st|
#  sum=0
#  b.each{|i|
#    x=st.index(i)
#    st.delete_at(x)
#    sum+=st[x..-1].inject(0){|s,r|s+w[r-1]}
#    st<< i
#  }
#  p sum
#}
st=(1..n).sort_by{|i|-(b.index(i)||n)}
sum=0
b.each{|i|
  x=st.index(i)
  st.delete_at(x)
  sum+=st[x..-1].inject(0){|s,r|s+w[r-1]}
  st<< i
}
p sum


def minimize_instability(n, a)
  min1 = min2 = 100_001
  max1 = max2 = 0
  n.times do |i|
    if a[i] < min1
      min2 = min1
      min1 = a[i]
    elsif a[i] < min2
      min2 = a[i]
    end
    if a[i] > max1
      max2 = max1
      max1 = a[i]
    elsif a[i] > max2
      max2 = a[i]
    end
  end

  min = 1_000_000
  n.times do |i|
    u = max1
    u = max2 if u == a[i]
    l = min1
    l = min2 if l == a[i]
    min = [min, u - l].min
  end
  return min
end

n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)

puts minimize_instability(n, a)

n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)

max = 0
maxtwo = 0
min = 1.0/0.0
mintwo = 1.0/0.0

arr.each do |x|
  if x > max
    maxtwo = max
    max = x
  elsif x > maxtwo
    maxtwo = x
  end

  if x < min
    mintwo = min
    min = x
  elsif x <mintwo
    mintwo = x
  end
end

puts [maxtwo-min,max-mintwo].min

# Input
n = gets.to_i
a = gets.split.map { |s| s.to_i }

# Processing
a.sort!
s_1 = a[-2] - a[0] # Deleted largest element
s_2 = a[-1] - a[1] # Deleted smallest element
result = [s_1, s_2].min

# Output
puts result

n = gets.to_i
a = gets.split.map &:to_i
a.sort!
puts [a[-1] - a[1], a[-2] - a[0]].min


n = gets.strip.to_i
s = gets.strip
ans = 0
i = 0
while i < n && s[i] == '<'
    ans += 1
    i += 1
end
i = n - 1
while i >= 0 && s[i] == '>'
    ans += 1
    i -= 1
end
puts ans

n = gets.to_i
s = gets.strip

i = 0
while i < n && s[i] == '<'
    i += 1
end

j = 0

while j < n && s[n - j - 1] == '>'
    j += 1
end

puts i + j

gets
s=gets.chop
r=0
s.chars{|c|c==?>&&break;r+=1}
s.reverse.chars{|c|c==?<&&break;r+=1}
p r

#!/usr/bin/ruby

size = gets.to_i
str = gets
right = true
left = true
continue = true
final_number = 0
i=0
while continue do
	if right
		if str[i] == '<'
			final_number += 1
		else
			right = false
		end
	end
	if left
		if str[size - 1 - i] == '>'
			final_number += 1
		else
			left = false
		end
	end
	i+=1
	if !left and !right
		continue = false
	end
end
puts final_number

n = gets.to_i
s = gets.chomp
puts (s.index('>') || n) + (n - 1 - (s.rindex('<') || -1))

n = gets.to_i
a = gets.strip
cnt = 0
i = 0
len = a.length
while(1) do
   break if a[i] == '>' || i == len
   cnt += 1
   i += 1
end

i = len - 1
while(1) do
   break if a[i] == '<' || i == -1
   cnt += 1
   i -=1
end

printf("%d\n", cnt)

length = gets.to_i
bumpers = gets

first_greater = bumpers.index('>')
last_lesser = bumpers.rindex('<')

if first_greater.nil? || last_lesser.nil?
  total_falling_positions = length
else
  total_falling_positions = length - (last_lesser - first_greater + 1)
end

puts total_falling_positions

n = gets.strip.to_i
s = gets.strip
first = s[0]
last = s[n - 1]
ans = 0
if first == '<'
    ans = 1
elsif last == '>'
    ans = n
end
puts ans

n = gets.strip.to_i
s = gets.strip
first = s[0]
last = s[n - 1]
ans = 0
if first == '<'
    ans = 1
elsif last == '>'
    ans = n
else
    if first == '>' && last == '<'
        ans = 0
    end
end
puts ans

n = gets.to_i
s = gets.strip

i = 0
while i < n && s[i] == '<'
    i += 1
end

j = 0

while j < n && s[n - j + 1] == '>'
    i += 1
end

puts i + j

gets
s=gets
r=0
s.chars{|c|c==?>&&break;r+=1}
s.reverse.chars{|c|c==?<&&break;r+=1}
p r


n = gets.chomp.to_i
hx = Hash.new(0)
hy = Hash.new(0)
hxy = Hash.new(0)
n.times do
  x, y = gets.chomp.split.map(&:to_i)
  hx[x] += 1
  hy[y] += 1
  hxy[(x + 10**9) * 10**10 + (y + 10**9)] += 1
end

ans = 0
hx.each do |_, v|
  ans += v * (v - 1) / 2
end
hy.each do |_, v|
  ans += v * (v - 1) / 2
end
hxy.each do |_, v|
  ans -= v * (v - 1) / 2
end

puts ans

n=gets.to_i
a=Hash.new(0)
b=Hash.new(0)
p=Hash.new(0)
for i in 1..n
	buf = gets.split(" ")
	a[buf[0]]+=1
	b[buf[1]]+=1
	p[buf]+=1
end
ans=0
a.each_value{|v| ans+=v*(v-1)/2 }
b.each_value{|v| ans+=v*(v-1)/2 }
p.each_value{|v| ans-=v*(v-1)/2 }
puts ans

#!/usr/bin/ruby
require 'scanf'

x_paintings  = Hash.new(0)
y_paintings  = Hash.new(0)
xy_paintings = Hash.new(0)
watchmen = gets().to_i

pairs = 0
watchmen.times do
  x, y = gets().split(' ').map(&:to_i)
  pairs += x_paintings[x]; x_paintings[x] += 1
  pairs += y_paintings[y]; y_paintings[y] += 1
  pairs -= xy_paintings["#{x},#{y}"]; xy_paintings["#{x},#{y}"] += 1
end

puts pairs

n = gets.to_i
x=[]
y=[]
a=[]
n.times do
	b,c = gets.split.map(&:to_i)
	x << b
	y << c
	a << [b,c]
end

x.sort!
y.sort!
a.sort!
c="a"
cc = 1
ac = 0
n.times do |i|
	cx=x[i]
	if c!=cx
		ac += cc*(cc-1)/2
		c=cx
		cc=1
	else
		cc+=1
	end
end
ac += cc*(cc-1)/2

c="a"
cc = 1
n.times do |i|
	cx=y[i]
	if c!=cx
		ac += cc*(cc-1)/2
		c=cx
		cc=1
	else
		cc+=1
	end
end
ac += cc*(cc-1)/2

c=-1
cc = 1
n.times do |i|
	cx=a[i]
	if c!=cx
		ac -= cc*(cc-1)/2
		c=cx
		cc=1
	else
		cc+=1
	end
end
ac -= cc*(cc-1)/2

puts ac

n = gets.to_i
locations = []
n.times do
  locations << gets.split.map(&:to_i)
end
digit_count_x = {}
digit_count_y = {}
digit_count_xy = {}

count = 0
locations.each do |x, y|
  count += digit_count_x[x] if digit_count_x[x] && digit_count_x[x] > 0
  digit_count_x[x] = digit_count_x[x] ? digit_count_x[x] + 1 : 1
  count += digit_count_y[y] if digit_count_y[y] && digit_count_y[y] > 0
  digit_count_y[y] = digit_count_y[y] ? digit_count_y[y] + 1 : 1
  count -= digit_count_xy[[x, y]] if digit_count_xy[[x, y]] && digit_count_xy[[x, y]] > 0
  digit_count_xy[[x, y]] = digit_count_xy[[x, y]] ? digit_count_xy[[x, y]] + 1 : 1
end
puts count

def cal hash
  hash.values.map{|n| n * (n - 1) / 2}.reduce(0, :+)
end

n = readline.to_i
hx, hy, hxy = Hash.new(0), Hash.new(0), Hash.new(0)
n.times do
  x, y = readline.split(' ').collect(&:to_i)
  hx[x] += 1
  hy[y] += 1
  hxy[[x, y]] += 1
end

puts cal(hx) + cal(hy) - cal(hxy)

#!/usr/bin/ruby
require 'scanf'

x_paintings  = Hash.new(0)
y_paintings  = Hash.new(0)
xy_paintings = Hash.new(0)
watchmen = scanf("%d\n").first.to_i

pairs = 0
watchmen.times do
  x, y = scanf("%d %d").map(&:to_i)
  pairs += x_paintings[x]; x_paintings[x] += 1
  pairs += y_paintings[y]; y_paintings[y] += 1
  pairs -= xy_paintings["#{x},#{y}"]; xy_paintings["#{x},#{y}"] += 1
end

puts pairs

#!/usr/bin/ruby
require 'scanf'

x_paintings  = Hash.new(0)
y_paintings  = Hash.new(0)
xy_paintings = Hash.new(0)
watchmen = scanf("%d").first.to_i

pairs = 0
watchmen.times do
  x, y = scanf("%d %d").map(&:to_i)
  pairs += x_paintings[x]; x_paintings[x] += 1
  pairs += y_paintings[y]; y_paintings[y] += 1
  pairs -= xy_paintings["#{x},#{y}"]; xy_paintings["#{x},#{y}"] += 1
end

puts pairs

n = gets.to_i

locations = gets.split.map(&:to_i)
digit_count_x = {}
digit_count_y = {}
digit_count_xy = {}

count = 0
locations.each do |x, y|
  count += digit_count_x[x] if digit_count_x[x] && digit_count_x[x] > 0
  digit_count_x[x] = digit_count_x[x] ? digit_count_x[x] + 1 : 1
  count += digit_count_y[y] if digit_count_y[y] && digit_count_y[y] > 0
  digit_count_y[y] = digit_count_y[y] ? digit_count_y[y] + 1 : 1
  count -= digit_count_xy[[x, y]] if digit_count_xy[[x, y]] && digit_count_xy[[x, y]] > 0
  digit_count_xy[[x, y]] = digit_count_xy[[x, y]] ? digit_count_xy[[x, y]] + 1 : 1
end
puts count


cases = STDIN.gets.chomp.to_i
cases.times.each do
    input_string = STDIN.gets.chomp
    input_query = STDIN.gets.chomp
    puts input_string.length.times.find_all { |i| input_string[i,input_query.length] == input_query && i%2 ==0 }.length>0 ? "YES" : "NO"
    end

t = gets.to_i
t.times {
  s = gets.strip

  c = gets[0]
  a = (0...s.length).find_all { |i| s[i] == c }
  found = false
  a.each { |i|
    found ||= (i % 2 == 0 && (s.length - i - 1) % 2 == 0)
    #puts("#{i} #{i % 2 == 0} #{(s.length - i - 1) % 2 == 0} #{(s.length - i - 1)}")
  }
  puts(found ? 'yes' : 'no')
}

cases = STDIN.gets.chomp.to_i
cases.times.each do
    input_string = STDIN.gets.chomp
    input_query = STDIN.gets.chomp
    puts input_string.length.times.find_all { |i| input_string[i,input_query.length] == input_query }.find_all { |i| i%2==0 }.length>0 ? "YES" : "NO"
    end

n=gets().chomp.to_i
for i in 1..n do
    s=gets().chomp
    n=gets().chomp
    c=0
    for i in 0...s.length do
        if (i+1)%2!=0 && s[i]==n
            puts "YES"
            c+=1
            break
        end
    end
    if c==0
        puts "NO"
    end
end

n=gets().chomp.to_i
for i in 1..n do
    s=gets().chomp
    n=gets().chomp
    i=0
    for i in 0...s.length do
        if (i+1)%2!=0 && s[i]==n
            puts "YES"
            break
        end
    end
    if i==s.length
        puts "NO"
    end
end


def tiles(n,x,y)
  if x == n-1 || x == 0 || y >= n-2
    return false
  else
    return [[x,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x,y+2]]
  end
end

def solve (n,board)
  n.times do |i|
    n.times do |j|
      if board[i][j] == "."
        #p [i,j]
        t = tiles(n,j,i)
        #p t
        return false if !t
        t.each do |x,y|
          if board[y][x] == "#"
            return false
          else
            board[y][x] = "#"
          end
        end
      end
    end
  end
  return true
end

n = gets.to_i
board = []
n.times do
  board << gets.chomp
end
puts solve(n,board) ? "YES" : "NO"

N = gets.to_i
board = N.times.map { gets.chomp.chars }

N.times do |y|
  N.times do |x|
    next if board[y][x] == '#'

    ay, ax = y, x
    by, bx = y + 1, x - 1
    cy, cx = y + 1, x
    dy, dx = y + 1, x + 1
    ey, ex = y + 2, x

    max_y = [ay, by, cy, dy, ey].max
    min_x, max_x = [ax, bx, cx, dx, ex].minmax

    if max_y >= N || min_x < 0 || N <= max_x
      puts 'NO'
      exit
    end

    [ay, by, cy, dy, ey].zip([ax, bx, cx, dx, ex]).each do |ny, nx|
      if board[ny][nx] == '#'
        puts 'NO'
        exit
      end

      board[ny][nx] = '#'
    end
  end
end

puts 'YES'

n = gets.to_i
s = n.times.map{gets.chomp}
d = [0,1,0,-1]
for i in 1..n-2
    for j in 1..n-2
        if s[i][j] == "."
            f = true
            4.times do |k|
                f = false if s[i+d[k]][j+d[k^1]] == "#"
            end
            if f
                4.times do |k|
                    s[i+d[k]][j+d[k^1]] = "#"
                end
                s[i][j] = "#"
            end
        end
    end
end
if s.join("").count(".") > 0
    puts "NO"
else
    puts "YES"
end


cityNum, capitalNum = gets.chomp.split(' ').map(&:to_i)
prices = gets.chomp.split(' ').map(&:to_i)
capitals = gets.chomp.split(' ').map(&:to_i)
isCapital = Array.new(cityNum) { |i| i = false }
#isMarked = Array.new(cityNum) { |i| i = false }

rawSum = 0
rawCapSum = 0

prices.each do |p|
	rawSum += p
end

capitals.each do |c|
	rawCapSum += prices[c - 1]
	isCapital[c - 1] = true
end

totalSum = 0

for i in 0...(prices.length)
	totalSum += (prices[i] * prices[(i + 1) % cityNum])
end

#puts "Total : #{totalSum}"

capitals.each do |c|
	partialSum = rawSum - prices[c - 1]
	#puts "Partial : #{partialSum}"
	totalSum += partialSum * prices[c - 1]
	#puts "c : #{c}, ts = #{totalSum}"
end

#puts "Total : #{totalSum}"

	count = 1
	capitals.each do |c|
		totalSum -= prices[c - 1] * (prices[c % cityNum] + prices[if c - 2 >= 0 then c - 2 else cityNum - 1 end])
		break if count == capitalNum

		partialSum = rawCapSum - prices[c - 1]

		if isCapital[c % cityNum]
			partialSum -= prices[c % cityNum]
		end

		if c == 1 && isCapital[if c - 2 >= 0 then c - 2 else cityNum - 1 end]
			partialSum -= prices[if c - 2 >= 0 then c - 2 else cityNum - 1 end]
		end

		#puts "c : #{c}"
		#puts "raw : #{rawCapSum}"
		#puts "partial : #{partialSum}"

		totalSum -= prices[c - 1] * partialSum
		rawCapSum -= prices[c - 1]
		count += 1
	end

puts totalSum

n, k = gets.split.map { |x| x.to_i }
c = gets.split.map { |x| x.to_i }
id = gets.split.map { |x| x.to_i }

cap = Array.new n
id.each do |x|
	cap[x - 1] = true
end

sc = 0
sc2 = 0
n.times do |i|
	if cap[i]
		sc += c[i]
		sc2 += c[i] ** 2
	end
end

sum = 0
ans = (sc ** 2 - sc2) / 2
n.times do |i|
	sum += c[i] if !cap[i]
end
n.times do |i|
	ans += c[i] * sum if cap[i]
end

n.times do |i|
	if !cap[i - 1] and !cap[i]
		ans += c[i - 1] * c[i]
	end
end

puts ans


##
# 15A
# Author: Sammy Teahan
#

if __FILE__ == $0

  n, t = gets.split.map(&:to_i)
  count = 2
  houses = []

  class House
    attr_reader :left, :right

    def initialize(x, a)
      @left = x - a / 2
      @right = x + a / 2
    end

  end

  n.times do |k|
    x, a = gets.split.map(&:to_f)
    house = House.new(x, a)
    houses << house
  end

  houses.sort_by! do |house|
    house.left
  end

  (houses.size - 1).times do |house|
    if houses[house].right + t < houses[house + 1].left
      count += 2
    elsif
      houses[house].right + t == houses[house + 1].left
      count += 1
    end
  end

  puts count
end

##
# 15A
# Author: Sammy Teahan
#

if __FILE__ == $0

  n, t = gets.split.map(&:to_i)
  count = 2
  houses = []

  class House
    attr_reader :left, :right

    def initialize(x, a)
      @left = (x - a / 2)
      @right = (x + a / 2)
    end

  end

  n.times do |k|
    x, a = gets.split.map(&:to_f)
    house = House.new(x, a)
    houses << house
  end

  # Sort our houses
  houses.sort_by! do |house|
    house.left
  end

  (houses.size - 1).times do |house|
    if houses[house].right + t < houses[house + 1].left
      count += 2
    elsif
      houses[house].right + t == houses[house + 1].left
      count += 1
    end
  end

  puts count
end

n, t = gets.split.map(&:to_i)
t = t.to_f
koord = Array.new
n.times { koord << gets.split.map(&:to_f) }
koord.sort!
ret = 2
(1...(n)).each do |i|
  x_i_1 = koord[i-1][0] + koord[i-1][1] / 2
  x_i_2 = koord[i][0]   - koord[i][1].abs / 2
  diff = (x_i_1 - x_i_2).abs
  if diff == t
    ret += 1
  elsif diff > t
    ret += 2
  end
end
puts ret

# Problem A
# by vi002

n, t = gets.split.map { |v| v.to_i }
data = Array.new(n) { gets.split.map { |v| v.to_i } }.sort

result = 2
(0 ... n - 1).each do |pos|
  distance = 2 * (data[pos + 1].first - data[pos].first)
  distance -= data[pos][1] + data[pos + 1][1]
  result += 2 if distance > 2 * t
  result += 1 if distance == 2 * t
end

puts result

n,t=gets.split.map(&:to_i)
built=[]
n.times{
	x,a=gets.split.map(&:to_i)
	built<<[2*x-a,2*x+a]
}
amount=2 #left and right most already possible
built.sort{|a,b|a[0]<=>b[0]}.each_cons(2){|l,r|amount+=r[0]-l[1]<=>2*t}
puts amount+n-1

require 'set'
def check(x,t)
        ok=false
        $m.each{|h|
                if x+t<=h[0] || h[0]+h[1]<=x then
                else
                        return false
                end
        }
        return true
end
n,t=gets.split.map(&:to_i)
t*=2
$m=[]
n.times{|i|
        $m.push(gets.split.map(&:to_i))
        $m[i][0]*=2
        $m[i][0]-=$m[i][1]
        $m[i][1]*=2
}

q=Set.new
$m.each{|h|
        if(check(h[0]-t,t)) then
                q.add(h[0]-t)
        end
        if(check(h[0]+h[1],t)) then
                q.add(h[0]+h[1])
        end
}
puts q.length

n,t,x,a=(gets+gets).split.map &:to_i;y=2;2.upto(n){s=x+x+a;x,a=gets.split.map &:to_i;s=x+x-a-s;y+=(s<=>t+t)+1};puts y

n,t=gets.split.map(&:to_i)
built=[]
n.times{
	x,a=gets.split.map(&:to_i)
	built<<[x-a/2,x+a/2]
}
amount=2 #left and right most already possible
built.each_cons(2){|(l,r)|
	amount+=(r[0]-l[1]<=>t)+1
}
puts amount

n,t=gets.split.map(&:to_i)
built=[]
n.times{
	x,a=gets.split.map(&:to_i)
	built<<[x-a/2,x+a/2]
}
amount=2 #left and right most already possible
built.sort{|a,b|a[0]<=>b[0]}.each_cons(2){|(l,r)|amount+=r[0]-l[1]<=>t}
puts amount+n-1


gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  abs = if k % 2 == 1
      (1..n).step(2).map do |i|
        [i, i + 1]
      end
    else
      odds = (1..n).step(2).to_a
      (4..n).step(4).map do |b|
        [odds.pop, b]
      end + (2..n).step(4).map do |a|
        [a, odds.pop]
      end
    end
  if abs.all? { |a, b| (a + k) * b % 4 == 0 }
    puts "YES"
    abs.each do |a, b|
      puts "#{a} #{b}"
    end
  else
    puts "NO"
  end
end

def main
  t = inp.to_i
  t.times do
    n, k = mapint(inarr)
    if k % 4 == 0
      puts "NO"
      next
    end
    puts "YES"
    ans = ""

    r = Array.new(n>>1)

    pos = 0
    i = 1
    while i <= n
        if ((i + k) * (i + 1) & 3) == 0
          r[pos] = "#{i} #{i+1}"
        else
          r[pos]  = "#{i + 1} #{i}"
        end
        pos+=1
        i+=2
      end

    puts r

  end

end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

def min(a, b)
  (a < b)? a:b
end

main

def main
  t = inp.to_i
  t.times do
    n, k = mapint(inarr)
    if k == 0
      puts "NO"
      next
    end
    puts "YES"
    ans = ""

      1.step(n, 2) do |i|
        if ((i + k) * (i + 1) & 3) == 0
          ans += "#{i} #{i+1}\n"
        else
          ans += "#{i + 1} #{i}\n"
        end

      end

    puts ans

  end
end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

def min(a, b)
  (a < b)? a:b
end

main

def main
  t = inp.to_i
  t.times do
    n, k = mapint(inarr)
    if k == 0
      puts "NO"
      next
    end
    puts "YES"
    ans = ""
    if k % 2 == 0
      1.step(n, 2) do |i|
        ans += "#{i + 1} #{i}\n"
      end
    else
      1.step(n, 2) do |i|
        ans += "#{i} #{i+1}\n"
      end
    end
    puts ans

  end
end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

def min(a, b)
  (a < b)? a:b
end

main

gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  if k == 0
    puts "NO"
  elsif k % 2 == 1
    puts "YES"
    (1..n).step(2).each do |i|
      puts "#{i} #{i + 1}"
    end
  else
    puts "YES"
    (1..n).step(2).each do |i|
      if (i + k) % 4 == 1
        puts "#{i} #{i + 1}"
      else
        puts "#{i + 1} #{i}"
      end
    end
  end
end


_,a,b=$<.map &:chomp
if !a.index('*')
	puts a==b ? :Yes : :No
else
	x,y=a.split('*')
	x||='';y||=''
	puts b.start_with?(x) && b.end_with?(y) && b.size>=x.size+y.size ? :Yes : :No
end

sl,tl=gets.strip.split.map(&:to_i)
ml = (sl<tl ? sl : tl)
s=gets.strip
t=gets.strip

if tl < sl-1
  puts "NO"
  exit 0
end

unless s.include?('*')
  puts (s==t ? "YES" : "NO")
else
  i=0
  while i<ml && s[i]==t[i] do
    i +=1
  end
  if i<ml && s[i]!='*'
    puts "NO"
    exit 0
  end
  i=sl-1
  j=tl-1
  while i>=0 && j>=0 && s[i]==t[j] do
    i -= 1
    j -= 1
  end
  if i>=0 && j>=0 && s[i]!='*'
    puts "NO"
    exit 0
  end
  puts "YES"
end

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp,gets.chomp
ans = true
if s.include?("*")
  if s[-1] == "*" && s.delete("*") != t[0,s.size-1]
    ans = false
  elsif s[-1] != "*"
    a,b = s.split("*")
    if a != t[0,a.size] || b != t[m-b.size..-1] || m < n-1
      ans = false
    end
  end
elsif s != t
  ans = false
end
puts ans ? "YES" : "NO"

#! /usr/bin/env ruby

n, m = gets.split.map(&:to_i)
s = gets.chomp
t = gets.chomp

if s.include?("*")
  a, b = s.split("*")
  if a == nil and b == nil
    puts "YES"
  elsif a == nil
    puts (t.end_with?(b) ? "YES" : "NO")
  elsif b == nil
    puts (t.start_with?(a) ? "YES" : "NO")
  else
    if t.start_with?(a) and t.end_with?(b)
      if a.size + b.size <= m
        puts "YES"
      else
        puts "NO"
      end
    else
      puts "NO"
    end
  end
else
  puts (s == t ? "YES" : "NO")
end

N, M = gets.split.map(&:to_i)
s = '!' + gets.chomp + '!'
t = '!' + gets.chomp + '!'
if s.include?('*')
    s1, s2 = s.split('*')
    if s.length-1 <= t.length && s1 == t[0, s1.length] && s2 == t[t.length-s2.length, s2.length]
        puts 'YES'
    else
        puts 'NO'
    end
else
    if s == t
        puts 'YES'
    else
        puts 'NO'
    end
end

gets;puts /#{gets.chomp.gsub('*','.*')}/=~gets.chomp ? :Yes : :No

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp,gets.chomp
first,second = true,true if s.include?("*")
if s[0] != "*" && first
  a = s.split("*")[0]
  i = t.index(a)
  if i
    t.slice!(0,i+a.size)
  else
    first = false
  end
end
if s[-1] != "*" && second
  b = s.split("*")[1]
  i = t.rindex(b)
  if i
    t.slice!(0,i+b.size)
    if t.size != 0
      second = false
    end
  else
    second = false
  end
end
puts (first && second) || s == t ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts ans ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
p s,t
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
  p t
end
puts (ans&&t.size==1) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts (ans&&t.size==0) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts (ans && (t.size == 0 || s.size == 1)) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts (ans && (t.size == 0 || s.size <= 1)) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
p s,t
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
  p t
end
puts (ans&&t.size==0) ? "YES" : "NO"

#! /usr/bin/env ruby

n, m = gets.split.map(&:to_i)
s = gets.chomp
t = gets.chomp

if s.include?("*")
  a, b = s.split("*")
  if a == nil and b == nil
    puts "YES"
  elsif a == nil
    puts (t.include?(b) ? "YES" : "NO")
  elsif b == nil
    puts (t.include?(a) ? "YES" : "NO")
  else
    li, ri = t.index(a), t.rindex(b)
    if li == nil or ri == nil
      puts "NO"
    else
      if li + a.size <= ri
        puts "YES"
      else
        puts "NO"
      end
    end
  end
else
  puts (s == t ? "YES" : "NO")
end


class CodeForces

  attr_reader :answer

  def initialize(test_cases)
    @test_cases = test_cases
  end

def answer

@test_cases.to_i.times do |i|

  args =  gets.chomp
  solve(args)

end
end

private

def solve(args)

  # multiple assign here!
  # use the right amount of variables

 # a = args.split(" ").map(&:to_i)

  # do work here
  answer = args.to_s.size

  puts answer
end
end

CodeForces.new(gets.chomp).answer

t = gets.chomp.to_i
t.times do
    n = gets.chomp.split("")
    puts n.size

end


gets.to_i.times do
    gets
    a = gets.split.map &:to_i
    a.delete a.min
    puts a.size
end

alias g gets;g.to_i.times{g;a=g.split.map &:to_i;p a.size-a.count(a.min)}

alias g gets;g.to_i.times{g;a=g.split.map &:to_i;puts a.size-a.count(a.min)}

alias g gets
g.to_i.times {
  g
  a = g.split.map &:to_i
  puts a.size - a.count(a.min)
}

def g
    gets
end
g.to_i.times {
    g
    a = g.split.map &:to_i
    puts a.size - a.count(a.min)
}

gets.to_i.times do
    gets
    a = gets.split.map &:to_i
    puts a.size - a.count(a.min)
end

gets.to_i.times do
  _ = gets
  arr = gets.split.map &:to_i
  min = arr.min
  sum = 0
  for i in arr
    sum += 1 if i > min
  end

  puts sum
end

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  a.delete(a.min)
  puts a.size
end