def floor(n, mod)
    return 1 if n == 1
    return n * floor(n-1, mod)
end
n, m = gets.split(' ').map { |i| i.to_i }
mod = 1000000007
light = gets.split(' ').map { |i| i.to_i}.sort
light.insert(0, 0)
light.push(n+1)
segment = []
for i in 1...light.size
    s = light[i] - light[i-1] - 1
    segment.push s if s != 0
end
total = segment.inject { |mem, var|  mem += var}
total = 1 if total.nil?
c1 = c2 = c3 = 1
for i in 0...segment.size
    c1 = c1 * floor(segment[i], mod)
    next if (i == 0 and light[1] != 1) or (i == segment.size - 1 and light[m] != n)
    c2 = c2 * (2**(segment[i]-1))
end
puts (floor(total, mod) / c1 * c2) % mod

# mod = 1000000007;

# for i in 0..99
#   p (2**i)%1000000007
# end

n = gets(' ').to_i
m = gets.to_i

inp=[0]
(m-1).times{
  inp.push gets(' ').to_i
}
inp.push gets.to_i

inp = inp.sort

a=[]

for i in 1..m
  a.push inp[i]-inp[i-1]-1
end
a.push n-inp[m]

a0 = a[0]
am = a[m]
em = m

# p a
# p m

ab = a.clone

m-=a.count 0
a=a-[0]

# p a
# p m

def silnia(x)
  ret=1
  for i in 2..x
    ret*=i
  end
  return ret
end

s=a.clone

for i in 1..s.length-1
  s[i] += s[i-1]
end

# p a
# p s
# p m
# p n
# p em
# p a0
# p am

if m > -1
  ret = silnia s[m]
  for i in a
    ret /= silnia i
  end

#   p n-em-a0-am-m+1
#
#   p ret

  if 0 == ab[0]
    m+=1
  end

  if 0 == ab[ab.length-1]
    m+=1
  end

  if 0 == m
    p ret%1000000007
  else
    p((ret * 2**(n-em-a0-am-m+1))%1000000007)
  end
else
  p 1
end

#!/usr/bin/ruby

class Solver
  def factorial(n)
    f = 1
    2.upto(n) {|i| f *= i }
    return f
  end

  def solve(lights)
    n = lights.length
    off = n
    lights.each {|v| off -= 1 if v }

    return 0 if off == n

    ans = factorial(off)
    off = 0
    den = 1
    0.upto(n) do |i|
      if i == n
        den *= factorial(off)
        break
      end

      if lights[i]
        den *= factorial(off)
        off = 0
      else
        off += 1
      end
    end

    ans /= den
    flag = true
    0.upto(n) do |i|
      if flag && lights[i]
        flag = false
        off = 0
      end

      if lights[i]
        ans *= 2 ** (off - 1) if off > 0
        off = 0
      else
        off += 1
      end
    end

    ans
  end
end

tokens = gets.split(" ")
n = tokens[0].to_i
m = tokens[1].to_i
lights = Array.new(n, false)

if m > 0
  gets.split(" ").map(&:to_i).each {|v| lights[v-1] = true }
end

solver = Solver.new
puts solver.solve(lights)%1000000007

def pow2 p
	t = 1
	(0...p).each{
		t = (t*2)%1000000007
	}
	return t
end

def fact n
	t = 1
	(0...p).each{
		t = (t*n)%1000000007
	}
	return t
end

def comb n,k
	a = [k,n-k].max
	b = [k,n-k].min
	t = 1
	p = 2
	(a+1..n).each{|k|
		t = t*k
		while t%p==0 and p<=b
			t = t/p
			p += 1
		end
	}
	return t%1000000007
end

n,m=gets.split(" ").collect! {|x| x.to_i}
arr = [0]*n
line = gets.split(" ").collect! {|x| x.to_i}
line.each {|v|
	arr[v-1] = 1
}

intervals = []
crt = 0
(0...n).each {|k|
	if arr[k]==0
		crt += 1
	else
		if crt!=0
			intervals.push crt
		end
		crt = 0
	end
}
if crt!= 0
	intervals.push crt
end
pp = intervals.collect {|x| pow2(x-1)}
if arr[0] == 0
	pp[0] = 1
end

if arr[-1] == 0
	pp[-1] = 1
end

ll = pp.size
res = 1
t = n - m
(0...ll).each {|y|
    #puts " - #{pp[y]} #{intervals[y]} #{t} #{comb(t,intervals[y])}"
	#puts res
	res = (res*pp[y])%1000000007
	if y<ll-1
		res = (res*comb(t,intervals[y+1]))%1000000007
		t -= intervals[y+1]
	end
}

puts res

def floor(n, mod)
    return 1 if n == 1
    return n * floor(n-1, mod) % mod
end
n, m = gets.split(' ').map { |i| i.to_i }
mod = 1000000007
light = gets.split(' ').map { |i| i.to_i}.sort
light.insert(0, 0)
light.push(n+1)
segment = []
for i in 1...light.size
    s = light[i] - light[i-1] - 1
    segment.push s if s != 0
end
total = segment.inject { |mem, var|  mem += var}
c1 = c2 = c3 = 1
for i in 0...segment.size
    c1 = c1 * floor(segment[i], mod) % mod
    c2 = c2 * (2**(segment[i]-1)) % mod if i != 0 and i != segment.size - 1
end
puts (floor(total, mod) / c1 * c2) % mod

def floor(n, mod)
    return 1 if n == 1
    return n * floor(n-1, mod)
end
n, m = gets.split(' ').map { |i| i.to_i }
mod = 1000000007
light = gets.split(' ').map { |i| i.to_i}.sort
light.insert(0, 0)
light.push(n+1)
segment = []
for i in 1...light.size
    s = light[i] - light[i-1] - 1
    segment.push s if s != 0
end
total = segment.inject { |mem, var|  mem += var}
total = 1 if total.nil?
c1 = c2 = c3 = 1
for i in 0...segment.size
    c1 = c1 * floor(segment[i], mod)
    next if (i == 0 and light[1] != 1) or (i == segment.size - 1 and light[n-2] != n)
    c2 = c2 * (2**(segment[i]-1))
end
puts (floor(total, mod) / c1 * c2) % mod

def floor(n, mod)
    return 1 if n == 1
    return n * floor(n-1, mod) % mod
end
n, m = gets.split(' ').map { |i| i.to_i }
mod = 1000000007
light = gets.split(' ').map { |i| i.to_i}.sort
light.insert(0, 0)
light.push(n+1)
segment = []
for i in 1...light.size
    s = light[i] - light[i-1] - 1
    segment.push s if s != 0
end
total = segment.inject { |mem, var|  mem += var}
total = 1 if total.nil?
c1 = c2 = c3 = 1
for i in 0...segment.size
    c1 = c1 * floor(segment[i], mod) % mod
    next if (i == 0 and light[1] != 1) or (i == segment.size - 1 and light[n-2] != n)
    c2 = c2 * (2**(segment[i]-1)) % mod
end
puts (floor(total, mod) / c1 * c2) % mod

def floor(n, mod)
    return 1 if n == 1
    return n * floor(n-1, mod) % mod
end
n, m = gets.split(' ').map { |i| i.to_i }
mod = 1000000007
light = gets.split(' ').map { |i| i.to_i}.sort
light.insert(0, 0)
light.push(n+1)
segment = []
for i in 1...light.size
    s = light[i] - light[i-1] - 1
    segment.push s if s != 0
end
total = segment.inject { |mem, var|  mem += var}
c1 = c2 = c3 = 1
for i in 0...segment.size
    c1 = c1 * floor(segment[i], mod) % mod
    next if (i == 0 and not light[1] != 1) or (i == segment.size - 1 and not light[n-1] != n)
    c2 = c2 * (2**(segment[i]-1)) % mod
end
puts (floor(total, mod) / c1 * c2) % mod

# mod = 1000000007;

# for i in 0..99
#   p (2**i)%1000000007
# end

n = gets(' ').to_i
m = gets.to_i

inp=[0]
(m-1).times{
  inp.push gets(' ').to_i
}
inp.push gets.to_i

inp = inp.sort

a=[]

for i in 1..m
  a.push inp[i]-inp[i-1]-1
end
a.push n-inp[m]

a0 = a[0]
am = a[m]
em = m

# p a
# p m

m-=a.count 0
a=a-[0]

# p a
# p m

def silnia(x)
  ret=1
  for i in 2..x
    ret*=i
  end
  return ret
end

s=a.clone

for i in 1..s.length-1
  s[i] += s[i-1]
end

# p a
# p s

if m > -1
  ret = silnia s[m]
  for i in a
    ret /= silnia i
  end

  p (ret * 2**(n-em-a0-am-m+1))%1000000007
else
  p 1
end

# mod = 1000000007;

# for i in 0..99
#   p (2**i)%1000000007
# end

n = gets(' ').to_i
m = gets.to_i

inp=[0]
(m-1).times{
  inp.push gets(' ').to_i
}
inp.push gets.to_i

inp = inp.sort

a=[]

for i in 1..m
  a.push inp[i]-inp[i-1]-1
end
a.push n-inp[m]

a0 = a[0]
am = a[m]
em = m

# p a
# p m

m-=a.count 0
a=a-[0]

# p a
# p m

def silnia(x)
  ret=1
  for i in 2..x
    ret*=i
  end
  return ret
end

s=a.clone

for i in 1..s.length-1
  s[i] += s[i-1]
end

# p a
# p s

if m > -1
  ret = silnia s[m]
  for i in a
    ret /= silnia i
  end

#   p n-em-a0-am-m+1

  if 0 == m
    p ret%1000000007
  else
    p (ret * 2**(n-em-a0-am-m+1))%1000000007
  end
else
  p 1
end

# mod = 1000000007;

# for i in 0..99
#   p (2**i)%1000000007
# end

n = gets(' ').to_i
m = gets.to_i

inp=[0]
(m-1).times{
  inp.push gets(' ').to_i
}
inp.push gets.to_i

a=[]

for i in 1..m
  a.push inp[i]-inp[i-1]-1
end
a.push n-inp[m]

# p a
# p m

m-=a.count 0
a=a-[0]

# p a
# p m

def silnia(x)
  ret=1
  for i in 2..x
    ret*=i
  end
  return ret
end

s=a.clone

for i in 1..s.length-1
  s[i] += s[i-1]
end

# p a
# p s

if m > -1
  ret = silnia s[m]
  for i in a
    ret /= silnia i
  end

  p ret * 2**(n-2*m-a[0]-a[m]+1)
else
  p 1
end

# mod = 1000000007;

# for i in 0..99
#   p (2**i)%1000000007
# end

n = gets(' ').to_i
m = gets.to_i

inp=[0]
(m-1).times{
  inp.push gets(' ').to_i
}
inp.push gets.to_i

inp = inp.sort

a=[]

for i in 1..m
  a.push inp[i]-inp[i-1]-1
end
a.push n-inp[m]

a0 = a[0]
am = a[m]
em = m

# p a
# p m

m-=a.count 0
a=a-[0]

# p a
# p m

def silnia(x)
  ret=1
  for i in 2..x
    ret*=i
  end
  return ret
end

s=a.clone

for i in 1..s.length-1
  s[i] += s[i-1]
end

# p a
# p s

if m > -1
  ret = silnia s[m]
  for i in a
    ret /= silnia i
  end

  p (ret * 2**(n-2*em-a0-am+1))%1000000007
else
  p 1
end

# mod = 1000000007;

# for i in 0..99
#   p (2**i)%1000000007
# end

n = gets(' ').to_i
m = gets.to_i

inp=[0]
(m-1).times{
  inp.push gets(' ').to_i
}
inp.push gets.to_i

a=[]

for i in 1..m
  a.push inp[i]-inp[i-1]-1
end
a.push n-inp[m]

a0 = a[0]
am = a[m]
em = m

# p a
# p m

m-=a.count 0
a=a-[0]

# p a
# p m

def silnia(x)
  ret=1
  for i in 2..x
    ret*=i
  end
  return ret
end

s=a.clone

for i in 1..s.length-1
  s[i] += s[i-1]
end

# p a
# p s

if m > -1
  ret = silnia s[m]
  for i in a
    ret /= silnia i
  end

  p ret * 2**(n-2*em-a0-am+1)
else
  p 1
end

# mod = 1000000007;

# for i in 0..99
#   p (2**i)%1000000007
# end

n = gets(' ').to_i
m = gets.to_i

inp=[0]
(m-1).times{
  inp.push gets(' ').to_i
}
inp.push gets.to_i

a=[]

for i in 1..m
  a.push inp[i]-inp[i-1]-1
end
a.push n-inp[m]

def silnia(x)
  ret=1
  for i in 2..x
    ret*=i
  end
  return ret
end

s=a.clone

for i in 1..s.length-1
  s[i] += s[i-1]
end

# p a
# p s

ret = silnia s[m]
for i in a
  ret /= silnia i
end

p ret * 2**(n-2*m-a[0]-a[m]+1)

#!/usr/bin/ruby

class Solver
  def factorial(n)
    f = 1
    2.upto(n) {|i| f *= i }
    return f
  end

  def solve(lights)
    n = lights.length
    off = n
    lights.each {|v| off -= 1 if v }

    return 0 if off == 0

    ans = factorial(off)
    off = 0
    den = 1
    0.upto(n) do |i|
      if i == n
        den *= factorial(off)
        break
      end

      if lights[i]
        den *= factorial(off)
        off = 0
      else
        off += 1
      end
    end

    ans /= den
    flag = true
    0.upto(n) do |i|
      if flag && lights[i]
        flag = false
        off = 0
      end

      if lights[i]
        ans *= 2 ** (off - 1) if off > 0
        off = 0
      else
        off += 1
      end
    end

    ans
  end
end

tokens = gets.split(" ")
n = tokens[0].to_i
m = tokens[1].to_i
lights = Array.new(n, false)

gets.split(" ").map(&:to_i).each {|v| lights[v-1] = true }

solver = Solver.new
puts solver.solve(lights)

#!/usr/bin/ruby

class Solver
  def factorial(n)
    f = 1
    2.upto(n) {|i| f *= i }
    return f
  end

  def solve(lights)
    n = lights.length
    off = n
    0.upto(n - 1) {|i| off -= 1 if lights[i] }

    ans = factorial(off)
    off = 0
    den = 1
    0.upto(n) do |i|
      if i == n
        den *= factorial(off)
        break
      end

      if lights[i]
        den *= factorial(off)
        off = 0
      else
        off += 1
      end
    end

    ans /= den
    flag = true
    0.upto(n) do |i|
      if flag && lights[i]
        flag = false
        off = 0
      end

      if lights[i]
        ans *= 2 ** (off - 1) if off > 0
      else
        off += 1
      end
    end

    ans
  end
end

tokens = gets.split(" ")
n = tokens[0].to_i
m = tokens[1].to_i
lights = Array.new(n, false)

gets.split(" ").map(&:to_i).each {|i| lights[i-1] = true }

solver = Solver.new
puts solver.solve(lights)

#!/usr/bin/ruby

class Solver
  def factorial(n)
    f = 1
    2.upto(n) {|i| f *= i }
    return f
  end

  def solve(lights)
    n = lights.length
    off = n
    lights.each {|v| off -= 1 if v }

    ans = factorial(off)
    off = 0
    den = 1
    0.upto(n) do |i|
      if i == n
        den *= factorial(off)
        break
      end

      if lights[i]
        den *= factorial(off)
        off = 0
      else
        off += 1
      end
    end

    ans /= den
    flag = true
    0.upto(n) do |i|
      if flag && lights[i]
        flag = false
        off = 0
      end

      if lights[i]
        ans *= 2 ** (off - 1) if off > 0
        off = 0
      else
        off += 1
      end
    end

    ans
  end
end

tokens = gets.split(" ")
n = tokens[0].to_i
m = tokens[1].to_i
lights = Array.new(n, false)

gets.split(" ").map(&:to_i).each {|i| lights[i-1] = true }

solver = Solver.new
puts solver.solve(lights)

#!/usr/bin/ruby

class Solver
  def factorial(n)
    f = 1
    2.upto(n) {|i| f *= i }
    return f
  end

  def solve(lights)
    n = lights.length
    off = n
    lights.each {|v| off -= 1 if v }

    return 0 if off == 0 || off == n

    ans = factorial(off)
    off = 0
    den = 1
    0.upto(n) do |i|
      if i == n
        den *= factorial(off)
        break
      end

      if lights[i]
        den *= factorial(off)
        off = 0
      else
        off += 1
      end
    end

    ans /= den
    flag = true
    0.upto(n) do |i|
      if flag && lights[i]
        flag = false
        off = 0
      end

      if lights[i]
        ans *= 2 ** (off - 1) if off > 0
        off = 0
      else
        off += 1
      end
    end

    ans
  end
end

tokens = gets.split(" ")
n = tokens[0].to_i
m = tokens[1].to_i
lights = Array.new(n, false)

if m > 0
  gets.split(" ").map(&:to_i).each {|v| lights[v-1] = true }
end

solver = Solver.new
puts solver.solve(lights)


n = gets.to_i
a = gets.split.map(&:to_i).sort
if a[-1] < a[-2] + a[-3]
  puts "YES"
  ans = []
  ans[0] = a[-2]
  ans[1] = a[-1]
  ans[2] = a[-3]
  (n-3).times do |i|
    ans[i+3] = a[-4-i]
  end
  puts ans.join(" ")
else
  puts "NO"
end

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

a.sort!

if a[-1] < a[0] + a[-2]
  puts "YES"
  puts a.join(' ')
else
  a[-2], a[-1] = a[-1], a[-2]
  if a[-1] < a[0] + a[-2] && a[-2] < a[-3] + a[-1]
    puts "YES"
    puts a.join(' ')
  else
    puts "NO"
  end
end

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

inp.sort!

if (inp[-2] + inp[-3] <= inp[-1])
  puts "NO"
else
  puts "YES"
  temp = inp[-1]
  inp[-1] = inp[-2]
  inp[-2] = temp
  inp.each {|x| print "#{x} "}
end

n = gets.to_i
a = gets.split.map(&:to_i).sort
if a[-1] < a[-2] + a[-3]
  puts "YES"
  ans = []
  ans[0] = a[-2]
  ans[1] = a[-1]
  ans[2] = a[-3]
  (n-3).times do |i|
    ans[i+3] = a[-3-i]
  end
  puts ans.join(" ")
else
  puts "NO"
end


l, r = gets.split.map &:to_i
t = (r-l+1)/2
puts "YES"
i = l
while i <= r
    print "#{i} #{i+1}\n"
    i += 2
end

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

puts "YES"
for i in 0..1000000
puts "#{a} #{a+1}"
a += 2
break if a > b
end

l,r = gets.split.map(&:to_i)
puts "YES"
((r-l+1)/2).times do |i|
  puts "#{l+2*i} #{l+2*i+1}"
end


c1, c2, c3, c4 = gets.split.map(&:to_i)
n, m = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
t = gets.split.map(&:to_i)

sumb = 0
sumt = 0

for i in 0...n
  b[i] = [c1*b[i], c2].min
  sumb += b[i]
end

for i in 0...m
  t[i] = [c1*t[i], c2].min
  sumt += t[i]
end

sumb = [sumb, c3].min
sumt = [sumt, c3].min

print [sumb+sumt, c4].min

costs = gets.split.map{|s| s.to_i}
bn, tn = gets.split.map{|s| s.to_i}
buses = gets.split.map{|s| s.to_i}
trolleys = gets.split.map{|s| s.to_i}

b1 = costs[1]/costs[0]
bsum = tsum = 0
buses.each_index{|i|
    buses[i] = buses[i] > b1 ? costs[1] : buses[i]*costs[0]
    bsum += buses[i]
}
trolleys.each_index{|i|
    trolleys[i] = trolleys[i] > b1 ? costs[1] : trolleys[i]*costs[0]
    tsum += trolleys[i]
}
bsum = costs[2] if bsum > costs[2]
tsum = costs[2] if tsum > costs[2]
sum = bsum + tsum
sum = costs[3] if sum > costs[3]
print sum

c= gets().split().map {|i| i.to_i}.take(4)
n,m= gets().split().map {|i| i.to_i}.take(2)
a= gets().split().map {|i| i.to_i}.take(n)
b= gets().split().map {|i| i.to_i}.take(m)

def f(x, k, c)
  res=0
  for i in 0..k
    res+=[c[1],x[i].to_i*c[0]].min
  end
  [c[2],res].min
end

print [c[3],f(a,n,c)+f(b,m,c)].min

c = gets.split.collect{|i| i.to_i}
n, m = gets.split.collect{|i| i.to_i}
sum_a = 0
gets.chomp.split.each do |a|
    sum_a += [a.to_i*c[0],c[1]].min
end
sum_a = c[2] if sum_a > c[2]
sum_b = 0
gets.chomp.split.each do |b|
    sum_b += [b.to_i*c[0],c[1]].min
end
sum_b = c[2] if sum_b > c[2]
puts [sum_a+sum_b, c[3]].min

cs   = gets.split.map(&:to_i)
n, m = gets.split.map(&:to_i)
ns   = gets.split.map(&:to_i)
ms   = gets.split.map(&:to_i)

sumn_min = ns.inject(0){|a, b| a + [cs[1], b * cs[0]].min }
summ_min = ms.inject(0){|a, b| a + [cs[1], b * cs[0]].min }
#puts sumn_min
#puts summ_min

nmin = [sumn_min, cs[2]].min
mmin = [summ_min, cs[2]].min

puts [cs[3], nmin + mmin].min

#f = File.open('test1.txt', 'r')
f = STDIN
c = f.gets.chomp.split.map &:to_i
n, m = f.gets.chomp.split.map &:to_i
a = []
2.times { a << f.gets.chomp.split.map(&:to_i) }
# Compute when don't use c4
total = 0
2.times do |i|
  sub_total = 0
  a[i].each do |ai|
    sub_total += [ai * c[0], c[1]].min
  end
  total += [sub_total, c[2]].min
end

puts [total, c[3]].min


class String
  def palindrome?
    self == self.reverse
  end
end

def solve(s)
  sa = s.split('')
  return 'Impossible' if sa.uniq.size == 1
  (0..s.size/2).to_a.each do |c|
    r = sa.rotate(c).join('')
    if r.palindrome? && r != s
      return 1
    end
  end

  left = s[0..(s.size/2)-1]
  right = s[s.size/2+(s.size.odd? ? 1 : 0)..s.size]
  if left.palindrome?
    left.split('').uniq.size == 1 ? 'Impossible' : 2
  else
    return s.size.odd? ? 2 : 1
  end
end

puts solve(gets.strip)

if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

s = gets.chomp
len = s.length

if s.each_char.to_a.uniq.count == 1
	puts "Impossible"
	exit
end

if len%2==1
	news = s[0..len/2-1]+s[len/2+1..-1]
	if news.each_char.to_a.uniq.count == 1
		puts "Impossible"
		exit
	end
end
#p "hi"
if len%2==0
	if(s[0..len/2-1] != s[len/2..-1])
		puts 1
		exit
	end
end

(1..len-1).each do |i|
	#break is before i
	news = s[i..-1]+s[0..i-1]
	if(news==news.reverse && news != s)
		puts 1
		exit
	end
end

puts 2

if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

s = gets.chomp
len = s.length

if s.each_char.to_a.uniq.count == 1
	puts "Impossible"
	exit
end

#p "hi"
if len%2==0
	if(s[0..len/2-1] != s[len/2..-1])
		puts 1
		exit
	end
end

(1..len-1).each do |i|
	#break is before i
	news = s[i..-1]+s[0..i-1]
	if(news==news.reverse && news != s)
		puts 1
		exit
	end
end
puts 2


n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).zip(1..n).sort!
ans = []
k.times do
  mx, mi = a.last, a.first
  break if mx[0] - mi[0] <= 1
  mx[0] -= 1
  mi[0] += 1
  ans << [mx[1], mi[1]]
  a.sort!
end
puts "#{a[-1][0] - a[0][0]} #{ans.size}", ans.map{|x| x.join(' ')}

n, k = STDIN.readline.split.map {|s| s.to_i }
heights = STDIN.readline.split.map {|s| s.to_i }
pairs = heights.each_with_index.to_a
moves = []
k.times do
	pairs.sort!
	break if pairs[0][0] + 1 >= pairs[-1][0]
	moves.push('%d %d' % [pairs[-1][1]+1, pairs[0][1]+1])
	pairs[-1][0] -= 1
	pairs[0][0] += 1
end
pairs.sort!
puts '%d %d' % [pairs[-1][0] - pairs[0][0], moves.length]
puts moves.join("\n")

n_and_k = gets.split(' ')
n = n_and_k[0].to_i
k = n_and_k[1].to_i
a = gets.split(' ').map { |s| s.to_i }

moves = Array.new

k.times do
  posmin = 0
  posmax = 0

  a.each_with_index do |val, index|
    if val > a[posmax]
      posmax = index
    else
      if val < a[posmin]
        posmin = index
      end
    end
  end

  if a[posmax] == a[posmin]
    break
  else
    moves.push [posmax + 1, posmin + 1]
    a[posmax] -= 1
    a[posmin] += 1
  end
end

maxdiff = a.max - a.min

puts "#{maxdiff} #{moves.length}"
moves.each do |move|
  puts "#{move[0]} #{move[1]}"
end

n, k = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}
ans = Array.new
cnt = 0
while cnt < k
    min, max = a.minmax
    if max-min <= 1
        break
    end
    minindex = a.index(min)
    maxindex = a.index(max)
    a[maxindex] -= 1
    a[minindex] += 1
    ans.push([maxindex+1, minindex+1])
    cnt += 1
end
min, max = a.minmax
print max-min, ' ', cnt, "\n"
ans.each{|i| puts i.join(' ')}

n, k = gets.chomp.split.map(&:to_i)
hs = gets.chomp.split.map(&:to_i)
list = []
c = 0
goal = (hs.inject(:+) / (n * 1.0)).ceil
k.times do
  for i in 0...n
    break if c >= k
    if hs[i] > goal
      for j in 0...n
        if hs[j] < goal && j != i
          hs[j] += 1
          hs[i] -= 1
          list << [i + 1, j + 1]
          c += 1
          break
        end
      end
      break
    end
  end
end
max = 0
min = 10000
for i in 0...n
  max = hs[i] if hs[i] > max
  min = hs[i] if hs[i] < min
end
ans = [max - min, c]
puts ans.join(" ")
puts list.map{|x| x.join(" ")}

n, k = gets.chomp.split.map(&:to_i)
h = gets.chomp.split.map(&:to_i) # 5 8 5
ans = []
list = []
c = 0
goal = (h.inject(:+) / (n * 1.0)).ceil
k.times do
  max = 0
  min = 10000
  ai, ii = 0
  for i in 0...n
    if h[i] > max
      max = h[i]
      ai = i
    end
    if h[i] < min
      min = h[i]
      ii = i
    end
  end
  if max > goal
    if min < goal && max != min
      h[ii] += 1
      min = h[ii]
      h[ai] -= 1
      max = h[ai]
      list << [ai + 1, ii + 1]
      c += 1
    end
  end
  for i in 0...n
    if h[i] > max
      max = h[i]
      ai = i
    end
    if h[i] < min
      min = h[i]
      ii = i
    end
  end
  dif = max - min
  ans = [dif, c]
end
puts ans.join(" ")
puts list.map{|x| x.join(" ")}


n = gets.to_i

if n < 4
	puts 'NO'
else
	puts 'YES'
	if n.even?
		puts '1 * 2 = 2'
		puts '2 * 3 = 6'
		puts '6 * 4 = 24'
		5.upto(n) do |i|
			if i.odd?
				puts (i + 1).to_s + ' - ' + i.to_s + ' = 1'
				puts '24 * 1 = 24'
			end
		end
	else
		puts '5 - 1 = 4'
		puts '4 - 2 = 2'
		puts '2 * 3 = 6'
		puts '6 * 4 = 24'
		6.upto(n) do |i|
			if i.even?
				puts (i + 1).to_s + ' - ' + i.to_s + ' = 1'
				puts '24 * 1 = 24'
			end
		end
	end
end

n = gets.to_i

if n < 4
  puts "NO"
elsif n.even?
  puts "YES"
  puts "1 * 2 = 2"
  puts "2 * 3 = 6"
  puts "6 * 4 = 24"
  5.upto(n).each_slice(2) do |a, b|
    puts "#{b} - #{a} = 1"
    puts "24 * 1 = 24"
  end
else
  puts "YES"
  puts "2 - 1 = 1"
  puts "1 + 3 = 4"
  puts "4 * 5 = 20"
  puts "20 + 4 = 24"
  6.upto(n).each_slice(2) do |a, b|
    puts "#{b} - #{a} = 1"
    puts "24 * 1 = 24"
  end
end

n = STDIN.readline.to_i
if n < 4
	puts 'NO'
	exit
end
puts 'YES'
lines = []
if n%2 == 0
	x = (n-4)/2
	x.times {|i| lines.push('%d - %d = 1' % [6+i*2, 5+i*2]) }
	x.times { lines.push('1 * 1 = 1') }
	lines.push('1 * 2 = 2')
	lines.push('2 * 3 = 6')
	lines.push('6 * 4 = 24')
else
	x = (n-5)/2
	x.times {|i| lines.push('%d - %d = 1' % [7+i*2, 6+i*2]) }
	lines.push('3 - 2 = 1')
	(x+1).times { lines.push('1 * 1 = 1') }
	lines.push('1 + 5 = 6')
	lines.push('6 * 4 = 24')
end
puts lines.join("\n")

#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 Szymon Sliwa <knight.erraunt@gmail.com>
#
# Distributed under terms of the MIT license.

n = gets.to_i

if n == 1
    puts "NO"
elsif n == 2
    puts "NO"
elsif n == 3
    puts "NO"
elsif n == 4
    puts "YES"
    puts "1 * 2 = 2\n2 * 3 = 6\n6 * 4 = 24"
elsif n == 5
    puts "YES"
    puts "5 - 1 = 4\n4 - 2 = 2\n4 * 2 = 8\n8 * 3 = 24"
else
    puts "YES"
    puts "2 * 4 = 8\n6 - 5 = 1\n1 - 1 = 0"
    (7..n).each do |x|
        puts "#{x} * 0 = 0"
    end
    puts "3 + 0 = 3"
    puts "8 * 3 = 24\n"
end

n = STDIN.readline.to_i
if n < 4
	puts 'NO'
	exit
end
lines = []
if n%2 == 0
	x = (n-4)/2
	x.times {|i| lines.push('%d - %d' % [6+i*2, 5+i*2]) }
	x.times { lines.push('1 * 1') }
	lines.push('1 * 2')
	lines.push('2 * 3')
	lines.push('6 * 4')
else
	x = (n-5)/2
	x.times {|i| lines.push('%d - %d' % [7+i*2, 6+i*2]) }
	lines.push('3 - 2')
	(x+1).times { lines.push('1 * 1') }
	lines.push('1 + 5')
	lines.push('6 * 4')
end
puts lines.join("\n")

n = STDIN.readline.to_i
if n < 4
	puts 'NO'
	exit
end
puts 'YES'
lines = []
if n%2 == 0
	x = (n-4)/2
	x.times {|i| lines.push('%d - %d' % [6+i*2, 5+i*2]) }
	x.times { lines.push('1 * 1') }
	lines.push('1 * 2')
	lines.push('2 * 3')
	lines.push('6 * 4')
else
	x = (n-5)/2
	x.times {|i| lines.push('%d - %d' % [7+i*2, 6+i*2]) }
	lines.push('3 - 2')
	(x+1).times { lines.push('1 * 1') }
	lines.push('1 + 5')
	lines.push('6 * 4')
end
puts lines.join("\n")

#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 Szymon Sliwa <knight.erraunt@gmail.com>
#
# Distributed under terms of the MIT license.

n = gets.to_i

if n == 1
    puts "NO"
elsif n == 2
    puts "NO"
elsif n == 3
    puts "NO"
elsif n == 4
    puts "1 * 2 = 2\n2 * 3 = 6\n6 * 4 = 24"
elsif n == 5
    puts "5 - 1 = 4\n4 - 2 = 2\n4 * 2 = 8\n8 * 3 = 24"
else
    puts "2 * 4 = 8\n6 - 5 = 1\n1 - 1 = 0"
    (7..n).each do |x|
        puts "#{x} * 0 = 0"
    end
    puts "8 * 3 = 24\n"
end

#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 Szymon Sliwa <knight.erraunt@gmail.com>
#
# Distributed under terms of the MIT license.

n = gets.to_i

if n == 1
    puts "NO"
elsif n == 2
    puts "NO"
elsif n == 3
    puts "NO"
elsif n == 4
    puts "1 * 2 = 2\n2 * 3 = 6\n6 * 4 = 24"
elsif n == 5
    puts "5 - 1 = 4\n4 - 2 = 2\n4 * 2 = 8\n8 * 3 = 24"
else
    puts "2 * 4 = 8\n6 - 5 = 1\n1 - 1 = 0"
    (7..n).each do |x|
        puts "#{x} * 0 = 0"
    end
    puts "3 + 0 = 3"
    puts "8 * 3 = 24\n"
end


m,n=gets.split.map &:to_i
a=[]
(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}
exit !puts(-1) if m>0
p a.size
puts a*' '

n,m=gets.chomp.split.map(&:to_i)
sc,i,j=Array.new(100005,0),1,1
while n!=0&&i<=m
	j=i
	while j<=m
		if n!=0 && ((n&i)!=0 || j+i*2<=m)
			sc[sc[0]+1]=j
			sc[0]+=1
			n-=i
		end
		j+=i*2
	end
	i<<=1
end
if n!=0
	puts "-1"
else
	puts sc[0]
	for i in 1...sc[0]
		print "#{sc[i]} "
	end
	puts sc[sc[0]]
end

#! /usr/bin/env ruby

def main
    sum, lmt = readline.split.map do |x| x.to_i end
    set = Array.new
    (1..lmt).reverse_each do |i|
        lb = i & -i
        if sum >= lb then
            sum -= lb
            set.push i
        end
    end
    if sum == 0 then
        puts set.size
        set.each do |x|
            print x, ' '
        end
        puts
    else
        puts -1
    end
end

main

m,n=gets.split.map &:to_i
a,now=[],0
ans=[]
(1..n).each{|i|a.push i}
a.sort_by!{|x|x&-x}
a.reverse.each{|i|if (i&-i)+now<=m then now+=i&-i;ans.push i end}
exit !puts(-1) if now!=m
p ans.size
puts ans*' '

def lowbit(n)
  i = 0
  while(n%2 == 0) do
    n /= 2
    i += 1
  end
  return 2**i
end

sum,limit = gets.split.map(&:to_i)
nums = []
(1..limit).each do |x|
  nums << [x,lowbit(x)]
end
nums.sort_by!{ |x| x[1] }
nums.reverse!
#puts nums.map{ |x| "[#{x[0]} #{x[1]}]" }.join " "
ret = []
nums.each do |x|
  next if(sum < x[1])
  sum -= x[1]
  ret << x[0]
end
if(sum == 0)
  puts ret.size
  puts ret.join(" ")
else
  puts -1
end

def subset_sum(s,n,arr)
	return true if s == 0
	return false if n == 0 && s != 0
	return subset_sum(s,n-1,arr) if arr[n-1] > s

	if subset_sum(s-arr[n-1],n-1,arr)
		@sum_arr << n
		return true
	end
	return subset_sum(s,n-1,arr)

end

sum,limit = gets.split(" ").map(&:to_i)
@sum_arr = []
arr = []
(1..limit).each do |i|
	arr << (i ^ (i & (i-1)))
end
ones = (limit/2.to_f).ceil
list = arr.each_with_index.sort.reverse
# puts "#{ones}"
# puts "#{list}"

ans = false
ans_list = []
list.each do |a|
	if sum <= ones
		ans = true
		break
	end
	break if a[0] == 1
	sum -= a[0]
	ans_list << a[1] + 1
end
if ans
	one_index = list.index{|i| i[0] == 1}
	while sum > 0
		sum -= 1
		ans_list << list[one_index][1] + 1
		one_index += 1
	end
	puts ans_list.length
	puts ans_list.join(" ")
else
	puts "-1"
end
# subset_sum(sum,arr.length,arr)

# if @sum_arr.length != 0
# 	puts @sum_arr.length
# 	puts @sum_arr.join(" ")
# else
# 	puts "-1"
# end

def lowbit(n)
  i = 0
  while(n%2 == 0) do
    n /= 2
    i += 1
  end
  return 2**i
end

sum,limit = gets.split.map(&:to_i)
nums = []
(1..limit).each do |x|
  nums << [x,lowbit(x)]
end
nums.sort_by!{ |x| x[1] }
nums.reverse!
#puts nums.map{ |x| "[#{x[0]} #{x[1]}]" }.join " "
ret = []
nums.each do |x|
  next if(sum < x[1])
  sum -= x[1]
  ret << x[0]
end
if(sum == 0)
  puts ret.join(" ")
else
  puts -1
end

def subset_sum(s,n,arr)
	return true if s == 0
	return false if n == 0 && s != 0
	return subset_sum(s,n-1,arr) if arr[n-1] > s

	if subset_sum(s-arr[n-1],n-1,arr)
		@sum_arr << n
		return true
	end
	return subset_sum(s,n-1,arr)

end

sum,limit = gets.split(" ").map(&:to_i)
@sum_arr = []
arr = []
(1..limit).each do |i|
	arr << (i ^ (i & (i-1)))
end
ones = (limit/2.to_f).ceil
list = arr.each_with_index.sort.reverse
# puts "#{ones}"
# puts "#{list}"

ans = false
ans_list = []
list.each do |a|
	break if a[0] == 1
	if sum <= ones
		ans = true
		break
	end
	sum -= a[0]
	ans_list << a[1] + 1
end
if ans
	one_index = list.index{|i| i[0] == 1}
	while sum > 0
		sum -= 1
		ans_list << list[one_index][1] + 1
		one_index += 1
	end
	puts ans_list.length
	puts ans_list.join(" ")
else
	puts "-1"
end
# subset_sum(sum,arr.length,arr)

# if @sum_arr.length != 0
# 	puts @sum_arr.length
# 	puts @sum_arr.join(" ")
# else
# 	puts "-1"
# end

def subset_sum(s,n,arr)
	return true if s == 0
	return false if n == 0 && s != 0
	return subset_sum(s,n-1,arr) if arr[n-1] > s

	if subset_sum(s-arr[n-1],n-1,arr)
		@sum_arr << n
		return true
	end
	return subset_sum(s,n-1,arr)

end

sum,limit = gets.split(" ").map(&:to_i)
@sum_arr = []
arr = []
(1..limit).each do |i|
	arr << (i ^ (i & (i-1)))
end
ones = (limit/2.to_f).ceil
list = arr.each_with_index.sort.reverse
# puts "#{ones}"
# puts "#{list}"

ans = false
ans_list = []
list.each do |a|
	if sum <= ones
		ans = true
		break
	end
	sum -= a[0]
	ans_list << a[1] + 1
end
if ans
	one_index = list.index{|i| i[0] == 1}
	while sum > 0
		sum -= 1
		ans_list << list[one_index][1] + 1
		one_index += 1
	end
	puts ans_list.length
	puts ans_list.join(" ")
else
	puts "-1"
end
# subset_sum(sum,arr.length,arr)

# if @sum_arr.length != 0
# 	puts @sum_arr.length
# 	puts @sum_arr.join(" ")
# else
# 	puts "-1"
# end


n = gets.to_i
a = gets.split(/\s+/).map(&:to_i)
mod = 998244353

ten = (0..21).map{|e| 10**e}
ans = 0
a.each do |x|
  t = 0
  while x > 0
    p = x % 10;
    ans = (ans + (p*n*ten[t]) % mod) % mod
    ans = (ans + (p*n*ten[t+1]) % mod) % mod
    x /= 10
    t += 2
  end
end

puts ans

MOD = 998244353
n = gets.to_i
a = gets.split.map(&:to_i)
cnt = [0]*10
a.each do |v|
  cnt[Math.log10(v).to_i] += 1
end
ans = 0
a.each do |v|
  d = Math.log10(v).to_i
  l,r,m = v,0,1
  10.times do |i|
    r += m*(l%10)
    l /= 10
    m *= 100
    if i < d
      ans += l * m * cnt[i] * 2 % MOD
      ans += (r + r*10) * cnt[i] % MOD
    else
      ans += (r + r*10) * cnt[i] % MOD
    end
    ans %= MOD
  end
end
p ans


n = gets.to_i

xs = gets.split(" ").map(&:to_i).sort

g = 0
(1...n).each do |i|
    g = g.gcd(xs[i] - xs[i-1])
end

res = 0
(1...n).each do |i|
    res += (xs[i] - xs[i-1]) / g - 1
end

puts res

def gcd(a,b)
    if (b == 0)
        a
    else
        gcd(b,a%b)
    end
end

n = readline.to_i

a = readline.split.map(&:to_i).sort

g = a[1] - a[0]
for i in 1 .. n-2
    g = gcd(g, a[i+1]-a[i])
end

ans = 0
for i in 0 .. n-2
    ans += (a[i+1] - a[i]) / g - 1
end

p ans

def gcd a, b
  return a if b.zero?
  gcd b, a % b
end

n = gets.to_i
a = gets.split.map(&:to_i)
t=0
a.sort!

$num = n
$i = 1
while $i < $num  do
   a[$i] = a[$i] - a[0]
   $i +=1
end
a[0]=0
ans = gcd(a[1],a[2])

$num = n
$i = 3
while $i < $num  do
   ans = gcd(ans, a[$i])
   $i +=1
end
ans = (a[n-1] / ans) - n + 1

puts ans

n = gets.to_i
arr = gets.split
i = 0
while i < arr.length
    arr[i] = arr[i].to_i
    i = i + 1
end
arr = arr.sort
if arr.length == 1
    puts 0
else
    g = arr[1] - arr[0]
    i = 2
    while i < arr.length
        a = g
        b = arr[i] - arr[i - 1]
        while b != 0
            tmp = b
            b = a % b
            a = tmp
        end
        i = i + 1
        g = a
        #puts g
    end
    i = 1
    ans = 0
    while i != arr.length
        ans = ans + (arr[i] - arr[i - 1]) / g - 1
        i = i + 1
    end
    puts ans
end

n = gets.to_i
arr = gets.chomp.split.map {|x| x.to_i}.sort

len = arr[-1] - arr[0]
gcd = arr[1] - arr[0]
arr.each_with_index do |x, i|
    gcd = gcd.gcd(x - arr[i - 1]) if i > 0
end

puts len / gcd + 1 - n

n = readline().to_i
a = readline().split().map(&:to_i).sort

g = 0
left = right = a[0]
for i in 1..n-1
	g = g.gcd(a[i]-a[i-1])
	left = [a[i], left].min
	right = [a[i], right].max
end

puts (right-left)/g + 1 - n


total_time, time_slice, constant = gets.strip.split(" ")

total_time = total_time.to_i
time_slice = time_slice.to_i
constant = constant.to_f

arr_requests = gets.strip.split(" ").map(&:to_i)

results = Hash.new

mean = 0.0
j = 0
while j < time_slice-1 do
    mean = (mean + (arr_requests[j].to_f / time_slice.to_f)) / constant
    j+=1
end

arr_summ = arr_requests.slice(0,time_slice-1).inject(:+).to_f

puts
i = time_slice-1
while i < arr_requests.length do
    k = i+1

    arr_summ+=arr_requests[i]
    arr_summ -= arr_requests[i-time_slice] if i > time_slice-1

    real = arr_summ / time_slice

    mean = (mean + (arr_requests[i].to_f / time_slice.to_f)) / constant
    err = (mean - real).abs / real

    results[k] = {real: real, mean: mean, error: err}
    i+=1
end

rq = gets
requests = gets.strip.split(" ")
requests.each do |r|
    r = r.to_i
    puts sprintf("%.6f %.6f %.6f\n", results[r][:real], results[r][:mean], results[r][:error])
end

(n, t, c) = gets.scan(/[\d\.]+/).map(&:to_f)
n = n.to_i
t = t.to_i
a = gets.scan(/\d+/).map(&:to_i)
m = gets.scan(/\d+/).map(&:to_i)
a = [a] if a.is_a? Fixnum
m = [m] if a.is_a? Fixnum
p_arr = gets.scan(/\d+/).map(&:to_i)

mean = [0.0]*(n+1)
real = [0.0]*(n+1)

(n).times do |i|
  mean[i+1] = (mean[i] + a[i].to_f/t) / c
  real[i+1] = real[i] + a[i]
end
p_arr.each do |p|
  r = (real[p] - real[p-t])/t
  puts "%.6f %.6f %.6f" % [r.round(6), mean[p].round(6), ((mean[p]-r).abs/r).round(6)]
end

total_time, time_slice, constant = gets.strip.split(" ")

total_time = total_time.to_i
time_slice = time_slice.to_i
constant = constant.to_f

arr_requests = gets.strip.split(" ").map(&:to_i)

results = Hash.new

mean = 0.0
j = 0
while j < time_slice-1 do
    mean = (mean + (arr_requests[j].to_f / time_slice.to_f)) / constant
    j+=1
end

arr_summ = arr_requests.slice(0,time_slice-1).inject(:+).to_f

puts
i = time_slice-1
while i < arr_requests.length do
    k = i+1

    arr_summ+=arr_requests[i]
    arr_summ -= arr_requests[i-4] if i > 3

    real = arr_summ / time_slice

    mean = (mean + (arr_requests[i].to_f / time_slice.to_f)) / constant
    err = (mean - real).abs / real

    results[k] = {real: real, mean: mean, error: err}
    i+=1
end

rq = gets
requests = gets.strip.split(" ")
requests.each do |r|
    r = r.to_i
    puts sprintf("%.6f %.6f %.6f\n", results[r][:real], results[r][:mean], results[r][:error])
end


input0 = gets.chomp
key = input0.split("").map { |e| e.ord - 48 }
input = gets.chomp.split
a = Integer(input[0])
b = Integer(input[1])

l = key.length
left = Array.new(l-1)
remainder = key[0].to_i % a
left[0] = remainder

for i in 1...l-1
    remainder_b = left[i-1] * 10
    left[i] = (key[i] + remainder_b) % a
end

right = key[l-1].to_i % b

split = 0
answer = false

if right == 0 && left[l-2] == 0 && key[l-1] != 0
    split = l-2
    answer = true
else
    c = 10 % b
    d = 1
    for i in 1...l-1
        d = (c * d) % b

        remainder_b = key[l-1-i] * d
        right = (right + remainder_b) % b
        if right == 0 && left[l-i-2] == 0
            if key[l-i-1] != 0
                split = l-i-2
                answer = true
                break
            end
        end
    end
end

if !answer
    puts "NO"
else
    x = input0[0..split]
    y = input0[split+1..l-1]
    puts "YES"
    print x
    puts
    print y
end

input = gets.chomp
a, b = gets.chomp.split(" ")
a = a.to_i
b = b.to_i

num = Array.new(input.length)
for i in 0...num.length
    num[i] = input[i].to_i
end

left = Array.new(num.length)
acc = 0
for i in 0...num.length
    acc = acc * 10 + num[i]
    acc %= a
    left[i] = acc
end

acc  = 0
rem = 1
ans = false
for i in (num.length-1).downto(1)
    acc = acc + num[i] * rem
    rem *= 10
    rem %= b
    acc %= b
    if num[i] != 0 && left[i-1] == 0 && acc == 0
        puts "YES"
        puts input[0...i]
        puts input[i...input.length]
        ans = true
        break
    end
end

if !ans
    puts "NO"
end

input = gets.chomp
a, b = gets.chomp.split(" ")
a = a.to_i
b = b.to_i

num = Array.new(input.length)
for i in 0...num.length
    num[i] = input[i].to_i
end

left = Array.new(num.length)
acc = 0
for i in 0...num.length
    acc = acc * 10 + num[i]
    acc %= a
    left[i] = acc
end

acc  = 0
rem = 1
ans = false
for i in (num.length-1).downto(1)
    acc = acc + num[i] * rem
    rem *= 10
    rem %= b
    acc %= b
    if num[i] != 0 && left[i-1] == 0 && acc == 0
        puts "YES"
        puts input[0, i - 1]
        puts input[i, -1]
        ans = true
        break
    end
end

if !ans
    puts "NO"
end

key = gets.chomp.split("")
input = gets.chomp.split
a = Integer(input[0])
b = Integer(input[1])

l = key.length
left = Array.new(l-1)
remainder = key[0].to_i % a
left[0] = remainder

for i in 1...l-1
    key_i = key[i].to_i
    remainder_a = key_i % a
    remainder_b = (left[i-1] * (10 % a)) % a
    left[i] = (remainder_a + remainder_b) % a
end

right = Array.new(l-1)
remainder = key[l-1].to_i % b
right[l-2] = remainder
ten_remainder = Array.new(l-1)
c = 10 % b
d = c
ten_remainder[0] = c
for i in 1...l-1
    d = (c * d) % b
    ten_remainder[i] = d
end

for i in 1...l-1
    key_i = key[l-1-i].to_i
    number = key_i * (10**i) + right[l-i-1][0]
    remainder_a = right[l-i-1]
    remainder_b = ((key_i % b) * ten_remainder[i-1]) % b
    right[l-i-2] = (remainder_a + remainder_b) % b
end
split = 0
answer = false
for i in 0...l-1
    if left[i] == right[i] && left[i] == 0
        split = i
        answer = true
        break
    end
end

if !answer
    puts "NO"
else
    puts "YES"
    print key[0..i].join
    puts
    print key[i+1..l-1].join
end

input0 = gets.chomp
key = input0.split("").map { |e| e.ord - 48 }
input = gets.chomp.split
a = Integer(input[0])
b = Integer(input[1])

l = key.length
left = Array.new(l-1)
remainder = key[0].to_i % a
left[0] = remainder

for i in 1...l-1
    remainder_b = left[i-1] * 10
    left[i] = (key[i] + remainder_b) % a
end

right = Array.new(l-1)
remainder = key[l-1].to_i % b
right[l-2] = remainder
ten_remainder = Array.new(l-1)
c = 10 % b
d = c
ten_remainder[0] = c
for i in 1...l-1
    d = (c * d) % b
    ten_remainder[i] = d
end

for i in 1...l-1
    remainder_a = right[l-i-1]
    remainder_b = key[l-1-i] * ten_remainder[i-1]
    right[l-i-2] = (remainder_a + remainder_b) % b
end
split = 0
answer = false
x = ""
y = ""
for i in 0...l-1
    if left[i] == right[i] && left[i] == 0
        split = i
        if key[i+1] != "0"
            answer = true
            break
        end
    end
end
x = input0[0..split]
y = input0[split+1..l-1]
if !answer
    puts "NO"
else
    puts "YES"
    print x
    puts
    print y
end

key = gets.chomp.split("")
input = gets.chomp.split
a = Integer(input[0])
b = Integer(input[1])

l = key.length
left = Array.new(l-1)
remainder = key[0].to_i % a
left[0] = remainder

for i in 1...l-1
    key_i = key[i].to_i
    remainder_a = key_i % a
    remainder_b = (left[i-1] * (10 % a)) % a
    left[i] = remainder_a + remainder_b
end

right = Array.new(l-1)
remainder = key[l-1].to_i % b
right[l-2] = remainder
ten_remainder = Array.new(l-1)
c = 10 % b
d = c
ten_remainder[0] = c
for i in 1...l-1
    d = (c * d) % b
    ten_remainder[i] = d
end

for i in 1...l-1
    key_i = key[l-1-i].to_i
    number = key_i * (10**i) + right[l-i-1][0]
    remainder_a = right[l-i-1]
    remainder_b = ((key_i % b) * ten_remainder[i-1]) % b
    right[l-i-2] = (remainder_a + remainder_b) % b
end
split = 0
answer = false
for i in 0...l-1
    if left[i] == right[i] && left[i] == 0
        split = i
        answer = true
        break
    end
end

if !answer
    puts "NO"
else
    puts "YES"
    print key[0..i].join
    puts
    print key[i+1..l-1].join
end


strings = Array.new
maxlen = 0
while line = gets
  line.strip!
  if line.size>maxlen
    maxlen=line.size
  end
  strings << line
end
left=true
(maxlen+1).times {print "*"}
puts "*"
strings.each do |s|
  diff = maxlen-s.size
  margin=diff/2
  print "*"
  margin.times {print " "}
  if diff%2==0
    print s
  else
    print " " if !left
    print s
    print " " if left
    left=!left
  end
  margin.times {print " "}
  puts "*"
end
(maxlen+1).times {print "*"}
puts "*"

def display(s,n,flag)
  t=(n-s.length)
  h=t/2
  t=t-h
  t,h=h,t if flag
  print " "*t
  print s
  print " "*h
end
memo=[]
max=0
while line=gets
  line.chomp!
  max=line.length if max <line.length
  memo.push(line)
end
print "*"*(max+2)
puts ""
tmp=0
memo.length.times do |i|
  tmp+=1 if (max-memo[i].length)%2==1
  print "*"
  display(memo[i],max,tmp % 2 == 1)
  puts "*"
end
print "*"*(max+2)
puts ""

lines = []
while line = gets do
  lines << line.chomp
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+2)
res += "\r\n"
flag=true
lines.each do |x|
  thisL = '*'
  s = (haba-x.size)
  if(s%2==0||flag)
    thisL += ' '*(s/2)
    flag=false unless s%2==0
  else
    thisL += ' '*(s/2+1)
    flag=true
  end
  thisL += x.chomp
  thisL += ' '*(haba+1-thisL.size)
  res += (thisL+"*\r\n")
end
res += '*'*(haba+2)
print res+"\r\n"

lines = []
max_width = 0
while (line = gets)
	line.strip!
	max_width = line.length if line.length > max_width
	lines << line
end

top_bottom = "*" * (max_width + 2)
left = true
puts top_bottom
lines.each do |line|
	space = max_width - line.length
	left_space = left ? space / 2 : (space / 2.0).ceil
	right_space = left ? (space / 2.0).ceil : space / 2
	left = !left if space % 2 == 1
	puts "*" + " " * left_space + line + " " * right_space + "*"
end
puts top_bottom

input, max = [], 0
while g = gets; input.push g.strip; end
input.map! { |i| l = i.length; max = l if l > max; [i, l] }

puts '*' * (max + 2)

left = false

input.each do |i|
	print '*'
	m = (max - i[1])
	s =  m / 2
	if (m % 2).even?
		print ' ' * s, i[0], ' ' * s
	else
		l, r = s, s
		left ? l += 1 : r += 1
		print ' ' * l, i[0], ' ' * r
		left = !left
	end
	puts '*'
end

puts '*' * (max + 2)

def justify(str_arr)
  len_arr = str_arr.collect {|s| s.size}
  len = len_arr.max

  justify_left = true
  res = ['*' * (len+2)]
  str_arr.each do |s|
    spaces = len - s.size
    sp_left = spaces/2
    sp_right = spaces - spaces/2
    if spaces % 2 != 0 and s != ''
      if !justify_left
        sp_left += 1
        sp_right -= 1
      end
      justify_left = !justify_left
    end
    ss = ' '*sp_left + s + ' '*sp_right
    res << '*' + ss + '*'
  end
  res << '*' * (len+2)
  res
end

if __FILE__ == $0
  arr = []
  while line = gets
    arr << line.chomp
  end
  res = justify(arr)
  res.each {|s| puts s}
end

def display(s,n)
  t=(n-s.length)
  h=t/2
  (t-h).times do
    print " "
  end
  print s
  h.times do
    print " "
  end
end
memo=Array.new
max=0
while line=gets
  line.chomp!
  max=max<line.length ? line.length : max
  memo.push(line)
end
(max+2).times do |i|
  print "*"
end
puts ""
memo.length.times do |i|
  print "*"
  display(memo[i],max)
  puts "*"
end
(max+2).times do |i|
  print "*"
end
puts ""

def display(s,n)
  t=(n-s.length)
  h=t/2
  (t-h).times do
    print " "
  end
  print s
  h.times do
    print " "
  end
end
memo=Array.new
max=0
while line=gets
  line.chomp!
  max=max<line.length ? line.length : max
  memo << line
end
(max+2).times do |i|
  print "*"
end
puts ""
memo.length.times do |i|
  print "*"
  display(memo[i],max)
  puts "*"
end
(max+2).times do |i|
  print "*"
end
puts ""

lines = []
while line = gets do
  lines << line.chomp
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+2)
lines.each do |x|
  res +="\n"
  res += '*'
  s = (haba-x.size)/2
  res += ' '*s
  res += x.chomp
  if (haba-x.size)%2 == 0
    res += ' '*s
  else
    res += ' '*(s+1)
  end
  res +='*'
end
res += "\n"
res += '*'*(haba+2)
puts res

lines = []
while line = gets do
  lines << line.chomp
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+2)
lines.each do |x|
  res +="\n"
  res += '*'
  s = (haba-x.size)/2
  if (haba-x.size)%2 == 0
    res += ' '*s
  else
    res += ' '*(s+1)
  end
  res += x.chomp
  res += ' '*s
  res +='*'
end
res += "\n"
res += '*'*(haba+2)
print res

lines = []
while line = gets do
  lines << line
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+1)
lines.each do |x|
  res +="\n"
  res += '*'
  s = (haba-x.size)/2
  res += ' '*s
  res += x.chomp
  if (haba-x.size)%2 == 0
    res += ' '*s
  else
    res += ' '*(s+1)
  end
  res +='*'
end
res += "\n"
res += '*'*(haba+1)
print res

lines = []
while line = gets do
  lines << line.chomp
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+2)
res += "\r\n"
lines.each do |x|
  res += '*'
  s = (haba-x.size)/2
  res += ' '*s
  res += x.chomp
  res += ' '*(haba-x.size-s)
  res += "*\r\n"
end
res += '*'*(haba+2)
print res+"\r\n"

lines = []
while line = gets do
  lines << line.chomp
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+2)
lines.each do |x|
  res +="\n"
  res += '*'
  s = (haba-x.size)/2
  res += ' '*s
  res += x.chomp
  if (haba-x.size)%2 == 0
    res += ' '*s
  else
    res += ' '*(s+1)
  end
  res +='*'
end
res += "\n"
res += '*'*(haba+2)
print res

lines = []
while line = gets do
  lines << line.chomp
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+2)
lines.each do |x|
  res +="\n"
  res += '*'
  s = (haba-x.size)/2
  if (haba-x.size)%2 == 0
    res += ' '*s
  else
    res += ' '*(s+1)
  end
  res += x.chomp
  res += ' '*s
  res +='*'
end
res += "\n"
res += '*'*(haba+2)
puts res

lines = []
while line = gets do
  lines << line
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+1)
lines.each do |x|
  res +="\n"
  res += '*'
  s = (haba-x.size)/2
  res += ' '*s
  res += x.chomp
  if (haba-x.size)%2 == 0
    res += ' '*s
  else
    res += ' '*(s+1)
  end
  res +='*'
end
res += "\n"
res += '*'*(haba+1)
puts res

lines = []
while line = gets do
  lines << line.chomp
end
haba = 0
lines.each do |x|
  haba = x.size if x.size>haba
end
res = '*'*(haba+2)
lines.each do |x|
  res +="\r\n"
  res += '*'
  s = (haba-x.size)/2
  if (haba-x.size)%2 == 0
    res += ' '*s
  else
    res += ' '*(s+1)
  end
  res += x.chomp
  res += ' '*s
  res +='*'
end
res += "\r\n"
res += '*'*(haba+2)
print res+"\r\n"

input, max = STDIN.read.split("\n"), 0
input.map! { |i| l = i.length; max = l if l > max; [i, l] }

puts '*' * (max + 2)

left = false

input.each do |i|
	print '*'
	m = (max - i[1])
	s =  m / 2
	if (m % 2).even?
		print ' ' * s, i[0], ' ' * s
	else
		l, r = s, s
		left ? l += 1 : r += 1
		print ' ' * l, i[0], ' ' * r
		left = !left
	end
	puts '*'
end

puts '*' * (max + 2)


#!/usr/bin/ruby2.0

n, s = STDIN.readline.split.collect {|s| s.to_i }
s *= 100
best = -1
n.times do
	d, c = STDIN.readline.split.collect {|s| s.to_i }
	change = s - (d*100 + c)
	best = [best, change%100].max if change >= 0
end
puts best

(n, s) = gets.chomp.split.map(&:to_i);
max = -1
n.times do |x|
  (x, y) = gets.chomp.split.map(&:to_i);
  if (s - x) * 100 >= y
    max = [max, (100 - y) % 100].max
  end
end
puts max;

n, s = gets.split.map(&:to_i)
max = -1
n.times do
  x, y = gets.split.map(&:to_i)
  if (s - x) * 100 >= y
    max = [max, (100 - y) % 100].max
  end
end
puts max

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

a = []
for i in 0...x[0]
    b = gets.split.map(&:to_i)
    a << 100 - b[1] if x[1] > b[0] and b[1] > 0
    a << 0 if x[1] >= b[0] and b[1] == 0
end

puts a.size > 0 ? a.max : -1

class Pair
  attr_accessor :first, :second

  def initialize(f, s)
    @first = f
    @second = s
  end

  def <=(y)
    return @first < y.first || (@first == y.first && @second <= y.second)
  end

  def -(y)
    if y.second == 0
      0
    else
      100 - y.second
    end
  end
end

n, s = gets.split.map{|c| c.to_i}

a = Pair.new(s, 0)

arr = []

n.times{ arr.push(Pair.new(*gets.split.map{|c| c.to_i})) }

mx = -1

arr.each do |p|
  mx = [mx, a - p].max if p <= a
end

puts mx

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

s *= 100
res = -1
n.times do
  x, y = gets.chomp.split(' ').map(&:to_i)
  cost = 100*x + y
  #(1..(s/cost).to_i).each do |kg|
    total = cost # * kg
    if s >= total
      ost = (100-(total % 100)) % 100
      res = [res, ost].max
    end
  #end
end

puts res

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

# $stdin = File.new('rf','r')

n, s = readline.chomp.split(' ').map(&:to_i)
x, y = [], []
n.times { |i| x[i], y[i] = *readline.chomp.split(' ').map(&:to_i) }

candies = -1
x.zip(y).each do |dollars, cents|
  next if dollars + cents.fdiv(100) > s
  candies = [candies, -cents % 100].max
end
puts candies

n, s = *gets.chomp.split(' ').map(&:to_i)

s *= 100

a = Array.new(n) do
  x, y = gets.chomp.split(' ').map(&:to_i)
  x*100 + y
end

def solve(a, s)
  a = a.select{|p| p <= s}
  return -1 if a.empty?
  a.map{|x| (s - x)%100 }.max
end

puts solve(a, s)

n,s = gets.split.map(&:to_i)
max = -1
n.times { |_|
	d,c = gets.split.map(&:to_i)
	if s * 100 >= d * 100 + c
		max = [max, (100 - c) % 100].max
	end
}
puts max

n, s = gets.split.map(&:to_i)
ans = -1
n.times do
  x, y = gets.split.map(&:to_i)
  next if s < x || (s == x && y > 0)
  c = (100 - y) % 100
  ans = [ans, c].max
end
puts ans

n, s = gets.split.map(&:to_i)
sweets = 0
can_buy = false
n.times do
  x, y = gets.split.map(&:to_i)
  x += 1 if y > 0
  sweets = 100 - y if s >= x && 100 - y > sweets && y != 0
  can_buy = true if s >= x
end
puts can_buy ? sweets : -1

def read_next_line
  gets.chomp.split.map(&:to_i)
end

n, s = read_next_line
puts (0...n).map { |_| read_next_line }.select { |a| a[0] < s || a[0] == s && a[1] == 0 }.map { |a| (100 - a[1]) % 100 }.max || -1

n, s = gets.chomp.split(" ").map { |e| e.to_i }
arr = []

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

arr.select! { |x, y| (x.to_f + y.to_f / 100.0) <= s.to_f  }
arr.map! { |x, y|
 if y == 0
  [x, y]
 else
  [x, 100 - y]
 end
}

if arr.empty?
  puts "-1"
else
  arr.sort_by! { |x, y| -y }
  puts arr.first.last
end

line = gets
i, s = line.split.map(&:to_i)
min_nu = 10 ** 9
zero_found = false
i.times do
	line = gets.split
	linetmp = "#{line[0]}.#{line[1]}".to_f
	next if linetmp > s
	num = line[1].to_i
	min_nu = num if num < min_nu && num != 0
	zero_found = true if num == 0
end
if min_nu == 10 ** 9 and zero_found
	puts "0"
elsif min_nu == 10 ** 9
	puts "-1"
else
	puts 100 - min_nu
end

n, s = gets.chomp.split(" ").map { |e| e.to_i }
arr = []

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

arr.select! { |x, y| (x.to_f + y.to_f / 100.0) <= s.to_f  }
arr.select! { |x, y| y != 0 }

if arr.empty?
  puts "-1"
else
  arr.sort_by! { |x, y| y }
  puts 100 - arr.first.last
end

(n, s) = gets.chomp.split.map(&:to_i);
max = -1
(1..n).each do |x|
  (x, y) = gets.chomp.split.map(&:to_i);
  if (x > s || (x == s && y != 0) || y == 0) then
    next;
  end
  max = [max, 100 - y].max;
end
puts max;

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

a = []
for i in 0...x[0]
    b = gets.split.map(&:to_i)
    a << 100 - b[1] if x[1] > b[0]
end

puts a.size > 0 ? a.max : -1

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

a = []
for i in 0...x[0]
    b = gets.split.map(&:to_i)
    a << 100 - b[1] if x[1] > b[0] and b[1] > 0
end

puts a.size > 0 ? a.max : -1

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

res = -1
n.times do
  x, y = gets.chomp.split(' ').map(&:to_i)
  cost = x + 0.01 * y
  (1..(s/cost).to_i).each do |kg|
    total = cost * kg
    if s >= total && y > 0
      res = [res, 100-y].max
    end
  end
end

puts res

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

res = -1
n.times do
  x, y = gets.chomp.split(' ').map(&:to_i)
  cost = x + 0.01 * y
  (1..(s/cost).to_i).each do |kg|
    total = cost * kg
    total_y = (y * kg) % 100
    if s >= total && total_y > 0
      res = [res, 100-total_y].max
    end
  end
end

puts res

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

s *= 100
res = -1
n.times do
  x, y = gets.chomp.split(' ').map(&:to_i)
  cost = 100*x + y
  (1..(s/cost).to_i).each do |kg|
    total = cost * kg
    if s >= total
      ost = total % 100 > 0 ? 100-(total % 100) : 0
      res = [res, ost].max
    end
  end
end

puts res

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

s *= 100
res = -1
n.times do
  x, y = gets.chomp.split(' ').map(&:to_i)
  cost = 100*x + y
  (1..(s/cost).to_i).each do |kg|
    total = cost * kg
    if s >= total && total % 100 > 0
      res = [res, 100-(total % 100)].max
    end
  end
end

puts res

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

res = -1
n.times do
  x, y = gets.chomp.split(' ').map(&:to_i)
  cost = x + 0.01 * y
  if s >= cost && y > 0
    res = [res, 100-y].max
  end
end

puts res

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

s *= 100
res = -1
n.times do
  x, y = gets.chomp.split(' ').map(&:to_i)
  cost = 100*x + y
  (1..(s/cost).to_i).each do |kg|
    total = cost * kg
    if s >= total
      #ost = total % 100 > 0 ? 100-(total % 100) : 0
      ost = (100-(total % 100)) % 100
      res = [res, ost].max
    end
  end
end

puts res

n, s = gets.split.map(&:to_i)
sweets = 0
can_buy = false
n.times do
  x, y = gets.split.map(&:to_i)
  sweets = 100 - y if s >= x && 100 - y > sweets && y != 0
  can_buy = true if s >= x
end
puts can_buy ? sweets : -1

n, s = gets.split.map(&:to_i)
sweets = -1
n.times do
  x, y = gets.split.map(&:to_i)
  next if s < x || (s == x && y > 0)
  sweets = 100 - y if 100 - y > sweets && y != 0
end
puts sweets

n, s = gets.split.map(&:to_i)
sweets = 0
n.times do
  x, y = gets.split.map(&:to_i)
  sweets = 100 - y if x < s && 100 - y > sweets && y != 0
end
puts sweets == 0 ? -1 : sweets

n, s = gets.split.map(&:to_i)
sweets = 0
can_buy = false
n.times do
  x, y = gets.split.map(&:to_i)
  sweets = 100 - y if s > x && 100 - y > sweets && y != 0
  can_buy = true if s > x
end
puts can_buy ? sweets : -1

line = gets
i, s = line.split.map(&:to_i)
min_nu = 10 ** 9
zero_found = false
i.times do
	line = gets.split
	linetmp = "#{line[0]}.#{line[1]}".to_f
	next if linetmp > s
	num = line[1].to_i
	min_nu = num if num < min_nu && num != 0
	zero_found = true if num == 0
end
if min_nu == 10 ** 9 and zero_found
	puts "0"
elsif min_nu == 10 ** 9
	puts "-1"
else
	puts min_nu
end

line = gets
i, s = line.split.map(&:to_i)
min_nu = -1
i.times do
	line = gets.split
	line = "#{line[0]}.#{line[1]}".to_f
	next if line > s
	num = s - line
	num = ("0." + num.to_s.split(".")[1]).to_f
	# puts num
	min_nu = num if num > min_nu && num >= 0
end
if min_nu != -1
	min_nu = min_nu.to_s.gsub!(/0\./, "")
	min_nu += "0" if min_nu.size  == 1 && min_nu != "0"
end
puts min_nu

line = gets
i, s = line.split.map(&:to_i)
min_nu = -1
i.times do
	line = gets.split
	line = "#{line[0]}.#{line[1]}".to_f
	next if line > s
	num = s - line
	num = ("0." + num.to_s.split(".")[1]).to_f
	# puts num
	min_nu = num if num > min_nu && num > 0
end
if min_nu != -1
	min_nu = min_nu.to_s.gsub!(/0\./, "")
	min_nu += "0" if min_nu.size  == 1
end
puts min_nu


t = gets.chomp.to_i

until t==0
    t-=1
    x = gets.split.map(&:to_i)
    puts("#{x[1]} #{x[2]} #{x[2]}")
end

t = gets.to_i
t.times do
  a, b, c, d = gets.split.map(&:to_i)
  x = a
  y = c
  z = c
  puts "#{x} #{y} #{z}"
end

1.upto(gets.to_i) do
  a, b, c, d = gets.split(' ').map(&:to_i)

  puts "#{b} #{c} #{[d, b + c - 1].min}"
end

(1).upto(gets.to_i) do
  a=gets.split(' ')
  puts a[1]+" "+a[2]+" "+a[2]
end

t=gets.to_i
(1).upto(t) do
  a = gets.split(' ')
  puts "#{a[1]} #{a[2]} #{a[2]}"
end

def valid?(a,b,c)
  # puts "#{a} #{b} #{c} -> #{((a+b)>c) && ((a+c)>b) && ((b+c)>a)}"
  ((a+b)>c) && ((a+c)>b) && ((b+c)>a)
end

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

  if(a>b)
    x=a
  else
    x=b
  end

  if(b>c)
    y=b
  else
    y=c
  end

  if(c<d)
    z=c
  else
    z=d
  end
  puts "#{x} #{y} #{z}"
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.to_i

while t != 0
  a,b,c,d = file.gets.split.map(&:to_i)
  puts "#{b} #{c} #{c}"
  t -= 1
end

t = gets.chomp.to_i
t.times do |testcase|
  (a,b,c,d) = gets.split.map(&:to_i)
  print "#{b} #{c} "
  print [b+c-1 , d].min ; print "\n"
end

t = gets.to_i
t.times do
    a, b, c, d = gets.strip.split.map(&:to_i)
    puts "#{b} #{c} #{c}"
end

gets.to_i.times {
  a, b, c, d = gets.split.map { |v| v.to_i }
  puts "%d %d %d" % [a, c, c]
}

1.upto(gets.chomp.to_i) do
  a,b,c,d = gets.chomp.split.map(&:to_i)
  puts [b,c,c].join(' ')
end

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

  puts "#{b} #{b} #{b}"
end

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

  puts "#{a} #{a} #{a}"
end

t = gets.chomp.to_i
t.times do
  a, b, c, d =  gets.chomp.split.map(&:to_i)
  puts "#{a} #{b} #{d}"
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.to_i

while t != 0
  a,b,c,d = file.gets.split.map(&:to_i)

  puts "#{a} #{b} #{c}"
  t -= 1
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.to_i

while t != 0
  a,b,c,d = file.gets.split.map(&:to_i)

  puts "#{b} #{c} #{d}"
  t -= 1
end

t = gets.chomp.to_i
t.times do |testcase|
  (a,b,c,d) = gets.split.map(&:to_i)
  puts "#{a} #{b} #{c}"
end

t = gets.chomp.to_i
t.times do |testcase|
  (a,b,c,d) = gets.split.map(&:to_i)
  puts "#{b} #{c} #{d}"
end

t = gets.chomp.to_i
t.times do |testcase|
  (a,b,c,d) = gets.split.map(&:to_i)
  print "#{b} #{c} "
  print b+c-1 ; print "\n"
end


input = STDIN.read.split("\n"); input.shift

x, y = [], []
input.each.with_index {|v, i| y.push i if v =~ /[*]/ }

input[0].length.times do |i|
	input.each do |v|
		if v[i] == '*'
			x.push i
			break
		end
	end
end

x, y = (x.first..x.last), (y.first..y.last)

y.each {|i| puts input[i][x]}

st = gets.split
n = st[0].to_i
m = st[1].to_i

str = []
maxn = -1
minn = 51
maxm = -1
minm = 51
n.times do |i|
    str << gets.chomp
    if k = str[i].index("*")
                q = str[i].rindex("*")
        if q > maxm then
            maxm = q
        end
        if k < minm then
            minm = k
        end
        if i < minn then
            minn = i
        end
        if i > maxn then
            maxn = i
        end
    end
end

result = []
n.times do |i|
    if i >= minn && i <= maxn then
        a = str[i][minm..maxm]
        result << a
    end
end

result.size.times do |i|
    puts result[i]
end

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i
ar = Array.new(n)
for i in 0..n-1
	ar[i] = gets.chomp
end

x1 = m-1
x2 = 0
y1 = -1
y2 = -1
for i in 0..n-1
	wx1 = ar[i].index("*")
	x1 = [x1, wx1].min if wx1 != nil
	wx2 = ar[i].rindex("*")
	x2 = [x2, wx2].max if wx2 != nil
	if wx1 != nil && y1 == -1
		y1 = i
	end
	y2 = i if wx1 != nil
end
for i in y1..y2
	puts ar[i].slice(x1..x2)
end

input=gets.split.map(&:to_i)
map=[]
for i in 0..input[0]-1
        map.push(gets.split[0])
end
sx=0
str=""
for i in 0..input[1]-1
        str+="."
end
for i in 0..input[0]-1
        if map[i]==str then
                sx=i+1
        else
                break
        end
end
ex=input[0]-1
(input[0]-1).downto(0){|i|
        if map[i]==str then
                ex=i-1
        else
                break
        end
}

sy=0
ey=input[1]-1
for i in 0..input[1]-1
        ok=1
        for j in 0..input[0]-1
                if map[j][i]=="*" then
                        ok=false
                        break
                end
        end
        if ok then
                sy=i+1
        else
                break
        end
end

(input[1]-1).downto(0){|i|
        ok=1
        for j in 0..input[0]-1
                if map[j][i]=="*" then
                        ok=false
                        break
                end
        end
        if ok then
                ey=i-1
        else
                break
        end
}

sx.upto(ex){|i|
        puts map[i][sy..ey]
}

n,m=gets.chomp!.split(" ").map(&:to_i)

g,f,l,b=[],[0,m-1],[0,0],false

n.times{|i|

  g<<gets.chomp!

  if c=/[*]/=~g[i] then

    if(!b) then

      f[0]=i

      b=true

    end

    if c<f[1] then f[1]=c end

  end

  if c=/[*][.]*$/=~g[i] then

    l[0]=i

    if c>l[1] then l[1]=c end

  end

}

g[f[0]..l[0]].each{|s|puts s[f[1]..l[1]]}

height,width = gets.split.map(&:to_i)
data = Array.new
range = Array.new(4,0)
range[0] = height
range[1] = width
height.times do |h|
  data[h] = gets.chomp
  width.times do |w|
    if(data[h][w,1] == "*") then
      if h < range[0] then range[0] = h end
      if h > range[2] then range[2] = h end
      if w < range[1] then range[1] = w end
      if w > range[3] then range[3] = w end
    end
  end
end
#p range
for h in range[0]..range[2] do
  puts data[h][range[1]..range[3]]
end

n, m = gets.split.map &:to_i
a = gets(p).split("\n")

t, d = -1, -1
l, r = m + 1, -1
a.size.times do |i|
    pos = a[i] =~ /\*/
    pos = m if pos == nil

    l = [l, pos].min

    if pos < m
        t = i if t == -1
        d = i
    end

    rpos = a[i] =~ /\.+\z/
    rpos = m if rpos == nil
    r = [r, rpos].max
end

a = a[t..d].map { |s| s[l...r] }
puts a * "\n"

s = gets.split(" ").map { |e| e.to_i }
str = Array.new(0)
for i in 0..s[0]-1 do
     str.push(gets)
end
xcord = Array.new(0)
ycord = Array.new(0)
for i in 0..s[0]-1 do
    k = str[i].count("*")
    if k > 0 then
        xcord.push(i)
    end
    for j in 0..s[1]-1 do
        if str[i][j] == '*' then
            ycord.push(j)
        end
    end
end

for i in xcord.min..xcord.max do
    for j in ycord.min..ycord.max do
        print str[i][j]
    end
    puts
end

require 'set'

n, m = gets.chomp!.split.map(&:to_i)
grid = []
indexes = []
n.times do |index|
	row = gets.chomp!
	grid << row
	indexes << index if (re = /[*]/) =~ row
end

width = Set.new
row_start, row_end = indexes.min, indexes.max
m.times do |col|
	(row_start..row_end).each do |row|
		(width << col and break) if grid[row][col] == "*"
	end
end

start_index, end_index = width.min, width.max
(row_start..row_end).each do |row|
	puts grid[row][start_index..end_index]
end

n,m = gets.split.map(&:to_i)
mat = []
n.times{mat = mat << gets }
x,a,y,b= m,n,0,0
for i in 0...n
  a = i if mat[i].count('.') != m && i < a ;b = i if mat[i].count('.') != m && i > b
end
for i in 0...m do
  cnt = 0
  for j in 0...n do
    cnt += 1 if mat[j][i,1] == '.'
  end
  x = i if cnt != n && x > i ;y = i if cnt != n && y < i
end
for i in a..b do
  puts mat[i][x..y]
end

n,m = gets.split.map{|str| str.to_i}
flats = []
n.times{
    flats = flats << gets
}

x1,y1,x2,y2= m,n,0,0
#puts flats[0]
for i in 0...n
    y1 = i if flats[i].count('.') != m && i < y1
    y2 = i if flats[i].count(".") != m && i > y2
end

for i in 0...m do
    cnt = 0
    for j in 0...n do
        cnt += 1 if flats[j][i,1] == '.'
    end
    x1 = i if cnt != n && x1 > i
    x2 = i if cnt != n && x2 < i
end
#puts "#{y1},#{y2},#{x1},#{x2}"
for i in y1..y2 do
    puts flats[i][x1..x2]
end

n, m = gets.split.collect{|i| i.to_i}
s = Array.new(n)
n.times{|i| s[i] = gets.chomp }
up, low, left, right = 0, n-1, m-1, m-1
(0...n).each do |i|
    if s[i].include?("*")
        up = i
        break
    end
end
(n-1).downto(0).each do |i|
    if s[i].include?("*")
        low = i
        break
    end
end
(0...n).each do |i|
    if s[i].index("*") and s[i].index("*")<left
        left = s[i].index("*")
    end
end
(0...n).each do |i|
    if s[i].index("*") and s[i].reverse.index("*")<right
        right = s[i].reverse.index("*")
    end
end
right = m-right
(up..low).each do |i|
    puts s[i][left...right]
end

n, m = gets.split.collect{|i| i.to_i}
s = Array.new(n)
n.times{|i| s[i] = gets.chomp}
up, low, left, right = 0, n-1, m-1, m-1
(0...n).each do |i|
    if s[i].include?("*")
        up = i
        break
    end
end
(n-1).downto(0).each do |i|
    if s[i].include?("*")
        low = i
        break
    end
end
(0...n).each{|i| left = s[i].index("*") if s[i].index("*") and s[i].index("*")<left}
(0...n).each{|i| right = s[i].reverse.index("*") if s[i].index("*") and s[i].reverse.index("*")<right}
right = m-right
(up..low).each{|i| puts s[i][left...right]}

def find_ast_range sheet
	s=0
	loop{
		break if sheet[s]=~/\*/
		s+=1
	}
	t=sheet.size-1
	loop{
		break if sheet[t]=~/\*/
		t-=1
	}
	s..t
end

n,m=gets.split.map{|str|str.to_i}
sheet=[]
temp=[]
n.times{
	str=gets.chomp!
	sheet<<str
	temp<<str.split(//)
}
sheet_t=temp.transpose.map{|strs|strs*""}

n_range=find_ast_range sheet
m_range=find_ast_range sheet_t
sheet[n_range].each{|str|puts str[m_range]}

def find_ast_range sheet
	s=0
	loop{
		break if sheet[s]=~/\*/
		s+=1
	}
	t=sheet.size-1
	loop{
		break if sheet[t]=~/\*/
		t-=1
	}
	s..t
end

n,m=gets.split.map(&:to_i)
sheet=[]
temp=[]
n.times{
	str=gets.chomp!
	sheet<<str
	temp<<str.split(//)
}
sheet_t=temp.transpose.map{|strs|strs*""}

n_range=find_ast_range sheet
m_range=find_ast_range sheet_t
sheet[n_range].each{|str|puts str[m_range]}

n, m = gets.split.collect{|i| i.to_i}
s = Array.new(n)
n.times{|i| s[i] = gets.chomp }
up, low, left, right = 0, n-1, 0, m-1
(0...n).each do |i|
    if s[i].include?("*")
        up = i
        break
    end
end
(n-1).downto(0).each do |i|
    if s[i].include?("*")
        low = i
        break
    end
end
(0...n).each do |i|
    if s[i].index("*") and s[i].index("*")<left
        left = s[i].index("*")
    end
end
(0...n).each do |i|
    if s[i].index("*") and s[i].reverse.index("*")<right
        right = s[i].reverse.index("*")
    end
end
right = m-right
(up..low).each do |i|
    puts s[i][left...right]
end

n, m = gets.split.collect{|i| i.to_i}
s = Array.new(n)
n.times{|i| s[i] = gets.chomp }
up, low, left, right = 0, n-1, 0, m-1
(0...n).each do |i|
    if s[i].include?("*")
        up = i
        break
    end
end
(n-1).downto(0).each do |i|
    if s[i].include?("*")
        low = i
        break
    end
end
(0...n).each do |i|
    if s[i].index("*") and s[i].index("*")>left
        left = s[i].index("*")
    end
end
(0...n).each do |i|
    if s[i].index("*") and s[i].reverse.index("*")<right
        right = s[i].reverse.index("*")
    end
end
right = m-right
(up..low).each do |i|
    puts s[i][left...right]
end

input = STDIN.read.split("\n"); input.shift
input -= ["." * input[0].length]

input.map! {|i| i.split('')}
input = input.transpose

input.map! {|i| i.join('')}
input -= ["." * input[0].length]

input.map! {|i| i.split('')}
input = input.transpose

input.each {|i| puts i.join}


T = gets.to_i

T.times do
  a, b, c, n = gets.split.map(&:to_i)

  max = [a, b, c].max

  [a, b, c].each do |v|
    n -= max - v
  end

  if n >= 0 && n % 3 == 0
    puts 'YES'
  else
    puts 'NO'
  end
end

def solve
	a, b, c, n = gets.split.map(&:to_i)
	sum = a+b+c+n
	return "NO" if sum % 3 != 0
	avg = sum / 3

	return "NO" if [a, b, c].max > avg
	return "YES"
end

gets.to_i.times do
	puts solve
end

gets.to_i.times {
  a, b, c, n = gets.split.map { |v| v.to_i }
  total = a + b + c + n

  if (a >= b) and (a >= c)
    m = a
  elsif (b >= a) and (b >= c)
    m = b
  else
    m = c
  end

  d = 3 * m - a - b - c
  if (total % 3 == 0) and (n >= d)
    puts "YES"
  else
    puts "NO"
  end
}

# coding: utf-8

# if __FILE__ == $0
# end

sets = gets.to_i
sets.times {
    a, b, c, n = gets.split.map(&:to_i)
    pick_max = [a, b, c].max
    fillups = (pick_max - a) + (pick_max - b) + (pick_max - c)
    remains = n - fillups

    if remains >= 0
        puts "YES" if remains % 3 == 0
        puts "NO" if remains % 3 != 0
    else
        puts "NO"
    end
}

def getline
    return gets.split.map(&:to_i)
end
t=gets.to_i
t.times do |i|
    a, b, c, n = getline()
    s = [a,b,c].sort!
    a, b, c = s[2], s[1], s[0]
    d = a-b+a-c
    if d <= n and (n-d) % 3 == 0 then
        print "YES\n"
    else
        print "NO\n"
    end
end

t = gets.to_i

1.upto(t) do
  a, b, c, n = gets.split(' ').map(&:to_i)

  eql = [a, b, c].inject(0) { |sum, x| sum + [a, b, c].max - x }

  puts (n - eql).modulo(3).zero? && (n >= eql) ? 'YES' : 'NO'
end

T = gets.to_i

T.times do
  a, b, c, n = gets.split.map(&:to_i)

  max = [a, b, c].max

  [a, b, c].each do |v|
    n -= max - v
  end

  if n % 3 == 0
    puts 'YES'
  else
    puts 'NO'
  end
end

gets.to_i.times {
  a, b, c, n = gets.split.map { |v| v.to_i }
  total = a + b + c + n
  if a > b and a > c
    m = a
  elsif b > a and b > c
    m = b
  else
    m = c
  end
  d = 3 * m - a - b - c
  if total % 3 == 0 and n >= d
    puts "YES"
  else
    puts "NO"
  end
}

# coding: utf-8

# if __FILE__ == $0
# end

sets = gets.to_i
sets.times {
    a, b, c, n = gets.split.map(&:to_i)
    pick_max = [a, b, c].max
    fillups = (pick_max - a) + (pick_max - b) + (pick_max - c)
    remains = n - fillups

    puts "YES" if remains % 3 == 0
    puts "NO" if remains % 3 != 0
}

t = gets.to_i

1.upto(t) do
  a, b, c, n = gets.split(' ').map(&:to_i)

  puts (n - [a, b, c].inject(0) { |sum, x| sum + [a, b, c].max - x }).modulo(3).zero? ? 'YES' : 'NO'
end


n = gets.chomp.to_i

for i in (0...n)
  puts gets.chomp
end

t=gets.to_i
for i in 1..t do
	n=gets.to_i
	puts n
end

# coding: utf-8

# 색의 개수가 아니라 1의 크기를 가진 다이아몬드로,
# 현재 n 길이의 도형(문제의 이미지를 참고)를 메꿀 수 있느냐를 묻는 문제
# 그 경우의 수! (partitioning, not coloring)

t = gets.to_i
t.times {
    n = gets.to_i
    puts n
}

1.upto(gets.chomp.to_i) do |tt|
  n = gets.chomp.to_i
  puts n
end

n = gets.to_i

n.times do
  t = gets.to_i
  puts t
end

t = gets.to_i
t.times do
  n = gets.to_i
  puts n
end

t = gets.to_i
t.times do
  n = gets.to_i
  puts (n >= 2) ? 2 : 1
end

1.upto(gets.to_i) do
  puts gets.to_i > 1 ? 2 : 1
end


t = gets.to_i
ans = []
t.times do
  n,x = gets.chomp.split(" ").map(&:to_i)
  a = gets.chomp.split(" ").map(&:to_i)
  min = (a.sum + x - 1) / x
  max = 0
  a.each do |v|
    max += (v + x - 1) / x
  end
  ans << [min,max]
end
ans.each do |v|
  puts v.join(" ")
end

def method(v, x)
  v / x + (v % x != 0 ? 1 : 0)
end

t = gets.to_i
t.times do
  n, x = gets.split.map { |x| x.to_i }
  a = gets.split.map { |x| x.to_i }
  puts "#{method(a.sum, x)} #{a.map { |v| method(v, x) }.sum}"
end

def method(v, x)
  v / x + (v % x != 0 ? 1 : 0)
end

t = gets.to_i
t.times do
  n, x = gets.split.map {|x| x.to_i }
  a = gets.split.map {|x| x.to_i }
  puts "#{method(a.sum, x)} #{a.map { |v| method(v, x) }.sum}"
end

a = gets.to_i
for i in 1..a do
    c = gets.chop.split(/ /).map(&:to_i)
    w = c[0]
    h = c[1].to_f
    b = gets.chop
    c = b.split(/ /).map(&:to_i)
    x = c.inject(0){|n, m| n + m}
    x = x / h
    x = x.ceil
    l = 0
    for i in 0..w-1 do
        q = c[i] / h
        q = q.ceil
        l += q
    end
    print x, " ", l, "\n"
end

#!/usr/bin/env ruby

gets.to_i.times {
    n, x = gets.split.map &:to_i
    nums = gets.split.map &:to_i
    max = nums.map{|num| (num.to_f / x).ceil }.sum
    min = (nums.sum.to_f / x).ceil
    puts "#{min} #{max}"
}

def read
  gets.split.map &:to_i
end
gets.to_i.times do
  _, x = read
  a = read
  print (a.sum + x - 1) / x, " ", a.map { |it| (it + x - 1) / x }.sum, "\n"
end

t = gets.chomp.to_i
t.times do
    n, x = gets.chomp.split(" ").map(&:to_i)
    a = gets.chomp.split(" ").map(&:to_i)
    max = 0
    ans = 0
    a.each do |i|
        max += (i/x.to_f).ceil
        ans += i
    end
    min = (ans/x.to_f).ceil
    print "#{min}" + " " + "#{max}" + "\n"
end

t = gets.chomp.to_i
t.times do
  n, x = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i)
  min = 0
  max = 0
  ind = 0
  sum = 0
  while ind < arr.size
    sum += arr[ind]
    max += (1.0 * arr[ind] / x).ceil
    ind += 1
  end
  min += (1.0 * sum / x).ceil
  puts [min, max].join ' '
end


n=gets.to_i
if n==1
    puts -1
else
    puts [n, n+1, n*(n+1)]*' '
end

$, = " "
n = gets.to_i
if n == 1
    puts -1
else
    print n, n + 1, n * (n + 1), "\n"
end

n = gets.strip.to_i
puts -1 if n == 1
puts [n, n+1, n*n+n].map(&:to_s).join(' ') if n != 1


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
########### ( ˘ω˘ )ｽﾔｧ… ###########
n=gi
a=gsmi
ar=[]
ans=[]
n.times do |i|
  ar<<[a[i],i+1]
end
desc ar

n.times do |i|
  cnt+=ar[i][0]*i+1
  ans<<ar[i][1]
end
puts "#{cnt}\n#{ans.join(" ")}"

n = gets.chomp.to_i
arr = gets.chomp.split(' ').map(&:to_i).zip(1..n).sort.reverse
res = 0
arr.each_with_index do |el, ind|
  res += el[0] * ind + 1
end
puts res
puts arr.map{|el| el[1]}.join(' ')

n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
b = []
n.times do |i|
    b[i] = [a[i], i]
end
b.sort!.reverse!
ans = 0
n.times do |i|
    a[i] = b[i][1] + 1
    ans += b[i][0] * i + 1
end
puts ans
puts a.join(' ')

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

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

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

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

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

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

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

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

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

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

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

n = inp[0]
a = inp
t = []
a.size.times do |i|
  t[i] = [a[i],i]
end
t.sort!.reverse!
ans = 0
anst = []
n.times do |i|
  ans += 1
  ans += t[i][0]*i
  anst.push(t[i][1]+1)
end
p ans
puts anst.join(" ")


def gen_matrix(n, seed = nil)
  m = n.times.map { [-1] * n }
  seed ||= n
  srand(seed)
  pool = 1.upto(n * n).map {|i| i}.shuffle

  0.upto(n - 1) do |i|
    0.upto(n - 1) do |j|
      upper = ((i >= 1) ? m[i - 1][j] : -1)
      left  = ((j >= 1) ? m[i][j - 1] : -1)
      x = pool.pop
      retry_count = 5
      while ((x - upper).abs == 1 || (x - left).abs == 1) && retry_count > 0
        retry_count -= 1
        pool.push(x)
        pool.shuffle!
        x = pool.pop
      end
      if retry_count == 0
        return gen_matrix(n, seed + 1)
      end
      m[i][j] = x
    end
  end
  return m
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  if n == 2
    printf("-1\n")
    next
  end
  m = gen_matrix(n, n)
  m.each {|row| printf("%s\n", row.join(" ")) }
end

gets.to_i.times {
  n = gets.to_i

  if n == 2
    p -1
    next
  end

  m = Array.new(n) { [0] * n }

  cur = 1
  n.times { |i|
    n.times { |j|
      next if (i & 1) != (j & 1)
      m[i][j] = cur
      cur += 1
    }
  }
  n.times { |i|
    n.times { |j|
      next if (i & 1) == (j & 1)
      m[i][j] = cur
      cur += 1
    }
  }

  n.times { |i|
    puts m[i].join(' ')
  }
}

t = Integer(gets.chomp)
t.times{
    n = Integer(gets.chomp)

    if(n==2)
        puts -1
    else
        ev = 1
        od = n%2 == 0 ? (n*n)/2 + 1 : ((n*n)+1)/2 + 1
        for i in 0...n do
            for j in 0...n do
                if((i+j)%2 == 0)
                    print "#{ev} "
                    ev += 1
                else
                    print "#{od} "
                    od += 1
                end
            end
            print "\n"
        end
    end
}

def gen_matrix(n, seed = nil)
  m = n.times.map { [-1] * n }
  seed ||= n
  srand(seed)
  pool = 1.upto(n * n).map {|i| i}.shuffle

  0.upto(n - 1) do |i|
    0.upto(n - 1) do |j|
      upper = ((i >= 1) ? m[i - 1][j] : -1)
      left  = ((j >= 1) ? m[i][j - 1] : -1)
      x = pool.pop
      retry_count = 5
      while (x - upper).abs == 1 || (x - left).abs == 1 && retry_count > 0
        retry_count -= 1
        pool.push(x)
        pool.shuffle!
        x = pool.pop
      end
      if retry_count == 0
        return gen_matrix(n, seed + 1)
      end
      m[i][j] = x
    end
  end
  return m
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  if n == 2
    printf("-1\n")
    next
  end
  m = gen_matrix(n, n)
  m.each {|row| printf("%s\n", row.join(" ")) }
end

def gen_matrix(n)
  m = n.times.map { [-1] * n }
  pool = 1.upto(n * n).map {|i| i}.shuffle

  0.upto(n - 1) do |i|
    0.upto(n - 1) do |j|
      upper = i >= 1 ? m[i - 1][j] : -1
      left  = j >= 1 ? m[i][j - i] : -1
      x = pool.pop
      return gen_matrix(n) if (x - upper).abs == 1 || (x - left).abs == 1
      m[i][j] = x
    end
  end
  return m
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  if n == 2
    printf("-1\n")
    next
  end
  m = gen_matrix(n)
  m.each {|row| printf("%s\n", row.join(" ")) }
end

def gen_matrix(n)
  m = n.times.map { [0] * n }
  srand(n)
  pool = 1.upto(n * n).map {|i| i}.shuffle

  0.upto(n - 1) do |i|
    0.upto(n - 1) do |j|
      upper = i >= 1 ? m[i - 1][j] : -1
      left  = j >= 1 ? m[i][j - i] : -1

      x = pool.shift
      while (x - upper).abs == 1 || (x - left).abs == 1
        pool.push(x)
        x = pool.shift
      end
      m[i][j] = x
    end
  end
  return m
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  if n == 2
    printf("-1\n")
    next
  end
  m = gen_matrix(n)
  m.each {|row| printf("%s\n", row.join(" ")) }
end

t = Integer(gets.chomp)
t.times{
    n = Integer(gets.chomp)

    if(n==2)
        puts -1
    else
        ev = 1
        od = n%2 == 0 ? (n*n)/2 : ((n*n)+1)/2 + 1
        for i in 0...n do
            for j in 0...n do
                if((i+j)%2 == 0)
                    print "#{ev} "
                    ev += 1
                else
                    print "#{od} "
                    od += 1
                end
            end
            print "\n"
        end
    end
}


class Array
  def n
    return length
  end
end

$__cur_readed = []
$__cur_index = 0

def rs
	while $__cur_index >= $__cur_readed.length
		$__cur_readed = gets.split(" ")
		$__cur_index = 0
	end
	$__cur_index+=1
	return $__cur_readed[$__cur_index-1]
end

def ri
	res = rs.to_i
end

def ra2 n
	return Array.new(n) {|i| ri}
end

def ra
	return ra2 ri
end

t = ri

for _ in (1..t)
	a = ra
	res = 0
	for i in (0..a.n-1)
		for j in (i..a.n-1)
			for k in (i..j)
				res+=1
				if a[k] == 0
					res +=1
				end
			end
		end
	end
	puts res
end

gets.to_i.times {
  n = gets.to_i
  xs = gets.split.map(&:to_i).map { _1 > 0 ? 0 : 1 }
  ans = 0
  n.times { |r|
    (r + 1).times { |l|
      ans += r - l + 1
      ans += xs[l..r].sum
    }
  }
  p ans
}

$__cur_readed = []
$__cur_index = 0

def rs
	while $__cur_index >= $__cur_readed.length
		$__cur_readed = gets.split(" ")
		$__cur_index = 0
	end
	$__cur_index+=1
	return $__cur_readed[$__cur_index-1]
end

def ri
	return rs.to_i
end

def ra2 n
	return Array.new(n) {|i| ri}
end

def ra
	return ra2 ri
end

t = ri

for _ in (1..t)
	a = ra
	res = 0
	for i in (0..a.length-1)
		for j in (i..a.length-1)
			for k in (i..j)
				res+=1
				if a[k] == 0
					res +=1
				end
			end
		end
	end
	puts res
end

$__cur_readed = []
$__cur_index = 0

def rs
	while $__cur_index >= $__cur_readed.length
		$__cur_readed = gets.split(" ")
		$__cur_index = 0
	end
	$__cur_index+=1
	return $__cur_readed[$__cur_index-1]
end

def ri
	return rs.to_i
end

def ra2 n
	return Array.new(n) {|i| ri}
end

def ra
	return ra2 ri
end

t = ri

for _ in (1..t)
	a = ra
	res = 0
	for i in (0..a.length-1)
		for j in (i..a.length-1)
			for k in (i..j)
				res+=1
				if a[res] == 0
					res +=1
				end
			end
		end
	end
	puts res
end


t = gets.to_i
t.times{
	n = gets.to_i
	arr = gets.split.map &:to_i
	if arr == arr.sort then
		p 0
	elsif arr[0] == arr.min || arr.last == arr.max then
		p 1
	elsif arr[0] == arr.max && arr.last == arr.min then
		p 3
	else
		p 2
	end
}

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

  if a.each_with_index.all?{|c, i| c == i + 1}
    puts 0
  elsif a.first == 1 || a.last == n
    puts 1
  elsif a.first == n && a.last == 1
    puts 3
  else
    puts 2
  end
end

t = gets.chomp.to_i
t.times do
  f()
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  a = gets.to_s.split.map {|x| x.to_i }
  sa = a.sort
  already_sorted = true
  a.each_with_index do |x, i|
    if sa[i] != x
      already_sorted = false
      break
    end
  end

  if a[0] == sa[-1] && a[-1] == sa[0]
    printf("3\n")
  elsif sa[0] == a[0] || sa[-1] == a[-1]
    printf("%d\n", already_sorted ? 0 : 1)
  else
    printf("2\n")
  end
end

def gli; gets.split.map(&:to_i); end
def gi; gets.to_i; end

gi.times do
  n = gi
  a = gli
  if a == (1..n).to_a
    puts 0
  elsif a[0] == n && a[-1] == 1
    puts 3
  elsif a[0] == 1 || a[-1] == n
    puts 1
  else
    puts 2
  end
end

def gli; gets.split.map(&:to_i); end
def gi; gets.to_i; end

gi.times do
  n = gi
  a = gli
  if a == (1..n).to_a
    puts 0
  elsif a[0] == 1 || a[-1] == n
    puts 1
  else
    puts 2
  end
end

def gli; gets.split.map(&:to_i); end
def gi; gets.to_i; end

gi.times do
  n = gi
  a = gli
  if a.sort == (1..n).to_a
    puts 0
  elsif a[0] == 1 || a[-1] == n
    puts 1
  else
    puts 2
  end
end

def gli; gets.split.map(&:to_i); end
def gi; gets.to_i; end

gi.times do
  _ = gi
  a = gli
  if a.sort == a
    puts 0
  elsif a.min == a[0] || a.max == a[-1]
    puts 1
  else
    puts 2
  end
end

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

  if a.each_with_index.all?{|c, i| c == i + 1}
    puts 0
  elsif a.first == 1 || a.last == n
    puts 1
  else
    puts 2
  end
end

t = gets.chomp.to_i
t.times do
  f()
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  a = gets.to_s.split.map {|x| x.to_i }
  sa = a.sort
  already_sorted = true
  a.each_with_index do |x, i|
    if sa[i] != x
      already_sorted = false
      break
    end
  end

  if sa[0] == a[0] || sa[-1] == a[-1]
    printf("%d\n", already_sorted ? 0 : 1)
  else
    printf("2\n")
  end
end

t = gets.to_i
t.times{
	n = gets.to_i
	arr = gets.split.map &:to_i
	if arr == arr.sort then
		p 0
	elsif arr[0] == arr.min || arr.last == arr.max then
		p 1
	else
		p 2
	end
}


f=->{gets.split.map &:to_i}
n,d=f[]
a,b=f[]
r=(1..n).map{|i|gets(" ").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}
puts r.size,r.map(&:last)*" "

n, d = gets.split.map(&:to_i)
a, b = gets.split.map(&:to_i)
xys = n.times.map{gets.split.map(&:to_i)}
array = xys.map.with_index{|(x, y), i| [i, x * a + y * b]}.sort_by(&:last)
taken = 0
array = array.take_while do |i, cost|
  flag = taken + cost <= d
  taken += cost
  flag
end
puts array.length
puts array.map(&:first).map{|i| i + 1}.join(" ")

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

w = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}

x = w.zip(1..n).sort_by(&:first)

s = 0
r = x.take_while{ |i| d >= s += i.first }
puts r.size
puts r.map(&:last)*" "

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

w = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}

x = w.zip(1..n).sort_by(&:first)

s = 0
r = x.take_while{ |i| d > s += i.first }
puts r.size
puts r.map(&:last)*" "

$n,$d = gets.split.map(&:to_i)
a,b = gets.split.map(&:to_i)

$w = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}

def more i,s,n
    (i...$n).map{ |j|
        next [s,n] if s+$w[j] > $d
        more j+1,s+$w[j],n+[j]
    }.max_by{|i|[i[0],i[1].size]} || [s,n]
end

r = more 0,0,[]
puts r[1].size
puts r[1].map{ |i| i+1 }*" "

$n,$d = gets.split.map(&:to_i)
a,b = gets.split.map(&:to_i)

$w = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}

def more i,s,n
    (i...$n).map{ |j|
        next [s,n] if s+$w[j] > $d
        more j+1,s+$w[j],n+[j]
    }.max_by(&:first) || [s,n]
end

r = more 0,0,[]
puts r[1].size
puts r[1].map{ |i| i+1 }*" "


gets.to_i.times do
    res = 'a' * 200
    puts res
    gets.to_i
    gets.split.map(&:to_i).each do |pos|
        res[pos] = res[pos] == 'a' ? 'b' : 'a'
        puts res
    end
end

require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  a = gets.chomp.split.map(&:to_i)

  s =  'a' * 100
  puts s
  a.each do |num|
    s = s[0, num] + ((s[num].ord + 1 - 97) % 26 + 97).chr * (100 - num)
    puts s
  end
end

def pin(a)
    a.each do |i|
        print((i%26+97).chr)
    end
    puts ""
end

# a = [1,2,3]
# pin(a)

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split.map(&:to_i)

    a = Array.new(51) {0}
    pin(a)
    arr.each do |x|
        for i in x...51
            a[i] += 1
        end
        pin(a)
    end
end

gets.to_i.times do
    res = 'a' * 200
    puts res
    gets.split.map(&:to_i).each do |pos|
        res[pos] = res[pos] == 'a' ? 'b' : 'a'
        puts res
    end
end

gets.to_i.times do
    res = "a" * 200
    puts res
    gets.to_i.times do
        pos = gets.to_i
        res[pos] = res[pos] == 'a' ? 'b' : 'a'
        puts res
    end
end

gets.to_i.times do
    res = 'a' * 200
    puts res
    gets.split.map(&:to_i).each do |pos|
        pos = gets.to_i
        res[pos] = res[pos] == 'a' ? 'b' : 'a'
        puts res
    end
end


num_chars = gets.chomp.to_i
chars = gets.chomp

alphabets = "abcdefghijklmnopqrstuvwxyz"

char_counts = {};

alphabets.each_char {|c| char_counts[c] = 0;}

chars.each_char do |c|
  char_counts[c]  = char_counts[c] + 1;
end

output_string = "";

while (output_string.length != num_chars) do
  sorted_array = char_counts.sort_by {|k,v| v}.reverse

  letter_1 = sorted_array[0][0];
  letter_2 = sorted_array[1][0];
  count_1 = sorted_array[0][1];
  count_2 = sorted_array[1][1];

  output_string += letter_1 * count_1;
  char_counts[letter_1] = 0;

  # if (count_1 > 0)
  #   if (count_1 >= count_2 + 1)
  #     output_string += "#{letter_1}#{letter_2}" * count_2
  #     output_string += letter_1
  #     char_counts[letter_1] -= count_2 + 1
  #     char_counts[letter_2] = 0;
  #   elsif (count_1 == count_2)
  #     output_string += "#{letter_1}#{letter_2}" * count_2
  #     char_counts[letter_1] = 0;
  #     char_counts[letter_2] = 0;
  #   end
  # end
end

puts output_string

num_chars = gets.chomp.to_i
chars = gets.chomp

alphabets = "abcdefghijklmnopqrstuvwxyz"

char_counts = {};

alphabets.each_char {|c| char_counts[c] = 0;}

chars.each_char do |c|
  char_counts[c]  = char_counts[c] + 1;
end

output_string = "";

while (output_string.length != num_chars) do
  sorted_array = char_counts.sort_by {|k,v| v}.reverse

  letter_1 = sorted_array[0][0];
  letter_2 = sorted_array[1][0];
  count_1 = sorted_array[0][1];
  count_2 = sorted_array[1][1];

  if (count_1 > 0)
    if (count_1 >= count_2 + 1)
      output_string += "#{letter_1}#{letter_2}" * count_2
      output_string += letter_1
      char_counts[letter_1] -= count_2 + 1
      char_counts[letter_2] = 0;
    elsif (count_1 == count_2)
      output_string += "#{letter_1}#{letter_2}" * count_2
      char_counts[letter_1] = 0;
      char_counts[letter_2] = 0;
    end
  end
end

puts output_string


priorities = [["rat"], ["woman", "child"], ["man"], ["captain"]]
n = gets.to_i
people = []
n.times { people << gets }

priorities.each do |statuses|
  people.each do |line|
    name, status = line.split
    puts name if statuses.include?(status)
  end
end

gets p
puts %w(rat child|woman man captain).map{|_|$_.scan /(.+) (?:#{_})/}

a=gets.chomp.to_i
a1=[]
a2=[]
a3=[]
a4=""
a.times do
temp=gets.chomp.split(" ")
if temp[1]=="woman" || temp[1]=="child"
a2 << temp[0]
elsif temp[1]=="rat"
a1 << temp[0]
elsif temp[1]=="man"
a3 << temp[0]
else
a4=temp[0]
end
end

puts a1
puts a2
puts a3
puts a4

s = gets(p)
puts %w(rat woman|child man captain).map { |x| s.scan(/(.+) (?:#{x})/) }

k=['rat','child','man','captain']
l=[[],[],[],[]]
1.upto(gets.to_i){m,n=gets.split;k.index(n)==nil ?o=1:o=k.index(n);l[o]<<m }
puts l

gets
data = ARGF.readlines

data.each do |l|
  a = l.split
  puts a[0] if a[1] == 'rat'
end

data.each do |l|
  a = l.split
  puts a[0] if a[1] == 'child' || a[1] == 'woman'
end

data.each do |l|
  a = l.split
  puts a[0] if a[1] == 'man'
end

data.each do |l|
  a = l.split
  puts a[0] if a[1] == 'captain'
end

members = {
    'rat' => [],
    'woman_and_child' => [],
    'man' => [],
    'captain' => [],
}

1.upto(gets.to_i) do
    (name, type) = gets.strip.split(/\s/)

    if ['rat', 'man', 'captain'].include?(type)
        members[type].push(name)
    else
        members['woman_and_child'].push(name)
    end
end

members['rat'].each do |name|
    puts name
end

members['woman_and_child'].each do |name|
    puts name
end

members['man'].each do |name|
    puts name
end

members['captain'].each do |name|
    puts name
end

members = []
for _ in 1..Integer(gets.chomp)
  members << gets.chomp.split
end
grouped = members.inject(Hash.new { |h, k| h[k] = (k == 'child') ? h['woman'] : [] }) { |h, m| h[m[1]] <<= m[0]; h }
%w(rat woman man captain).map.each { |c| puts grouped[c] }

members = []
for _ in 1..Integer(gets.chomp)
  members << gets.chomp.split
end
grouped = members.inject(Hash.new { |h, k| h[k] = (k == 'child') ? h['woman'] : [] }) { |h, m| h[m[1]] <<= m[0]; h }
puts grouped['rat']
puts grouped['woman']
puts grouped['man']
puts grouped['captain']

members = []
for _ in 1..Integer(gets.chomp)
  members << gets.chomp.split
end
grouped = members.inject(Hash.new { |h, k| h[k] = (k == 'child') ? h['woman'] : [] }) { |h, m| h[m[1]] <<= m[0]; h }
%w(rat woman man captain).each { |c| puts grouped[c] }

class Person
    attr_accessor :name, :priority, :number
    def initialize(name, type, number)
        @name = name
        @number = number
        case type
        when "rat"
            @priority = 0
        when "woman", "child"
            @priority = 1
        when "man"
            @priority = 2
        when "captain"
            @priority = 3
        end
    end
end

n = gets.to_i
h = []
n.times do |number|
    name = gets(" ")
    type = gets.chomp
    h.push(Person.new(name, type, number))
end

h.sort! do |a, b|
    if (a.priority == b.priority)
        a.number <=> b.number
    else
        a.priority <=> b.priority
    end
end

h.each{|person| puts person.name}

input = STDIN.read.split("\n").map {|i| i.split}; input.shift

rt, cw, mn, ca,= [], [], [], []

input.each do |i|
	case i.last
	when 'captain'
		ca.push i.first
	when 'man'
		mn.push i.first
	when 'woman'
		cw.push i.first
	when 'child'
		cw.push i.first
	when 'rat'
		rt.push i.first
	end
end

[rt, cw, mn, ca].each.each {|i| puts i}

input = STDIN.read.split("\n").map {|i| i.split}; input.shift

rt, cw, mn, ca,= [], [], [], []

input.each do |i|
	case i.last
	when 'captain'
		ca.push i.first
	when 'man'
		mn.push i.first
	when 'woman', 'child'
		cw.push i.first
	when 'rat'
		rt.push i.first
	end
end

[rt, cw, mn, ca].each.each {|i| puts i}

n = gets.to_i
r, w, m, c = [], [], [], []
n.times do
	a, b = gets.split
	case b
		when 'rat'
			r << a
		when 'child'
			w << a
		when 'woman'
			w << a
		when 'man'
			m << a
		when 'captain'
			c << a
	end
end
puts (r + w + m + c) * "\n"

class Person
    attr_accessor :name, :priority, :number
    def initialize(name, type, number)
        @name = name
        @priority = case type
            when "rat" then 0
            when "child", "woman" then 1
            when "man" then 2
            when "captain" then 3
        end
        @number = number
    end
end

n = gets.to_i
h = []

n.times do |number|
    name = gets(" ")
    type = gets.chomp
    h.push(Person.new(name, type, number))
end

h.sort! do |a, b|
    if (a.priority == b.priority)
        a.number <=> b.number
    else
        a.priority <=> b.priority
    end
end

h.each { |e| puts e.name }

name=[]
type=[]
n=gets.to_i
for i in 0..n-1
        input=gets.split
        name.push(input[0])
        type.push(input[1])
end
for i in 0..n-1
        if type[i]=="rat" then
                puts name[i]
        end
end
for i in 0..n-1
        if type[i]=="child" || type[i]=="woman" then
                puts name[i]
        end
end
for i in 0..n-1
        if type[i]=="man" then
                puts name[i]
        end
end
for i in 0..n-1
        if type[i]=="captain" then
                puts name[i]
        end
end

include Math
require "prime"
require "bigdecimal"
require "bigdecimal/math"
require "matrix"
require "pp"
require "benchmark"

Infinity = 1/0.0
class Matrix;def inspect;empty? ? "Matrix.empty(#{row_size}, #{column_size})":"Matrix[\n#{@rows.map(&:inspect).join("\n")}\n]";end;end
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;end
class String;def to_ia;split.map(&:to_i);end;end
def getia;gets.to_ia;end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end

n=geti
s=[]
n.times{
    s<<gets.split(" ")
}
k=[["rat"],["woman","child"],["man"],["captain"]]
for i in 0...4
    for j in 0...n
        if k[i].include?(s[j][1])
            puts s[j][0]
        end
    end
end

n = gets.to_i
a = Array.new(n)

a.each_index {|i| a[i] = gets.to_s.split(" ") }

rats       = []
womanchild = []
man        = []
captain    = []

for i in 0..(n-1)
  rats       << a[i][0] if a[i][1] == 'rat'
  womanchild << a[i][0] if(a[i][1] == 'woman') or (a[i][1] == 'child')
  man        << a[i][0] if a[i][1] == 'man'
  captain    << a[i][0] if a[i][1] == 'captain'
end

z = rats + womanchild + man + captain

z.each {|y| puts y}

k = %w(rat child man captain)
gets
(a = readlines.map(&:chomp).map!(&:split).each { |x| x[1] = "child" if x[1] == "woman" }).sort_by {|x| [k.index(x[1]), a.index(x)] }.each { |x| puts x[0] }

k = %w(rat child man captain)
gets
a = readlines.map(&:chomp).map!(&:split).each { |x| x[1] = "child" if x[1] == "woman" }
a.sort_by! {|x| [k.index(x[1]), a.index(x)] }
a.each { |x| puts x[0] }

k = %w(rat child man captain)
gets
readlines.map(&:chomp).map(&:split).each { |x| x[1] = "child" if x[1] == "woman" }.sort_by {|x| k.index(x[1]) }.each do |x|
  puts x[0]
end

t = %w(rat woman|child man captain)
n, a = gets.to_i, []
n.times { a << gets.scan(/\w+/) }
a.sort_by { |x| t.index {|i| i =~ /([|]|^)#{x[1]}/} }.each { |x| puts x[0] }

t = %w(rat child man captain)
gets
a = gets(p).split.sort_by {|x| t.index(/ (\w+)$/)}
p a

class Person
    attr_accessor :name, :priority
    def initialize(name, type)
        @name = name
        case type
        when "rat"
            @priority = 0
        when "woman", "child"
            @priority = 1
        when "man"
            @priority = 2
        when "captain"
            @priority = 3
        end
    end
end

n = gets.to_i
h = []
n.times do
    name = gets(" ")
    type = gets.chomp
    h.push(Person.new(name, type))
end

h.sort! {|a, b| a.priority <=> b.priority}

h.each{|person| puts person.name}


#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/1227/B

n = gets.to_i

n.times do
 size = gets.to_i
 input = gets.split(' ').map(&:to_i)
 used = Array.new(size, false)

 maxi = 0
 min = 0
 output = []
 failed = false

 input.each_with_index do |num, i|
  if num < i + 1
   puts "-1"
   failed = true
   break
  end

  maxi = [maxi, num].max

  if !used[maxi-1]
   output << maxi
   used[maxi-1] = true
  else
   while used[min] == true do min += 1 end
   output << min += 1
  end
 end
 puts output.join(' ') unless failed
end

gets.to_i.times do
  n = gets.to_i
  q = gets.split.map &:to_i
  p = [q[0]]*n
  u = 1
  used = [false]*(n + 1)
  used[q[0]] = true
  (1...n).each do |i|
    if q[i] < i + 1
      p = [-1]
      break
    end
    if q[i] == q[i - 1]
      u += 1 while used[u]
      p[i] = u
      used[u] = true
      u += 1
    else
      p[i] = q[i]
      used[q[i]] = true
    end
  end
  puts p.join ' '
end

gets.to_i.times do
  n = gets.to_i
  q = gets.split.map &:to_i
  p = [q[0]]*n
  u = 1
  used = [false]*(n + 1)
  used[q[0]] = true
  (1...n).each do |i|
    if q[i] < i + 1
      p = [-1]
      break
    end
    if q[i] == q[i - 1]
      u += 1 while used[u]
      p[i] = u
      used[u] = true
      u += 1
    else
      p[i] = q[i]
    end
  end
  puts p.join ' '
end


#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/1260/B

n = gets.to_i

n.times do
 input = gets.split(' ').map(&:to_i)
 a = input.max
 b = input.min

 if b < a/2.0
  puts "NO"
 elsif (a % 3 + b % 3) % 3 == 0
  puts "YES"
 else
  puts "NO"
 end
end

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

require 'prime'

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

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

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

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

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

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

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

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

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

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

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

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

n = inp[0]
n.times do
  a,b = inp
  if(a > 2*b or b > 2*a or (a+b)%3 != 0)
    puts "NO"
    next
  end
  sa = (a-b).abs
  min = [a,b].min
  max = [a,b].max
  a = min - sa
  b = max - sa*2
  if(a != b)
    min = [a,b].min
    max = [a,b].max
    a = min - 1
  end
  if(a%3 == 0)
    puts "YES"
  else
    puts "NO"
  end
end

#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/1260/B

n = gets.to_i

n.times do
 input = gets.split(' ').map(&:to_i)
 a = input.max
 b = input.min

 if b < a/2.0
  puts "NO"
  break
 end

 if (a % 3 + b % 3) % 3 == 0
  puts "YES"
 else
  puts "NO"
 end
end

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

require 'prime'

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

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

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

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

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

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

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

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

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

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

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

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

n = inp[0]
n.times do
  a,b = inp
  if(a > 2*b or b > 2*a)
    puts "NO"
    exit
  end
  puts ((a+b)%3==0)? "YES" : "NO"
end


t = gets.to_i
t.times {
  r, c = gets.split.map &:to_i
  for p in (0...r/2)
    stuff = []
    stuff2 = []
    for q in (0...c/2)
      parity = (p + q) % 2
      stuff.push(parity)
      stuff.push((parity + 1) % 2)
      stuff2.push((parity + 1) % 2)
      stuff2.push(parity)
    end
    puts stuff.join(" ")
    puts stuff2.join(" ")
  end
}

step = ->xs {
  y = xs.map { |x| x + x.reverse }
  y + y.reverse
}

matrix = [[1, 0], [0, 1]]
5.times do
  matrix = step[matrix]
end

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  puts matrix.take(n).map { |row| row.take(m).join(" ") }.join("\n")
end