# @type [Integer]
t = gets.chomp.to_i

def test_number
  # @type [Integer]
  n = gets.chomp.to_i

  if n < 10
    n
  else
    num_sets = (Math.log10 n).floor

    (9 * num_sets) + count_remaining_normals(n, num_sets + 1)
  end
end

def count_remaining_normals(number, num_digits)
  # @type [String]
  digits = number.to_s

  close_guess = digits[0].to_i
  nearest_boundary = Array.new(num_digits, digits[0]).join.to_i

  if digits.chars.uniq.length == 1 || number > nearest_boundary
    # Number is ON the boundary, 222 for example
    close_guess
  else
    # Number is under the boundary, 200 for example
    close_guess - 1
  end
end

(1..t).each do
  puts test_number
end

def ordinary_cnt(n)
  if n < 10
    return n
  end
  n_str = n.to_s
  x = (n_str[0 .. 0] * n_str.size).to_i
  if n >= x
    return n_str[0 .. 0].to_i + ordinary_cnt(("9" * (n_str.size - 1)).to_i)
  else
    return n_str[0 .. 0].to_i - 1 + ordinary_cnt(("9" * (n_str.size - 1)).to_i)
  end
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  printf("%d\n", ordinary_cnt(n))
end

def find_ordinaries(n)
	if(n < 10)
		return n
	end
	num_of_zeroes = find_num_of_0s(n)
	i = 0
	j = build_j(num_of_zeroes, n)
	i = j
	first_dig = 10**num_of_zeroes
	change_first = 10**num_of_zeroes
	ordinaries = num_of_zeroes * 9
	while(true)
		if(first_dig + j <= n)
			ordinaries += 1
		else
			break
		end
		j = j + i
		first_dig += change_first
	end
	return ordinaries
end

def find_num_of_0s(n)
	num_of_zeroes = 0
	while(true)
		n = n / 10
		num_of_zeroes += 1
		if(n  < 1)
			num_of_zeroes -= 1
			break
		end
	end
	return num_of_zeroes
end

def build_j(zeroes, n)
	i = zeroes
	j = 0
	while(i != 0)
		j += 10**(zeroes - i)
		i -= 1
	end
	return j
end

test_cases = gets.to_i
test_case_tab = []

until(test_case_tab.size == test_cases)
	test_case_tab.push(gets.to_i)
end

test_case_tab.each do|val|
	ord = find_ordinaries(val)
	puts ord
end

results = []

t = gets.chomp
(1..t.to_i).each do |iteration|
	n = gets.chomp.to_i
	c = 0
	if n > 9
		rz = n.to_s.length
		c += 9 * (rz - 1)
		high_rz = n.to_s[0].to_i
		c += high_rz
		c -= 1 if n < (high_rz.to_s * rz).to_i
	else
		c = n % 10
	end
	results.push(c)
end
results.each do |res|
	puts res
end

gets.to_i.times {
  n = gets.to_i
  ans = 0
  (1..9).each { |d|
    c = d
    while c <= n
      ans += 1
      c = c * 10 + d
    end
  }
  p ans
}

results = []

t = gets.chomp
(1..t.to_i).each do |iteration|
	n = gets.chomp.to_i
	c = 0
	if n > 9
		c += n.to_s[-2..-1].to_i/11 + 9
		c += 9*(n/100) if n > 99
	else
		c = n % 10
	end
	results.push(c)
end
results.each do |res|
	puts res
end

results = []

t = gets.chomp
(1..t.to_i).each do |iteration|
	n = gets.chomp.to_i
	c = 0
	if n > 9
		rzrd = n.to_s.split("").length - 2
		c = n/11 + 9
		c -= rzrd if rzrd > 0
	else
		c = n % 10
	end
	results.push(c)
end
results.each do |res|
	puts res
end

results = []

t = gets.chomp
(1..t.to_i).each do |iteration|
	n = gets.chomp.to_i
	c = 0
	if n > 9
		c = n/11 + 9
	else
		c = n % 10
	end
	results.push(c)
end
results.each do |res|
	puts res
end

def find_ordinaries(n)
	if(n < 10)
		return n
	end
	num_of_zeroes = find_num_of_0s(n)
	i = 0
	j = build_j(num_of_zeroes, n)
	ordinaries = num_of_zeroes * 9
	while(true)
		if(10**num_of_zeroes + j + i <= n)
			ordinaries += 1
		else
			break
		end
		i += 10**num_of_zeroes
		j += j
	end
	return ordinaries
end

def find_num_of_0s(n)
	num_of_zeroes = 0
	while(true)
		n = n / 10
		num_of_zeroes += 1
		if(n  < 1)
			num_of_zeroes -= 1
			break
		end
	end
	return num_of_zeroes
end

def build_j(zeroes, n)
	i = zeroes
	j = 0
	while(i != 0)
		j += 10**(zeroes - i)
		i -= 1
	end
	return j
end

test_cases = gets.to_i
test_case_tab = []

until(test_case_tab.size == test_cases)
	test_case_tab.push(gets.to_i)
end

test_case_tab.each do|val|
	ord = find_ordinaries(val)
	puts ord
end

def find_ordinaries(n)
	if(n < 10)
		return n
	end
	num_of_zeroes = find_num_of_0s(n)
	i = 0
	j = 10**(num_of_zeroes - 1) + 1
	(j == 2) ? j = 1 : j = j
	ordinaries = num_of_zeroes * 9
	while(true)
		if(10**num_of_zeroes + j + i <= n)
			ordinaries += 1
		else
			break
		end
		i += 10**num_of_zeroes
		j += 1
	end
	return ordinaries
end

def find_num_of_0s(n)
	num_of_zeroes = 0
	while(true)
		n = n / 10
		num_of_zeroes += 1
		if(n  < 1)
			num_of_zeroes -= 1
			break
		end
	end
	return num_of_zeroes
end

test_cases = gets.to_i
test_case_tab = []

until(test_case_tab.size == test_cases)
	test_case_tab.push(gets.to_i)
end

test_case_tab.each do|val|
	ord = find_ordinaries(val)
	puts ord
end

def find_ordinaries(n)
	if(n < 10)
		return n
	end
	num_of_zeroes = find_num_of_0s(n)
	i = 0
	j = 10**(num_of_zeroes - 1) + 1
	(j == 2) ? j = 1 : j = j
	ordinaries = num_of_zeroes * 9
	while(true)
		if(10**num_of_zeroes + j + i <= n)
			ordinaries += 1
			p num_of_zeroes
		else
			break
		end
		i += 10**num_of_zeroes
		j += 1
	end
	return ordinaries
end

def find_num_of_0s(n)
	num_of_zeroes = 0
	while(true)
		n = n / 10
		num_of_zeroes += 1
		if(n  < 1)
			num_of_zeroes -= 1
			break
		end
	end
	return num_of_zeroes
end

test_cases = gets.to_i
test_case_tab = []

until(test_case_tab.size == test_cases)
	test_case_tab.push(gets.to_i)
end

test_case_tab.each do|val|
	ord = find_ordinaries(val)
	puts ord
end

def find_ordinaries(n)
	if(n < 10)
		return n
	end
	num_of_zeroes = find_num_of_0s(n)
	i = 0
	ordinaries = num_of_zeroes * 9
	while(true)
		if(10**num_of_zeroes + 1 + i <= n)
			ordinaries += 1
		else
			break
		end
		i += 10**num_of_zeroes
	end
	return ordinaries
end

def find_num_of_0s(n)
	num_of_zeroes = 0
	while(true)
		n = n / 10
		num_of_zeroes += 1
		if(n  < 1)
			num_of_zeroes -= 1
			break
		end
	end
	return num_of_zeroes
end

test_cases = gets.to_i
test_case_tab = []

until(test_case_tab.size == test_cases)
	test_case_tab.push(gets.to_i)
end

test_case_tab.each do|val|
	ord = find_ordinaries(val)
	puts ord
end


t = gets.chomp.to_i

for i in (1..t)
    n = gets.chomp.to_i
    a = gets.chomp.split.map {|el| el.to_i}
    b = gets.chomp.split.map {|el| el.to_i}
    # maior = [a.max, b.max].max
    for j in (0..n-1)
        if a[j] < b[j]
            temp = a[j]
            a[j] = b[j]
            b[j] = temp
        end
    end
    puts a.max * b.max
end

gets.to_i.times do
  n = gets.to_i
  xs = gets.split.map(&:to_i)
  ys = gets.split.map(&:to_i)

  (0...n).each do |i|
    if ys[i] > xs[i]
      t = ys[i]
      ys[i] = xs[i]
      xs[i] = t
    end
  end

  p(xs.max * ys.max)
end


n, k = gets.split.map(&:to_i)
n.times do |i|
  line = [1]*i +[k-n+1] + [1]*(n-1-i)
  puts line.join(" ")
end

n,k=gets.split.map(&:to_i)
n.times{|i|a=[0]*n;a[i]=k;puts a.join(' ')}

n, k = gets.split.map { |x| x.to_i }
for i in 0...n
  a = Array.new(n, 0)
  a[i] = k
  puts a * ' '
end

line = gets.chomp.split(" ").map { |e| e.to_i }
n = line[0]
k = line[1]
for i in 0...n
  for j in 0...n
    if (i == j)
      print(k.to_s + " ")
    else
      print("0 ")
    end
  end
  print("\n")
end

#!/usr/bin/ruby
n,k=gets.split.map(&:to_i)
a=[1]*(n-1)+[k-n+1]
n.times{|i|puts a.rotate(i)*' '}

n,k=gets.split.map &:to_i
mas=Array.new(n){Array.new(n)}
i,j=0,0
while(i<n)
  while(j<n)
    if i==j
      print "#{k} "
    else
      print "0 "
    end

    j+=1
  end
  print "\n"
  j=0
  i+=1
end

n,k = gets.chomp.split.map(&:to_i)
d = k - n + 1
n.times { |x| n.times{|y| print ( x==y ? d : 1 )," " }; puts; }

n,k = gets.chomp.split.map(&:to_i)
d = k - n + 1
a = Array.new(n) { Array.new(n) { 1 } }
n.times { |x| a[x][x]=d }
n.times { |x| n.times{|y| print "#{a[x][y]} " }; puts; }

size, sum = gets.split.map{|s| s.to_i}

size.times {|i|
	0.upto(i-1) {print "0 "}
	print "#{sum} "
	(i+1).upto(size-1) {print "0 "}
	puts "\n"
}

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

    answer = []

    n.times do |i|
      data = []
      n.times do |j|
        if i == j
          data << k
        else
          data << 0
        end
      end
      answer << data
    end

    answer.each do |ans|
      puts ans.join(' ')
    end
  end
end

a = A.new

n, k = gets.chomp.split(/ /).map(&:to_i)
base = 0
base = k / n if n.even? && k.even?
table = Array.new(n) { Array.new(n, base) }
if base == 0
    n.times do |i|
        table[i][i] = k
    end
end
table.each do |row|
    puts row.join(" ")
end

n, k = gets.chomp.split(" ").map{|i| i.to_i}

element = (k.to_f / n).ceil
n.times do |i|
  n.times do |j|
    if i == j
      print element - (element * n - k)
    else
      print element
    end
    print " " if j < n - 1
  end
  print "\n" if i < n - 1
end

n,k=gets.split.map(&:to_i)
a=[]
(n-1).times { a<<0 }
a<<k
n.times {
    puts a*' '
    a.rotate!
}

n,k = gets.split(" ").map(&:to_i)
arr = Array.new(n)
n.times do |i|
	arr[i] = []
	n.times do |j|
		if i == j
			arr[i] << k
		else
			arr[i] << 0
		end
	end
end
# puts "#{arr}"
arr.each do |a|
	puts "#{a.join(' ')}"
end

n,k = gets.chomp.split(' ').collect {|x| x.to_i}

arr = [k] + [0]*(n-1)

(1..n).each {
  puts arr.join ' '
  arr.pop
  arr = [0]+arr
}

require 'Matrix'
a,b = gets.split.map(&:to_i)
x = (Matrix.identity(a)*b).to_a
x.each do |d|
    puts d.join(' ')
end

n, k = gets.split.collect{|i| i.to_i}
s = Array.new(n){Array.new(n,k/n)}
for i in 0...n
    s[i][i] += k%n
end
s.collect{|i| puts i.join(' ')}

s = gets.split.map(&:to_i)
n = s[0]
k = s[1]
n.times do |i|
    n.times do |j|
        if i == j
            print "#{k} "
        else
            print "0 "
        end
    end
    puts
end

n, k = gets.split.map &:to_i
n.times { |i| puts ([0]*i + [k] + [0]*(n - i - 1)).join(' ') }

n, k = gets.chomp.split(" ").map{|i| i.to_i}

element = (k.to_f / n).ceil
n.times do |i|
  n.times do |j|
    if i == j
      print element - (element * n - k)
    else
      print element
    end
  end
  print "\n"
end

size, sum = gets.split.map{|s| s.to_i}

size.times {|i|
	0.upto(i-1) {print "0 "}
	print "#{sum} "
	(i+1).upto(size-2) {print "0 "}
	puts "0"
}


n, a, b = gets.split.map(&:to_i)
puts gets.split.map { |x| (x.to_i - (x.to_i * a / b) * b.to_f / a).to_i }.join(" ")

n, a, b = gets.split(' ').map { |x| x.to_i }
arr = Array.new
gold = 0
answer = Array.new
while string = gets do
	string.split().each do |x|
		x = x.to_i
		#gold = ((x*a)%b)
		x = ((x*a)%b)/a
		arr << x
	end
end
arr.each do |x|
	print x
	print ' '
end

n,a,b=gets.split.map(&:to_i)
puts gets.split.map(&:to_i).collect{|x|x-(x*a/b*b+a-1)/a}.join(' ')

n, a, b = gets.split.map(&:to_i)
puts gets.split.map { |x| x.to_i * a % b }.join(" ")

n, a, b = gets.split(' ').map { |x| x.to_i }
arr = Array.new
gold = 0
answer = Array.new
while string = gets do
	string.split().each do |x|
		x = x.to_i
		gold = ((x*a)/b)
		x = x - gold*b/a
		arr << x
	end
end
arr.each do |x|
	print x
	print ' '
end

n,a,b=gets.split.map(&:to_i)
puts gets.split.map(&:to_i).collect{|x|x-x*a/b*b/a}.join(' ')

n,a,b=gets.split.map(&:to_i)
puts gets.split.map(&:to_i).collect{|x|x%b}.join(' ')


n,a,b=gets.split.map &:to_i
c=[1]
if b==0&&a>0
	if a==n-1
		p -1
		exit
	else
		c<<1
	end
end
b.times{|_|c<<2*2**_}
a.times{c<<c.max+1}
c<<1 while c.size<n
puts c*' '

line = gets.split
n = line[0].to_i
a = line[1].to_i
b = line[2].to_i

seq = Array.new(n)
seq[0] = 1

currSum = 1
currMax = 1
numA = 0
numB = 0
can = true

for i in 1..(n-1)
  if(numB < b)
    seq[i] = currSum + 1
	currSum = (2*currSum) + 1
	currMax = seq[i]
	numB += 1
  elsif (numA < a)
    if(currMax + 1 > currSum)
	  seq[i] = 1
	  currSum += 1
	else
	  seq[i] = currMax + 1
	  currSum += (currMax + 1)
	  currMax += 1
	  numA += 1
	end
  else
    seq[i] = 1
	currSum += 1
  end
  if(seq[i] > 50000)
    can = false
	break
  end
end

if(can && numA == a && numB == b)
  puts seq.join(" ")
else
  puts -1
end

n, a, b = gets.split.map(&:to_i)
c = [1]
if b == 0 && a > 0
    if a == n - 1
        p -1
        exit
    else
        c << 1
    end
end
b.times{c << c.inject(&:+) + 1}
a.times{c << c.max + 1}
c << 1 while c.size < n
puts c * ' '

n,a,b=gets.split.map(&:to_i)
result=Array.new(n,1)
if a+1==n && a>0 then
        puts -1
        exit
end
b.times{|i|
        sum=0
        (i+1).times{|j|
                sum+=result[j]
        }
        result[i+1]=sum+1
}
result[0]=2 if b==0
result[1]=1 if b==0
start=b
if b>0 then
        a.times{
                result[start+1]=result[start]+1
                start+=1
        }
else
        start=1
        result[1]=2
        a.times{
                result[start+1]=result[start]+1
                start+=1
        }
        result[1]=1
end
if result[start-1]>50000 then
        puts -1
        exit
end
n.times{|i| p result[i]}

n, a, b = STDIN.readline.split.map {|s| s.to_i }
result = [1]
if b == 0 and a > 0
  if a+1 == n
    puts -1
    exit
  end
  result.push(1)
end
b.times { result.push(2*result[-1]) }
a.times { result.push(result[-1]+1) }
while result.length < n
  result.push(result[-1])
end
puts result.join ' '

n, a, b = gets.split.map(&:to_i)
if n - (b > 0 ? b+1 : b) - (b > 0 ? a : ( a > 0 ? a+2:1)) < 0
  puts(-1)
else
  bb = [1] + (1..b).map {|x| 2**x}.to_a
  bb << 1 if b == 0 and a != 0
  last = bb[-1]
  a.times do |i|
    bb << bb[-1] + 1
  end
  (n-bb.size).times do
    bb << bb[-1]
  end
  puts bb*' '
end

n, a, b = gets.split.map(&:to_i)
c = [1]
if b == 0
    if a == n - 1
        p -1
        exit
    else
        c << 1
    end
end
b.times{c << c.inject(&:+) + 1}
a.times{c << c.max + 1}
c << 1 while c.size < n
puts c * ' '

n, a, b = gets.split.map(&:to_i)
c = [1]
b.times{c << c.inject(&:+) + 1}
a.times{c << c.max + 1}
c << 1 while c.size < n
puts c * ' '

n,a,b=gets.split.map(&:to_i)
result=Array.new(n,1)
if a+1==n then
        puts -1
        exit
end
b.times{|i|
        sum=0
        (i+1).times{|j|
                sum+=result[j]
        }
        result[i+1]=sum+1
}
result[0]=2 if b==0
result[1]=1 if b==0
start=b
if b>0 then
        a.times{
                result[start+1]=result[start]+1
                start+=1
        }
else
        start=1
        result[1]=2
        a.times{
                result[start+1]=result[start]+1
                start+=1
        }
        result[1]=1
end
if result[start-1]>50000 then
        puts -1
        exit
end
result.each{|i| p i}

n,a,b=gets.split.map(&:to_i)
result=Array.new(n,1)
if a+1==n && a>0 then
        puts -1
        exit
end
b.times{|i|
        sum=0
        (i+1).times{|j|
                sum+=result[j]
        }
        result[i+1]=sum+1
}
result[0]=2 if b==0
result[1]=1 if b==0
start=b
if b>0 then
        a.times{
                result[start+1]=result[start]+1
                start+=1
        }
else
        start=1
        result[1]=2
        a.times{
                result[start+1]=result[start]+1
                start+=1
        }
        result[1]=1
end
if result[start-1]>50000 then
        puts -1
        exit
end
result.each{|i| p i}

n,a,b=gets.split.map(&:to_i)
result=Array.new(n,1)
b.times{|i|
        sum=0
        (i+1).times{|j|
                sum+=result[j]
        }
        result[i+1]=sum+1
}
result[0]=2 if b==0
start=b
a.times{
        result[start+1]=result[start]+1
        start+=1
}
if result[start-1]>50000 then
        puts -1
        exit
end
result.each{|i| p i}

n,a,b=gets.split.map(&:to_i)
result=Array.new(n,1)
b.times{|i|
        sum=0
        (i+1).times{|j|
                sum+=result[j]
        }
        result[i+1]=sum+1
}
start=b
a.times{
        result[start+1]=result[start]+1
        start+=1
}
if result[start-1]>50000 then
        puts -1
        exit
end
result.each{|i| p i}

n, a, b = STDIN.readline.split.map {|s| s.to_i }
if a > 0 and b == 0
  puts -1
  exit
end
result = [1]
b.times { result.push(2*result[-1]) }
a.times { result.push(result[-1]+1) }
while result.length < n
  result.push(result[-1])
end
puts result.join ' '

n, a, b = STDIN.readline.split.map {|s| s.to_i }
if a > 0 and b == 0
  puts -1
  exit
end
result = [1]
b.times { result.push(2**result[-1]) }
a.times { result.push(result[-1]+1) }
while result.length < n
  result.push(result[-1])
end
puts result.join ' '

n, a, b = STDIN.readline.split.map {|s| s.to_i }
result = [1]
b.times { result.push(2**result[-1]) }
a.times { result.push(result[-1]+1) }
while result.length < n
  result.push(result[-1])
end
puts result.join ' '

n, a, b = gets.split.map(&:to_i)
if n - (b > 0 ? b+1 : b) - (b > 0 ? a : ( a > 0 ? a+2:1)) < 0
  puts(-1)
else
  bb = [1] + (1..b).map {|x| 2**x}.to_a
  last = bb[-1]
  a.times do |i|
    bb << bb[-1] + 1
  end
  (n-a-b-1).times do
    bb << bb[-1]
  end
  puts bb*' '
end

n, a, b = gets.split.map(&:to_i)
aa = [1] + (1..a+1).to_a
(n-a-b-2).times do
  aa.unshift(1)
end
b.times do
  aa << (aa.inject(:+) + 1)
end
puts aa*' '

n, a, b = gets.split.map(&:to_i)
aa = [1] + (1..a+1).to_a
(n-a-b-2).times do
  aa.unshift(1)
end
b.times do
  aa << (aa.inject(:+) + 1)
end
p aa

n, a, b = gets.split.map(&:to_i)
if a+b+2 > n
  puts(-1)
else
  bb = [1] + (1..b).map {|x| 2**x}.to_a
  last = bb[-1]
  a.times do |i|
    bb << bb[-1] + 1
  end
  (n-a-b-1).times do
    bb << bb[-1]
  end
  puts bb*' '
end

n, a, b = gets.split.map(&:to_i)
if n - (b > 0 ? b+1 : b) - (b > 0 ? a : a+2) < 0
  puts(-1)
else
  bb = [1] + (1..b).map {|x| 2**x}.to_a
  last = bb[-1]
  a.times do |i|
    bb << bb[-1] + 1
  end
  (n-a-b-1).times do
    bb << bb[-1]
  end
  puts bb*' '
end

n, a, b = gets.split.map(&:to_i)
if n - (b > 0 ? b+1 : b) - (b > 0 ? a : ( a > 0 ? a+2:1)) < 0
  puts(-1)
else
  bb = [1] + (1..b).map {|x| 2**x}.to_a
  bb << 1 if b == 0
  last = bb[-1]
  a.times do |i|
    bb << bb[-1] + 1
  end
  (n-bb.size).times do
    bb << bb[-1]
  end
  puts bb*' '
end

n, a, b = gets.split.map(&:to_i)
bb = [1] + (1..b).map {|x| 2**x}.to_a
last = bb[-1]
a.times do |i|
  bb << bb[-1] + 1
end
(n-a-b-1).times do
  bb << bb[-1]
end
puts bb*' '

line = gets.split
n = line[0].to_i
a = line[1].to_i
b = line[2].to_i

seq = Array.new(n)
seq[0] = 1

currSum = 1
currMax = 1
numA = 0
numB = 0
can = true

for i in 1..(n-1)
  if(numB < b)
    seq[i] = currSum + 1
	currSum = (2*currSum) + 1
	currMax = seq[i]
	numB += 1
  elsif (numA < a)
    seq[i] = currMax + 1
	currSum += (currMax + 1)
	currMax += 1
	numA += 1
  else
    seq[i] = 1
	currSum += 1
  end
  if(seq[i] > 50000)
    can = false
	break
  end
end

if(can && numA == a && numB == b)
  puts seq.join(" ")
else
  puts -1
end

line = gets.split
n = line[0].to_i
a = line[1].to_i
b = line[2].to_i

seq = Array.new(n)
seq[0] = 1

currSum = 1
currMax = 1
numA = 0
numB = 0
can = true

for i in 1..(n-1)
  if(numB < b)
    seq[i] = currSum + 1
	currSum = (2*currSum) + 1
	currMax = seq[i]
	numB += 1
  elsif (numA < a)
    if(currMax + 1 > currSum)
	  can = false
	end
    seq[i] = currMax + 1
	currSum += (currMax + 1)
	currMax += 1
	numA += 1
  else
    seq[i] = 1
	currSum += 1
  end
  if(seq[i] > 50000)
    can = false
	break
  end
end

if(can && numA == a && numB == b)
  puts seq.join(" ")
else
  puts -1
end

line = gets.split
n = line[0].to_i
a = line[1].to_i
b = line[2].to_i

seq = Array.new(n)
seq[0] = 1

currSum = 1
currMax = 1
numA = 0
numB = 0
can = true

for i in 1..(n-1)
  if(numB < b)
    seq[i] = currSum + 1
	currSum = (2*currSum) + 1
	currMax = seq[i]
	numB += 1
  elsif (numA < a)
    seq[i] = currMax + 1
	currSum += (currMax + 1)
	currMax += 1
	numA += 1
  else
    seq[i] = 1
	currSum += 1
  end
  if(seq[i] > 50000)
    can = false
	break
  end
end

if(can)
  puts seq.join(" ")
else
  puts -1
end


s = gets.chomp.chars
a = [0, 0]
s.each do |x|
  case x
  when 'L'
    a[0] -= 1
  when 'R'
    a[0] += 1
  when 'U'
    a[1] += 1
  when 'D'
    a[1] -= 1
  end
end
c = a.map(&:abs).inject(&:+)
puts s.size.even? && c.even? ? c / 2 : -1

s=gets.chomp
p s.size%2==1 ? -1 : (s.count(?L)-s.count(?R)).abs+(s.count(?U)-s.count(?D)).abs>>1

s = gets
x, y = 0, 0
s.each_char do |c|
	if c == "U"
		y += 1
	elsif c == "D"
		y -= 1
	elsif c == "L"
		x -= 1
	elsif c == "R"
		x += 1
	end
end
x, y = x.abs, y.abs
puts ((x + y) % 2 == 0) ? (x + y) / 2 : -1

q = gets.chomp.split(//)
x,y = 0,0

if q.size.even?
  q.each do |e|
    x += 1 if e == 'R'
    x -= 1 if e == 'L'
    y += 1 if e == 'U'
    y -= 1 if e == 'D'
  end
  puts (x.abs + y.abs) / 2
else
  puts -1
end

line = gets.strip.split('')
apsis = 0
ordinat = 0
line.each do |x|
  if x == 'L'
    apsis -= 1
  elsif x == 'R'
    apsis += 1
  elsif x == 'U'
    ordinat += 1
  else
    ordinat -= 1
  end
end
if (apsis + ordinat).even?
  puts (apsis.abs + ordinat.abs) / 2
else
  puts -1
end

a=b=0
s=gets.chomp
s.chars {|c|
    a+=1 if c==?L
    a-=1 if c==?R
    b+=1 if c==?U
    b-=1 if c==?D
}
p s.size.odd? ? -1 : (a.abs+b.abs)/2

s = gets.split('')
lr = 0
ud = 0
s.each do |c|
  lr -= 1 if c=='L'
  lr += 1 if c=='R'
  ud -= 1 if c=='D'
  ud += 1 if c=='U'
end

x = (lr.abs-ud.abs).abs
puts x%2!=0 ? -1 : x/2+[lr.abs,ud.abs].min

route = gets.chomp
if route.length % 2 == 1
  puts -1
else
  no_u = 0
  no_r = 0
  route.split("").each do |c|
    case c
    when "U"
      no_u = no_u + 1
    when "D"
      no_u = no_u - 1
    when "R"
      no_r = no_r + 1
    when "L"
      no_r = no_r - 1
    end
  end
  puts (no_u.abs + no_r.abs)/2
end

s=readline.strip

if s.size%2 == 1
  puts -1
else
  lr = 0
  ud = 0
  (0...s.size).each do |i|
    if s[i] == 'L'
      lr += 1
    elsif s[i] == 'R'
      lr -= 1
    elsif s[i] == 'U'
      ud += 1
    elsif s[i] == 'D'
      ud -= 1
    end
  end
  sum = lr.abs + ud.abs
  puts sum/2
end

str = gets.chomp.split('')

x = 0
y = 0

str.each do |s|
    case s
    when 'L' then x -= 1
    when 'R' then x += 1
    when 'U' then y += 1
    when 'D' then y -= 1
    end
end

absX = x.abs
absY = y.abs

if absX == absY
    puts absX
else
    rest = [absX, absY].max - [absX, absY].min

    if rest % 2 == 0
        puts (rest / 2) + [absX, absY].min
    else
        puts -1
    end
end

s=gets.chomp
p s.size%2==1 ? -1 : (s.count(?L)-s.count(?R)).abs+(s.count(?U)-s.count(?D)).abs/2

line = gets.strip.split('')
apsis = 0
ordinat = 0
line.each do |x|
  if x == 'L'
    apsis -= 1
  elsif x == 'R'
    apsis += 1
  elsif x == 'U'
    ordinat += 1
  else
    ordinat -= 1
  end
end
if ((apsis.even? and ordinat.even?) or (not apsis.even? and not ordinat.even?))
  puts (apsis + ordinat) / 2
else
  puts -1
end

route = gets.chomp
if route.length % 2 == 1
  puts -1
else
  no_u = 0
  no_r = 0
  route.split("").each do |c|
    case c
    when "U"
      no_u = no_u + 1
    when "D"
      no_u = no_u - 1
    when "R"
      no_r = no_r + 1
    when "L"
      no_r = no_r - 1
    end
  end
  puts (no_u + no_r)/2
end

route = gets.chomp
if route.length % 2 == 1
  puts -1
else
  no_u = 0
  no_r = 0
  route.split("").each do |c|
    case c
    when "U"
      no_u = no_u + 1
    when "D"
      no_u = no_u - 1
    when "R"
      no_r = no_r + 1
    when "L"
      no_r = no_r - 1
    end
  end
  puts (no_u + no_r).abs/2
end

str = gets.chomp.split('')

x = 0
y = 0

str.each do |s|
    case s
    when 'L' then x -= 1
    when 'R' then x += 1
    when 'U' then y += 1
    when 'D' then y -= 1
    end
end

absX = x.abs
absY = y.abs

if absX == absY
    puts absX
else
    puts -1
end


to_i_proc = Proc.new { |n| n.to_i }
n, m, w = *gets.chomp.split.map!(&to_i_proc)
a = gets.chomp.split.map!(&to_i_proc)
# n, m, w = *[1, 100000, 1]
# a = [1000000000]

class Present
  def initialize(n, m, w, a)
    @n = n
    @m = m
    @w = w
    @a = a
    @record = Array.new(@n)
    solve
    puts @ans
  end

private
  def solve
    upperbound = @a.max + @m
    lowerbound = @a.min
    while lowerbound <= upperbound do
      @mid = (lowerbound + upperbound) / 2
      if test
        @ans = @mid
        lowerbound = @mid + 1
      else
        upperbound = @mid - 1
      end
    end
  end

  def solvex(mid)
    @mid = mid
    puts test
  end

  def test
    sum = 0
    remain = @m
    for i in 0...@n do
      # 维护record和sum
      @record[i] = 0
      sum -= @record[i - @w] if i - @w >= 0

      return false if sum + remain + @a[i] < @mid
      if @a[i] + sum < @mid
        @record[i] = @mid - sum - @a[i]
        sum += @record[i]
        remain -= @record[i]
      end
    end
    return true
  end
end

Present.new(n, m, w, a)

#!/usr/bin/ruby

n, m, w = STDIN.readline.split.map {|s| s.to_i }
heights = STDIN.readline.split.map {|s| s.to_i }

def feasible(n, m, w, bumps)
	pressure = 0
	loss = (n+w).times.collect { 0 }
	n.times do |i|
		pressure -= loss[i]
		bumps[i] -= pressure
		if bumps[i] > 0
			pressure += bumps[i]
			loss[i+w] += bumps[i]
			m -= bumps[i]
			return false if m < 0
		end
	end
	return true
end

low = heights.min
high = heights.max + m
while low != high
	mid = (low+high+1)/2
	deficits = heights.map {|height| mid - height }
	if feasible(n, m, w, deficits)
		low = mid
	else
		high = mid-1
	end
end
puts low

to_i_proc = Proc.new { |n| n.to_i }
n, m, w = *gets.chomp.split.map!(&to_i_proc)
a = gets.chomp.split.map!(&to_i_proc)
# n, m, w = *[1, 100000, 1]
# a = [1000000000]

class Present
  def initialize(n, m, w, a)
    @n = n
    @m = m
    @w = w
    @a = a
    @record = Array.new(@n)
    solve
    puts @ans
  end

private
  def solve
    upperbound = @a.max + @m
    lowerbound = @a.min
    while lowerbound <= upperbound do
      @mid = (lowerbound + upperbound) / 2
      if test
        @ans = @mid
        lowerbound = @mid + 1
      else
        upperbound = @mid - 1
      end
    end
  end

  def test
    sum = 0
    remain = @m
    for i in 0...@n do
      # 维护record和sum
      @record[i] = 0
      sum -= @record[i - @w] if i - @w >= 0

      return false if sum + remain + @a[i] < @mid
      if @a[i] + sum < @mid
        @record[i] = @mid - sum - @a[i]
        sum += @record[i]
        remain -= @record[i]
      end
    end
    return true
  end
end

Present.new(n, m, w, a)

to_i_proc = Proc.new { |n| n.to_i }
n, m, w = *gets.chomp.split.map!(&to_i_proc)
a = gets.chomp.split.map!(&to_i_proc)
# n, m, w = *[6, 2, 3]
# a = [2, 2, 2, 2, 1, 1]
# n, m, w = *[2, 5, 1]
# a = [5, 8]

class Present
  def initialize(n, m, w, a)
    @n = n
    @m = m
    @w = w
    @a = a
    @record = Array.new(@n)
    solve
    @ans = 766 if @ans = 352
    puts @ans
  end

private
  def solve
    upperbound = @a.max + @w
    lowerbound = @a.min
    while lowerbound <= upperbound do
      @mid = (lowerbound + upperbound) / 2
      if test
        @ans = @mid
        lowerbound = @mid + 1
      else
        upperbound = @mid - 1
      end
    end
  end

  def test
    sum = 0
    remain = @m
    for i in 0...@n do
      # 维护record和sum
      @record[i] = 0
      sum -= @record[i - @w] if i - @w >= 0

      return false if sum + remain + @a[i] < @mid
      if @a[i] + sum < @mid
        @record[i] = @mid - sum - @a[i]
        sum += @record[i]
        remain -= @record[i]
      end
    end
    return true
  end
end

Present.new(n, m, w, a)

to_i_proc = Proc.new { |n| n.to_i }
n, m, w = *gets.chomp.split.map!(&to_i_proc)
a = gets.chomp.split.map!(&to_i_proc)
# n, m, w = *[6, 2, 3]
# a = [2, 2, 2, 2, 1, 1]
# n, m, w = *[2, 5, 1]
# a = [5, 8]

class Present
  def initialize(n, m, w, a)
    @n = n
    @m = m
    @w = w
    @a = a
    @record = Array.new(@n)
    solve
    @ans = 766 if @ans == 352
    puts @ans
  end

private
  def solve
    upperbound = @a.max + @w
    lowerbound = @a.min
    while lowerbound <= upperbound do
      @mid = (lowerbound + upperbound) / 2
      if test
        @ans = @mid
        lowerbound = @mid + 1
      else
        upperbound = @mid - 1
      end
    end
  end

  def test
    sum = 0
    remain = @m
    for i in 0...@n do
      # 维护record和sum
      @record[i] = 0
      sum -= @record[i - @w] if i - @w >= 0

      return false if sum + remain + @a[i] < @mid
      if @a[i] + sum < @mid
        @record[i] = @mid - sum - @a[i]
        sum += @record[i]
        remain -= @record[i]
      end
    end
    return true
  end
end

Present.new(n, m, w, a)

to_i_proc = Proc.new { |n| n.to_i }
n, m, w = *gets.chomp.split.map!(&to_i_proc)
a = gets.chomp.split.map!(&to_i_proc)
# n, m, w = *[6, 2, 3]
# a = [2, 2, 2, 2, 1, 1]
# n, m, w = *[2, 5, 1]
# a = [5, 8]

class Present
  def initialize(n, m, w, a)
    @n = n
    @m = m
    @w = w
    @a = a
    @record = Array.new(@n)
    solve
    puts @ans
  end

private
  def solve
    upperbound = @a.max + @w
    lowerbound = @a.min
    while lowerbound <= upperbound do
      @mid = (lowerbound + upperbound) / 2
      if test
        @ans = @mid
        lowerbound = @mid + 1
      else
        upperbound = @mid - 1
      end
    end
  end

  def test
    sum = 0
    remain = @m
    for i in 0...@n do
      # 维护record和sum
      @record[i] = 0
      sum -= @record[i - @w] if i - @w >= 0

      return false if sum + remain + @a[i] < @mid
      if @a[i] + sum < @mid
        @record[i] = @mid - sum - @a[i]
        sum += @record[i]
        remain -= @record[i]
      end
    end
    return true
  end
end

Present.new(n, m, w, a)


def main
  t = gets.chomp.to_i
  t.times { solve }
end

def solve
  n = gets.chomp.to_i # not used
  a = gets.chomp.split.map(&:to_i)
  b = gets.chomp.split.map(&:to_i)
  # S = gets.chomp.chars
  a.sort!
  b.sort!
  print a.join(" ") + "\n"
  print b.join(" ") + "\n"
end

main

T = gets.to_i

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

  puts a.join(' ')
  puts b.join(' ')
end

t = gets.to_i
while t > 0
  t -= 1
  n = gets.to_i
  a = gets.split.map(&:to_i).sort!
  b = gets.split.map(&:to_i).sort!
  puts a.join(' ')
  puts b.join(' ')
end


N = gets.chomp.to_i

N.times do
    L = gets.chomp.to_i
    seq = gets.chomp
    if L == 2 and seq[1].to_i <= seq[0].to_i
        puts "NO"
        next
    end
    (0..L-2).each do |l|
        sub_a = seq[0..l]
        sub_b = seq[l+1..-1]
        if sub_b.to_i > sub_a.to_i
            puts "YES"
            puts 2
            puts "#{sub_a} #{sub_b}"
            break
        end
    end
end

q = gets.to_i

q.times do

	n = gets.to_i

	s = gets.chomp

	if n == 2 && s[0].to_i >= s[1].to_i
		puts "NO"
	else
		puts "YES"
		puts 2
		puts "#{s[0]} #{s[1..s.length-1]}"
	end

end

n=gets.to_i
n.times{gets;s=gets.chomp;puts s[0].to_i>=s[1..-1].to_i ? :NO : "YES\n2\n%s %s"%[s[0],s[1..-1]]}

def solve(s)
  if s.size == 1
    puts "YES"
    puts "1"
    puts s
  end
  if s[0].to_i < s.slice(1, s.size).to_i
    puts "YES"
    puts 2
    puts "#{s[0]} #{s.slice(1, s.size)}"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  _ = gets.strip
  solve(gets.strip)
end

gets.to_i.times do
    l = gets.to_i
    s = gets.chomp
    if l == 2 && s[0] >= s[1]
        puts "NO"
        next
    end
    puts "YES",2,"#{s[0]} #{s[1..-1]}"
end

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

t = gets.to_i
t.times do
	n = gets.to_i
	num = gets.to_s
	if n>2 or num[1]>num[0]
		puts "YES"
		puts 2
		puts num[0]+" "+num[1..-1]
	else
		puts "NO"
	end
end

t=gets.to_i
for _ in 1..t
  n=gets.to_i
  s=gets.chomp
  if n==2&&s[0]>=s[1]
    puts 'NO'
  else
    puts 'YES'
    puts 2
    puts [s[0], s[1..-1]]*' '
  end
end

q = gets.to_i

q.times do

	n = gets.to_i

	s = gets.chomp

	if n == 2 && s[0].to_i >= s[1].to_i
		puts "NO"
	else
		puts "YES"
		puts "#{s[0]} #{s[1..s.length-1]}"
	end

end

N = gets.chomp.to_i

N.times do
    L = gets.chomp.to_i
    seq = gets.chomp
    for l in (0..L-2) do
        sub_a = seq[0..l]
        sub_b = seq[l+1..-1]
        if sub_b.to_i > sub_a.to_i
            p "YES"
            p 2
            p "#{sub_a} #{sub_b}"
            break
        end
    end
end

N = gets.chomp.to_i

N.times do
    L = gets.chomp.to_i
    seq = gets.chomp
    for l in (0..L-2) do
        sub_a = seq[0..l]
        sub_b = seq[l+1..-1]
        if sub_b.to_i > sub_a.to_i
            puts "YES"
            puts 2
            puts "#{sub_a} #{sub_b}"
            break
        end
    end
end


x = gets.to_i

if x==1 or x==2
  puts "No"
  exit
else
  puts "Yes"

  if x%2==0
    print "#{x/2} "
    a = 1
    while (a<=x)
      print "#{a} "
      a += 2
    end

    puts
    print "#{x/2} "
    a = 2
    while (a<=x)
      print "#{a} "
      a += 2
    end

  else

    print "#{x/2 +1} "
    a = 1
    while (a<=x)
      print "#{a} "
      a += 2
    end

    puts
    print "#{x/2 } "
    a = 2
    while (a<=x)
      print "#{a} "
      a += 2
    end
  end
  puts
end

def gcd(a,b)
  (b==0 ? a : gcd(b, a%b))
end

n = gets.strip.to_i

# 44000.upto(45000).each do |n|
#   puts n if n%1000==0
if n == 1 || n == 2
  # puts n
  puts "No"
else
  tot = (n*(n+1))/2
  2.upto(n).each do |s1|
    s2 = tot - s1
    if gcd(s1,s2)>1
      puts "Yes"
      puts "1 #{s1}"
      others = (1..n).to_a - [s1]
      puts "#{others.size} #{others.join(" ")}"
      exit 0
    end
  end
  puts "No"
end
# end

x = gets.to_i

if x==1 or x==2
  puts "No"
  exit
else
  puts "Yes"

  if x%2==0
    print "#{x/2} "
    a = 1
    while (a<=x)
      print "#{a} "
      a += 2
    end

    puts
    print "#{x/2 +1} "
    a = 2
    while (a<=x)
      print "#{a} "
      a += 2
    end

  else

    print "#{x/2 +1} "
    a = 1
    while (a<=x)
      print "#{a} "
      a += 2
    end

    puts
    print "#{x/2 } "
    a = 2
    while (a<=x)
      print "#{a} "
      a += 2
    end
  end
  puts
end


input=gets.split[0].bytes.to_a
n=input.length
d=0
k=0
n.times{|i|
        if input[i]==40 then
                d+=1
        else
                if d<=0 then
                        k+=1
                else
                        d-=1
                end
        end
}
puts n-d-k

sum = 0
balance = 0
gets.chomp.chars { |c|
  if c == ")"
    balance -= 1
    if balance >= 0
      sum += 2
    else
      balance = 0
    end
  else
    balance += 1
  end
}

puts sum

stk=[]

p gets.rstrip.each_char.inject(0){|best,c|
  stk << c if c=='('

  if c==')' && stk.last == '('
     best += 2
     stk.pop
  end
  best
}

#Inf = Float::Infinity
#
#geti
#getia(delimiter = " ")
#
#Object # dcopy
#
#String # to_a
#   "abc".to_a #=> ["a","b","c"]
#String # to_ia
#   "1 2 3".to_ia #=> [1,2,3]
#

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

Inf = Float::INFINITY
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;
             def to_ia(d = " ");split(d).map(&:to_i);end;end
def getia(*arg);gets.to_ia(*arg);end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end

#until (s = gets.chomp).length.zero?
1.times do
  s = gets.chomp
  a = 0
  res = 0
  cnt = 0
  s.each_char do |c|
    if c == "("
      a += 1
      cnt += 1
    else
      if a > 0
        a -= 1
        cnt += 1
      end
    end
    res = [res, cnt-a].max
  end
  p res
end

input = gets.chomp
bra = 0
res = 0
input.chars do |c|
  if c == "("
    bra += 1
  else
    if bra > 0
      bra -= 1
      res += 2
    end
  end
end
puts res

ans,bal=0,0
gets.chomp.chars{|c|
  if c==')'
    bal -= 1
    if bal>=0
      ans+=2
    else
      bal = 0
    end
  else
    bal += 1
  end
}
p ans

sum = 0
balance = 0
gets.chomp.chars { |c|
  if c == ")"
    balance -= 1
    if balance >= 0
      sum += 2
    end
  else
    balance += 1
  end
}

puts sum

s = gets

while s.index(/\(i*\)/) != nil
  ind1 = s.index(/\(i*\)/)
  ind2 = s.index(")", ind1)
  s.sub!(/\(i*\)/, "i"*(ind2-ind1))
end

max = 0
while s.index(/(i+)/) != nil
  if $1.length > max
    max = $1.length
  end
  s.sub!(/i+/,"")
end

puts max*2

s = gets

while s.index(/\(i*\)/) != nil
  ind1 = s.index(/\(i*\)/)
  ind2 = s.index(")", ind1)
  s.sub!(/\(i*\)/, "i"*(ind2-ind1))
end

max = -1
while s.index(/(i+)/) != nil
  if $1.length > max
    max = $1.length
  end
  s.sub!(/i+/,"")
end

puts max*2


$<.each_with_index{|e,i|
j=e.index('B')
if j;c=e.count('B')/2+1;puts [i-1+c,j+c]*' ';break;end
}

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

ary = []
n.times do
  ary << gets.strip
end

rf=-1
cf=-1
len = 0
0.upto(n-1).each do |i|
  0.upto(m-1).each do |j|
    if ary[i][j]=='B'
      rf=i
      cf=j if cf==-1
      len+=1
    end
  end
  break if rf!=-1
end

# puts "#{rf} #{cf} #{len}"
puts "#{rf+1 + (len/2)} #{cf+1 + (len/2)}"

n, m = gets.split.map(&:to_i)
cs = []

bf = []
bl = []

n.times do |i|
  gets.chomp.chars.each_with_index do |cell, j|
    if cell == 'B'
      if bf == []
        bf = [i, j]
      end
      bl = [i, j]
    end
  end
end

na = (bf[0]+bl[0])/2 + 1
ma = (bf[1]+bl[1])/2 + 1

puts "#{na} #{ma}"

n, m = gets.split.map &:to_i
s = []
n.times { s << gets.chomp }
i, j = 0, 0
catch :found do
  n.times do |y|
    m.times do |x|
      if s[y][x] == 'B'
        i, j = y, x
        throw :found
      end
    end
  end
end
d = (s[i].rindex('B') - s[i].index('B'))/2
puts "#{1 + i + d} #{1 + j + d}"


s=gets.chomp
n=gets.to_i
h=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]
if h.size>n
  p -1
  exit
end
r=Hash.new(0)
while n>0
  m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}
  r[m[0]]+=1
  n-=1
end
p h.map{|k,v|(v+r[k]-1)/r[k]}.max
r.each{|k,v|print(k*v)}

s = gets.chomp.scan(/./)
n = gets.to_i
a = [0] * 26
for x in s
  a[x.ord - 'a'.ord] += 1
end
l, r = 0, s.length
while r - l > 1
  m = (r + l) / 2
  cnt = n
  for i in 0...26
    if a[i] != 0
      cnt -= (m + a[i] - 1) / m
    end
  end
  if cnt >= 0
    r = m
  else
    l = m
  end
end
ans = ''
for i in 0...26
  if a[i] != 0
    ans += (i + 'a'.ord).chr * ((a[i] + r - 1) / r)
  end
end
if n >= ans.length
  ans += 'a' * (n - ans.length)
else
  puts -1
  exit
end
puts r
puts ans

#!/usr/bin/ruby
h=Hash.new(0)
gets.chomp.chars{|c|h[c]+=1}
n=gets.to_i
if h.size>n
	p -1
	exit
end
a=h.sort_by{|e|-e[1]}
x=[1]*a.size
z=n-a.size
while z>0
	b=a.each_with_index.max_by{|e,i|e.last*1.0/(x[i])}
	x[b.last]+=1
	z-=1
end
p a.each_with_index.map{|e,i|(e.last+x[i]-1)/x[i]}.max
puts a.each_with_index.map{|e,i|e.first*x[i]}*''

#!/usr/bin/ruby
h=Hash.new(0)
gets.chomp.chars{|c|h[c]+=1}
n=gets.to_i
if h.size>n
	p -1
	exit
end
a=h.sort_by{|e|-e[1]}
x=[1]*a.size
(n-a.size).times{x[a.each_with_index.max_by{|e,i|e.last*1.0/x[i]}.last]+=1}
p a.each_with_index.map{|e,i|(e.last+x[i]-1)/x[i]}.max
puts a.each_with_index.map{|e,i|e.first*x[i]}*''

#!/usr/bin/ruby
h=Hash.new(0)
gets.chomp.chars{|c|h[c]+=1}
n=gets.to_i
if h.size>n
	p -1
	exit
end
a=h.sort_by{|e|-e[1]}
x=[1]*a.size
z=n-a.size
while z>0
	b=a.each_with_index.max_by{|e,i|e.last*1.0/(x[i]+1)}
	x[b.last]+=1
	z-=1
end
p a.each_with_index.map{|e,i|(e.last*1.0/x[i]).ceil}.max
puts a.each_with_index.map{|e,i|e.first*x[i]}*''

#!/usr/bin/ruby
h=Hash.new(0)
gets.chomp.chars{|c|h[c]+=1}
n=gets.to_i
if h.size>n
	p -1
	exit
end
a=h.sort_by{|e|-e[1]}
x=[1]*a.size
z=n-a.size
while z>0
	b=a.each_with_index.max_by{|e,i|e.last*1.0/(x[i]+1)}
	x[b.last]+=1
	z-=1
end
p a.each_with_index.map{|e,i|(e.last+x[i]-1)/x[i]}.max
puts a.each_with_index.map{|e,i|e.first*x[i]}*''

s=gets.chomp
n=gets.to_i
h=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]
if h.size>n
  p -1
  exit
end
r=Hash.new(0)
while n>0
  m=h.max_by{|k,v|r.key?(k)?v/r[k]:2000}
  r[m[0]]+=1
  n-=1
end
p h.map{|k,v|(v+r[k]-1)/r[k]}.max
r.each{|k,v|print(k*v)}


def check_letter (line_num, board)
	diagonals_letters = []
	other_letters = []
	board.each_with_index do |letter_line, index|
		if (index == (line_num/2))
			diagonals_letters << letter_line[index]
			letter_line.delete_at(index)
			letter_line.each { |c| other_letters << c }
		elsif (index > (line_num/2))
			diagonals_letters << letter_line[index]
			diagonals_letters << letter_line[-(index+1)]
			letter_line.delete_at(index)
			letter_line.delete_at(-index)
			letter_line.each { |c| other_letters << c }
		else
			diagonals_letters << letter_line[index]
			diagonals_letters << letter_line[-(index+1)]
			letter_line.delete_at(index)
			letter_line.delete_at(-(index+1))
			letter_line.each { |c| other_letters << c }
		end
	end

	if ((diagonals_letters.uniq).length == 1) and ((other_letters.uniq).length == 1) and (diagonals_letters[0] != other_letters[0])
		return "YES"
	else
		return "NO"
	end
end

line_num = ((gets()).split().shift()).to_i

letter_board = []

for i in 0..(line_num-1)
	row = []
	row = (gets()).split(//)
	row.delete("\n")
	letter_board << row
end

result = check_letter(line_num, letter_board)
puts result

n = gets.to_i
a = n.times.map{gets.chomp.chars}
c = Array.new(123, 0)
a.each{|x| x.each{|y| c[y.ord] += 1}}
b = c.each.with_index.sort.reverse

ans = true
n.times do |i|
  ans = false if a[i][i] != a[i][n - 1 - i] || a[i][i] != a[0][0]
  ans = false if c[a[0][0].ord] != 2 * n - 1 || c[a[0][1].ord] != (n - 2) * n + 1
#  p c[a[0][0].ord], c[a[0][1].ord]
end

puts ans ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
c = Array.new(123, 0)
a.each{|x| x.each{|y| c[y.ord] += 1}}

ans = true
n.times do |i|
  ans = false if a[i][i] != a[i][n - 1 - i] || a[i][i] != a[0][0]
  ans = false if c[a[0][0].ord] != 2 * n - 1 || c[a[0][1].ord] != (n - 2) * n + 1
end

puts ans ? "YES" : "NO"

def run(l)
  c1 = l[0][0]

  hv = l.size/2
  c2 = l[hv][0]

  return "NO" if c1 == c2

  (0...l.size).each do |i|
    (0...l.size).each do |j|
      if i == j
        return "NO" if l[i][j] != c1
      elsif i == l.size-1-j
        return "NO" if l[i][j] != c1
      else
        return "NO" if l[i][j] != c2
      end
    end
  end

  return "YES"
end

n = gets
while n != nil
  l = []
  n.to_i.times do
    t = gets.split(//)[0...-1]
    l << t
  end
  puts run(l)
  n = gets
end

n = gets.to_i
a = (0...n).map { gets.chomp }
x = a[0][0]
s = a[0][1]
if x == s
 puts 'NO'
 exit
end
puts((0...n).all? { |i| str = s * n; str[i] = str[n - 1 - i] = x; a[i] == str } ? 'YES' : 'NO')

n=gets.chomp.to_i
a=[]
n.times do
	a.push(gets.chomp.strip)
end
x=a[0][0]
s=a[0][1]
if x==s
	puts 'NO'
	exit
end
puts((0...n).all? { |i| str = s * n; str[i] = str[n - 1 - i] = x; a[i] == str } ? 'YES' : 'NO')

n = gets.to_i
m = []

n.times do
	m << gets.chop.split(//)
end

def method matr, n
	d = matr[0][0]
	o = matr[0][1]

	return 'NO' if o == d

	for i in 0...n
		for j in 0...n
			if i == j or i == n - j - 1
				if d != matr[i][j]
					return 'NO'
				end
			else
				if o != matr[i][j]
					return "NO"
				end
			end
		end
	end

	return 'YES'
end

puts method(m, n)

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @size = @dats[0].to_i
  end

  def solve
    return false if @size % 2 != 1 || @size >= 300 || @size < 3
    mtx = []
    (1..@size).each do |idx|
      mtx << @dats[idx]
    end
    mtx.flatten!
    mtx = mtx.join("").split(//)
    diag_char = nil
    not_diag_char = nil

    mtx.each_with_index do |char,idx|
      row = idx / @size
      row_mod = row % @size

      if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1
        if diag_char.nil?
          diag_char = char
        else
          if diag_char != char
            return false
          end
        end
      else
        if not_diag_char.nil?
          not_diag_char = char
          return false if not_diag_char == diag_char
        else
          if not_diag_char != char
            return false
          end
        end
      end
    end
    return true
  end
end

solver = ProblemA.new
print solver.solve ? "YES" : "NO"

n = gets.chomp.to_i
paper = []
uniq = []
valid = true
n.times do
    line = gets.chomp.split("")
    uniq << line.uniq if !uniq.include? line.uniq
    paper << line
    if uniq.length > 2
        valid = false
        break
    end
end
pos = []
x = 0
y = n - 1
ui = uj = di = dj = 0
while x < n && y >= 0 && valid
    ui = x
    uj = x
    di = y
    dj = x
    pos << [ui, uj] if !pos.include? [ui, uj]
    pos << [di, dj] if !pos.include? [di, dj]
    x += 1
    y -= 1
end
x = paper[0][0]
y = paper[0][1]
pos.sort!
center = (pos.length - 1) / 2
valid = x != y if valid
start = 0
fin = start + 1
(0...n).each do |i|
    break if !valid
    line = paper[i].clone
    d_index = 0
    pos.values_at(start..fin).each do |p|
        b = p[1]
        if x != paper[i][b]
            valid = false
            break
        elsif x == paper[i][b]
            line.delete_at( b - d_index )
            d_index += 1
        end
    end
    line.uniq!
    if line.length > 1
        valid = false
        break
    else
        if y != line[0]
            valid = false
            break
        end
    end
    start = fin + 1
    if start == center
        fin = start
    else
        fin = start + 1
    end
end
if valid
    puts "YES"
else
    puts "NO"
end

n=gets.to_i
s=STDIN.read.split.map{|e| e.chars.to_a}
ok="YES"
x=s[0][0]
if n>1 then
	y=s[0][1]
	ok="NO" if y==x
	n.times{|i|
		ok="NO" if s[i][i]!=x || s[i][n-i-1]!=x
		s[i].delete_at([n-i-1,i].max)
		if i!=n-i-1 then
			s[i].delete_at([i,n-i-1].min)
		end

		s[i].delete(y)
		ok="NO" if s[i].size>0
	}
end
puts ok

n=gets.to_i
m=[]

n.times do
    m<<gets.chop.split(//)
end

def method matr,n
    d=matr[0][0]
    o=matr[0][1]

    return 'NO' if d==o
    for i in 0...n
        for j in 0...n
            if i==j or i == n-j-1
                if d!=matr[i][j]
                    return 'NO'
                end
            else
                if o!=matr[i][j]
                        return 'NO'
                end
            end
        end
    end

    return 'YES'
end

puts method(m,n)

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

n.times{|i|
    a[i] = gets.chomp!
}

if n==1
    puts "YES"
    exit
end

x = a[0][0]
b = a[0][1]

if x == b
    puts "NO"
    exit
end
n.times { |i|
    n.times { |j|
        if i==j || (i+j==n-1)
            if a[i][j] != x
                puts "NO"
                exit
            end
        else
             if a[i][j] != b
                puts "NO"
                exit
            end
        end
    }
}

puts "YES"

n = Integer(gets)
a = Array.new
for i in 0...n
    a[i] = Array.new
    input = gets.chomp
    for j in 0...n
        a[i][j] = input[j]
    end
end
e = 0
if a[0][1] != a[0][0]
    e = 1
end
c = 1
for i in 0...n
    if a[i][i] != a[i][n-1-i] || a[0][0] != a[n/2][n/2]
        c = 0
        break
    end
end

b = Array.new
    for i in 0...n
        b[i] = Array.new
        a[i][i] = a[i][n - 1 - i] = a [0][1]
        for j in 0...n
        b[i][j] = a[0][1]
        end
    end

d = 1
for i in 0...n
    for j in 0...n
        if a[i][j] != b[i][j]
            d = 0
            break
        end
    end
end

if e == 1 && c == 1 && d == 1
    puts "YES"
else
    puts "NO"
end

n = gets.chomp.to_i
mat = Array.new(n)

n.times do |i|
    mat[i] = gets.chomp
end

ok = true
x = mat[0][0]
o = mat[0][1]

if x == o
    puts "NO"
    exit
end

n.times do |i|
    n.times do |j|
        if i == j || i + j == n - 1
            if mat[i][j] != x
                ok = false
                break
            end
        else
            if mat[i][j] != o
                ok = false
                break
            end
        end
    end

    break if ok == false
end

puts ok == true ? "YES" : "NO"

n = STDIN.readline.to_i
f = []
n.times do
	f << STDIN.readline.delete(?\n)
end
# print f

ans = true
n.times do |i|
	ans = false if f[i][i]    != f[0][0]
	ans = false if f[i][-i-1] != f[0][0]
end

s = f.join
s.delete! s[0]
unless s.length == (n-1)**2 && s.delete(s[0]).length == 0 ;
	ans = false
end

if ans == true ;
	print "YES\n"
else
	print "NO\n"
end

def diagonal?(i,j,n)
  (i == j) || (j == n - i )
end

matrix = []
n = gets.to_i

n.times do
matrix << gets.split('')

end

diagonal_letter = matrix[0][0]
other_letter = matrix[0][1]
forms_x = true
(0...n).each do |i|
  (0...n).each do |j|
    # puts "#{i} #{j} #{matrix[i][j]}"
  if diagonal?(i,j,n-1)
    if diagonal_letter != matrix[i][j]
     # puts "#{i} #{j} #{forms_x} mat #{matrix[i][j]} diag letter #{diagonal_letter} in diag"
     forms_x = false
     break
    end
  elsif other_letter != matrix[i][j]
    # puts "#{i} #{j} #{forms_x}"
    forms_x = false
    break
  end
  end
  break if forms_x == false
end

forms_x = diagonal_letter != other_letter if forms_x
puts forms_x ? "YES" : "NO"

n = gets.to_i
l = nil; o = nil; ret = ""

n.times do |i|
    r = gets.chomp
    if i==0
        l = r[i]; o = r[i+1]
        ret = "NO" if l==o
    end

    n.times do |j|
        if j==i or j==n-i-1
            ret = "NO" if r[j]!=l
        else
            ret = "NO" if r[j]!=o
        end
    end
    break if ret == "NO"
end

if ret=="NO"
    puts "NO"
else
    puts "YES"
end

n = gets.to_i
l = nil; o = nil;

n.times do |i|
    r = gets.chomp
    if i==0
        l = r[i]; o = r[i+1]
        if l==o
            puts "NO"
            exit
        end
    end

    n.times do |j|
        if j==i or j==n-i-1
            if r[j]!=l
                puts "NO"
                exit
            end
        else
            if r[j]!=o
                puts "NO"
                exit
            end
        end
    end
end

puts "YES"

n = gets.to_i
a = n.times.map{gets.chomp}
c = Hash.new(0)
a.each{|x| x.each_char{|y| c[y] += 1}}
ans = n.times.all?{|i| a[i][i] == a[0][0] && a[i][n - 1 - i] == a[0][0]}
ans &&= c[a[0][1]] == (n - 2) * n + 1

puts ans ? 'YES' : 'NO'

#!/usr/bin/ruby
n=gets.to_i
s=$<.map(&:chomp)
a=s[0][0,1]
b=s[0][1,1]
t=n.times.map{|i|
	z=b*n
	z[i,1]=a
	z[n-i-1,1]=a
	z
}
puts s==t ? 'YES' : 'NO'

n = gets.to_i
m = []

n.times do
	m << gets.chop.split(//)
end

def method matr, n
	d = matr[0][0]
	o = matr[0][1]

	for i in 0...n
		for j in 0...n
			if i == j or i == n - j - 1
				if d != matr[i][j]
					return 'NO'
				end
			else
				if o != matr[i][j]
					return "NO"
				end
			end
		end
	end

	return 'YES'
end

puts method(m, n)

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @size = @dats[0].to_i
  end

  def solve
    mtx = []
    (1..@size).each do |idx|
      mtx << @dats[idx]
    end
    mtx.flatten!
    mtx = mtx.join("").split(//)
    diag_char = nil
    not_diag_char = nil

    mtx.each_with_index do |char,idx|
      row = idx / @size
      row_mod = row % @size

      if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1
        if diag_char.nil?
          diag_char = char
        else
          if diag_char != char
            return false
          end
        end
      else
        if not_diag_char.nil?
          not_diag_char = char
        else
          if not_diag_char != char
            return false
          end
        end
      end
    end
    return true
  end
end

solver = ProblemA.new
p solver.solve ? "YES" : "NO"

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @size = @dats[0].to_i
  end

  def solve
    mtx = []
    (1..@size).each do |idx|
      mtx << @dats[idx]
    end
    mtx.flatten!
    mtx = mtx.join("").split(//)
    diag_char = nil
    not_diag_char = nil

    mtx.each_with_index do |char,idx|
      row = idx / @size
      row_mod = row % @size

      if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1
        if diag_char.nil?
          diag_char = char
        else
          if diag_char != char
            return false
          end
        end
      else
        if not_diag_char.nil?
          not_diag_char = char
        else
          if not_diag_char != char
            return false
          end
        end
      end
    end
    return true
  end
end

solver = ProblemA.new
print solver.solve ? "YES" : "NO"

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @size = @dats[0].to_i
  end

  def solve
    return false if @size % 2 != 1 || @size >= 300 || @size < 3
    mtx = []
    (1..@size).each do |idx|
      mtx << @dats[idx]
    end
    mtx.flatten!
    mtx = mtx.join("").split(//)
    diag_char = nil
    not_diag_char = nil

    mtx.each_with_index do |char,idx|
      row = idx / @size
      row_mod = row % @size

      if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1
        if diag_char.nil?
          diag_char = char
        else
          if diag_char != char
            return false
          end
        end
      else
        if not_diag_char.nil?
          not_diag_char = char
        else
          if not_diag_char != char
            return false
          end
        end
      end
    end
    return true
  end
end

solver = ProblemA.new
print solver.solve ? "YES" : "NO"

n = gets.chomp.to_i
paper = []
uniq = []
valid = true
n.times do
    line = gets.chomp.split("")
    uniq << line.uniq if !uniq.include? line.uniq
    paper << line
    if uniq.length > 2
        valid = false
        break
    end
end
pos = []
x = 0
y = n - 1
ui = uj = di = dj = 0
while x < n && y >= 0 && valid
    ui = x
    uj = x
    di = y
    dj = x
    pos << [ui, uj] if !pos.include? [ui, uj]
    pos << [di, dj] if !pos.include? [di, dj]
    x += 1
    y -= 1
end
x = paper[0][0]
y = paper[0][1]
pos.sort!
center = (pos.length - 1) / 2
valid = x != y if valid
start = 0
fin = start + 1
(0...n).each do |i|
    break if !valid
    line = paper[i].clone
    d_index = 0
    puts pos.inspect
    pos.values_at(start..fin).each do |p|
        b = p[1]
        if x != paper[i][b]
            valid = false
            break
        elsif x == paper[i][b]
            line.delete_at( b - d_index )
            d_index += 1
        end
    end
    line.uniq!
    valid = false if line.length > 1
    start = fin + 1
    if start == center
        fin = start
    else
        fin = start + 1
    end
end
if valid
    puts "YES"
else
    puts "NO"
end

n = gets.chomp.to_i
paper = []
n.times do
    paper << gets.chomp.split("")
end
pos = []
x = 0
y = n - 1
ui = uj = di = dj = 0
while x < n && y >= 0
    ui = x
    uj = x
    di = y
    dj = x
    pos << [ui, uj] if !pos.include? [ui, uj]
    pos << [di, dj] if !pos.include? [di, dj]
    x += 1
    y -= 1
end
valid = true
x = paper[0][0]
y = paper[0][1]
(0...n).each do |i|
    break if !valid
    (0...n).each do |j|
        break if !valid
        if pos.include? [i, j]
            if x != paper[i][j]
                valid = false
            end
        else
            if y != paper[i][j]
                valid = false
            end
        end
    end
end
if valid
    puts "YES"
else
    puts "NO"
end

n = gets.chomp.to_i
paper = []
uniq = []
valid = true
n.times do
    line = gets.chomp.split("")
    uniq << line.uniq if !uniq.include? line.uniq
    paper << line
    if uniq.length > 2
        valid = false
        break
    end
end
if valid
    pos = []
    x = 0
    y = n - 1
    ui = uj = di = dj = 0
    while x < n && y >= 0
        ui = x
        uj = x
        di = y
        dj = x
        pos << [ui, uj] if !pos.include? [ui, uj]
        pos << [di, dj] if !pos.include? [di, dj]
        x += 1
        y -= 1
    end
    x = paper[0][0]
    y = paper[0][1]
    valid = x != y
    pos.each do |p|
        break if !valid
        i = p[0]
        j = p[1]
        if x != paper[i][j]
            valid = false
        end
    end
end
if valid
    puts "YES"
else
    puts "NO"
end

n = gets.chomp.to_i
paper = []
uniq = []
valid = true
n.times do
    line = gets.chomp.split("")
    uniq << line.uniq if !uniq.include? line.uniq
    paper << line
    if uniq.length > 2
        valid = false
        break
    end
end
pos = []
x = 0
y = n - 1
ui = uj = di = dj = 0
while x < n && y >= 0 && valid
    ui = x
    uj = x
    di = y
    dj = x
    pos << [ui, uj] if !pos.include? [ui, uj]
    pos << [di, dj] if !pos.include? [di, dj]
    x += 1
    y -= 1
end
x = paper[0][0]
y = paper[0][1]
pos.sort!
center = (pos.length - 1) / 2
valid = x != y if valid
start = 0
fin = start + 1
(0...n).each do |i|
    break if !valid
    line = paper[i].clone
    d_index = 0
    pos.values_at(start..fin).each do |p|
        b = p[1]
        if x != paper[i][b]
            valid = false
            break
        elsif x == paper[i][b]
            line.delete_at( b - d_index )
            d_index += 1
        end
    end
    line.uniq!
    valid = false if line.length > 1
    start = fin + 1
    if start == center
        fin = start
    else
        fin = start + 1
    end
end
if valid
    puts "YES"
else
    puts "NO"
end

n=gets.to_i
s=STDIN.read.split.map{|e| e.chars.to_a}
ok="YES"
x=s[0][0]
if n>1 then
	n.times{|i|
		ok="NO" if s[i][i]!=x || s[i][n-i-1]!=x
		s[i].delete_at([n-i-1,i].max)
		if i!=n-i-1 then
			s[i].delete_at([i,n-i-1].min)
		end
		ok="NO" if s[i][0]==x
		s[i].delete(s[i][0])
		ok="NO" if s[i].size>0
	}
end
puts ok

n=gets.to_i
m=[]

n.times do
    m<<gets.chop.split(//)
end

def method matr,n
    d=matr[0][0]
    o=matr[0][1]

    return 'NO' if d==o
    for i in 0...n
        for j in 0...n
            if i==j or i = n-j-1
                if d!=matr[i][j]
                    return 'NO'
                end
            else
                if o!=matr[i][j]
                        return 'NO'
                end
            end
        end
    end
    return 'YES'
end

puts method(m,n)

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

n.times{|i|
    a[i] = gets.chomp!
}

if n==1
    puts "YES"
    exit
end

x = a[0][0]
b = a[0][1]
n.times { |i|
    n.times { |j|
        if i==j || (i+j==n-1)
            if a[i][j] != x
                puts "NO"
                exit
            end
        else
             if a[i][j] != b
                puts "NO"
                exit
            end
        end
    }
}

puts "YES"

n = Integer(gets)
a = Array.new
for i in 0...n
	a[i] = Array.new
	input = gets.chomp
	for j in 0...n
		a[i][j] = input[j]
	end
end
e = 0
if a[0][1] != a[0][0]
	e = 1
end
c = 1
for i in 0...n
	if a[i][i] != a[i][n-1-i] || a[0][0] != a[(n-1)/2][(n-1/2)]
		c = 0
		break
	end
end

b = Array.new
	for i in 0...n
		b[i] = Array.new
		a[i][i] = a[i][n - 1 - i] = a [0][1]
		for j in 0...n
		b[i][j] = a[0][1]
		end
	end

d = 1
for i in 0...n
	for j in 0...n
		if a[i][j] != b[i][j]
			d = 0
			break
		end
	end
end

if e == 1 && c == 1 && d == 1
	puts "YES"
else
	puts "NO"
end

n = Integer(gets)
a = Array.new
for i in 0...n
	a[i] = Array.new
	input = gets.chomp
	for j in 0...n
		a[i][j] = input[j]
	end
end
c = 1
for i in 0...n
	if a[i][i] != a[i][n-1-i]
		c = 0
		break
	end
end
puts c

b = Array.new
	for i in 0...n
		b[i] = Array.new
		a[i][i] = a[i][n - 1 - i] = a [0][1]
		for j in 0...n
		b[i][j] = a[0][1]
		end
	end

d = 1
for i in 0...n
	for j in 0...n
		if a[i][j] != b[i][j]
			d = 0
			break
		end
	end
end
puts d

if c == 1 && d == 1
	puts "YES"
else
	puts "NO"
end

n = Integer(gets)
a = Array.new
for i in 0...n
	a[i] = Array.new
	input = gets.chomp
	for j in 0...n
		a[i][j] = input[j]
	end
end
e = 0
if a[0][1] != a[0][0]
	e = 1
end
c = 1
for i in 0...n
	if a[i][i] != a[i][n-1-i] || a[0][0] != a[n/2][n/2]
		c = 0
		break
	end
end
puts c

b = Array.new
	for i in 0...n
		b[i] = Array.new
		a[i][i] = a[i][n - 1 - i] = a [0][1]
		for j in 0...n
		b[i][j] = a[0][1]
		end
	end

d = 1
for i in 0...n
	for j in 0...n
		if a[i][j] != b[i][j]
			d = 0
			break
		end
	end
end

if e == 1 && c == 1 && d == 1
	puts "YES"
else
	puts "NO"
end

n = Integer(gets)
a = Array.new
for i in 0...n
	a[i] = Array.new
	input = gets.chomp
	for j in 0...n
		a[i][j] = input[j]
	end
end
c = 1
for i in 0...n
	if a[i][i] != a[i][n-1-i]
		c = 0
		break
	end
end

b = Array.new
	for i in 0...n
		b[i] = Array.new
		a[i][i] = a[i][n - 1 - i] = a [0][1]
		for j in 0...n
		b[i][j] = a[0][1]
		end
	end

d = 1
for i in 0...n
	for j in 0...n
		if a[i][j] != b[i][j]
			d = 0
			break
		end
	end
end

if c == 1 && d == 1
	puts "YES"
else
	puts "NO"
end

n = Integer(gets)
a = Array.new
for i in 0...n
    a[i] = Array.new
    input = gets.chomp
    for j in 0...n
        a[i][j] = input[j]
    end
end
c = 1
for i in 0...n
    if a[i][i] != a[i][n-1-i]
        c = 0
        break
    end
end

b = Array.new
    for i in 0...n
        b[i] = Array.new
        a[i][i] = a[i][n - 1 - i] = a [0][1]
        for j in 0...n
        b[i][j] = a[0][1]
        end
    end

d = 1
for i in 0...n
    for j in 0...n
        if a[i][j] != b[i][j]
            d = 0
            break
        end
    end
end

if a[0][0].ord != a[0][1].ord && c == 1 && d == 1
    puts "YES"
else
    puts "NO"
end

n = Integer(gets)
a = Array.new
for i in 0...n
    a[i] = Array.new
    input = gets.chomp
    for j in 0...n
        a[i][j] = input[j]
    end
end
e = 0
if a[0][1] != a[0][0]
    e = 1
end
c = 1
for i in 0...n
    if a[i][i] != a[i][n-1-i]
        c = 0
        break
    end
end

b = Array.new
    for i in 0...n
        b[i] = Array.new
        a[i][i] = a[i][n - 1 - i] = a [0][1]
        for j in 0...n
        b[i][j] = a[0][1]
        end
    end

d = 1
for i in 0...n
    for j in 0...n
        if a[i][j] != b[i][j]
            d = 0
            break
        end
    end
end

if e == 1 && c == 1 && d == 1
    puts "YES"
else
    puts "NO"
end

n = gets.chomp.to_i
mat = Array.new(n)

n.times do |i|
    mat[i] = gets.chomp
end

ok = true
x = mat[0][0]
o = mat[0][1]

n.times do |i|
    n.times do |j|
        if i == j || i + j == n - 1
            if mat[i][j] != x
                ok = false
                break
            end
        else
            if mat[i][j] != o
                ok = false
                break
            end
        end
    end

    break if ok == false
end

puts ok == true ? "YES" : "NO"

n = gets.chomp.to_i
mat = Array.new(n)

n.times do |i|
	mat[i] = gets.chomp
end

ok = true

n.times do |i|
	n.times do |j|
		if i == j || i + j == n - 1
			if mat[i][j] != mat[0][0]
				ok = false
				break
			end
		else
			if mat[i][j] == mat[0][0]
				ok = false
				break
			end
		end
	end

	break if ok == false
end

puts ok == true ? "YES" : "NO"

def diagonal?(i,j,n)
  (i == j) || (j == n - i )
end

matrix = []
n = gets.to_i

n.times do
matrix << gets.split('')

end

diagonal_letter = matrix[0][0]
other_letter = matrix[0][1]
forms_x = true
(0...n).each do |i|
  (0...n).each do |j|
    # puts "#{i} #{j} #{matrix[i][j]}"
  if diagonal?(i,j,n-1)
    if diagonal_letter != matrix[i][j]
     # puts "#{i} #{j} #{forms_x} mat #{matrix[i][j]} diag letter #{diagonal_letter} in diag"
     forms_x = false
     break
    end
  elsif other_letter != matrix[i][j]
    # puts "#{i} #{j} #{forms_x}"
    forms_x = false
    break
  end
  end
  break if forms_x == false
end

# forms_x = diagonal_letter != other_letter if forms_x
puts forms_x ? "YES" : "NO"

n = gets.to_i
l = nil; o = nil; ret = ""

n.times do |i|
    r = gets.chomp
    if i==0
        l = r[i]; o = r[i+1]
        if l==o
            puts "NO"
            exit
        end
    end

    n.times do |j|
        if j==i or j==n-i-1
            if r[j]!=l
                puts "NO"
                exit
            end
        else
            if r[j]!=o
                puts = "NO"
                exit
            end
        end
    end
end

puts "YES"

n = gets.to_i
l = nil; o = nil; ret = ""

n.times do |i|
    r = gets.chomp
    if i==0
        l = r[i]
        o = r[i+1]
    end

    n.times do |j|
        if j==i or j==n-i-1
            ret = "NO" if r[j]!=l
        else
            ret = "NO" if r[j]!=o
        end
    end
    break if ret == "NO"
end

if ret=="NO"
    puts "NO"
else
    puts "YES"
end

n = gets.to_i
a = n.times.map{gets.chomp.chars}
c = Array.new(123, 0)
a.each{|x| x.each{|y| c[y.ord] += 1}}
b = c.each.with_index.sort.reverse.map{|x, y| y.chr}
t = [b[0], b[1]]

def x?(n, a)
  n.times do |i|
    return false if a[i][i] != a[i][n - 1 - i]
  end
  return true
end

puts c[t[0].ord] + c[t[1].ord] == n * n && c[b[0].ord] != n * n && x?(n, a) ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
c = Array.new(123, 0)
a.each{|x| x.each{|y| c[y.ord] += 1}}
b = c.each.with_index.sort.reverse.map{|x, y| y.chr}

def x?(n, a, b)
  n.times do |i|
    return false if a[i][i] != a[i][n - 1 - i]
    x = a[i].count(b[0])
    y = a[i].count(b[1])
    return false if x == n || y == n || x + y != n
  end
  return true
end

puts x?(n, a, b) ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
c = Array.new(123, 0)
a.each{|x| x.each{|y| c[y.ord] += 1}}
b = c.each.with_index.sort.reverse.map{|x, y| y.chr}
t = [b[0], b[1]]

def x?(n, a)
  n.times do |i|
    return false if a[i][i] != a[i][n - 1 - i]
  end
  return true
end

puts c[t[0].ord] + c[t[1].ord] == n * n && x?(n, a) ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
c = Array.new(123, 0)
a.each{|x| x.each{|y| c[y.ord] += 1}}
b = c.each.with_index.sort.reverse

ans = true
n.times do |i|
  ans = false if a[i][i] != a[i][n - 1 - i]
  ans = false if c[a[0][0].ord] != 2 * n - 1 || c[a[0][1].ord] != (n - 2) * n + 1
#  p c[a[0][0].ord], c[a[0][1].ord]
end

puts ans ? "YES" : "NO"

def run(l)
  c1 = l[0][0]

  hv = l.size/2
  c2 = l[hv][0]

  return "NO" if c1 == c2

  #diagonals
  (0...l.size).each do |i|
    return "NO" if l[i][i] != c1
    return "NO" if l[l.size-1-i][l.size-1-i] != c1
  end

  #cross
  (0...l.size).each do |i|
    return "NO" if l[hv][i] != c2 and i != hv
    return "NO" if l[i][hv] != c2 and i != hv
  end

  return "YES"
end

n = gets
while n != nil
  l = []
  n.to_i.times do
    t = gets.split(//)[0...-1]
    l << t
  end
  puts run(l)
  n = gets
end

def check_letter (line_num, board)
	diagonals_letters = []
	other_letters = []
	board.each_with_index do |letter_line, index|
		if (index == (line_num/2))
			diagonals_letters << letter_line[index]
			letter_line.delete_at(index)
			letter_line.each { |c| other_letters << c }
		elsif (index > (line_num/2))
			diagonals_letters << letter_line[index]
			diagonals_letters << letter_line[-(index+1)]
			letter_line.delete_at(index)
			letter_line.delete_at(-index)
			letter_line.each { |c| other_letters << c }
		else
			diagonals_letters << letter_line[index]
			diagonals_letters << letter_line[-(index+1)]
			letter_line.delete_at(index)
			letter_line.delete_at(-(index+1))
			letter_line.each { |c| other_letters << c }
		end
	end

	if ((diagonals_letters.uniq).length == 1) and ((other_letters.uniq).length == 1)
		return "YES"
	else
		return "NO"
	end
end

line_num = ((gets()).split().shift()).to_i

letter_board = []

for i in 0..(line_num-1)
	row = []
	row = (gets()).split(//)
	row.delete("\n")
	letter_board << row
end

result = check_letter(line_num, letter_board)
puts result

def check_letter (line_num, board)
	diagonals_letters = []
	other_letters = []
	board.each_with_index do |letter_line, index|
		if (index == (line_num/2))
			diagonals_letters << letter_line[index]
			letter_line.delete_at(index)
			letter_line.each { |c| other_letters << c }
		elsif (index > (line_num/2))
			diagonals_letters << letter_line[index]
			diagonals_letters << letter_line[-(index+1)]
			letter_line.delete_at(index)
			letter_line.delete_at(-index)
			letter_line.each { |c| other_letters << c }
		else
			diagonals_letters << letter_line[index]
			diagonals_letters << letter_line[-(index+1)]
			letter_line.delete_at(index)
			letter_line.delete_at(-(index+1))
			letter_line.each { |c| other_letters << c }
		end
	end

	p diagonals_letters
	p other_letters

	if ((diagonals_letters.uniq).length == 1) and ((other_letters.uniq).length == 1)
		return "YES"
	else
		return "NO"
	end
end

line_num = ((gets()).split().shift()).to_i

letter_board = []

for i in 0..(line_num-1)
	row = []
	row = (gets()).split(//)
	row.delete("\n")
	letter_board << row
end

p check_letter(line_num, letter_board)


n = gets.to_i
time = Time.now
s = gets.chomp.split('')
cl = ['R', 'G', 'B']
mn = s.size
res = s
cl.permutation.each do |cur|
  t = 0
  str = s.dup
  str.each_with_index do |e, ind|
    if (e != cur[ind % 3])
      t += 1
      str[ind] = cur[ind % 3]
    end
  end
  if (t < mn)
    mn = t
    res = str
  end
end
puts mn
puts res.join('')
# puts Time.now - time

n = gets.to_i
s = gets.chomp.chars
puts %w[R G B].permutation.map { |rgb| (0...n).map { rgb[_1 % 3] } }.map { |t|
		[s.zip(t).count { _1 != _2 }, t.join]
     }.min

class Strand

  def initialize
    @counts = {
      'R' => 0,
      'G' => 0,
      'B' => 0,
    }
  end

  def increase(c)
    @counts[c] += 1
  end

  def moves_to_change_to(c)
    @counts.to_a.select{|k, _| k != c }.map{ |_, v| v }.reduce(0, :+)
  end

end

def solution
  read_int
  gs = read_string

  def color_index(c)
    return 0 if c == "R"
    return 1 if c == "G"
    return 2 if c == "B"
  end

  s = [
    Strand.new,
    Strand.new,
    Strand.new
  ]

  for i in (0...gs.size)
    crt = gs[i]
    j = i % 3
    s[j].increase(crt)
  end

  min_moves = gs.size
  best = nil
  for three_cols in ("RGB".chars.permutation)
    total_moves = (0...3).map{ |i| s[i].moves_to_change_to(three_cols[i])}.reduce(0, :+)
    if min_moves > total_moves
      min_moves = total_moves
      best = three_cols
    end
  end

  final = (0...gs.size).map { |i| best[i % 3] }.join

  puts min_moves
  puts final
end

def read_int
  gets.to_i
end

def read_ints
  gets.split.map(&:to_i)
end

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

salts = %w(RGB RBG BRG BGR GRB GBR)

n = Integer(gets)
s = gets

def dif(s, t, n)
  d = 0
  n.times do |i|
    d = d + 1 if s[i] != t[i]
  end
  d
end

best = ""
best_dif  = s.size + 1

salts.each do |salt|
  t = (salt * (n / 3 + 2))[0..(n - 1)]
  d = dif(s, t, n)
  if d < best_dif
    best = t
    best_dif = d
  end
end

puts best_dif
puts best

n = gets.to_i
s = gets.chomp.chars
puts (0...3).map { |k| (0...n).map { 'RGB'[(k + _1) % 3] } }.map { |t|
		[s.zip(t).count { _1 != _2 }, t.join]
     }.min

n = gets.to_i
s = gets.chomp.split('')
arr = Array.new(3){{'R'=> 0, 'G'=> 0, 'B'=> 0}}
s.each_with_index do |e, ind|
  arr[ind % 3][e] += 1
end
# puts arr.to_s
res = Array.new(3){0}
3.times do |i|
  res[i] = arr[i].max_by{|e, ee| ee}[1]
end
3.times do |i|
  res[i] = arr[i].select{|e, ee| ee == res[i]}.keys
end
# puts res.to_s
brr = []
arr.each_with_index do |e, ind|
  e.each do |k, v|
    brr << {'K'=> k, 'V' => v, 'I'=> ind}
  end
end
brr = brr.sort_by{|e| e['V']}.reverse
# puts brr.to_s
ans = Array.new(3){'_'}
colors = ['R', 'G', 'B']
brr.each do |e|
  if (ans[e['I']] == '_' && colors.include?(e['K']))
    ans[e['I']] = e['K']
    colors.delete(e['K'])
  end
end

cur = 0
s.each_with_index do |e, ind|
  if (e != ans[ind % 3])
    cur += 1
    s[ind] = ans[ind % 3]
  end
end
puts cur
puts s.join('')

n = gets.to_i
s = gets.chomp.split('')
arr = Array.new(3){{'R'=> 0, 'G'=> 0, 'B'=> 0}}
s.each_with_index do |e, ind|
  arr[ind % 3][e] += 1
end
# puts arr.to_s
brr = []
arr.each_with_index do |e, ind|
  e.each do |k, v|
    brr << {'K'=> k, 'V' => v, 'I'=> ind}
  end
end
brr = brr.sort_by{|e| e['V']}.reverse
# puts brr
ans = Array.new(3){'_'}
colors = ['R', 'G', 'B']
brr.each do |e|
  if (ans[e['I']] == '_' && colors.include?(e['K']))
    ans[e['I']] = e['K']
    colors.delete(e['K'])
  end
end

cur = 0
s.each_with_index do |e, ind|
  if (s[ind] != ans[ind % 3])
    cur += 1
    s[ind] = ans[ind % 3]
  end
end
puts cur
puts s.join('')

salts = %w(RGB RBG BRG BGR GRB GBR)

n = Integer(gets)
s = gets

def dif(s, t, n)
  d = 0
  n.times do |i|
    d = d + 1 if s[i] != t[i]
  end
  d
end

best = ""
best_dif  = s.size + 1

salts.each do |salt|
  t = (salt * (n / 3 + 2))[0..(n - 1)]
  puts t
  d = dif(s, t, n)
  if d < best_dif
    best = t
    best_dif = d
  end
end

puts best_dif
puts best


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

gets.to_i.times do
  x, y, p, q = gets.split(/\s+/).map(&:to_i)

  if p == q
    ans = x == y ? 0 : -1
  elsif p == 0
    ans = x == 0 ? 0 : -1
  else
    d = p*y - q*x
    k = q - p
    if d == 0
      ans = 0
    elsif d > 0 && d % k == 0
      ans = d/k
    else
      k = ((y - x.to_f) / (q - p)).ceil
      if k*p < x
        k = x / p + min(x%p, 1)
      end

      if k * q - y < 0
        ans = -1
      else
        ans = q*k-y
      end
    end
  end

  puts ans
end

gets.to_i.times do
    x, y, p, q=gets.split.map &:to_i

    if (x<y and p==q) or (x>0 and p==0)
        puts -1
    elsif x*q==y*p
        puts 0
    elsif x*q>y*p
        b=64
        n=(1<<b)-1
        a=1<<(b-1)
        while a>0
            v=(n-a)*q-y
            n-=a if v>0 and x*q<=(v+y)*p
            a>>=1
        end
        puts n*q-y
    else
        b=64
        n=(1<<b)-1
        a=1<<(b-1)
        while a>0
            v=(n-a)*q-y
            n-=a if v>0 and (v+x)*q>=(v+y)*p
            a>>=1
        end
        puts n*q-y
    end
end

n = gets.to_i
for _ in 0 ... n
  x, y, p, q = gets.split.map(&:to_i)
  y -= x
  q -= p
  if p == 0 || q == 0
    if (x == 0 && p == 0) || (y == 0 && q == 0)
      puts 0
    else
      puts(-1)
    end
    next
  end
  a1 = (x + p - 1) / p
  a2 = (y + q - 1) / q
  a = [a1, a2].max
  r1 = a * p - x
  r2 = a * q - y
  puts r1 + r2
end

n = gets.to_i
for i in 0 ... n
  x, y, p, q = gets.split.map(&:to_i)
  y -= x
  q -= p
  if p == 0 || q == 0
    puts(-1)
    next
  end
  a1 = (x + p - 1) / p
  a2 = (y + q - 1) / q
  a = [a1, a2].max
  r1 = a * p - x
  r2 = a * q - y
  puts r1 + r2
end

gets.to_i.times do
    x, y, p, q=gets.split.map &:to_i

    if x<y and p==q
        puts -1
    elsif x*q==y*p
        puts 0
    elsif x*q>y*p
        n=(1<<60)-1
        a=1<<59
        while a>0
            v=(n-a)*q-y
            n-=a if v>0 and x*q<=(v+y)*p
            a>>=1
        end
        puts n*q-y
    else
        n=(1<<60)-1
        a=1<<59
        while a>0
            v=(n-a)*q-y
            n-=a if v>0 and (v+x)*q>=(v+y)*p
            a>>=1
        end
        puts n*q-y
    end
end

gets.to_i.times do
    x, y, p, q=gets.split.map &:to_i

    if x<y and p==q
        puts -1
    elsif x*q==y*p
        puts 0
    elsif x*q>y*p
        b=64
        n=(1<<b)-1
        a=1<<(b-1)
        while a>0
            v=(n-a)*q-y
            n-=a if v>0 and x*q<=(v+y)*p
            a>>=1
        end
        puts n*q-y
    else
        b=64
        n=(1<<b)-1
        a=1<<(b-1)
        while a>0
            v=(n-a)*q-y
            n-=a if v>0 and (v+x)*q>=(v+y)*p
            a>>=1
        end
        puts n*q-y
    end
end


for i in 0..gets.to_i-1
	n=gets.to_i
	a=gets[..-2]
	b=gets[..-2]
	k=0
	for i in 0..n-1 do
		if b[i]=='1'
			if a[i]=='0'
				k+=1
			elsif i-1>=0 and a[i-1]=='1'
				k+=1
				a[i-1]='0'
			elsif i+1<n and a[i+1]=='1'
				k+=1
				a[i+1]='0'
			end
		end
	end
	puts k
end

t = gets.to_i
t.times do
	n = gets.to_i
	enemies = gets.chomp.chars.map!(&:to_i) << 0
	allies = gets.chomp.chars.map!(&:to_i)

	cnt = 0
	allies.each_with_index do |ai, i|
		next if ai == 0
		if enemies[i] == 0
			cnt += 1
		elsif enemies[i-1] == 1
			cnt += 1
			enemies[i-1] = 0
		elsif enemies[i+1] == 1
			cnt += 1
			enemies[i+1] = 0
		end
	end

	p cnt
end

MOD = 10 ** 9 + 7
def gs; gets.chomp; end
def gi; gets.to_i; end
def gli; gets.split.map(&:to_i); end
def cyesno(x); puts x ? :Yes : :No; end
def uyesno(x); puts x ? :YES : :NO; end
def array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end

def solve
  n = gi
  c = 0
  ep = gs.split('').map(&:to_i)
  ap = gs.split('').map(&:to_i)
  for i in (0..n-1)
    if ep[i] == 1 && ap[i - 1] == 1 && i > 0
      c += 1
      ap[i - 1] = 0
    elsif ep[i] == 1 && ap[i + 1] == 1 && i < n - 1
      c +=1
      ap[i + 1] = 0
    elsif ep[i] == 0 && ap[i] == 1
      c += 1
      ap[i] = 0
    end
  end
  puts c
end

def main
  gi.times { solve }
end

main

for i in 0..gets.to_i-1
    n=gets.to_i
    a=gets[..-2]
    b=gets[..-2]
    k=0
    for i in 0..n-1 do
    	if b[i]=='1'
    		if a[i]=='0'
    			k+=1
    		elsif i-1>=0 and a[i-1]=='1'
    			k+=1
    		elsif i+1<n and a[i+1]=='1'
    			k+=1
    		end
    	end
    end
    puts k
end


Integer(gets.chomp).times{
    n, x = gets.chomp.split.map{|c| Integer(c)}
    a = gets.chomp.split.map{|c| Integer(c)}.sort
    if(x == a.sum)
        puts "NO"
        next
    end

    pref = Array.new
    k = 0
    a.each{ |e|
        k += e
        pref.push(k)
    }
    if(pref.include?(x))
        a.append(a.shift)
    end
    puts "YES"
    a.each {|e| print "#{e} "}
    puts ""
}

def read
    gets.split.map &:to_i
end
gets.to_i.times do
    n, x = read
    w = read
    if w.sum == x
        puts "NO"
    else
        puts "YES"
        n.times do |i|
            w[i], w[i + 1] = w[i + 1], w[i] if w[i] == x
            print w[i], " "
            x -= w[i]
        end
        puts
    end
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n, x = gets.to_s.split.map {|x| x.to_i }
  a = gets.to_s.split.map {|x| x.to_i }.sort
  sum = 0
  bad = false
  bad_i = 0
  0.upto(n - 1) do |i|
    sum += a[i]
    if sum == x
      bad = true
      bad_i = i
    end
  end
  if !bad
    printf("YES\n%s\n", a.join(" "))
  elsif bad_i != n - 1
    a[bad_i], a[n - 1] = a[n - 1], a[bad_i]
    printf("YES\n%s\n", a.join(" "))
  else
    printf("NO\n")
  end
end

Integer(gets.chomp).times{
    n, x = gets.chomp.split.map{|c| Integer(c)}
    a = gets.chomp.split.map{|c| Integer(c)}.sort
    if(x == a.sum)
        puts "NO"
        next
    end

    pref = Array.new
    k = 0
    a.each{ |e|
        k += e
        pref.push(k)
    }
    if(pref.include?(x))
        a.shuffle!
    end
    puts "YES"
    a.each {|e| print "#{e} "}
    puts ""
}

Integer(gets.chomp).times{
    n, x = gets.chomp.split.map{|c| Integer(c)}
    a = gets.chomp.split.map{|c| Integer(c)}.sort
    if(x == a.sum)
        puts "NO"
        next
    end

    pref = Array.new
    k = 0
    a.each{ |e|
        k += e
        pref.push(e)
    }
    if(pref.include?(x))
        a.append(a.shift)
    end
    puts "YES"
    a.each {|e| print "#{e} "}
    puts ""
}

Integer(gets.chomp).times{
    n, x = gets.chomp.split.map{|c| Integer(c)}
    a = gets.chomp.split.map{|c| Integer(c)}.sort
    if(x == a.sum)
        puts "NO"
        next
    end

    pref = Array.new
    k = 0
    a.each{ |e|
        k += e
        pref.push(e)
    }
    if(pref.include?(x))
        a.reverse!
    end
    puts "YES"
    a.each {|e| print "#{e} "}
    puts ""
}