results = []

t = gets.chomp
(1..t.to_i).each do |iteration|
	n, k = gets.chomp.split(' ').map{|e| e.to_i}
	rest = gets.chomp
	index  = 0
	fragments = []
	places_count = 0
	while index < rest.length do
		if rest[index] == '0'
			places_count += 1
			index += 1
		else
			places_count -= k
			if places_count > 0
				fragments.push(places_count)
			end
			places_count = 0
			index += k + 1
		end
	end
	if places_count > 0
		fragments.push(places_count)
	end

	optimal_tables = 0
	fragments.each do |el|
		optimal_tables += el / (k + 1)
		optimal_tables += 1 if el % (k + 1) > 0
	end

	results.push(optimal_tables)
end

results.each do |result|
	puts result
end

t = gets.to_i
t.times do
	n, k = gets.split.map(&:to_i)
	s = "1" + "0" * k + gets.chomp + "0" * k + "1" # append sentinel

	occupy = 0

	# count continous 0s and check
	count = 0
	s.each_char do |c|
		case c
		when "0"
			count += 1
		when "1"
			avail = [(count + 1) / (k+1) - 1, 0].max
			occupy += avail
			#puts "  #{count} -> #{avail}"
			count = 0
		end
	end

	p occupy
end

results = []
t = gets.chomp
(1..t.to_i).each do
	tables_count, k = gets.chomp.split(' ').map{|e| e.to_i}
	tabels_map = gets.chomp
	index  = 0
	empty_places_counts = []
	empty_places_count = 0
	while index < tables_count do
		if tabels_map[index] == '0'
			empty_places_count += 1
			index += 1
		else
			empty_places_count -= k
			if empty_places_count > 0
				empty_places_counts.push(empty_places_count)
			end
			empty_places_count = 0
			index += k + 1
		end
	end
	if empty_places_count > 0
		empty_places_counts.push(empty_places_count)
	end
	optimal_tables = 0
	empty_places_counts.each do |el|
		optimal_tables += el / (k + 1)
		optimal_tables += 1 if el % (k + 1) > 0
	end

	results.push(optimal_tables)
end

results.each do |res|
	puts res
end

def solve(arr_str, k)
	parts = arr_str
		.scan(/1+|0+/)
		.map { |i| i[0] == '0' ? i.size : 0}

	parts.map.with_index
		.reduce(0) do |res, (i, ind)|
			without_borders = i
			without_borders -= k unless ind == 0
			without_borders -= k unless ind == parts.size - 1
			if without_borders > 0
				res += 1 + (without_borders - 1) / (k + 1)
			end

			res
		end
end

total_qty = ARGF.gets.to_i
total_qty.times do
	_n, k = ARGF.gets.split.map(&:to_i)
	a = ARGF.gets.strip
	puts solve(a, k)
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	allows = gets.chomp.split(" ")
	tables_count = allows.first.to_i
	k = allows.last.to_i
	map = gets.chomp
	reserved_count = 0
	(0..(map.length-1)).each do |index|
		if map[index] == '1'
			reserved_count += 1
			if reserved_count == 1
				ost = index % (k+1)
				if  ost > 0
					tables_count -= ost
				end
			end
		end
	end
	if tables_count <= k
		result.push(1 - reserved_count)
		next
	end
	all_possible_tables = tables_count / (k + 1)
	if tables_count % (k + 1) > 0
		all_possible_tables += 1
	end
	res = all_possible_tables - reserved_count
	result.push(res)
end
result.each do |res|
	puts res
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	allows = gets.chomp.split(" ")
	tables_count = allows.first.to_i
	k = allows.last.to_i
	map = gets.chomp
	reserved_count = 0
	(0..(map.length-1)).each do |index|
		if map[index] == '1'
			reserved_count += 1
			if reserved_count == 1
				ost = index % (k+1)
				if  ost > 0
					tables_count -= ost
				end
			end
		end
	end
	all_possible_tables = tables_count / (k + 1)
	if tables_count % (k + 1) > 0
		all_possible_tables += 1
	end
	res = all_possible_tables - reserved_count
	if res < 0
		result.push(1)
		next
	end
	if reserved_count == 0 && res < 1
		result.push(1)
		next
	end
	result.push(res)
end
result.each do |res|
	puts res
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	allows = gets.chomp.split(" ")
	tables_count = allows.first.to_i
	k = allows.last.to_i
	map = gets.chomp
	reserved_count = 0
	(0..(map.length-1)).each do |index|
		if map[index] == '1'
			reserved_count += 1
			if reserved_count == 1
				ost = index % (k+1)
				if  ost > 0
					tables_count -= ost
				end
			end
		end
	end
	all_possible_tables = tables_count / (k + 1)
	if tables_count % (k + 1) > 0
		all_possible_tables += 1
	end
	result.push(all_possible_tables - reserved_count)
end
result.each do |res|
	puts res
end

def solve(arr_str, k)
	parts = arr_str
		.split('1')
		.map(&:size)
		.reject(&:zero?)

	parts.map.with_index
		.reduce(0) do |res, (i, ind)|
			without_borders = i
			without_borders -= k unless ind == 0
			without_borders -= k unless ind == parts.size - 1
			if without_borders > 0
				res += 1 + (without_borders - 1) / (k + 1)
			end

			res
		end
end

total_qty = ARGF.gets.to_i
total_qty.times do
	_n, k = ARGF.gets.split.map(&:to_i)
	a = ARGF.gets.strip
	puts solve(a, k)
end


  t = gets.to_i
  t.times do
    n, k = gets.split(' ').map(&:to_i)

    # solution 3

    if n == k
      puts n + 1
      next
    end
    if k < n
      puts k
      next
    end
    # puts n*(k/n) + (k-1)
    # puts n*(k/n-1) + (k-1)
    # 5 12
    ra = k / (n - 1)
    re = k % (n - 1)
    koef = re == 0 ? -1 : re
    puts n * ra + koef
  end

t = gets.to_i

(1).upto(t) do
  x = gets.split(' ')
  n = x[0].to_i
  k = x[1].to_i
  val = k/(n-1)
  val+=k
  val-= (k.modulo(n-1)==0?1:0)
  puts val
end

# Created by IntelliJ IDEA.
# User: gautam.roy
# Date: 5/10/20
# Time: 1:25 PM
# To change this template use File | Settings | File Templates.

# if n > k -> k
# return k/n + k
t = gets.to_i
for i in 1..t do
  n, k = gets.split(' ').map { |s| s.to_i }
  if n > k
    puts k
    next
  end
  puts (k - 1) / (n-1) + k
end

def func(n, k)
    res=2**40-1
    a=2**39
    while a>0
        t=res^a
        cnt=t-t/n
        res=t if cnt>=k
        a>>=1
    end
    res
end

gets.to_i.times{
    n, k=gets.split.map &:to_i
    puts func(n, k)
}

1.upto(gets.to_i) do
  n, k = gets.split(' ').map(&:to_i)

  puts k.div(n-1)*n + (k.modulo(n-1).zero? ? -1 : k.modulo(n-1))
end

  t = gets.to_i
  t.times do
    n, k = gets.split(' ').map(&:to_i)
    if n == k
      puts n + 1
      next
    end
    if k < n
      puts k
      next
    end
    ra = k / (n - 1)
    re = k % ra
    koef = re == 0 ? -1 : re
    puts n * ra + koef
end


t = gets.chomp.to_i
t.times do
  px, py = gets.chomp.split(' ').map(&:to_i)
  s = gets.chomp.split('')
  lx = rx = uy = ly = 0
  s.each do |el|
    case el
    when "U"
      uy += 1
    when "D"
      ly -= 1
    when "R"
      rx += 1
    when "L"
      lx -= 1
    end
  end
  resx = px > 0 && px <= rx || px < 0 && px >= lx || px == 0
  resy = py > 0 && py <= uy || py < 0 && py >= ly || py == 0
  puts resx && resy ? "YES" : "NO"
end

def solve
  x, y = ints
  s = string.chars.tally
  enough = true
  enough = false if x > 0 and (s[?R] || 0) < x.abs
  enough = false if x < 0 and (s[?L] || 0) < x.abs
  enough = false if y > 0 and (s[?U] || 0) < y.abs
  enough = false if y < 0 and (s[?D] || 0) < y.abs
  yesno enough
end

def main
  t = int
  t.times do
    solve
  end
end

DEBUG = true
MOD = 10**9+7
YESNO = %w(NO YES)
INF = 10**9

def int; gets.to_s.to_i end
def ints; gets.to_s.split.map { |s| s.to_i } end
def int1s; gets.to_s.split.map { |s| s.to_i - 1 } end
def float; gets.to_s.to_f end
def floats; gets.to_s.split.map { |s| s.to_f } end
def array_of(&convert); gets.to_s.split.map(&convert) end
def string; gets.to_s.chomp end
def rep(n, &b); Array.new(n, &b) end
def yes; puts YESNO[1] end
def no; puts YESNO[0] end
def yesno t; puts YESNO[t] end
def zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end
def max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end
def min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end
def minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end
def gcd(*xs); xs.inject(0, :gcd) end
def matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end
def debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end
def debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join("") } if DEBUG; grid end
def if_debug; yield if DEBUG end

module Boolean
  def coerce(other); [other, to_i] end
  def +@; to_i end
  def to_int; to_i end
  def *(other); to_i * other end
end

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

class Integer
  def div_ceil(y); (self + y - 1) / y end
  def mod_inv(mod = MOD); pow(mod-2, mod) end
  def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end
  def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end
  def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster
  def bitbrute(&block); (1<<self).times(&block) end
  def nCr(r); x = 1; (1..r).each { |i| x *= self + 1 - i; x /= i }; x; end
  def each_divisor; return Enumerator.new { |y| each_divisor { |k| y << k } } unless block_given?; k = 1; while k * k < self; if self % k == 0; yield k; yield self / k end; k += 1; end; yield k if k * k == self end
  def divisors; each_divisor.to_a end
end

class Range
  def end_open; exclude_end? ? self.end : self.end + 1 end
  def end_close; exclude_end? ? self.end - 1 : self.end end
  def upper_bound; ac, wa = self.begin, self.end_open; while wa - ac > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end
  def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end
end

class Array
  def power(&block); (0 ... 1 << size).each(&block) end
  def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end
  def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end
  def cumdiff(range); self[range.end_open] - self[range.begin]; end
end

module Enumerable
  def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end
  def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end
end

main

gets.to_i.times do
  x,y=gets.split.map(&:to_i)
  s=gets.chomp.chars
  s.each do |ss|
    if ss=="U"&&y>0
      y-=1
    elsif ss=="D"&&y<0
      y+=1
    elsif ss=="R"&&x>0
      x-=1
    elsif ss=="L"&&x<0
      x+=1
    end
  end
  if [x,y]==[0,0]
    puts "YES"
  else
    puts "NO"
  end
end

t= gets.chomp.to_i
while t > 0
 flag = false
 x, y = 	gets.chomp.split(' ')
 s = gets.chomp
 x = x.to_i
 y = y.to_i
 count_u = s.count('U')
 count_r = s.count('R')
 count_l = s.count('L')
 count_d = s.count('D')
 if (x >= 0 && y >= 0)
   if (count_r >= x.abs && count_u >= y.abs)
     puts "YES"
     flag = true
   end
 elsif  (x >= 0 && y < 0)
   if (count_r >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y >= 0)
 	if (count_l >= x.abs && count_u >= y.abs)
 		 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y < 0)
   if (count_l >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 end
 if(!flag)
   puts "NO"
 end
 t = t - 1
end

gets.to_i.times do
    x, y = gets.split.map &:to_i
    s = gets.chomp
    l, r, d, u = 0, 0, 0, 0
    s.each_char do |c|
        case c
            when 'L'
                l += 1
            when 'R'
                r += 1
            when 'D'
                d += 1
            else
                u += 1
        end
    end
    x = 0 if x > 0 && r >= x || x < 0 && l >= -x
    y = 0 if y > 0 && u >= y || y < 0 && d >= -y
    puts x == 0 && y == 0 ? "YES" : "NO"
end

t= gets.chomp.to_i
while t > 0
 flag = false
 x, y = 	gets.chomp.split(' ')
 s = gets.chomp
 x = x.to_i
 y = y.to_i
 count_u = s.count('U')
 count_r = s.count('R')
 count_l = s.count('L')
 count_d = s.count('D')
 if (x > 0 && y > 0)
   if (count_r >= x.abs && count_u >= y.abs)
     puts "YES"
     flag = true
   end
 elsif  (x > 0 && y < 0)
   if (count_r >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y > 0)
 	if (count_l >= x.abs && count_u >= y.abs)
 		 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y < 0)
   if (count_l >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 end
 if(!flag)
   puts "NO"
 end
 t = t - 1
end


n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
cnt = 0
cur = a[0]
1.upto(n - 1) do |i|
    if cur == n
        puts n
        break
    elsif cur > a[i]
        cnt += 1
        if cnt == k
            puts cur
            break
        end
    else
        cnt = 1
        cur = a[i]
        if cnt == k || cur == n
            puts cur
            break
        end
    end
end

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
c=m
while true
	if x[0]==y
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		c-=1
	else
		a=x.shift
		x.push(a)
		c=m-1
	end
	break if c==0
end
puts x[0]

n, k = gets.split.map &:to_i
b, c = 0, 0
gets.split.map(&:to_i).each{|v|
	c = b > 0 ? (b > v ? c + 1 : 1) : 0
	b = [b, v].max
	c == k && break
}
p b

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
hs=Hash.new(0)
while true
	if x[0]==y
		hs[y]+=m
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		hs[a]+=1
	else
		a=x.shift
		x.push(a)
		hs[x[0]]+=1
	end
	m-=1
end
ans=-1
ansV=-1
hs.each{|k,v|
	if v>ansV
		ansV=v
		ans=k
	end
}
puts ans

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
hs=Hash.new(0)
while true
	if x[0]==y
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		hs[a]+=1
	else
		a=x.shift
		x.push(a)
		hs[x[0]]+=1
	end
end
puts x[0]

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
hs=Hash.new(0)
while true
	break if m==0
	if x[0]==y
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		hs[a]+=1
	else
		a=x.shift
		x.push(a)
		hs[x[0]]+=1
	end
	m-=1
end
puts x[0]


n , m = gets.chomp.split(" ").map(&:to_i)
ns = gets.chomp.split(" ")
ms = gets.chomp.split(" ")
ngo = gets.chomp.to_i
ngo.times do |i|
	oil = gets.chomp.to_i - 1
	puts ns[oil % n] + ms[oil % m]
end

n,m=gets.split().map(&:to_i)
a=gets.split().map(&:to_s)
b=gets.split().map(&:to_s)
t=gets.to_i
    for i in 1..t do
    q=gets.to_i-1
    f1,f2=q % n,q % m
    puts a[f1]+b[f2]
end

n, m = gets.split.map &:to_i
s = gets.split
t = gets.split
q = gets.to_i
q.times do
  y = gets.to_i - 1
  puts s[y%n] + t[y%m]
end

n, m = gets.chomp.split.map(&:to_i)
a = gets.chomp.split
b = gets.chomp.split
q = gets.chomp.to_i

(0...q).each do |_|
  t = gets.chomp.to_i
  puts "#{a[(t-1) % n]}#{b[(t-1) % m]}"
end


gets
a = gets.split.map(&:to_i)
b = a.size - 1
while b > 0 do
  break if a[b - 1] > a[b]
  b -= 1
end
p b

n = gets.chomp.to_i
line = gets.chomp.split(" ").collect! {|x| x.to_i}
p = n-1
ok = true
q = n
while p>0 and ok

	if line[p]<line[p-1]
		ok = false
	end
	p -= 1
	q -= 1
end

if ok
 q-=1
end
puts q

#!/usr/bin/env ruby

n, mn, tot = gets.to_i, 0x3f3f3f3f, 0
gets.split.map(&:to_i).reverse.each_with_index do |v, i|
	if v > mn
		tot = n - i
		break
	end
	mn = v
end
puts tot

n = gets.to_i

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

a.reverse!

l = 0
result = 0
ok = false

a.each do |i|
	if l == 0
		l = i
	else
		if ok
			result += 1
			next
		end
		if i > l
			ok = true
			result = 1
		else
			l = i
		end
	end
end

puts result

n = gets.to_i
a = gets.split.map(&:to_i)
i = n-1
while i >= 1 and a[i] > a[i-1] do
	i -= 1
end
puts i

n = Integer(gets.chomp)
count = 1
input = gets.split.collect{|element| element.to_i}
(input.length-1).downto 1 do |i|
    if input[i-1]<input[i]
        count = count+1
    else
        break
    end
end
puts n-count

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
a = STDIN.readline.split.collect {|s| s.to_i }

i = n-1
while i > 0
	break if a[i-1] > a[i]
	i -= 1
end
puts i

n = gets.to_i
a = gets.split(" ").map(&:to_i)
j = 0
n.times{ |i|
    if ( i==n-1 || a[n-1-i] < a [n-2-i] ) then j = i; break; end;
}
puts n - j - 1

gets
a = gets.split.map{|e| e.to_i}
l = a.pop
ans = a.length
a.reverse.each do |e|
  if e < l
    ans -= 1
    l = e
  else
    break
  end
end
puts ans

def run
  n = ARGF.readline
  s = ARGF.readline.split.map(&:to_i)
  len = s.length

  cnt = 0
  (len - 1).times do |i|
    cnt = i + 1 if s[i] > s[i + 1]
  end

  puts cnt
end

run if $0 == __FILE__

n = gets.to_i
a = gets.split.map &:to_i
i = n-1
while (i > 0) && (a[i-1] < a[i]) do
  i -= 1
end
puts i

n=gets.to_i
a = gets.split(' ').collect!{|i| i.to_i}

x = 0
n.times do |i|
	break if i == n-1
	if a[n-i-1] < a[n-i-2]
		x = n-i-1
		break
	end
end
puts x

gets
a = gets.split.map(&:to_i)
b = a.size - 1
while b >= 0 do
  break if a[b - 1] > a[b]
  b -= 1
end
p b

n = gets.to_i
a = gets.split.map(&:to_i)
i = n-1
while i >= 1 and a[i] < a[i-1] do
	i -= 1
end
puts n-i-1

n = gets.to_i
a = gets.split.map(&:to_i)
ans = 0
i = 0
while a[i] != 1 do
	ans +=1
	i += 1
end
puts ans

n = gets.to_i
puts gets.split.map(&:to_i).index(1)

n = gets.to_i
a = gets.split.map(&:to_i)
i = 0
ans = 0
while a[i] != 1 and i <= n-1 do
	i += 1
	ans += 1
end
while i < n-2 do
	if a[i+1] > a[i+2] then
		ans += 1
	end
	i+=1
end

puts ans

n = gets.to_i
a = gets.split.map(&:to_i)
ans = 0
for i in 0...(n-1) do
	ans +=1  if a[i] > a[i+1]
end
puts ans

n = Integer(gets.chomp)
count = 1
input = gets
input = input.split
(input.length-1).downto 1 do |i|
    if input[i-1]<input[i]
        count = count+1
    else
        break
    end
end
puts n-count

n = Integer(gets)
count = 1
input = gets.split
(n-1).downto 1 do |i|
    if input[i-1]<input[i]
        count = count+1
    else
        break
    end
end
puts n-count

n = gets.to_i

a = gets.split(" ").map(&:to_i)
puts a.index(1)

gets
a = gets.split(" ").map(&:to_i)
puts a.index(a.min)

n = gets.to_i

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

puts a.index(a.min)

gets
a = gets.split.map{|e| e.to_i}
i = a.index(1)
puts a[0...i].length

def run
  n = ARGF.readline
  s = ARGF.readline.split.map(&:to_i)
  len = s.length

  cnt = 0
  (len - 1).times do |i|
    cnt += 1 if s[i] > s[i + 1]
  end

  puts cnt
end

run if $0 == __FILE__

n=gets.to_i
a = gets.split(' ')

x = 0
n.times do |i|
	if a[n-i-1] < a[n-i-2]
		x = n-i-1
		break
	end
end
puts x

_, mn, tot = gets, 0x3f3f3f3f, 0
gets.split.map(&:to_i).reverse.each { |i| i < mn ? mn = i : tot += 1 }
puts tot

n = gets.to_i

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

a.reverse!

l = 0
result = 0

a.each do |i|
	if l == 0
		l = i
	else
		if i > l
			result += 1
		end
		l = i
	end
end

puts result

n = gets.to_i

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

a.reverse!

l = 0
result = 0

a.each do |i|
	if l == 0
		l = i
	else
		if i > l
			result += 1
		else
			l = i
		end
	end
end

puts result


n=gets.chomp.to_i
a=gets.chomp.split('').to_a.collect{|i| i.to_i}
b=gets.chomp.split('').to_a.collect{|i| i.to_i}
x=0
n.times{|i| t = (a[i]-b[i]).abs; x+=[t,10-t].min}
puts x

num = gets.to_i
start = gets
finish = gets

# a > b
def cnt_forward(a, b)
 return a - b
end

# a > b
def cnt_backward(a, b)
 return (10 - a) + b
end

start_a = start.split('').map { |s| s.to_i }
finish_a = finish.split('').map { |s| s.to_i }
data = start_a.zip(finish_a)

result = 0

data.each { |pair|
 fcnt = cnt_forward(pair.max, pair.min)
 bcnt = cnt_backward(pair.max, pair.min)
 if fcnt > bcnt
 result += bcnt
 else
 result += fcnt
 end
}

puts result

# http://codeforces.com/contest/540/problem/A
n = gets.chomp.to_i
origin = gets.chomp.chars.map(&:to_i)
present = gets.chomp.chars.map(&:to_i)

arr = (0...n).map do |idx|
  a, b = [origin[idx], present[idx]].sort

  (b - a <= 10 - b + a) ? b - a : 10 - b + a
end

puts arr.inject(0, :+)

n = gets.to_i

a = gets
b = gets

s = 0

(0..n - 1).each do |i|
  ai = a[i].to_i
  bi = b[i].to_i
  x = (ai - bi).abs
  x = [x, (ai + 10 - bi).abs].min
  x = [x, (bi + 10 - ai).abs].min

  s += x
end

puts s

def count(currNum, targetNum)
	return (currNum - targetNum).abs if (currNum - targetNum).abs <= 5
	return ([currNum, targetNum].min + 10) - ([currNum, targetNum].max)
end

diskNum = gets.chomp.to_i
originState = gets.chomp.split('').map {|item| item.to_i}
correctState = gets.chomp.split('').map {|item| item.to_i}

result = 0
loopNum = 0

until loopNum == originState.length
	#puts originState[loopNum]
	#puts correctState[loopNum]
	result += count(originState[loopNum], correctState[loopNum])

	#puts "result is #{result}"
	loopNum += 1
end

puts result

n=gets.chomp.strip.to_i
#puts n
a=gets.chomp.strip
b=gets.chomp.strip
count=0
i=0
while i<n
	k=(a[i].to_i-b[i].to_i).abs
	#puts k
	if k>5
		count+=10-k
	else
		count+=k
	end
	i+=1
end
puts count

def f a
  a = -a if a < 0
  return 10 - a if a > 5
  a
end
n = gets.to_i; a = gets; b = gets;
sum = 0
n.times {|i| sum += f(a[i].to_i-b[i].to_i)}
puts sum

number = gets.to_i
origin = gets
final = gets
ans = 0
for i in 0...number
  c1 = origin[i]
  c2 = final[i]
  moves = (c1.to_i - c2.to_i).abs
  moves = 10 - moves if moves > 5
  ans = ans + moves
end
puts ans

n = gets.to_i
s = gets.chomp
t = gets.chomp

a = s.split(//).map{|r| r.to_i}
b = t.split(//).map{|r| r.to_i}

counter = 0
for i in 0..n-1
    counter+=[(a[i]-b[i]).abs,(10-a[i]+b[i]).abs,(10-b[i]+a[i]).abs].min
end
puts counter

mv = 0
gets
init = gets.chomp.split("").map &:to_i
pass = gets.chomp.split("").map &:to_i
init.each_with_index do |value, index|
  mv += [(value - pass[index]).abs,
         (10 - value).abs + pass[index],
         (10 - pass[index]).abs + value].min
end
puts mv

  n = gets.chomp.to_i
  a = gets.chomp ; b = gets.chomp
  res = 0
  n.times do |i|
    res += [(a[i].to_i - b[i].to_i).abs , 10 - (a[i].to_i - b[i].to_i).abs].min
  end
  puts res

n = gets.to_i
a = gets.split(//)
b = gets.split(//)
sum=0
(0..n-1).each{ |i|
  a[i]=a[i].to_i
  b[i]=b[i].to_i
  sum += [(a[i]-b[i]).abs,10-(a[i]-b[i]).abs].min
}
print sum

n = gets.to_i
origin = gets.chomp.chars.map(&:to_i)
after = gets.chomp.chars.map(&:to_i)

ans = 0
(0..n-1).each do |i|
  o = origin[i]
  a = after[i]
  ans += [(o-a).abs, (o+10-a).abs, (a+10-o)].min
end
puts ans

n=gets.chomp.to_i

position=gets.chomp.split(//)

pos=[]
position.each do |i|
	pos.push(i.to_i)
end

code=gets.chomp.split(//)

script=[]
code.each do |i|
	script.push(i.to_i)
end

change=0
for item in(0..script.length-1)
	b=(pos[item]-script[item]).abs
	if b>5
		change=change+10-b
	else
		change=change+b
	end
end

puts change

n=gets.to_i
s=gets.chomp
t=gets.chomp

ans=0
s.chars.zip(t.chars).each{|a|
	e1=a[0].to_i
	e2=a[1].to_i
	f1=(e1-e2).abs
	f2=([e1,e2].min+10-[e1,e2].max).abs
	ans=ans+[f1,f2].min
}
puts ans

t = gets.chomp.to_i
start = gets.chomp.split('').map(&:to_i)
stop = gets.chomp.split('').map(&:to_i)

r = 0
t.times.each do |i|
#  p [start[i],stop[i],(start[i]-stop[i]).abs]
  if ((start[i]-stop[i]).abs) > 5
    r += (10 - ((start[i]-stop[i]).abs))
  else
    r += (start[i]-stop[i]).abs
  end
end

puts r

gets
a = gets.chomp
b = gets.chomp
r = 0
0.upto(a.length) do |i|
	x = a[i].to_i
	y = b[i].to_i
	if x > y

		r += [ x - y, x + 10 - y, y + 10 - x ].min
	else
		r += [ y - x, y + 10 - y, x + 10 - y].min
	end
end
puts r

n = gets.chomp.to_i
orig = gets.chomp
combi = gets.chomp
first_state = orig.split("")
combination = combi.split("")

count = 0
for i in (0..n-1)
	if first_state[i].to_i - combination[i].to_i > 5
		count += (10 - (first_state[i].to_i - combination[i].to_i))
	elsif combination[i].to_i - first_state[i].to_i > 5
		count += (10 - (combination[i].to_i - first_state[i].to_i))
	else
		temp = combination[i].to_i - first_state[i].to_i
		if temp < 0
			temp = temp * -1
			count += temp
		else
			count += temp
		end
	end
end

puts count

gets
p gets.chomp.split(//).zip(gets.chomp.split(//)).map { |x, y| [(x.to_i - y.to_i).abs, (x.to_i - y.to_i - 10).abs, (x.to_i - y.to_i + 10).abs].min }.reduce(:+)

n = gets.to_i
origin = gets.split("").collect{|i| i.to_i}
destin = gets.split("").collect{|i| i.to_i}
ans = 0
(0...n).each do |i|
    tmp = (origin[i]-destin[i]).abs
    ans += [tmp,10-tmp].min
end
puts ans

n = gets.to_i
a = gets.chomp.chars.map(&:to_i)
b = gets.chomp.chars.map(&:to_i)
ans = 0
n.times do |i|
  c = (a[i] - b[i]).abs
  if c > 5
    c = 10 - c
  end
  ans += c
end
puts ans

len = gets.to_i
curr = gets.strip
answ = gets.strip
acts = 0

for pos in 0...len
    if not curr[pos] == answ[pos]
        first = (curr[pos].to_i - answ[pos].to_i).abs
        acts += [first, 10 - first].min
    end
end

puts acts

gets
a = gets.chomp.chars.map(&:to_i)
b = gets.chomp.chars.map(&:to_i)

puts(a.zip(b).inject(0) { |s, d| [d.max-d.min, 10-d.max+d.min].min + s })

n = gets.to_i
s = gets.chomp
t = gets.chomp

a = s.split(//).map{|r| r.to_i}
b = t.split(//).map{|r| r.to_i}

def MIN(q,w,c)
   if q>w && w<c
       mini = w
   elsif q<w && q<c
       mini = q
   else
       mini = c
   end
end

counter = 0
for i in 0..n-1
  counter+=MIN((a[i]-b[i]).abs, 10-a[i]+b[i], 10-b[i]+a[i])
end
counter

n = gets.to_i
s = gets.chomp
t = gets.chomp

a = s.split(//).map{|r| r.to_i}
b = t.split(//).map{|r| r.to_i}

def MIN(q,w,c)
   if q>w && w<c
       mini = w
   elsif q<w && q<c
       mini = q
   else
       mini = c
   end
end

counter = 0
for i in 0..n-1
  counter+=MIN((a[i]-b[i]).abs, 10-a[i]+b[i], 10-b[i]+a[i])
end
puts counter

n = gets.to_i
s = gets.chomp
t = gets.chomp

a = s.split(//).map{|r| r.to_i}
b = t.split(//).map{|r| r.to_i}

def MIN(q,w,c)
   if q>w && w<c
       mini = w
   elsif q<w && q<c
       mini = q
   else
       mini = c
   end
end

counter = 0
for i in 0..n-1
  if a[i] != b[i]
     counter+=MIN((a[i]-b[i]).abs, 10-a[i]+b[i], 10-b[i]+a[i])
  else
     counter+=0
  end
end
puts counter

n = gets.chomp.to_i
a = gets.chomp ; b = gets.chomp
res = 0
5.times do |i|
  res += [(a[i].to_i - b[i].to_i).abs , 10 - (a[i].to_i - b[i].to_i).abs].min
end
puts res

len = gets.to_i
beg_state = gets.strip
end_state = gets.strip
acts = 0

for pos in 0...len
    if not beg_state[pos] == end_state[pos]
        acts += [
            (beg_state[pos].to_i - end_state[pos].to_i).abs,
            10 - beg_state[pos].to_i
        ].min
    end
end

puts acts


n, k = gets.split.map(&:to_i)
str = gets.strip
newStr = ""
str.each_char.with_index do |c, i|
  if c.ord >= 'n'.ord
    while k > 0 && c.ord > 'a'.ord
      k -= 1
      c = (c.ord - 1).chr
    end
  else
    while k > 0 && c.ord < 'z'.ord
      k -= 1
      c = c.ord.next.chr
    end
  end
  newStr += c
  if k == 0
    newStr += str[i+1..str.length]
    break
  end
end
if k == 0
  puts newStr
else
  puts -1
end

$an_str = ""

def solve1(el)
  if el.ord + $k < 'z'.ord + 1
    $an_str += (el.ord + $k).chr
    $k = 0
  else
    $an_str += 'z'
    $k -= ('z'.ord - el.ord)
  end
end

def solve2(el)
  if el.ord - $k >= 'a'.ord
    $an_str += (el.ord - $k).chr
    $k = 0
  else
    $an_str += 'a'
    $k -= (el.ord - 'a'.ord)
  end
end

_ , $k = gets.strip.split(' ').map(&:to_i)
str = gets.strip

str.each_char.with_index { |el, i|
  if $k == 0
    $an_str += str[i..str.length]
    break
  end
  if 'z'.ord - el.ord > el.ord - 'a'.ord
    solve1(el)
  else
    solve2(el)
  end
}

if !$k.zero?
  puts "-1"
else
  puts $an_str
end

n, k = gets.split.map(&:to_i)
str = gets.chomp
newStr = ""
str.each_char.with_index do |c, i|
  if c.ord > 'n'.ord
    while k > 0 && c.ord > 'a'.ord
      k -= 1
      c = (c.ord - 1).chr
    end
  else
    while k > 0 && c.ord < 'z'.ord
      k -= 1
      c = c.ord.next.chr
    end
  end
  newStr += c
  if k == 0
    newStr += str[i+1..str.length]
    break
  end
end
if k == 0
  puts newStr
else
  puts -1
end

n, k = gets.split.map(&:to_i)
str = gets
newStr = ""
str.each_char do |c|
  if c.ord > 'n'.ord
    while k > 0 && c.ord > 'a'.ord
      k -= 1
      c = (c.ord - 1).chr
    end
  else
    while k > 0 && c.ord < 'z'.ord
      k -= 1
      c = c.ord.next.chr
    end
  end
  newStr += c
end
if k == 0
  puts newStr
else
  puts -1
end

n, k = gets.split.map(&:to_i)
str = gets.strip
newStr = ""
str.each_char.with_index do |c, i|
  if c.ord > 'n'.ord
    while k > 0 && c.ord > 'a'.ord
      k -= 1
      c = (c.ord - 1).chr
    end
  else
    while k > 0 && c.ord < 'z'.ord
      k -= 1
      c = c.ord.next.chr
      puts "#{k} #{c}"
    end
  end
  newStr += c
  if k == 0
    newStr += str[i+1..str.length]
    break
  end
end
if k == 0
  puts newStr
else
  puts -1
end

n, k = gets.split.map(&:to_i)
str = gets.strip
newStr = ""
str.each_char.with_index do |c, i|
  if c.ord > 'n'.ord
    while k > 0 && c.ord > 'a'.ord
      k -= 1
      c = (c.ord - 1).chr
    end
  else
    while k > 0 && c.ord < 'z'.ord
      k -= 1
      c = c.ord.next.chr
    end
  end
  newStr += c
  if k == 0
    newStr += str[i+1..str.length]
    break
  end
end
if k == 0
  puts newStr
else
  puts -1
end

$an_str = ""

def solve1(el)
  if el.ord + $k < 'z'.ord + 1
    $an_str += (el.ord + $k).chr
    $k = 0
  else
    $an_str += 'z'
    $k -= ('z'.ord - el.ord)
  end
end

def solve2(el)
  if el.ord - $k >= 'a'.ord
    $an_str += (el.ord - $k).chr
    $k = 0
  else
    $an_str += 'a'
    $k -= (el.ord - 'a'.ord)
  end
end

_ , $k = gets.strip.split(' ').map(&:to_i)
str = gets.strip

str.each_char { |el|
  puts $k
  if 'z'.ord - el.ord > el.ord - 'a'.ord
    solve1(el)
  else
    solve2(el)
  end
}

if !$k.zero?
  puts "-1"
else
  puts $an_str
end

c = ('a'..'z').to_a

_ , k = gets.strip.split(' ').map(&:to_i)
str = gets.strip.split('')
an_str = ""

str.each { |el|
  mx = -1
  c.each { |el2|
    if (el2.ord - el.ord).abs <= k
      mx = [mx, (el2.ord - el.ord).abs].max
    end
  }
  an_str += (el.ord + mx).chr
  k -= mx
}

if !k.zero?
  puts "-1"
else
  puts an_str
end

c = ('a'..'z').to_a

_ , k = gets.strip.split(' ').map(&:to_i)
str = gets.strip.split('')
an_str = ""

str.each { |el|
  mx = -1
  c.each { |el2|
    if (el2.ord - el.ord).abs <= k

      mx = [mx, el2.ord - el.ord].max
    end
  }
  an_str += (el.ord + mx).chr
  k -= mx
}

if !k.zero?
  puts "-1"
else
  puts an_str
end


v = STDIN.readline().to_i
@costs = STDIN.readline().split().collect(&:to_i)

def solve(v)
  x, xv = @costs.rindex(@costs.min), @costs.min
  @costs.collect! { |y| y - xv }
  n, r = v / xv, v % xv

  if n == 0
    STDOUT.write(-1)
    return
  end

  while r > 0
    y = @costs.rindex {|y| y <= r}
    break if !y || @costs[y] == 0
    STDOUT.write(y + 1)
    r -= @costs[y]
    n -= 1
  end

  STDOUT.write ((x+1).to_s * n)
end

solve v

total = gets.to_i
a = gets.split(" ").map{|ai| ai.to_i}
res = []
digit = -1
minw = a.min
a.each_index{|i|
	if a[i] == minw
		digit = i
	end
}
mcnt = total / minw
if mcnt == 0
	print "-1"
elsif total % a[digit] == 0
	print (digit+1).to_s*mcnt
else
	result = ""
	rest = total % a[digit]
	quant = a[digit]
	8.downto(digit+1) {|i|
		n = 1
		while rest + quant*n >= a[i]*n
			n += 1
		end
		n -= 1
		if n > 0
			mcnt -= n
			result += (i+1).to_s * n
		end
		rest = rest + quant*n - a[i]*n
		break if rest == 0
	}
	result += (digit+1).to_s * mcnt
	print result
end

m=gets.to_i
a=gets.chomp.split.map(&:to_i)
min,len=a.min,m/a.min
if len==0
	puts "-1"
	exit
end
mod=m%min
for i in 0...len
	j=8
	while j>=0
		if a[j]-min<=mod
			mod-=a[j]-min
			break
		end
		j-=1
	end
	print j+1
end
puts

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

min = arr.min
max = n/min
max_ind = arr.index(min)
digit = max_ind + 1
if max == 0
	ans = "-1"
else
	ans = "#{digit}" * max
	curr_paint = n - (max * arr[digit - 1])
	# puts "digit #{digit - 1}"
	# puts "#{curr_paint}"
	pos = 0
	while curr_paint >= 0
		replace = false
 		8.downto(digit).each do |ind|
 			# puts "rem #{ind} #{curr_paint + arr[a-1] - arr[ind]}"
 			x = arr[ind] - arr[digit-1]
 			if curr_paint >= x
 				ans[pos] = (ind + 1).to_s
 				curr_paint =  curr_paint - x
 				replace = true
 				break
 			end
 		end
 		break unless replace
 		pos += 1
 		break if pos == max
	end

end

puts ans

v=gets.to_i
a=gets.split.map(&:to_i)
b,c=a.each_with_index.to_a.min_by{|x,i|[x,-i]}
c+=1
d=v/b
(p -1;exit)if d==0
e=d*b
f=8
r=''
while true
  f-=1 while v-e+b<a[f]
  break if f==c-1
  r<<(f+1).to_s
  v-=a[f]-b
  d-=1
end
r<<c.to_s*d
puts r

v=gets.to_i
a=gets.split.map(&:to_i)
b,c=a.each_with_index.to_a.min_by{|x,i|[x,-i]}
c+=1
d=v/b
(p -1;exit)if d==0
e=d*b
f=8
r=''
while true
  f-=1 while v-e+b<a[f]
  break if f==c-1
  print f+1
  v-=a[f]-b
  d-=1
end
d.times{print c}

v=gets.to_i
a=gets.split.map(&:to_i)
b,c=a.each_with_index.to_a.min_by{|x,i|[x,-i]}
c+=1
d=v/b
(p -1;exit)if d==0
e=d*b
f=8
r=''
while true
  f-=1 while v-e+b<a[f]
  break if f==c-1
  print f+1
  v-=a[f]-b
  d-=1
end
puts c.to_s*d

p = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
inp = a
a = (1..a.length).zip(a).sort_by{|d| [d[1], -d[0]]}
initp = p
can = false
ans = []
if p > 0
  best_digit = a.select{|d| d[1] <= p}.first
  can = true if best_digit
end
if !can
  puts -1
else
  ans = [best_digit[0]] * (p / best_digit[1])
  p -= best_digit[1] * ans.length
  # puts p
  a = a.sort_by{|d| -d[0]}
  ans.each_with_index do |dig_in_ans, ind|
    better_dig = a.select{|d| dig_in_ans < d[0] && d[1] - inp[dig_in_ans - 1] <= p}.first
    break if better_dig.nil?
    # puts better_dig.join(" - ")
    ans[ind] = better_dig[0]
    p -= better_dig[1] - inp[dig_in_ans - 1]
  end
  puts ans.join("")
end

#! /usr/bin/env ruby

paint = STDIN.gets.to_i
digits = STDIN.gets.to_s.split(' ').map(&:to_i).first(9)

def paint_fence(paint, digits)
  if paint == 0 || digits.min > paint
    puts '-1'
    return
  end

  min = 100000
  idx = nil
  digits.each_with_index do |require_digit, i|
    next if require_digit > min

    min = require_digit
    idx = i + 1
  end

  count = paint / min
  rest = paint % min
  suppl = []

  end_flg = false
  while rest > 0 && !end_flg
    digits.reverse.each_with_index do |require_digit, i|
      if (digit = 9 - i) <= idx
        end_flg = true
        break
      end
      if rest >= (require_digit - min)
        rest -= (require_digit - min)
        suppl << digit
        break
      end
    end
  end

  puts (idx.to_s * count).chars.map {|i|
    (alter = suppl.shift) ? alter : i
  }.join
end

paint_fence(paint, digits)

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
dp = []
arr.each do |a|
	dp << n / a
end
max = dp.max
puts "#{dp}"
max_ind = dp.rindex(max)
digit = max_ind + 1
if max == 0
	ans = "-1"
else
	ans = "#{digit}" * max
	curr_paint = n - max * arr[digit - 1]
	puts "#{curr_paint}"
	ans.each_char.with_index do |a,i|
 		a = a.to_i
 		8.downto(0).each do |ind|
 			# puts "rem #{ind} #{curr_paint + arr[a-1] - arr[ind]}"
 			if curr_paint + arr[a-1] - arr[ind]  >= 0
 				ans[i] = (ind + 1).to_s
 				curr_paint =  curr_paint + arr[a-1] - arr[ind]
 				break
 			end
 		end
 	end
end

puts ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
dp = []
arr.each do |a|
	dp << n / a
end
max = dp.max
# puts "#{dp}"
max_ind = dp.index(max)
digit = max_ind + 1
if max == 0
	ans = "-1"
else
	ans = "#{digit}" * max
	curr_paint = n - (max * arr[digit - 1])
	# puts "digit #{digit - 1}"
	# puts "#{curr_paint}"
	pos = 0
	while curr_paint > 0
		replace = false
 		8.downto(digit).each do |ind|
 			# puts "rem #{ind} #{curr_paint + arr[a-1] - arr[ind]}"
 			x = arr[ind] - arr[digit-1]
 			if curr_paint >= x
 				ans[pos] = (ind + 1).to_s
 				curr_paint =  curr_paint - x
 				replace = true
 				break
 			end
 		end
 		break unless replace
 		pos += 1
 		break if pos == max
	end

end

puts ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
dp = []
arr.each do |a|
	dp << n / a
end
max = dp.max
# puts "#{dp}"
max_ind = dp.rindex(max)
digit = max_ind + 1
if max == 0
	ans = "-1"
else
	ans = "#{digit}" * max
	curr_paint = n - max * arr[digit - 1]
	# puts "#{curr_paint}"
	ans.each_char.with_index do |a,i|
 		a = a.to_i
 		8.downto(0).each do |ind|
 			# puts "rem #{ind} #{curr_paint + arr[a-1] - arr[ind]}"
 			if curr_paint + arr[a-1] - arr[ind]  >= 0
 				ans[i] = (ind + 1).to_s
 				curr_paint =  curr_paint + arr[a-1] - arr[ind]
 				break
 			end
 		end
 	end
end

puts ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
dp = []
arr.each do |a|
	dp << n / a
end
max = dp.max
max_ind = dp.rindex(max)
digit = max_ind + 1
puts max == 0 ? "-1" : "#{digit}" * max

v=gets.to_i
a=gets.split.map(&:to_i)
b,c=a.each_with_index.to_a.min_by{|x,i|[x,-i]}
c+=1
d=v/b
(p -1;exit)if d==0
e=d*b
f=8
r=''
while true
  f-=1 while v-e+b<a[f]
  break if f==c-1
  r<<f+1
  v-=a[f]-b
  d-=1
end
r<<c.to_s*d
puts r

p = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
a = (1..a.length).zip(a).sort_by{|d| [d[1], -d[0]]}
initp = p
can = false
while p > 0
  digit = a.select{|d| d[1] <= p}.first
  break if digit.nil?
  can = true
  p -= digit[1]
  printf("%d", digit[0])
end
if !can
  puts -1
else
  puts
end

p = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
a = (1..a.length).zip(a).sort_by{|d| [d[1], -d[0]]}
initp = p
can = true
while p > 0
  digit = a.select{|d| d[1] <= p}.first
  if digit.nil? && p == initp
    can = false
    break
  end
  p -= digit[1]
  printf("%d", digit[0])
end
if can
  puts
else
  puts -1
end

p = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
a = (1..a.length).zip(a).sort_by{|d| [d[1], -d[0]]}
initp = p
can = true
while p > 0
  digit = a.select{|d| d[1] <= p}.first
  if digit.nil? && p == initp
    can = false
    break
  end
  p -= digit[1]
  printf("%d", digit[0])
end
if !can || p == 0
  puts -1
else
  puts
end

v = STDIN.readline().to_i
@costs = STDIN.readline().split().collect(&:to_i)
@memo = {}

def solve(v)
  return -1 if v <= 0
  return @memo[v] if @memo[v]

  ret = 0
  @costs.each_with_index do |x, idx|
    if v >= x
      rest = solve(v - x) * 10
      ans = [0, rest].max + idx + 1
      ret = [ret, ans].max
    end
  end

  @memo[v] = ret
end

puts solve(v).to_s

v = STDIN.readline().to_i
@costs = STDIN.readline().split().collect(&:to_i)

#v = 1000000
#@costs = %w(1 1 1 1 1 1 1 1 1).collect(&:to_i)
#v = 898207
#@costs = %w(99745 99746 99748 99752 99760 99776 99808 99872 100000).collect(&:to_i)

def solve(v)
  x, xv = @costs.rindex(@costs.min), @costs.min
  @costs.collect! { |y| y - xv }
  n, r = v / xv, v % xv

  return -1 if n == 0

  while r > 0
    y = @costs.rindex {|y| y <= r}
    break if !y || @costs[y] == 0
    STDOUT.write(y + 1)
    r -= @costs[y]
    n -= 1
  end

  while n > 0
    STDOUT.write(x + 1)
    n -= 1
  end
end

solve v

v = STDIN.readline().to_i
@costs = STDIN.readline().split().collect(&:to_i)
#v = 5
#@costs = %w(5 4 3 2 1 2 3 4 5).collect(&:to_i)

def solve(v)
  x, xv = @costs.rindex(@costs.min), @costs.min
  @costs.collect! { |y| y - x }
  n, r = v / xv, v % xv

  return -1 if n == 0

  ans = []
  while r > 0
    y = @costs.rindex {|y| y <= x}
    break if !y || @costs[y] == 0
    ans << (y + 1)
    r -= @costs[y]
  end

  while ans.length < n
    ans << (x + 1)
  end

  ans.collect(&:to_s).join().to_i
end

puts solve(v).to_s

def iter1 t, arr, lh
	cnt = t/arr[0]
	digit = lh[arr[0]]
	arr[1..-1].each {|cl|
		ccnt = t/cl
		break if ccnt < cnt
		digit = lh[cl] if lh[cl] > digit
	}
	cnt == 0 ? -1 : digit
end

total = gets.to_i
a = gets.split(" ").map{|ai| ai.to_i}
ln = {}
a.each_index{|i| ln[a[i]] = i}
len = a.sort.uniq
res = []
while total>0
	cd = iter1(total, len, ln)
	break if cd == -1
	res << cd + 1
	total -= a[cd]
end

if res.size == 0
	print "-1"
else
	print res.sort.reverse.join
end

def iter t, arr
	mlen = 0
	digit = -1
	arr.each_index {|i|
		cml = t/arr[i]
		# digit weights are ordered from 1 to 9
		if cml >= mlen
			mlen = cml
			digit = i
		end
	}
	mlen == 0 ? -1 : digit
end

total = gets.to_i
a = gets.split(" ").map{|ai| ai.to_i}
exit if a.include?(0)
res = []
while total > 0
	cd = iter(total, a)
	break if cd == -1
	res << cd + 1
	total -= a[cd]
end

if res.size == 0
	print "-1"
else
	print res.sort.reverse.join
end


def func(i)
  if i == 0
    0
  else
    func(i / 2) + i % 2
  end
end

def run
  n = ARGF.readline.to_i
  list = ARGF.readline.split.map(&:to_i)

  hash = Hash.new(0)
  list.each { |a| hash[func(a)] += 1 }

  cnt = 0
  hash.each do |key, val|
    cnt += (val * (val - 1)) / 2
  end

  puts cnt
end

run if $0 == __FILE__

def no_of_ones t
	p = 0
	while t>0
	  p += (t%2)
	  t = t/2
	end
	return p
end

n =gets.chomp.to_i
a = [0]*40
arr = gets.chomp.split(" ").collect! {|x| x.to_i}
arr.each { |k|
	a[no_of_ones(k)] += 1
}

s = 0
a.each { |p|
	s += (p*(p-1))/2
}

p s

def func(x)
  return 0 if x == 0
  return func(x / 2) if x % 2 == 0
  func(x / 2) + 1
end

n = gets.to_i
f = Array.new(100,0)
gets.split(' ').each { |i|
  f[func(i.to_i)] += 1
}
ans = 0
f.each { |i|
  j = i.to_i
  ans += j * (j - 1) / 2
}
p ans

def run
  n = ARGF.readline.to_i
  list = ARGF.readline.split.map(&:to_i)

  f = [0]
  1.upto(10 ** 5) {|i| f[i] = f[i / 2] + i % 2 }

  hash = Hash.new(0)
  list.each { |a| hash[f[a]] += 1 }

  cnt = 0
  hash.each do |key, val|
    cnt += (val * (val - 1)) / 2
  end

  puts cnt
end

run if $0 == __FILE__


n = gets.to_i

curr = 0
result = ""
for i in 1..n-1
  curr = (curr + i) % n
  result += (curr+1).to_s + " "
end

puts(result)

n = gets.chomp.to_i

step = 1
last_child = 1
(n - 1).times do
  last_child += step
  step += 1
  ch = last_child % n
  print "#{ch == 0 ? n : ch} "
end
puts

n = gets.to_i

cur, step = 0, 0

(n - 1).times do
  step += 1
  cur = (cur + step) % n
  printf("%d ", cur + 1)
end

printf("\n")

n, c, str = gets.to_i, 1, ""
0.upto(n - 2) { |i| str << (c = ((c + i) % n) + 1).to_s << " " }
print str.chop

n = gets.chop.to_i
kids = Array.new
i = 0
a = 1
(n - 1).times do
  i += 1
  a += i
  (a > n)? a = a - n : a
  kids.push(a)
end

puts kids

n = gets.chop.to_i
i = 0
a = 1
(n - 1).times do
  i += 1
  a += i
  (a > n)? a = a - n : a
  print "#{a} "
end

n = gets.to_i
passer = 0
passed = []
(1...n).each do |i|
  passer += i
  passed << passer % n + 1
end
puts passed.join(" ")

n = gets.to_i

r = 1
res = []
1.upto(n-1) do |m|
 r += m
 r %= n
 r = n if r == 0
 res << r
 res << ' '
end
res.pop
res.each {|e| print e}

n = gets.to_i
i=1
(n-1).times{|x|i+=x;i%=n;i+=1;print i;print " "}

n=gets.to_i

c=[*1..n]
s=0
for i in 1..n-1
	s+=i
	if c[s].nil?
		print "#{c[s%n]} "
	else
		print "#{c[s]} "
	end
end

n = gets.to_i
passer = 1
passed = []
(1...n).each do |i|
  passer = (passer + i) % n
  passed << passer
end
puts passed.join(" ")

n = gets.to_i

r = 1
res = []
1.upto(n-1) do |m|
 r += m
 r %= n
 res << r
 res << ' '
end
res.pop
res.each {|e| print e}

def f(n)
  l = [nil] * 5 + ['S','M','L','XL','XXL'] + [nil] * 5
  i = n+5
  r = []
  r << l[i]
  r << l[i+1]
  r << l[i-1]
  r << l[i+2]
  r << l[i-2]
  r << l[i+3]
  r << l[i-3]
  r << l[i+4]
  r << l[i-4]
  r << l[i+5]
  r << l[i-5]
  r.select {|e| e}
end

pr = []
l = ['S','M','L','XL','XXL']
1.upto(5) {|n| pr << f(n)}

a = gets.split(' ')
a = a.map{|e| e.to_i}

gets.to_i.times do
  size = gets.chomp
  tl = pr[l.index(size)]
  n = 0
  while a[l.index(tl[n])] == 0
    n += 1
  end
  a[l.index(tl[n])] -= 1
  puts tl[n]
end

n = gets.to_i

r = 1
1.upto(n-1) do |m|
 r += m
 r %= n
 print r," "
end

n=gets.to_i

man_id = 1
[*1..n-1].each do |v|
	man_id+=v

	rem=man_id%n
	rem+=1 if rem==0
	print "#{rem} "

	man_id=rem
end

n = gets.chomp.to_i
puts 2 or exit if n == 2
step = 1
last_child = 1
(n - 1).times do
  last_child += step
  step += 1
  print "#{last_child % n} "
end
puts

n, c, str = gets.to_i, 1, ""
1.upto(n - 1) { |i| str << (c = (c + i) % n).to_s << " " }
print str.chop

n, c, str = gets.to_i, 1, ""
1.upto(n - 1) do |i|
	c = (c + i) % n
	c = 4 if c == 0
	str << c.to_s << " "
end
print str.chop

n, c, str = gets.to_i, 0, ""
1.upto(n - 1) { |i| str << (c = (c + i) % n).to_s << " " }
print str.chop


def get_array(hash, pos)
  arr = []
  until pos.nil? or pos == 0
    arr.push(pos)
    pos = hash[pos]
  end
  arr
end

n = gets.to_i

prev = {}
for s in STDIN.each_line.take n
  p, n = s.split(' ').map &:to_i
  prev[p] = n
end

first = (prev.keys - prev.values)[0]
frs = get_array(prev, first)
scn = get_array(prev, prev[0])

puts frs.zip(scn).join(" ")

require 'set'
n = gets.to_i
a = Set.new
b = Set.new
map = Hash.new
(0...n).each do |i|
    aa, bb = gets.split.collect{|j| j.to_i}
    a.add(aa)
    b.add(bb)
    map[aa] = bb
end
ans = Array.new(n)
ans[0] = (a-b).to_a[0]
ans[1] = map[0]
for i in 2...n
    ans[i] = map[ans[i-2]]
end
puts ans.join(" ")

def get_array(hash, pos, array, ind)
  until pos.nil? or pos == 0
    array[ind] = pos
    pos = hash[pos]
    ind += 2
  end
end

n = gets.to_i
array = Array.new(n, 0)

prev = {}
for s in STDIN.each_line.take n
  p, n = s.split(' ').map &:to_i
  prev[p] = n
end

first = (prev.keys - prev.values)[0]
get_array(prev, first, array, 0)
get_array(prev, prev[0], array, 1)

puts array.join(" ")

def get_array(hash, pos)
  arr = []
  until pos.nil? or pos == 0
    arr << pos
    pos = hash[pos]
  end
  arr
end

n = gets.to_i

prev, after = {}, {}
for s in STDIN.each_line.take n
  p, n =s.split(' ').map &:to_i

  prev[p], after[n] = n, p
end

first = (prev.keys - after.keys)[0]
frs = get_array(prev, first)

scn = get_array(prev, prev[0])

puts frs.zip(scn).join(" ")

def get_array(hash, pos)
  arr = []
  until pos.nil? or pos == 0
    arr << pos
    pos = hash[pos]
  end
  arr
end

n = gets.to_i

prev = {}
for s in STDIN.each_line.take n
  p, n =s.split(' ').map &:to_i

  prev[p] = n
end

first = (prev.keys - prev.values)[0]
frs = get_array(prev, first)

scn = get_array(prev, prev[0])

puts frs.zip(scn).join(" ")

n = gets.to_i
bef_s = {}
aft_s = {}

n.times do
  bef, aft = gets.split.map(&:to_i)
  bef_s[bef] = aft
  aft_s[aft] = bef
end

ans = []
start_id = 0
start_pos = 1
while true
  if bef_s[start_id] and start_pos < n
    ans[start_pos] = bef_s[start_id]
    start_id = bef_s[start_id]
    start_pos += 2
  else
    break
  end
end

bef_s.each_key do |k|
  is_break = false
  if !aft_s[k]
    start_id = k
    break
  end
end

ans[0] = start_id
start_pos = 2
while true
  if bef_s[start_id] and start_pos < n
    ans[start_pos] = bef_s[start_id]
    start_id = bef_s[start_id]
    start_pos += 2
  else
    break
  end
end

puts ans.join(' ')

#! /usr/bin/env ruby
# coding: utf-8

def increment(i, a)
  i += 1
  i = 0 if a.length <= i
  i
end

n = gets.to_i

ans = Array.new(n, -1)
bef_s = []
aft_s = []
n.times do |i|
  bef, aft = gets.split.map(&:to_i)
  if (bef != 0 and aft != 0) and (!ans.index(bef) and !ans.index(aft))
    bef_s << bef
    aft_s << aft
    next
  else
    ans[1] = aft and next if bef == 0
    ans[-2] = bef and next if aft == 0

    if ans.index(bef)
      index = ans.index(bef)
      ans[index + 2] = aft
      next
    end

    if ans.index(aft)
      index = ans.index(aft)
      ans[index - 2] = bef
      next
    end
  end
end

formals = []
while 0 < bef_s.length
  bef = bef_s.shift
  aft = aft_s.shift

  if ans.index(bef)
    i = ans.index(bef)
    tmp_b = ans[0..(i + 1)]
    tmp_a = ans[(i + 2)..-1]
    tmp_a.unshift(aft)
    ans = tmp_b + tmp_a
    next
  end

  if ans.index(aft)
    i = ans.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = ans[0..(i - 2)]
      tmp_a = ans[(i - 1)..-1]
      tmp_a.unshift(bef)
      ans = tmp_b + tmp_a
    else
      ans.unshift(bef)
    end
    next
  end

  if formals.length == 0
    formals << bef
    formals << aft
    next
  end

  if formals.index(bef)
    i = formals.index(bef)
    if i + 2 < formals.length
      tmp_b = formals[0..(i + 1)]
      tmp_a = formals[(i + 2)..-1]
      tmp_a.unshift(aft)
      formals = tmp_b + tmp_a
    else
      formals << aft
    end
    next
  end

  if formals.index(aft)
    i = formals.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = formals[0..(i - 2)]
      tmp_a = formals[(i - 1)..-1]
      tmp_a.unshift(bef)
      formals = tmp_b + tmp_a
    else
      formals.unshift(bef)
    end
    next
  end

  bef_s << bef
  aft_s << aft
end

formals.each do |i|
  if ans.index(-1) and !ans.index(i)
    index = ans.index(-1)
    puts "#{ans}"
    ans[index] = i
  end
end

puts ans.uniq.select {|i| i != -1}.join(' ')

#! /usr/bin/env ruby
# coding: utf-8

def increment(i, a)
  i += 1
  i = 0 if a.length <= i
  i
end

n = gets.to_i

ans = Array.new(n, -1)
bef_s = []
aft_s = []
n.times do |i|
  bef, aft = gets.split.map(&:to_i)
  if (bef != 0 and aft != 0) and (!ans.index(bef) and !ans.index(aft))
    bef_s << bef
    aft_s << aft
    next
  else
    ans[1] = aft and next if bef == 0
    ans[-2] = bef and next if aft == 0

    if ans.index(bef)
      index = ans.index(bef)
      ans[index + 2] = aft
      next
    end

    if ans.index(aft)
      index = ans.index(aft)
      ans[index - 2] = bef
      next
    end
  end
end

formals = []
while 0 < bef_s.length
  bef = bef_s.shift
  aft = aft_s.shift

  if ans.index(bef)
    i = ans.index(bef)
    tmp_b = ans[0..(i + 1)]
    tmp_a = ans[(i + 2)..-1]
    tmp_a.unshift(aft)
    ans = tmp_b + tmp_a
    next
  end

  if ans.index(aft)
    i = ans.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = ans[0..(i - 2)]
      tmp_a = ans[(i - 1)..-1]
      tmp_a.unshift(bef)
      ans = tmp_b + tmp_a
    else
      ans.unshift(bef)
    end
    next
  end

  if formals.length == 0
    formals << bef
    formals << aft
    next
  end

  if formals.index(bef)
    i = formals.index(bef)
    if i + 2 < formals.length
      tmp_b = formals[0..(i + 1)]
      tmp_a = formals[(i + 2)..-1]
      tmp_a.unshift(aft)
      formals = tmp_b + tmp_a
    else
      formals << aft
    end
    next
  end

  if formals.index(aft)
    i = formals.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = formals[0..(i - 2)]
      tmp_a = formals[(i - 1)..-1]
      tmp_a.unshift(bef)
      formals = tmp_b + tmp_a
    else
      formals.unshift(bef)
    end
    next
  end

  bef_s << bef
  aft_s << aft
end

formals.each do |i|
  if ans.index(-1) and !ans.index(i)
    index = ans.index(-1)
    ans[index] = i
  end
end

puts ans.select {|i| i != -1}.join(' ')

#! /usr/bin/env ruby
# coding: utf-8

def increment(i, a)
  i += 1
  i = 0 if a.length <= i
  i
end

n = gets.to_i

ans = Array.new(n, -1)
bef_s = []
aft_s = []
n.times do |i|
  bef, aft = gets.split.map(&:to_i)
  if (bef != 0 and aft != 0) and (!ans.index(bef) and !ans.index(aft))
    bef_s << bef
    aft_s << aft
    next
  else
    ans[1] = aft and next if bef == 0
    ans[-2] = bef and next if aft == 0

    if ans.index(bef)
      index = ans.index(bef)
      ans[index + 2] = aft
      next
    end

    if ans.index(aft)
      index = ans.index(aft)
      ans[index - 2] = bef
      next
    end
  end
end

formals = []
while 0 < bef_s.length
  bef = bef_s.shift
  aft = aft_s.shift

  if ans.index(bef)
    i = ans.index(bef)
    tmp_b = ans[0..(i + 1)]
    tmp_a = ans[(i + 2)..-1]
    tmp_a.unshift(aft)
    ans = tmp_b + tmp_a
    next
  end

  if ans.index(aft)
    i = ans.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = ans[0..(i - 2)]
      tmp_a = ans[(i - 1)..-1]
      tmp_a.unshift(bef)
      ans = tmp_b + tmp_a
    else
      ans.unshift(bef)
    end
    next
  end

  if formals.length == 0
    formals << bef
    formals << aft
    next
  end

  if formals.index(bef)
    i = formals.index(bef)
    if i + 2 < formals.length
      tmp_b = formals[0..(i + 1)]
      tmp_a = formals[(i + 2)..-1]
      tmp_a.unshift(aft)
      formals = tmp_b + tmp_a
    else
      formals << aft
    end
    next
  end

  if formals.index(aft)
    i = formals.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = formals[0..(i - 2)]
      tmp_a = formals[(i - 1)..-1]
      tmp_a.unshift(bef)
      formals = tmp_b + tmp_a
    else
      formals.unshift(bef)
    end
    next
  end

  bef_s << bef
  aft_s << aft
end

puts "ans: #{ans}, formals: #{formals}"
formals.each do |i|
  if ans.index(-1) and !ans.index(i)
    index = ans.index(-1)
    ans[index] = i
  end
end

puts ans.select {|i| i != -1}.join(' ')

def find_pos(arr, pos, reverse = false)
  p = reverse ? 1 : 0

  arr.each_with_index do |x, i|
    if pos == x[p]
      return i
    end
  end

  -1
end

n = gets.to_i

arr = []
for s in STDIN.each_line.take n
  i, j = s.split(" ").map &:to_i

  arr << [i, j]
end

pos, start = 1, 0
result = Array.new(n, 0)
while pos < n
  p = find_pos(arr, start)

  if p != -1
    result[pos] = arr[p][1]
    start = arr[p][1]
    pos += 2
  else
    break
  end
end

pos = arr.length - 2
start = 0
while pos < n
  p = find_pos(arr, start, true)

  if p != -1
    result[pos] = arr[p][0]
    start = arr[p][0]
    pos -= 2
  else
    break
  end

end

puts result.inspect

#! /usr/bin/env ruby
# coding: utf-8

def increment(i, a)
  i += 1
  i = 0 if a.length <= i
  i
end

n = gets.to_i

ans = Array.new(n, -1)
bef_s = []
aft_s = []
n.times do |i|
  bef, aft = gets.split.map(&:to_i)
  if (bef != 0 and aft != 0) and (!ans.index(bef) and !ans.index(aft))
    bef_s << bef
    aft_s << aft
    next
  else
    ans[1] = aft and next if bef == 0
    ans[-2] = bef and next if aft == 0

    if ans.index(bef)
      index = ans.index(bef)
      ans[index + 2] = aft
      next
    end

    if ans.index(aft)
      index = ans.index(aft)
      ans[index - 2] = bef
      next
    end
  end
end

formals = []
while 0 < bef_s.length
  bef = bef_s.shift
  aft = aft_s.shift

  if formals.length == 0
    formals << bef
    formals << aft
    next
  end

  if formals.index(bef)
    i = formals.index(bef)
    tmp_b = formals[0..(i + 1)]
    tmp_a = formals[(i + 2)..-1]
    tmp_a.unshift(aft)
    formals = tmp_b + tmp_a
    next
  end

  if formals.index(aft)
    i = formals.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = formals[0..(i - 2)]
      tmp_a = formals[(i - 1)..-1]
      tmp_a.unshift(bef)
      formals = tmp_b + tmp_a
    else
      formals.unshift(bef)
    end
    next
  end
  bef_s << bef
  aft_s << aft
end

formals.each do |i|
  if ans.index(-1)
    index = ans.index(-1)
    ans[index] = i
  end
end

puts ans.join(' ')

#! /usr/bin/env ruby
# coding: utf-8

def increment(i, a)
  i += 1
  i = 0 if a.length <= i
  i
end

n = gets.to_i

ans = Array.new(n, -1)
bef_s = []
aft_s = []
n.times do |i|
  bef, aft = gets.split.map(&:to_i)
  if (bef != 0 and aft != 0) and (!ans.index(bef) and !ans.index(aft))
    bef_s << bef
    aft_s << aft
    next
  else
    ans[1] = aft and next if bef == 0
    ans[-2] = bef and next if aft == 0

    if ans.index(bef)
      index = ans.index(bef)
      ans[index + 2] = aft
      next
    end

    if ans.index(aft)
      index = ans.index(aft)
      ans[index - 2] = bef
      next
    end
  end
end

formals = []
while 0 < bef_s.length
  bef = bef_s.shift
  aft = aft_s.shift

  if ans.index(bef)
    i = ans.index(bef)
    tmp_b = ans[0..(i + 1)]
    tmp_a = ans[(i + 2)..-1]
    tmp_a.unshift(aft)
    ans = tmp_b + tmp_a
    next
  end

  if ans.index(aft)
    i = ans.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = ans[0..(i - 2)]
      tmp_a = ans[(i - 1)..-1]
      tmp_a.unshift(bef)
      ans = tmp_b + tmp_a
    else
      ans.unshift(bef)
    end
    next
  end

  if formals.length == 0
    formals << bef
    formals << aft
    next
  end

  if formals.index(bef)
    i = formals.index(bef)
    if i + 2 < formals.length
      tmp_b = formals[0..(i + 1)]
      tmp_a = formals[(i + 2)..-1]
      tmp_a.unshift(aft)
      formals = tmp_b + tmp_a
    else
      formals << aft
    end
    next
  end

  if formals.index(aft)
    i = formals.index(aft)
    tmp_b = nil
    tmp_a = nil
    if 0 <= i - 2
      tmp_b = formals[0..(i - 2)]
      tmp_a = formals[(i - 1)..-1]
      tmp_a.unshift(bef)
      formals = tmp_b + tmp_a
    else
      formals.unshift(bef)
    end
    next
  end

  bef_s << bef
  aft_s << aft
end

formals.each do |i|
  if ans.index(-1) and !ans.index(i)
    index = ans.index(-1)
    ans[index] = i
  end
end

puts ans.uniq.select {|i| i != -1}.join(' ')


n = gets.to_i
s = gets
a = []
for i in 0...n
    l = r = 1
    j = i - 1
    while j < n - 2 && s[j += 1] != 'R'
        l += s[j].ord / 70
    end
    j = i
    while j > 0 && s[j -= 1] != 'L'
        r += s[j].ord / 70
    end
    a << [l, r].max
end
puts a.join(' ')

class Fairness
    def initialize(num_students, directions)
        @num_students = num_students
        @directions = directions
        @toffees = (1..num_students).map { 1 }
    end

    def solve
        @directions.each_with_index { |direction, index| reward direction, index }
        reversed.each { |index| reward @directions[index], index }
    end

    def display
        @toffees.each {|n| print "#{n} "}
        print "\n"
    end

    private

    def reward(direction, index)
        reward_left(index)  if direction == 'L'
        reward_right(index) if direction == 'R'
        equalize(index)     if direction == '='
    end

    def reward_left(index)
        while @toffees[index] <= @toffees[index+1]
            @toffees[index] += 1
        end
    end

    def reward_right(index)
        while @toffees[index+1] <= @toffees[index]
            @toffees[index+1] += 1
        end
    end

    def equalize(index)
        max = [@toffees[index], @toffees[index+1]].max
        @toffees[index] = max
        @toffees[index+1] = max
    end

    def reversed
        (0...@directions.length).to_a.reverse
    end
end

fair = Fairness.new(gets.chomp.to_i, gets.chomp.split(""))
fair.solve()
fair.display()

n = gets.to_i
a = []
s = gets.chomp
(0...n).each { |i|
    l = r = 1
    j = i - 1
    while j < n - 2 && s[j += 1] != 'R'
        l += 1 if s[j] == 'L'
    end
    j = i
    while j > 0 && s[j -= 1] != 'L'
        r += 1 if s[j] == 'R'
    end
    a << [l, r].max
}
puts a.join(' ')

n = gets.to_i
a = []
s = gets.chomp
(0...n).each { |i|
    l = r = 1
    (i..n-2).each { |j|
        break if s[j] == 'R'
        l += 1 if s[j] == 'L'
    }
    (i-1).downto(0) { |j|
        break if s[j] == 'L'
        r += 1 if s[j] == 'R'
    }
    a << [l, r].max
}
puts a.join(' ')

class Fairness
    def initialize(num_students, directions)
        @num_students = num_students
        @directions = directions
        @toffees = (1..num_students).map { 1 }
    end

    def solve
        @directions.each_with_index do |direction, index|
            reward_left(index)  if direction == 'L'
            reward_right(index) if direction == 'R'
            equalize(index)     if direction == '='
        end

        @toffees.each do |toffee|
            print "#{toffee} "
        end
        print "\n"
    end

    def reward_left(index)
        while @toffees[index] <= @toffees[index+1]
            @toffees[index] += 1
        end
    end

    def reward_right(index)
        while @toffees[index+1] <= @toffees[index]
            @toffees[index+1] += 1
        end
    end

    def equalize(index)
        max = [@toffees[index], @toffees[index+1]].max
        @toffees[index] = max
        @toffees[index+1] = max
    end
end

fair = Fairness.new(gets.chomp.to_i, gets.chomp.split(""))
fair.solve()

gets
t = 1
a = [1]
gets.chomp.each_byte { |c|
    t += (c - 79) / 3 if c != 61
    a << t
}
t = a.min - 1
puts a.map{|x| x - t}.join(' ')

n = gets.to_i
a = []
s = gets.chomp
(0...n).each { |i|
    l = r = 1
    j = i - 1
    while j < n - 3 && s[j += 1] != 'R'
        l += 1 if s[j] == 'L'
    end
    j = i
    while j > 1 && s[j -= 1] != 'L'
        r += 1 if s[j] == 'R'
    end
    a << [l, r].max
}
puts a.join(' ')


h, w = gets.chomp.split.map(&:to_i)
board = []
h.times do
  board << gets.chomp
end

rs = Array.new(h).map {Array.new(w, 0)}
for r in 0...h
  cnt = 0
  for c in 1...w
    cnt += 1 if board[r][c] == '.' && board[r][c - 1] == '.'
    rs[r][c] = cnt
    rs[r][c] += rs[r - 1][c] if 0 < r
  end
end

cs = Array.new(h).map {Array.new(w, 0)}
for c in 0...w
  cnt = 0
  for r in 1...h
    cnt += 1 if board[r][c] == '.' && board[r - 1][c] == '.'
    cs[r][c] = cnt
    cs[r][c] += cs[r][c - 1] if 0 < c
  end
end

q = gets.chomp.to_i
q.times do
  cnt = 0
  r1, c1, r2, c2 = gets.chomp.split.map { |x| x.to_i - 1 }
  cnt += rs[r2][c2]
  cnt += rs[r1 - 1][c1] if 0 < r1
  cnt -= rs[r1 - 1][c2] if 0 < r1
  cnt -= rs[r2][c1]

  cnt += cs[r2][c2]
  cnt += cs[r1][c1 - 1] if 0 < c1
  cnt -= cs[r1][c2]
  cnt -= cs[r2][c1 - 1] if 0 < c1
  puts cnt
end


n = gets.to_i
a = gets.split.map(&:to_i)
m = a.min
f = (0...n).find_all{|x| a[x]==m}
if f.size == 1
  puts f[0] + 1
else
  puts "Still Rozdil"
end

gets
a=gets.split.map &:to_i
puts(a.count(m=a.min)>1?"Still Rozdil":1+a.index(m))

n = gets.to_i
a = gets.split.map(&:to_i)
if a.count(a.min)>1
  puts "Still Rozdil"
else
  puts a.index(a.min)+1
end

gets
a = gets.split.map { |i| i.to_i }
puts a.count(a.min) == 1 ? a.index(a.min) + 1 : "Still Rozdil"

gets
a=gets.split.map &:to_i
m=a.min
puts a.count(m)>1 ? "Still Rozdil" : a.index(m)+1

n = gets.to_i

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

min = 10**9 + 1
i = -1
t = false
n.times do |k|
    if city[k] < min then
        min = city[k]
        t = false
        i = k
    elsif city[k] == min then
        t = true
    end
end

if t then
    puts "Still Rozdil"
else
    puts i+1
end

gets
a = gets.split.map &:to_i
m = a.min
i = a.index m
j = a.rindex m
puts (i != j ? "Still Rozdil" : i+1)

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

min = times.min
if times.count(min) == 1
  puts times.index(min) + 1
else
  puts "Still Rozdil"
end

num_cities = gets
distances = gets.split.collect {|i| i.to_i}
min_distance = distances.min
num_occured = distances.count(min_distance)
if num_occured == 1
	puts distances.index(min_distance) + 1
else
	puts "Still Rozdil"
end

#!/usr/bin/ruby

num_entries = gets.to_i

line = gets

numbers = line.split(' ')
numbers.map! { |e| e.to_i}
min = numbers.min
counter = 0
numbers.each { |n|
  if(n == min) then
    counter += 1
  end
}

if (counter > 1) then
  puts "Still Rozdil"
else
  puts numbers.index(min) + 1
end

n = gets.to_i

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

min = 10**5
i = -1
t = false
n.times do |k|
    if city[k] < min then
        min = city[k]
        t = false
        i = k
    elsif city[k] == min then
        t = true
    end
end

if t then
    puts "Still Rozdil"
else
    puts i+1
end

num_cities = gets
distances = gets.split.collect {|i| i.to_i}
min_distance = distances.min
num_occured = distances.count(min_distance)
if num_occured == 1
	puts min_distance.to_s
else
	puts "Still Rozdil"
end


t = gets.strip.to_i
t.times do
	n = gets.strip.to_i
	r = gets.strip.split.map(&:to_i)
	up_votes = ans = 0
	n.times do |i|
		current = r[i]
		ans += 1 if current == 1 || current == 3
	end
	puts ans
end

t = gets.to_s.to_i

1.upto(t) do |casenum|
  n = gets.to_s.split.map {|x| x.to_i }
  ar = gets.to_s.split.map {|x| x.to_i }

  vn = 0
  vp = 0

  ar.each do |r|
    if r == 1
      vp += 1
    elsif r == 2
      vn -= 1
    else
      vp += 1
    end
  end

  printf("%d\n", vp)
end


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end
#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q = gi
ans = []
MAX = 10**5
MIN = -10**5
q.times do |i|
  n = gi
  xf = n.times.map{gsmi}
  # x,y,xl,yu,xr,yd
  x_ran = [MIN,MAX]
  y_ran = [MIN,MAX]
  xf.each do |x,y,xl,yu,xr,yd|
    if xl==0&&xr==0
      unless x_ran[0]<=x && x<=x_ran[1]
        ans<<'0'
        break
      else
        x_ran = [x,x]
      end
    elsif xl==1&&xr==0
      if x<x_ran[0]
        ans<<'0'
        break
      else
        x_ran[1] = min(x_ran[1],x)
      end
    elsif xl==0&&xr==1
      if x_ran[1]<x
        ans<<'0'
        break
      else
        x_ran[0] = max(x_ran[0],x)
      end
    end

    # y
    if yd==0&&yu==0
      unless y_ran[0]<=y && y<=y_ran[1]
        ans<<'0'
        break
      else
        y_ran = [y,y]
      end
    elsif yd==1&&yu==0
      if y<y_ran[0]
        ans<<'0'
        break
      else
        y_ran[1] = min(y_ran[1],y)
      end
    elsif yd==0&&yu==1
      if y_ran[1]<y
        ans<<'0'
        break
      else
        y_ran[0] = max(y_ran[0],y)
      end
    end
  end

  ans << [1,x_ran[0],y_ran[0]].join(" ") if ans.size != i+1
end
puts ans

INF = 10**5
gets.to_i.times do
  l, r, d, u = -INF, INF, -INF, INF
  gets.to_i.times do
    x, y, f1, f2, f3, f4 = gets.split.map(&:to_i)
    l = [x, l].max if f1 == 0
    u = [y, u].min if f2 == 0
    r = [x, r].min if f3 == 0
    d = [y, d].max if f4 == 0
  end
  if r < l or u < d
    puts 0
  else
    puts [1, l, d].join ' '
  end
end


input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        if i != empty
            ops.push [i, empty]
            memory[empty] = memory[i]
            pos[memory[i]] = empty
        end

        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        if i != empty
            ops.push [i, empty]
            memory[empty] = memory[i]
            pos[memory[i]] = empty
        end

        ops.push [pos[i], i]
        memory[i] = i
        empty = pos[i]
        pos[i] = i
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

#!/usr/bin/ruby

# move sects[cur] to correct position using temporary
# return tmpidx
def move(sects, target, cur, tmp, acc)
  return tmp if sects[cur] == target[cur]
  return tmp if cur == tmp
  return tmp if sects[cur].nil?

  to = target.index(sects[cur])
  acc << [to, tmp] if to != tmp
  sects[tmp] = sects[to]

  acc << [cur, to]
  sects[to] = sects[cur]
  move(sects, target, tmp, cur, acc)
end

n, m = gets.split.map(&:to_i)
sects = Array.new(n, nil)
len = 0
target = []
m.times do |i|
  k, *as = gets.split.map(&:to_i)
  as.map{|v| v-1}.each_with_index do |a, j|
    sects[a] = [i,j]
    target << [i,j]
  end
  len = [len, as.max].max
end
target.freeze

tmpidx = sects.index(nil)

ans = []
cur = 0
while cur < len
  if cur != tmpidx and sects[cur] and sects[cur] != target[cur]
    tmpidx = move(sects, target, cur, tmpidx, ans)
  end
  cur += 1
end

puts ans.size
ans.each do |v|
  puts v.map{|a| a+1}.join(' ')
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 0...input.length
        cluster[i][j] = Integer(input[j])
    end
end

before_defrag = []
for i in 0...n
    before_defrag[i] = 0
end
after_defrag = []
k = 0
while k < m do
    for i in 0...m
        for j in 0...cluster[i][0]
            after_defrag.push((k+1) * 1000 + j + 1)
        end
        k += 1
    end
end
length = after_defrag.length
(n - length).times {after_defrag.push(0)}
for i in 0...m
    for j in 1..cluster[i][0]
        before_defrag[cluster[i][j]-1] = (i+1) * 1000 + j
    end
end
empty = 0
for i in 0...n
    if before_defrag[i] == 0
        empty = i
        break
    end
end
count = 0
operation = []
for i in 0...length
    if before_defrag[i] != after_defrag[i]
        if empty != i
            operation.push([i+1, empty+1])
            count += 1
            before_defrag[empty] = before_defrag[i]
        end
        for j in i+1...n
            if before_defrag[j] == after_defrag[i]
                empty = j
                operation.push([empty+1, i+1])
                count += 1
                before_defrag[i] = before_defrag[empty]
            end
        end
    end
end
puts count
if operation != []
    for i in 0...operation.length
        print operation[i][0].to_s + " " + operation[i][1].to_s
        puts
    end
end

#!/usr/bin/ruby

# move sects[cur] to correct position using temporary
# return tmpidx
def move(sects, target, cur, tmp, acc)
  return tmp if sects[cur] == target[cur]

  to = target.index(sects[cur])
  acc << [to, tmp] if to != tmp
  sects[tmp] = sects[to]

  acc << [cur, to]
  sects[to] = sects[cur]
  move(sects, target, tmp, cur, acc)
end

n, m = gets.split.map(&:to_i)
sects = Array.new(n, nil)
len = 0
target = []
m.times do |i|
  k, *as = gets.split.map(&:to_i).map{|v| v-1}
  as.each_with_index do |a, j|
    sects[a] = [i,j]
    target << [i,j]
  end
  len += k
end
target.freeze

tmpidx = sects.index(nil)

ans = []
cur = 0
while cur < len
  if sects[cur] != target[cur]
    tmpidx = move(sects, target, cur, tmpidx, ans)
  end
  cur += 1
end

puts ans.size
ans.each do |v|
  puts v.map{|a| a+1}.join(' ')
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        if i != empty
            ops.push [i, empty]
            pos[memory[i]] = empty
        end

        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        ops.push [i, empty]
        memory[empty] = memory[i]
        pos[memory[i]] = empty

        ops.push [pos[i], i]
        memory[i] = i
        empty = pos[i]
        pos[i] = i
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 0...input.length
        cluster[i][j] = Integer(input[j])
    end
end

before_defrag = []
for i in 0...n
    before_defrag[i] = 0
end
after_defrag = []
k = 0
while k < m do
    for i in 0...m
        for j in 0...cluster[i][0]
            after_defrag.push((k+1) * 10 + j + 1)
        end
        k += 1
    end
end
length = after_defrag.length
(n - length).times {after_defrag.push(0)}
for i in 0...m
    for j in 1..cluster[i][0]
        before_defrag[cluster[i][j]-1] = (i+1) * 10 + j
    end
end
empty = 0
for i in 0...n
    if before_defrag[i] == 0
        empty = i
        break
    end
end
count = 0
operation = []
for i in 0...length
    if before_defrag[i] != after_defrag[i]
        if empty != i
            operation.push([i+1, empty+1])
            count += 1
            before_defrag[empty] = before_defrag[i]
        end
        for j in i+1...n
            if before_defrag[j] == after_defrag[i]
                empty = j
                operation.push([empty+1, i+1])
                count += 1
                before_defrag[i] = before_defrag[empty]
            end
        end
    end
end
puts count
if operation != []
    for i in 0...operation.length
        print operation[i][0].to_s + " " + operation[i][1].to_s
        puts
    end
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if i != empty && memory[i] != -1
        ops.push [i, empty]
        pos[memory[i]] = empty
    end
    if pos[i] != i
        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if i != empty
        ops.push [i, empty]
        pos[memory[i]] = empty
    end
    if pos[i] != i
        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end


n, k = gets.split.collect{|i| i.to_i}
if n/2 > k
    puts -1
elsif n == 1
    puts -1 if k > 0
    puts 1 if k == 0
else
    ans = Array.new(n)
    ans[0] = k-n/2+1
    ans[1] = 2*ans[0]
    for i in 2...n
        ans[i] = ans[i-1]+1
    end
    puts ans.join(' ')
end

n, m = gets.split.map &:to_i
if n == 1
	puts m == 0 ? '1' : '-1'
	exit
end
one = n / 2 - 1
noone = m - one
if noone <= 0
	puts '-1'
	exit
end
ans = []
use = {}
if noone > 0
	ans << noone
	ans << noone * 2
	use[noone] = true
	use[noone * 2] = true
end
if one > 0
	1.step(1000000000,2) do |i|
		if !use[i] && !use[i + 1]
			ans << i
			ans << i + 1
			one -= 1
			break if one == 0
		end
	end
end
ans << 1000000000 if ans.size < n
flag = false
ans.each do |x|
	flag = true if x > 1000000000
end
puts flag ? '-1' : ans.join(' ')

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

pairs = n / 2

remaining = k - (pairs - 1)
if n == 1
	ans = k == 0 ? [1] : [-1]
elsif remaining > 0
	ans = []
	x = remaining * 2 + 2
	((pairs-1)*2).times do
		ans << x
		x += 1
	end
	ans << remaining << remaining * 2
	one_off = ans[-1] + 1
	ans << one_off if n % 2 != 0
else
	ans = [-1]
end

puts ans.join(" ")

n, k = gets.split.collect{|i| i.to_i}
if n/2 > k
    puts -1
elsif n == 1
    if k > 0
        puts -1
    else
        puts 1
    end
else
    ans = Array.new(n)
    ans[0] = k-n/2+1
    ans[1] = 2*ans[0]
    for i in 2...n
        ans[i] = ans[i-1]+1
    end
    puts ans.join(' ')
end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < (n/2).floor)
	puts '-1'
	exit
end
arr[0] = k - ((n-2)/2).floor
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

if n == 1 && k != 0
	puts '-1'
	exit
elsif n == 1 && k == 0
	puts '1'
	exit
end

arr.each do|x|
	print x
	print ' '
	end

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if k == 0 and n == 1
  p 1
  exit
elsif n == 1
  if k == 0
    p 1
  else
    p (-1)
  end
  exit
elsif n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]*2
end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n
	arr[i] = arr[i-1]*2
end

arr.each do|x|
	print x
	print ' '
	end

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

pairs = n / 2

remaining = k - (pairs - 1)
if n == 1
	ans = k == 0 ? [1] : [-1]
elsif remaining > 0
	ans = []
	x = remaining * 2 + 1
	((pairs-1)*2).times do
		ans << x
		x += 1
	end
	one_off = ans[-1] + 1 if ans[-1]
	ans << remaining << remaining * 2
	ans << one_off if n % 2 != 0
else
	ans = [-1]
end

puts ans.join(" ")

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

pairs = n / 2

remaining = k - (pairs - 1)

if n > 1 && remaining > 0
	ans = []
	x = remaining * 2 + 1
	((pairs-1)*2).times do
		ans << x
		x += 1
	end
	one_off = ans[-1] + 1
	ans << remaining << remaining * 2
	ans << one_off if n % 2 != 0
else
	ans = [-1]
end

puts ans.join(" ")

n, k = gets.split.collect{|i| i.to_i}
if n/2 > k
    puts -1
else
    ans = Array.new(n)
    ans[0] = k-n/2+1
    ans[1] = 2*ans[0]
    for i in 2...n
        ans[i] = ans[i-1]+1
    end
    puts ans.join(' ')
end

n, k = gets.split.map(&:to_i)
if k < n / 2
	puts -1
	exit 0
end
m = n / 2 - 1
s = "1 1 " * m + (k - m).to_s + " " + (k - m).to_s
s = s + " 1" if n % 2 == 1
puts s

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []
c = 1

if n / 2 > k
  p (-1)
  exit
end

while n > 1
  m = k - (n - 2) / 2

  while true

    if m == 1
      a = c
      b = c + 1
      c += 2
      while used.member?(b) || used.member?(a)
        a = c
        b = c + 1
        c += 2
      end
    else
      a = m
      b = 2 * m
    end

    if used.member?(b) || used.member?(a)
      m -= 1
      next
    else
      break
    end
  end

  used << a
  used << b
  ans << a
  ans << b
  k -= a.gcd(b)
  # p [[a, b], a.gcd(b)]

  n -= 2
end

while n > 0
  while used.member?(c)
    c += 1
  end
  ans << c
  n = 0
end

puts ans.join(' ')

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if k == 0 and n == 1
  p 1
  exit
elsif n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

if n == 1
	puts '-1'
	exit
	end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
arr[2] = 1
for i in 3..n-1
	arr[i] = arr[i-1]+1
end

if n == 1
	puts '-1'
	exit
	end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < (n/2).floor)
	puts '-1'
	exit
end
arr[0] = k - ((n-2)/2).floor
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

if n == 1
	puts '-1'
	exit
	end

arr.each do|x|
	print x
	print ' '
	end

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if n / 2 > k

  if n == 1
    p 1
  else
    p (-1)
  end
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  if used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  if used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end
def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
h,w=gsmi
r=gsmi
c=gsmi

# check
h.times do |i|
  tar = r[i]
  next if tar==w
  if c[tar]>i
    putsend 0
  end
end
w.times do |i|
  tar = c[i]
  next if tar==h
  if r[tar]>i
    putsend 0
  end
end

# cnt
h.times do |i|
  w.times do |j|
    cnt+=1 if r[i]<j && c[j]<i
  end
end

puts 2**cnt % MOD

h,w = gets.split.map(&:to_i)
r = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

cell = []
h.times { cell<<Array.new(w) }

h.times {|i|
  r[i].times{|j|
    cell[i][j] = true
  }
  if r[i] < w
    cell[i][r[i]] = false
  end
}
w.times {|j|
  c[j].times{|i|
    if cell[i][j] == false
      p 0
      exit
    end
    cell[i][j] = true
  }
  if c[j] < h
    if cell[c[j]][j] == true
      p 0
      exit
    end
    cell[c[j]][j] = false
  end
}

freedom = cell.map {|cell_i|
  cell_i.count(nil)
}.inject(:+)

p (2**freedom) % 1000000007

n, m = readline.split.map(&:to_i)
l1 = readline.split.map(&:to_i)
l2 = readline.split.map(&:to_i)
a = Array.new(n, Array.new(m, 0))
for i in 0..n-1 do a[i] = Array.new(m, 0) end
for i in 0..n-1
	for j in 0..l1[i]-1
		a[i][j] = 1
	end
end

for j in 0..m-1
	if l2[j]<n && a[l2[j]][j] == 1
		puts 0
		exit(0)
	end
	for i in 0..l2[j]-1
		a[i][j] = 1
	end
end
for i in 0..n-1
	if l1[i]<m && a[i][l1[i]] == 1
		puts 0
		exit(0)
	end
end
for i in 0..n-1
	if l1[i] < m then l1[i] += 1 end
	for j in 0..l1[i]-1
		a[i][j] = 1
	end
end
for j in 0..m-1
	if l2[j] < n then l2[j] += 1 end
	for i in 0..l2[j]-1
		a[i][j] = 1
	end
end
ans = 1
mod = 1000000007
for i in 0..n-1
	for j in 0..m-1
		if a[i][j] == 0
			ans = ans * 2 % mod
		end
	end
end
puts ans

h,w = gets.split.map(&:to_i)
r = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

cell = []
h.times { cell<<Array.new(w) }

h.times {|i|
  r[i].times{|j|
    cell[i][j] = true
  }
  cell[i][r[i]] = false if r[i] < w
}
w.times {|j|
  c[j].times{|i|
    if cell[i][j] == false
      p 0
      exit
    end
    cell[i][j] = true
  }
  cell[c[j]][j] = false if c[j] < h
}

freedom = cell.map {|cell_i|
  cell_i.count(nil)
}.inject(:+)

p (2**freedom) % 1000000007


f = File.open('input.txt', 'r') or die "Unable to open file..."
input = []  # start with an empty array
  f.each_line {|line|
    input.push line
  }

selected = input[0].scan(/\w+/)[1].to_i - 1
items = input[1].scan(/\w+/)

while items[selected].to_i != 1 do
 (selected < items.length)? selected += 1 : selected = 0
end

if (items[selected].to_i == 1)
   File.open('output.txt', 'w') { |o| o.puts "#{selected+1}"}
end

n, k, a = nil
File.open("input.txt") do |f|
  n, k = f.gets.split.map(&:to_i)
  a = f.gets.split.map(&:to_i)
end

a += a
r = (k..k+n).find{|x| a[x-1] == 1}
r -= n if r > n

File.open("output.txt", "w") do |f|
  f.puts r
end

f = File.open('input.txt', 'r') or die "Unable to open file..."
input = []  # start with an empty array
  f.each_line {|line|
    input.push line
  }

selected = input[0].scan(/\w+/)[1].to_i
items = input[1].scan(/\w+/)

until items[selected].to_i == 1 do
 (selected < items.length)? selected += 1 : selected = 0
 if (items[selected].to_i == 1)
   File.open('output.txt', 'w') { |o| o.puts "#{selected+1}"}
 end
end


n=gets.to_i
a,b,c,d={},0,-1,[0]*n
1.upto(n){a[c+=1]=gets.split.map(&:to_i)}
n.times{|i|
	x,t=a.to_a.sort_by{|x,(y,z)|(10**7+1)*[y,b+1].max+z}.shift
	y,z=t
	b=b<y ? y : b+=1
	d[x]=b
	a.delete x
}
puts d*" "

class Event
	def initialize(st, en, id)
		@st = st
		@en = en
		@id = id
		@used = 0
	end
	attr_accessor :st, :en, :id, :used
end

n = gets.chop.to_i

aused = Array.new(1000000)
ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e,i))
end

ar.sort!{|a,b|
	if a.en == b.en
		a.st <=> b.st
	else
		a.en <=> b.en
	end
}

for i in 0..n-1
	st = ar[i].st
	while aused[st-1] == true
		st += 1
	end
	aused[st-1] = true
	ar[i].used = st
end

ret = ""
ar.sort!{|a,b|
	a.id <=> b.id
}
for i in 0..n-1
	ret << " " if i > 0
	ret << ar[i].used.to_s
end
puts ret

class Segment
  attr_accessor :from, :to

  def initialize(from, to)
    @from, @to = from, to
  end

  def <=> (rhs)
    return -1 if from < rhs.from
    return to <=> rhs.to if from == rhs.from
    return +1
  end
end

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

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }
result = Array.new(n)
order = (0 ... n).to_a

while not order.empty?
  order = order.sort_by { |i| segs[i] }
  cur = order.shift
  result[cur] = segs[cur].from
  order.each do |index|
    segs[index].from = max(segs[index].from, segs[cur].from + 1)
  end
end

puts result.join(' ')

class Event
	def initialize(st, en)
		@st = st
		@en = en
		@used = false
	end
	attr_accessor :st, :en, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

ret = ""
cnt = 0
startidx = 0
seq = 0
while cnt < n
	wkidx = -1
	wken = 0
	for i in startidx..n-1
		next if ar[i].used
		if seq < ar[i].st
			wkidx = i
			wken = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
		end
		break if ar[i].st > seq
	end
	ret << " " if cnt > 0
	cnt += 1
	ar[wkidx].used = true
	seq = wken
	ret << seq.to_s
	for i in startidx+1..n-1
		break if ar[i].used == false
		startidx = i
	end
end

puts ret

class Event
	def initialize(st, en)
		@st = st
		@en = en
	end
	attr_accessor :st, :en
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ret = ""
seq = 0

for i in 0..n-1
	if seq < ar[i].st
		wkidx = i
		seq = ar[i].st
	else
		seq = seq+1
	end
	ret << " " if i > 0
	ret << seq.to_s
end

puts ret

class Event
	def initialize(st, en)
		@st = st
		@en = en
		@used = false
	end
	attr_accessor :st, :en, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

ret = ""
cnt = 0
startidx = 0
seq = 0
nseq = 0
while cnt < n
	wkidx = -1
	wken = 0
	for i in startidx..n-1
		next if ar[i].used
		if seq < ar[i].st
			wkidx = i
			nseq = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
			nseq = seq+1
		end
		break if ar[i].st > seq
	end
	ret << " " if cnt > 0
	cnt += 1
	ar[wkidx].used = true
	seq = nseq
	ret << seq.to_s
	for i in startidx+1..n-1
		break if ar[i].used == false
		startidx = i
	end
end

puts ret

class Event
	def initialize(st, en, id)
		@st = st
		@en = en
		@id = id
		@used = 0
	end
	attr_accessor :st, :en, :id, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e,i))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

cnt = 0
startidx = 0
seq = 0
nseq = 0
while cnt < n
	wkidx = -1
	wken = 0
	for i in startidx..n-1
		next if ar[i].used > 0
		if seq < ar[i].st
			wkidx = i
			nseq = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
			nseq = seq+1
		end
		break if ar[i].st > seq
	end

	cnt += 1
	ar[wkidx].used = nseq
	seq = nseq

	for i in startidx+1..n-1
		break if ar[i].used == 0
		startidx = i
	end
end

ret = ""
ar.sort!{|a,b|
	a.id <=> b.id
}
for i in 0..n-1
	ret << " " if i > 0
	ret << ar[i].used.to_s
end
puts ret

class Event
	def initialize(st, en)
		@st = st
		@en = en
		@used = false
	end
	attr_accessor :st, :en, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

ret = ""
cnt = 0
startidx = 0
seq = 0
while cnt < n
	wkidx = -1
	wken = 0
	nseq = 0
	for i in startidx..n-1
		next if ar[i].used
		if seq < ar[i].st
			wkidx = i
			nseq = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
			nseq = seq+1
		end
		break if ar[i].st > seq
	end
	ret << " " if cnt > 0
	cnt += 1
	ar[wkidx].used = true
	seq = nseq
	ret << seq.to_s
	for i in startidx+1..n-1
		break if ar[i].used == false
		startidx = i
	end
end

puts ret

class Segment
  attr_reader :from, :to

  def initialize(from, to)
    @from, @to = from, to
  end

  def <=> (rhs)
    return -1 if @to < rhs.to
    return @from <=> rhs.from if @to == rhs.to
    return +1
  end
end

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

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }
result, last = [], -1

(0 ... n).to_a.sort_by { |i| segs[i] }.each do |i|
  last = max(last, segs[i].from) + 1
  result[i] = last - 1
end

puts result.join(' ')

class Segment
  attr_reader :from, :to

  def initialize(from, to)
    @from, @to = from, to
  end

  def <=> (rhs)
    return -1 if @from < rhs.from
    return @to <=> rhs.to if @from == rhs.from
    return +1
  end
end

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

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }
result, last = [], -1

(0 ... n).to_a.sort_by { |i| segs[i] }.each do |i|
  last = max(last, segs[i].from) + 1
  result[i] = last - 1
end

puts result.join(' ')

class Segment
  attr_reader :from, :to

  def initialize(from, to)
    @from, @to = from, to
  end

  def <=> (rhs)
    return -1 if @from < rhs.from
    return @to <=> rhs.to if @from == rhs.from
    return +1
  end
end

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

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }.sort

result, from = [], -1

segs.each do |seg|
  result.push(max(seg.from, from))
  from = max(seg.from, from) + 1
end

puts result.join(' ')


*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
u = v = -1
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

a = d
for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            p d - 2
            puts [c[i][j], c[j][i]] * ' '
            exit
        end
    end
    if x[i] && y[i]
        a = d - 1
        u, v = x[i], y[i]
    end
end

p a
puts puts [u, v] * ' '

*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            puts d - 2
            puts [c[i][j], c[j][i]].join(' ')
            exit
        end
    end
end

for i in 0...26
    if x[i] && y[i]
        puts d - 1
        puts [x[i], y[i]].join(' ')
        exit
    end
end

puts d
puts "-1 -1"

*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
u = v = -1
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

a = d
for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            p d - 2
            puts [c[i][j], c[j][i]] * ' '
            exit
        end
    end
    if x[i] && y[i]
        t = d - 1
        u, v = x[i], y[i]
    end
end

p a
puts puts [u, v] * ' '

*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
u = v = -1
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            p d - 2
            puts [c[i][j], c[j][i]] * ' '
            exit
        end
    end
    if x[i] && y[i]
        p d - 1
        puts [x[i], y[i]] * ' '
        exit
    end
end

p d
puts puts [u, v] * ' '


gets
$stdin.each do |t|
  n, m = t.split.map(&:to_i)
  puts m.bit_length.downto(0).reduce(0) { |s, i|
    (s ^ n | ~(-1 << i)) <= m ? s | 1 << i : s
  }
end

gets
$stdin.each do |t|
  n, m = t.split.map(&:to_i)
  l = n.bit_length
  s = m & -1 << l
  (l - 1).downto(0) do |i|
    break if (s ^ n) > m
    if m[i] == 1 && n[i] == 0
      s |= 1 << i
    end
  end
  if (s ^ n) == m
    s = ((s ^ n) + 1) & ~n
  end
  puts s
end

gets
$stdin.each do |t|
  n, m = t.split.map(&:to_i)
  s = m & (-1 << n.bit_length)
  (n.bit_length - 1).downto(0) do |i|
    break if (s ^ n) > m
    if m[i] == 1 && n[i] == 0
      s |= 1 << i
    end
  end
  s += 1 if (s ^ n) == m
  puts s
end


gets.to_i.times do
    a, b, x, y, n = gets.split.map &:to_i
    res = 1E18.to_i
    3.times do |i|
        d1 = [n, a - x].min
        d2 = [n - d1, b - y].min
        res = [res, (a - d1) * (b - d2)].min
        a, b, x, y = b, a, y, x
    end
    puts res
end

def solve
  a, b, x, y, n = gets.strip.split(' ').map(&:to_i)
  ck1 = a - x > n ? a - n : a - (a - x)
  ck2 = b - y > n ? b - n : b - (b - y)
  if ck1 < ck2
    n -= (a - ck1)
    if (b - y) > n
      b = b - n
    else
      b = b - (b - y)
    end
    puts ck1 * b
  else
    n = n - (b - ck2)
    if (a - x) > n
      a = a - n
    else
      a = a - (a - x)
    end
    puts ck2 * a
  end
end

gets.strip.to_i.times { solve }

def solve(a, b, x, y, n)
	b_dec = [[0, b - y].max, n].min
	b -= b_dec
	n -= b_dec
	a_dec = [[0, a - x].max, n].min
	a -= a_dec
	n -= a_dec
	a * b
end

t = gets.to_i
t.times do
	a, b, x, y, n = gets.split.map &:to_i
	p [solve(a, b, x, y, n), solve(b, a, y, x, n)].min
end

t = gets.to_i
t.times do
    a, b, x, y, n = gets.chomp.split(" ").map(&:to_i)
    if a < b
        tmp = a
        a = b
        b = tmp
        tmpx = x
        x = y
        y = tmpx
    end
    iti = b - n
    if iti >= y
        b = b - n
    else
        #puts "IIII"
        if y <= x
            over = n - (b - y)
            a = [x,a-over].max
            b = y
        elsif x < y
            point = a - n
            if point >= y
                over = n - (b - y)
                a = [x,a-over].max
                b = y
            elsif point < y
                if point >= x
                    a = point
                    # puts "OOOOOO"
                    # p a
                    # p b
                    # puts "UU"
                else
                    over = n - (a - x)
                    b = [b - over,y].max
                    a = x
                end
            end
        end
    end
    puts a * b
end