tc = gets.chomp.to_i
while tc > 0
  n = gets.chomp.to_i
  a = gets.split.collect{|i| i.to_i}
  i = n - 1
  while i >= 1 && a[i - 1] >= a[i]
    i -= 1
  end
  while i >= 1 && a[i - 1] <= a[i]
    i -= 1
  end
  puts i
  tc -= 1
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i).chunk_while { _1 == _2 }.to_a
	pos =
		(1...a.size - 1).reverse_each.find do |i|
			a[i - 1][0] > a[i][0] && a[i][0] < a[i + 1][0]
		end || 0
	puts (0...pos).sum { a[_1].size }
end

t = gets.chomp.to_i
t.times do
    ln = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    i = ln - 1
    while i >= 1 && a[i-1] >= a[i]
             i -= 1
    end
    while i >= 1 && a[i-1] <= a[i]
        i -= 1
    end
    puts i
end

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|i| i.to_i}
    ans = 0
    crr = Array.new(n) {0}
    maxi = arr[n-1]
    ((n-2).step(0,-1)).each do |i|
        maxi  = [maxi,arr[i+1]].max
        crr[i] = maxi
    end
    (1...n-1).each do |i|
        if(arr[i] < arr[i-1] && arr[i] < crr[i])
            ans = i
        end
    end
    puts(ans)
end

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

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  rm = n - 1
  state = :inc
  (n - 2).downto(0) do |i|
    if a[i] < a[i + 1] then state = :dec end
    break if state == :dec and a[i] > a[i + 1]
    rm = i
  end
  puts rm
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i)
	puts (1...n - 1).reverse_each.find { |i|
			a[i - 1] > a[i] && a[i] < a[i + 1]
	     } || 0
end

t = gets.chomp.to_i
t.times do
    ln = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    i = ln - 1
    while i >= 0 && a[i-1] >= a[i]
             i -= 1
    end
    while i >= 0 && a[i-1] < a[i]
        i -= 1
    end
    puts i
end

t = gets.chomp.to_i
t.times do
    ln = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    i = ln - 1
    while i >= 0 && a[i-1] >= a[i]
             i -= 1
    end
    while i >= 0 && a[i-1] <= a[i]
        i -= 1
    end
    puts i
end

t = gets.chomp.to_i
t.times do
    ln = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    i = ln - 1
    while i >= 0 && a[i-1] > a[i]
             i -= 1
    end
    while i >= 0 && a[i-1] < a[i]
        i -= 1
    end
    puts i
end

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|i| i.to_i}

    brr = Array.new(n) {0}
    crr = Array.new(n) {0}

    maxi = arr[0]
    (1...n).each do |i|
        maxi = [maxi,arr[i-1]].max
        brr[i] = maxi
    end
    # print(brr)
    maxi = arr[n-1]
    ((n-2).step(0,-1)).each do |i|
        maxi  = [maxi,arr[i+1]].max
        crr[i] = maxi
    end
    # print(crr)
    ans = 0
    (1...n-1).each do |i|
        if(arr[i] < brr[i]  && arr[i] < crr[i])
            ans = i
        end
    end
    puts(ans)
end

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|i| i.to_i}
    # print(arr)
    brr = Array.new(n) {0}
    crr = Array.new(n) {0}

    maxi = arr[0]
    (1...n).each do |i|
        maxi = [maxi,arr[i-1]].max
        brr[i] = maxi
    end
    # print(brr)
    maxi = arr[n-1]
    ((n-2).step(0,-1)).each do |i|
        maxi  = [maxi,arr[i+1]].max
        crr[i] = maxi
    end
    # print(crr)
    ans = 0
    (1...n-1).each do |i|
        if(arr[i] < brr[i]  && arr[i] < crr[i])
            if(arr[i] != arr[i-1])
                ans = i
            end
        end
    end
    puts(ans)
end

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|i| i.to_i}
    ans = 0
    (1...n-1).each do |i|
        if(arr[i] < arr[i-1] && arr[i] < arr[i+1])
            ans = i
        end
    end
    puts(ans)
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  rm = n - 2
  state = :inc
  (n - 2).downto(0) do |i|
    if a[i] < a[i + 1] then state = :dec end
    break if state == :dec and a[i] > a[i + 1]
    rm = i
  end
  puts rm
end


t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  a.sort!
  b = Array.new(n, nil)
  pos = (n - 1) / 2
  n.times do |i|
    b[i] = a[pos]
    if i % 2 == 0
      pos += i + 1
    else
      pos -= i + 1
    end
  end
  puts b.join(" ")
end

# coding: utf-8

# |a1 - a2| <= |a2 - a3| <= |a3 - a4| ....
# 를 만족시키도록 a를 정렬
# 꽤 재밌는 유형의 문제이다. 이렇게 숫자 배열을 타고 순회하는걸 oscillation이라 한다는군.
# naive하게 구성한 알고리즘은 다음과 같음
# 1. 삽입 정렬 구조를 가짐
# 2. 빈 배열에 a1 삽입
# 3. 빈 배열에 a2 삽입
# (여기까진 |a1 - a2| = |a2 - a1|이므로 큰 지장은 없음)
# 4. 빈 배열에 a3 을 넣기 위한 자리를 선택함( 전체 순회가 필요함 O(n))
# 5. 이후 a3부터 aN까지 모든 절차를 반복해야하므로 O(n^2)
# 이렇게 잡았지만 부분적으로는 해답일 수 있으나 아무래도 오답이 자꾸 나오네.
# 결국은 몇번 고민해보다가 찾은 해가 이 접근법이다.

# 일단 숫자를 모두 정렬한다. 1 2 9 5 8 8을 감소가 없는 순서대로 정렬하면,
# 1 2 5 8 8 9 이다. 이 숫자는 재밌는 성질을 갖고 있는데, 한 숫자를 기준으로
# 오른쪽은 0혹은 1이상만큼 크며 왼쪽은 0혹은 1이상만큼 작다.
# 그래서 자신에게 k 거리만큼 먼쪽일 수록 자신과의 차이가 매우 커질거란 기대를 할 수 있다.
# 자신에게 0에 가까운 거리일수록 자신과의 차이는 매우 작아진다.
# 그래서 중앙에서부터 왼쪽 절반의 제일 후미부터, 오른쪽 절반의 제일 선두부터 숫자를
# 추출하면 거리가 가까운 순서부터 먼 순서대로 뽑을 수 있다.

# 처음에 평균을 이용한 접근을 한건 평균이 중앙에 올 경우에는 성공할 수 있는 방법이지만,
# 평균값이 중앙에 있지 못한 경우에는 잘못된 동작을 할 수 있다.
# 결국 평균이 아니라 '중간값'을 찾아야하는 문제. 정렬은 이러한 방법으로 순회하기 위해
# 안정적인 전제를 제공하는 것이다.
t = gets.to_i
t.times {
    n = gets.to_i
    a = gets.split.map(&:to_i)
    a.sort!
    mid = n/2
    la, ra = a[0...mid], a[mid...n]
    b = []
    while !la.empty? || !ra.empty?
        b << ra.shift unless ra.empty?
        b << la.pop unless la.empty?
    end

    #p la
    #p ra

    puts b.join(' ')
}

# coding: utf-8

# |a1 - a2| <= |a2 - a3| <= |a3 - a4| ....
# 를 만족시키도록 a를 정렬

t = gets.to_i
t.times {
    n = gets.to_i
    a = gets.split.map(&:to_i)
    a.sort!
    mid = n/2
    la, ra = a[0...mid], a[mid...n]
    b = []
    while !la.empty? && !ra.empty?
        b << ra.shift unless ra.empty?
        b << la.pop unless la.empty?
    end

    puts b.join(' ')
}

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  a.sort!
  b = Array.new(n, nil)
  pos = (n - 1) / 2
  n.times do |i|
    puts pos
    b[i] = a[pos]
    if i % 2 == 0
      pos += i + 1
    else
      pos -= i + 1
    end
  end
  puts b.join(" ")
end


k = gets.to_i

while (k > 0)
  n, x, t = gets.split(' ').map(&:to_i)

  forFirst = [t / x, n - 1].min;

  puts(forFirst * [n - forFirst, 0].max + forFirst * (forFirst - 1) / 2)

  k -= 1
end

gets.to_i.times do
  n,x,t=gets.split.map(&:to_i)
  if t>=x*(n-1)
    puts (n-1)*n/2
  else
    cc=t/x
    puts cc*(cc+1)/2+(n-cc-1)*cc
  end
end


r,c = gets.split.map(&:to_i)
a = []

check = true
r.times do
  a << gets.chomp
end
r.times do |i|
  c.times do |j|
    if a[i][j] == "S"
      check = false if i > 0 && a[i-1][j] == "W"
      check = false if i < r-1 && a[i+1][j] == "W"
      check = false if j > 0 && a[i][j-1] == "W"
      check = false if j < c-1 && a[i][j+1] == "W"
    end
    a[i][j] = "D" if a[i][j] == "."
  end
end
if check
  puts "Yes"
  r.times do |i|
    puts a[i]
  end
else
  puts "No"
end

r, c = gets.split.map &:to_i

pasture = Array.new(){Array.new(c)}

r.times do
  pasture << gets.strip.split("")
end

def place_dogs(pasture, i, j)
  r = pasture.length
  c = pasture[0].length
  pasture[i - 1][j] = "D" if i - 1 >= 0 && pasture[i - 1][j] == "."
  pasture[i + 1][j] = "D" if i + 1 < r && pasture[i + 1][j] == "."
  pasture[i][j - 1] = "D" if j - 1 >= 0 && pasture[i][j - 1] == "."
  pasture[i][j + 1] = "D" if j + 1 < c && pasture[i][j + 1] == "."
end

def dead_sheep?(pasture)
  r = pasture.length
  c = pasture[0].length
  dead_sheep = false
  pasture.each_index do |i|
    pasture[i].each_index do |j|
      if pasture[i][j] == "W"
        up = pasture[i - 1][j] if i - 1 >= 0
        down = pasture[i + 1][j] if i + 1 < r
        right = pasture[i][j + 1] if j + 1 < c
        left = pasture[i][j - 1] if j - 1 >= 0
        if up == "S" || down == "S" || right == "S" || left == "S"
          dead_sheep = true
          break
        end
      end
    end
  end
  dead_sheep
end

if dead_sheep?(pasture)
  puts "No"
else
  pasture.each_index do |i|
    pasture[i].each_index do |j|
      if pasture[i][j] == "W"
        place_dogs(pasture, i, j)
      end
    end
  end
  pasture = pasture.map &:join
  puts "Yes", pasture.join("\n")
end

n, m = gets.split.map(&:to_i)
ar = Array.new(0)
res = false
n.times { ar << gets.chomp}
n.times do |i|
  ar[i] = ar[i].gsub('.', 'D')
  if ar[i].include?('WS') || ar[i].include?('SW')
    res = true
    break
  end
end
n.times do |i|
  m.times do |j|
    break if i == n-1
    curr = ar[i][j]
    bottom = ar[i+1][j] if ar[i+1]
    if [curr, bottom].sort == ['S','W']
      res = true
      break
    end
  end
end
puts res ? "NO" : "YES"
puts ar if !res

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

arr = Array.new(a) { Array.new(b)  }

(0...a).to_a.each do |i|
	inp = gets.chomp.split("")
	(0...b).to_a.each do |j|
		arr[i][j] = inp[j]
		if arr[i][j] == '.'
			arr[i][j] = 'D'
		end
	end
end

(0...a).to_a.each do |i|
	(0...b).to_a.each do |j|
		if arr[i][j] == 'W' && (((j-1) >= 0 && arr[i][j-1] == 'S') || ((j+1)<b && arr[i][j+1] == 'S') || ((i-1)>=0 && arr[i-1][j] == 'S') || (i+1) < a && arr[i+1][j] == 'S')
			puts "No"
			exit
		end
	end
end
puts 'Yes'
arr.each do |el|
	puts el.join("")
end


t = gets.to_i
t.times do
  n = gets.to_i.to_s(2)
  l = 1 << (n.size - 1)
  puts l - 1
end

gets.to_i.times {
  n = gets.to_i

  p = 1
  p *= 2 while p < n
  p /= 2 if p > n

  puts(p - 1)
}

gets.to_i.times do
    n = gets.to_i
    res = 0
    while n > 0
        n /= 2
        res += 1
    end
    puts (1 << (res - 1)) - 1
end

t = gets.chomp.to_i

def solve(n)
  cnt = 0
  while n > 0 do
    cnt += 1
    n /= 2
  end

  (1 << (cnt - 1)) - 1
end

t.times do
  n = gets.chomp.to_i
  puts solve(n)
end

gets.to_i.times{|c|
	n = gets.to_i
	puts ('1'*(n.to_s(2).size-1)).to_i(2)
}

gets.to_i.times{|c|
	n = gets.to_i
	puts n-n.digits(2).count(1)
}


r,c=gets.split.map(&:to_i)
x,y=0,0
r.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}
puts "#{x+1} #{y+1}"

#!/usr/bin/ruby1.9

(n, m) = STDIN.readline.split.map {|s| s.to_i}

rs = []
cs = []
(1..n).each do |r|
	row = STDIN.readline.strip
	(1..row.length).each do |c|
		if row[c-1].chr == '*'
			rs.push(r)
			cs.push(c)
		end
	end
end

def get_odd_one_out(xs)
	xs = xs.sort
	if xs[0] == xs[1]
		return xs[2]
	end
	return xs[0]
end

r = get_odd_one_out(rs)
c = get_odd_one_out(cs)
puts '%d %d' % [r, c]

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

list = []
n.times do |j|
  gets.chomp.split("").each_with_index do |c, i|
    if c == '*'
      list << [j, i]
    end
  end
end

count_y = Hash.new(0)
count_x = Hash.new(0)

list.each do |y, x|
  count_y[y] += 1
  count_x[x] += 1
end

puts "#{count_y.key(1) + 1} #{count_x.key(1) + 1}"

n,m = gets.to_s.split(" ").map{|x| Integer x}
points = []
n.times do |i|
	a = gets.to_s.split("")
	a.each_index do |j|
		if a[j] == "*" then
			points.push [i,j]
		end
	end
end
p = []
3.times do |x|
	pnt = points.pop
	if (pnt[0] == points[0][0] or pnt[0] == points[1][0]) and (pnt[1] == points[0][1] or pnt[1] == points[1][1]) then
		p = pnt
		break
	end
	points.push pnt
	points.rotate!
end
#puts "#{p[0]} #{p[1]}"
newP = [0,0]
if p[0] == points[0][0] then
	p1 = points[0]
	p2 = points[1]
else
	p1 = points[1]
	p2 = points[0]
end
#puts "#{p1[0]} #{p1[1]}"
#puts "#{p2[0]} #{p2[1]}"
newP[1] = p[1] + p1[1] - p[1]
newP[0] = p[0] + p2[0] - p[0]

puts "#{newP[0]+1} #{newP[1]+1}"

a = gets.to_i.times.map{ gets.strip }
b = []
a.each_with_index do |s,i|
  s.split(//).each_with_index do |c,j|
    b << [i,j] if c == "*"
  end
end
hx = Hash.new(0)
hy = Hash.new(0)
b.each do |i,j|
  hx[i] += 1
  hy[j] += 1
end

puts [hx.key(1)+1,hy.key(1)+1] * " "

require "scanf"

input = ARGF.read.lines

crime_scenes = []
row = 0
input.drop(1).each do |row_data|
	crime_scenes += row_data
		.chars
		.each_with_index
		.select { |e,i| e == '*' }
		.map { |e,i| [row, i] }

	row += 1
end

res =
	if crime_scenes[0][0] == crime_scenes[1][0] # on row with second point
		if crime_scenes[0][1] == crime_scenes[2][1]
			[crime_scenes[2][0], crime_scenes[1][1]]
		else
			[crime_scenes[2][0], crime_scenes[0][1]]
		end
	else
		if crime_scenes[0][1] == crime_scenes[2][1] # on one column with third point
			[crime_scenes[0][0], crime_scenes[1][1]]
		else
			if crime_scenes[1][0] == crime_scenes[2][0]
				[crime_scenes[0][0], crime_scenes[2][1]]
			else
				[crime_scenes[1][0], crime_scenes[2][1]]
			end
		end
	end

puts [res.first + 1, res.last + 1].join ' '

h, w = gets.split.map(&:to_i)
m = Array.new(h)
x, y, xx, yy = 101, 102, -1, -2
h.times{ |i| m[i] = gets
	w.times{ |j|
		if m[i][j, 1] == "*" then
			xx = [xx, j].max
			x = [x, j].min
			yy = [yy, i].max
			y = [y, i].min
		end
	}
}
[x, xx].each{ |a|
	[y, yy].each{ |b|
		if m[b][a, 1] == "." then
			print b + 1, " ", a + 1, "\n"
		end
	}
}

n, m = gets.split.map &:to_i
a = []
n.times { a << gets.chomp.split('') }
(0...n).each { |i| (0...m).each { |j| puts "#{i + 1} #{j + 1}" if a[i].count('*') == 1 && a.transpose[j].count('*') == 1 } }

n,m=gets.split.map(&:to_i)
i=x=y=0
table=n.times.map{i+=1;x=i if gets.count(?*)==1;$_.each_char.to_a}
i=0
table.transpose.each{|l|i+=1;y=i if l.join.count(?*)==1}
puts "%d %d"%[x,y]

n, m = gets.split.map &:to_i
mat = Array.new(n) { gets.strip }
dx = Hash.new(0)
dy = Hash.new(0)

mat.each_with_index do |row, i|
  row.chars.to_a.each_with_index do |char, j|
    if char == '*'
      dx[i] += 1
      dy[j] += 1
    end
  end
end

p dx.select { |k, v| v.odd? }.keys[0].to_i + 1
p dy.select { |k, v| v.odd? }.keys[0].to_i + 1

n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
sa = []
rety = 0
retx = 0
xa = []
for i in 0..n-1
    sa << gets.chomp
    wk = sa[i].index("*")
    if wk != nil
        wk2 = sa[i].rindex("*")
        if wk == wk2
            rety = i+1
            retx = wk+1
        else
            xa = [wk+1, wk2+1]
        end
    end
end
xa.delete(retx)
puts rety.to_s + " " + xa[0].to_s

r, c = gets.split.map(&:to_i)
x, y = 0, 0
1.upto(r){|i|
    gets.chars.each_with_index{|c, j|
        if c == '*'
            x ^= i
            y ^= j + 1
        end
    }
}
puts "#{x} #{y}"

require 'scanf'

input = ARGF.read.lines

crime_scenes = []
row = 0
input.drop(1).each do |row_data|
	crime_scenes += row_data
		.chars
		.each_with_index
		.select { |e,i| e == '*' }
		.map { |e,i| [row, i] }

	row += 1
end

res =
	if crime_scenes[0][0] == crime_scenes[1][0] # on row
		if crime_scenes[0][1] == crime_scenes[2][1]
			[crime_scenes[2][0], crime_scenes[1][1]]
		else
			[crime_scenes[2][0], crime_scenes[0][1]]
		end
	else
		if crime_scenes[0][0] == crime_scenes[2][0]
			[crime_scenes[1][0], crime_scenes[0][1]]
		else
			[crime_scenes[0][0], crime_scenes[1][1]]
		end
	end

puts [res.first + 1, res.last + 1].join ' '

require 'scanf'

input = ARGF.read.lines

crime_scenes = []
row = 0
input.drop(1).each do |row_data|
	crime_scenes += row_data
		.chars
		.each_with_index
		.select { |e,i| e == '*' }
		.map { |e,i| [row, i] }

	row += 1
end

res =
	if crime_scenes[0][0] == crime_scenes[1][0] # on row with second point
		if crime_scenes[0][1] == crime_scenes[2][1]
			[crime_scenes[2][0], crime_scenes[1][1]]
		else
			[crime_scenes[2][0], crime_scenes[0][1]]
		end
	else
		if crime_scenes[0][1] == crime_scenes[2][1] # on one column with third point
			[crime_scenes[0][0], crime_scenes[1][1]]
		else
			[crime_scenes[1][0], crime_scenes[0][1]]
		end
	end

puts [res.first + 1, res.last + 1].join ' '

require "scanf"

input = ARGF.read.lines

crime_scenes = []
row = 0
input.drop(1).each do |row_data|
	crime_scenes += row_data
		.chars
		.each_with_index
		.select { |e,i| e == '*' }
		.map { |e,i| [row, i] }

	row += 1
end

res =
	if crime_scenes[0][0] == crime_scenes[1][0] # on row with second point
		if crime_scenes[0][1] == crime_scenes[2][1]
			[crime_scenes[2][0], crime_scenes[1][1]]
		else
			[crime_scenes[2][0], crime_scenes[0][1]]
		end
	else
		if crime_scenes[0][1] == crime_scenes[2][1] # on one column with third point
			[crime_scenes[0][0], crime_scenes[1][1]]
		else
			[crime_scenes[1][0], crime_scenes[0][1]]
		end
	end

puts [res.first + 1, res.last + 1].join ' '


n,m=gets.split.map(&:to_i)
g=Array.new(n){Array.new(n,0)}
cnt=Array.new(n,0)
m.times{
        a,b=gets.split.map(&:to_i)
        a-=1
        b-=1
        g[a][b]=1
        g[b][a]=1
        cnt[a]+=1
        cnt[b]+=1
}
ans=0
loop {
        t=0
        n.times{|i|
                t+=(cnt[i]>0?1:0)
        }
        if t<=1 then break end
        t=0
        n.times{|i|
                t+=(cnt[i]==1?1:0)
        }
        if t==0 then break end
        r=cnt.clone
        n.times{|i|
                if r[i]==1 then
                        n.times{|j|
                                if g[i][j]>0 then
                                        g[i][j]-=1
                                        g[j][i]-=1
                                        cnt[i]-=1
                                        cnt[j]-=1
                                end
                        }
                end
        }
        ans+=1
}
p ans

n,m=gets.chomp.split.map(&:to_i)
i,j,f,a,b,ar=0,0,0,[],[],Array.new(100,0)
m.times do |i|
	x,y=gets.chomp.split.map(&:to_i)
	a<<(x-1);b<<(y-1)
end
while i<105
	f,c=0,Array.new(100,0)
	m.times do |j|
		if ar[a[j]]==0 && ar[b[j]]==0
			c[a[j]]+=1
			c[b[j]]+=1
		end
	end
	n.times do |j|
		if c[j]==1
			f=1
			ar[j]=1
		end
	end
	break if f==0
	i+=1
end
puts i

class Kid
    attr_accessor :tied_to, :number
    def initialize (num)
        @tied_to = []
        @number = num
    end
end

result = -1
input = STDIN.gets.strip
laces = []
input.split.last.to_i.times do |i|
    laces.push STDIN.gets.strip.split.collect {|i| i.to_i}
end

while true
    kids = Array.new(input.split.first.to_i) {|i| Kid.new(i+1)}
    result += 1
    laces.each do |i|
        kids[i[0]-1].tied_to.push i[1] unless kids[i[0]-1].tied_to.include? i[1]
        kids[i[1]-1].tied_to.push i[0] unless kids[i[1]-1].tied_to.include? i[0]
    end
    remove = []
    kids.each do |i|
        remove.push(i.number) if i.tied_to.size == 1
    end
    break if remove.size == 0
    remove.each do |i|
        laces.delete_if {|l| l.include? i}
    end
end
p result

include Math

n, m=gets.split.map(&:to_i)
graph={}

m.times{
	a,b=gets.split.map(&:to_i)

	if graph.has_key?(a)
		graph[a] << b
	else
		graph[a] = [b]
	end

	if graph.has_key?(b)
		graph[b] << a
	else
		graph[b] = [a]
	end
}

ans=0
loop{
	delete_them = []

	graph.keys.each{|key|
		if graph[key].size==1
			delete_them << key
		end
	}

	#deleting vals in arrays
	graph.keys.each{|key|
		delete_them.each{|el|	graph[key].delete(el)}
	}

	# deleting keys
	delete_them.each{|el| graph.delete(el)}

	ans+=1 if delete_them.size!=0
	break if delete_them.size==0
}

puts ans

n,m=gets.split.map(&:to_i);g=Hash.new []
m.times{a,b=gets.split.map(&:to_i);g[a]+=[b];g[b]+=[a]}
s=0;loop{d=[];g.keys.each{|k| d << k if g[k].size==1};g.keys.each{|k|d.each{|l|g[k].delete(l)}};d.each{|l|g.delete(l)};s+=1 if d.size!=0;break if d.size==0};p s

n,m=gets.split.map(&:to_i);g=Hash.new [];m.times{a,b=gets.split.map(&:to_i);g[a]+=[b];g[b]+=[a]};s=0;loop{d=[];g.keys.each{|k|d<<k if g[k].size==1};g.keys.each{|k|d.each{|l|g[k].delete(l)}};d.each{|l|g.delete(l)};d.empty? ? break: s+=1};p s

n,m=gets.split.map(&:to_i);g=Hash.new [];m.times{a,b=gets.split.map(&:to_i);g[a]+=[b];g[b]+=[a]};s=0;loop{d=[];g.keys.each{|k|d << k if g[k].size==1};g.keys.each{|k|d.each{|l|g[k].delete(l)}};d.each{|l|g.delete(l)};s+=1 if !d.empty?;break if d.empty?};p s

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
lace_connect = []
for i in 0...m
    lace_connect[i] = gets.chomp.split.map{|j| Integer(j)}
end
require 'pp'
connection = []
for i in 0...n
    connection[i] = Array.new(n)
end
for j in 0...m
    connection[lace_connect[j][1] - 1][lace_connect[j][0] - 1] = true
    connection[lace_connect[j][0] - 1][lace_connect[j][1] - 1] = true
end

groups = []
count = 0
kick_out = 0
for h in 0...n
    for i in 0...n
        for j in 0...n
            if connection[i][j]
                count += 1
            end
        end
        if count == 1
            groups.push(i)
        end
        count = 0
    end
    if groups.length != 0
        for i in groups
            for j in 0...n
                if connection[i][j]
                    connection[i][j] = false
                    connection[j][i] = false
                end
            end
        end
        kick_out += 1
        groups = []
    else
        break
    end
end
puts kick_out

require 'Set'

class Vertex
	attr_accessor :list,:name
	def initialize(name)
		self.name = name
		self.list = Set.new
	end

	def add_element(v)
		self.list << v
	end

	def remove_element(v)
		self.list.delete(v)
	end
end

class Graph
	attr_accessor :vertices
	def initialize
		self.vertices = Set.new
	end

	def add(v)
		self.vertices << v
	end

	def get(name)
		self.vertices.detect{|v| v.name == name}
	end
end

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

g = Graph.new
n.times do |i|
	g.add(Vertex.new(i+1))
end

m.times do
	a,b = gets.split(" ").map(&:to_i)
	x,y = g.get(a),g.get(b)
	x.add_element(y)
	y.add_element(x)
end
count = 0
while true
	removed = false
	removal = []
	g.vertices.each do |a|
		if a.list.count == 1
			removal << [a,a.list.first]
			removed = true
		end
	end
	if removed
		removal.each do |(a,el)|
			a.remove_element(el)
			el.remove_element(a)
		end
		count += 1
	else
		break
	end
end

puts count

require 'Set'

class Vertex
	attr_accessor :list,:name
	def initialize(name)
		self.name = name
		self.list = Set.new
	end

	def add_element(v)
		self.list << v
	end

	def remove_element(v)
		self.list.delete(v)
	end
end

class Graph
	attr_accessor :vertices
	def initialize
		self.vertices = Set.new
	end

	def add(v)
		self.vertices << v
	end

	def get(name)
		self.vertices.detect{|v| v.name == name}
	end
end

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

g = Graph.new
n.times do |i|
	g.add(Vertex.new(i+1))
end

m.times do
	a,b = gets.split(" ").map(&:to_i)
	x,y = g.get(a),g.get(b)
	x.add_element(y)
	y.add_element(x)
end
count = 0
while true
	removed = false
	dont_check = Hash.new(false)

	g.vertices.each do |a|
		next if dont_check[a.name]
		if a.list.count == 1
			el = a.list.first
			a.remove_element(el)
			el.remove_element(a)
			dont_check[el.name] = true
			removed = true
		end
	end
	if removed
		count += 1
	else
		break
	end
end

puts count

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

tied = m.times.map { gets.split.map(&:to_i) }

hash = Hash.new { |h, k| h[k] = [] }
tied.each do |a, b|
  hash[a] << b
  hash[b] << a
end

kicked = 0
loop do
  kick = hash.select { |k, v| v.size == 1 }
  if kick.any?
    kick.each do |a, b|
      b = b.first
      hash.delete a
      hash[b].delete a
    end
    kicked += 1
  else
    break
  end
end

p kicked

n, m = gets.split.map(&:to_i)
a = Array.new(n){[false] * n}
c = [0] * n
v = n.times.to_a
m.times do
	x, y = gets.split.map{|_| _.to_i - 1}
	a[x][y] = a[y][x] = true
	c[x] += 1
	c[y] += 1
end

ans = 0
loop do
	w = v.select{|i| c[i] == 1}
	break if w.empty?
	w.each do |i|
		next if c[i] != 1
		j = a[i].index(true)
		a[i][j] = a[j][i] = false
		c[i] -= 1
		c[j] -= 1
	end
	ans += 1
end
p ans

n, m = gets.split.map(&:to_i)
e = []
0.upto(n+1){ |i| e[i] = [] }
m.times{
 a, b = gets.split.map(&:to_i)
 e[a] << b
 e[b] << a
}
ans = 0
while true
 f = true
 ne = Marshal.load(Marshal.dump(e))
 e.each_with_index{ |x,i|
  next if x.length != 1
  ne[x[0]].delete(i)
  ne[i] = []
  f = false
 }
 e = ne
 break if f
 ans += 1
end
p ans

class Kid
    attr_accessor :tied_to, :number
    def initialize (num)
        @tied_to = []
        @number = num
    end
end

result = -1
input = STDIN.gets.strip
laces = []
input.split.last.to_i.times do |i|
    laces.push STDIN.gets.strip.split.collect {|i| i.to_i}
end

while true
    kids = Array.new(input.split.first.to_i) {|i| Kid.new(i+1)}
    result += 1
    laces.each do |i|
        kids[i[0]-1].tied_to.push i[1] unless kids[i[0]-1].tied_to.include? i[1]
        kids[i[1]-1].tied_to.push i[0] unless kids[i[1]-1].tied_to.include? i[0]
    end
    remove = []
    kids.each do |i|
        remove.push(i.number) if i.tied_to.size == 1
    end
    break if remove.size == 0
    remove.each do |i|
        laces.delete_if {|l| l.include? i}
    end
    break if result == 2
end
p result

n, m = gets.split.map(&:to_i)
e = []
0.upto(n+1){ |i| e[i] = [] }
m.times{
 a, b = gets.split.map(&:to_i)
 e[a] << b
 e[b] << a
}
ans = 0
while true
 f = true
 ne = e.clone
 e.each_with_index{ |x,i|
  next if x.length != 1
  x.each{ |y| ne.each{ |z| z.delete(y) } }
  e[i] = []
  f = false
 }
 e = ne
 break if f
 ans += 1
end
p ans


n, k = gets.split.map(&:to_i)
s = gets.chop
arr = [0] * 1000
cur = [0, '0']
s.split('').each do |c|
  if (c == cur[1])
    cur[0] += 1
  else
    cur[0] = 1
    cur[1] = c
  end
  if (cur[0] == k)
    arr[c.ord] += 1
    cur[0] = 0
  end
end
puts arr.max

DBG = true
n,k = gets.split.map{|z| z.to_i}
s = gets.chomp
alpha = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
          'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
           'w', 'x', 'y', 'z' ]
lv = 0
for c in alpha
  cnt = 0
  len = 0
  for i in 0...n
    if s[i] == c
      len += 1
      if len == k
        len = 0
        cnt += 1
      end
    else
      len = 0
    end
  end
  lv = cnt if cnt > lv
end
puts "#{lv}"

require 'set'

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

nk = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]

hash = Hash.new(0)
last = Hash.new(-1)
str = gets.chomp

breakpoints = Set.new
(0..n-1).each do |i|
	if str[i]!=str[i+1]
		breakpoints.add(i)
	end
end

toskip = Set.new
breakpoints.each do |br|
	(br-k+2..br).each do |x|
		toskip.add(x)
	end
end

(0..n-k).each do |st|
	next if toskip.include?(st)
	char = str[st]
#	thestr = str[st..st+k-1]
#	next if thestr.each_char.to_a.uniq.length>1
	itslast = last[char]
	next if itslast>=st

	hash[char]+=1
	last[char] = st+k-1
end
#p hash
#p toskip
if hash.empty?
	puts 0
else
	puts hash.values.max
end

#for 1 to k starting index, take steps of k


n = gets.to_i
count = 0
for i in 1..n
  buf = gets.split(' ').map(&:to_i)
  a, b, c = buf[0], buf[1], buf[2]
  count += 1 if a + b + c >= 2
end
puts count

ans=0
a=[]
gets.to_i.times{|i|
	a<<gets.chomp.delete(' ').delete('0')
}
a.each {|i|
	if i[/11/]=="11" then ans+=1 end	}
p ans

number_of_problems =  gets.chomp.to_i
number_of_solved = 0
(0...number_of_problems).each do |iteration|
  arr = gets.split(" ").map(&:to_i)
  number_of_solved+=1 if  arr.count(1) >= 2
end
puts number_of_solved

x=0
gets.chomp.to_i.times {
	a,b,c=gets.split.map &:to_i
	x+=1 unless a+b+c < 2
}
puts x

puts gets.to_i.times.map{gets}.count{|x| x.count("1") >= 2}

T = gets.chomp.to_i
ans = 0
for i in (1..T)
  a = gets.chomp.split(' ')
  ans += (a.count("1") > 1) ? 1 : 0
end
puts ans

n = gets.to_i
ans = 0
n.times do
	f, s, t = gets.split.map{|x| x.to_i}
	ans += 1 if f + s + t >= 2
end
p ans

b = gets.chomp
a = 0
array = []
while a < b.to_i
  n = STDIN.gets.split(" ")
  if (n[0].to_i + n[1].to_i + n[2].to_i) > 1
    array << 1
  end
  a += 1
end

puts array.size

#my code goes here
a = []
k = 0
n = gets.to_i
for c in 1..n
s = 0
a = gets.chomp.split().map { |e| e.to_i }
a.each do |x|
  s += x
end
if s >= 2
k = k + 1
else
k = k + 0
end
end
puts "#{k}"

n = gets.chomp.to_i
cnt = 0
n.times do
	cnt += 1 if gets.chomp.split.map(&:to_i).count(1) >= 2
end
puts cnt

num = gets.chomp.to_i
array = []
i = 0
while (i < num)
	array.push gets.chomp
	i = i+1
end
aux = 0
array.each do |leng|
	line = leng.split
	if (line[0].to_i + line[1].to_i + line[2].to_i >= 2)
		aux = aux + 1
	end
end
puts aux.to_s

n = gets.to_i
puts n.times.map{gets.split.map(&:to_i).inject(:+)}.select{|x| x >= 2}.size

going_to_solve = 0
gets.to_i.times { going_to_solve += 1 if gets.gsub(" ", "").count("1") >= 2 }
puts going_to_solve

#!/usr/bin/env ruby
input = gets.to_i

answer = 0
input.times do
  inp = gets.split(' ').map(&:to_i).inject(0, :+)
  answer += 1 if inp >= 2
end

puts answer

p = 0
gets.chomp.to_i.times do |variable|
	c = gets.chomp.count("1")
	(p = p + 1) if c > 1
end
puts p

a=gets.chomp.to_i
ans=0
a.times do
b=gets.chomp
if b.count("1")>=2
ans+=1
end
end
puts "#{ans}"

a=0
STDIN.read.split("\n").drop(1).each do |w|
  a += 1 if w.chomp.split(' ').map{|n| n.to_i}.inject(:+) >= 2
end
puts a

n = gets.to_i
a =[]
j = 0

for i in 1..n do
	a = gets.split.map(&:to_i)
	if a.count(1) > 1
		j = j+1
	end
end

puts j

n = gets.to_i

problems = []
1.upto(n) do |idx|
    problem = gets.gsub("\n", "")
    problem = problem.split(" ").map(&:to_i)
    problems.push(problem)
end

problems_implemented = 0
problems.each do |problem|
    sureness = 0
    problem.each do |friend|
        sureness += friend
    end
    if sureness >= 2
        problems_implemented += 1
    end
end

puts problems_implemented

p gets.to_i.times.map{gets}.count{|x| x.count('1') >= 2}

n, *s = STDIN.read.split("\n") #gets.sFile.readlines('inp.txt')
ans = 0
s.each do |i|
	ans += 1 if i.count('1') > 1
end
p ans

def answers ()
  n = gets.chomp
  n = n.to_i
  final = 0

  for i in 0...n
    count = 0
    test = Array.new
    test = gets.split(' ').map(&:to_i)
    test.each { |x|
      if x == 1
      count+=1
      end
    }
      if count >= 2 then
        final +=1
      end
  end
  puts final
end

answers

res=0
gets.to_i.times{(gets.count(?1))>=2?res+=1:""}
puts res

n = gets.chomp.to_i

count = 0

while n > 0
   a = gets.chomp
   if a[0].to_i + a[2].to_i + a[-1].to_i >= 2
      count += 1
   end
   n -= 1
end

puts count

n = gets.to_i
m = 0;
n.times do
  x, y, z = gets.split.map(&:to_i)
  m += 1 if x + y + z >= 2
end
puts m

line = gets.chomp
string_value = []
line.to_i.times do |i|
	value = gets.chomp
	string_value << value
end

count = 0
string_value.each do |value|
	if value.split.count{|x| x.to_i == 1} >= 2
		count += 1
	end

end
puts count

n, count = gets.chomp.to_i, 0
n.times do
  count += 1 if (gets.chomp.split(' ').map(&:to_i).reduce(:+) > 1)
end
puts count

$res = 0
$cnt = 0

gets.to_i.times{|x| x = gets.split.map(&:to_i)
    x.each do |num|
        if num == 1
            $cnt += 1
        end
    end
    if $cnt >= 2
        $res += 1
    end
    $cnt = 0
}

puts $res

n = gets.chomp.to_i
puts n.times.select {|i|gets.chomp.split(' ').map(&:to_i).select{|i| i > 0}.count > 1 }.count

def get_num
    input = gets.chomp.split
    x = 0
    for i in 0..input.length - 1
        x = x + input[i].to_i
    end
    if x >= 2
        return 1
    else
        return 0
    end
end

$n = gets.chomp.to_i
ans = 0
for i in 1..$n
    ans += get_num
end

puts ans

n = gets.chomp.to_i
c = 0
while n > 0
    s1, s2, s3= gets.chomp.split.map(&:to_i)
    if s1+s2+s3 >= 2
         c = c + 1
    end
        n -=1
end
puts c

problems_count = gets.chomp.to_i

counter = 0
problems_count.times.each do
  submission_string = gets.chomp
  submission = submission_string.split(' ').map(&:to_i)
  el_counter = 0
  submission.each do |el|
    el_counter += 1 if el.eql?(1)
  end
  counter += 1 if el_counter >= 2
end

puts counter

puts STDIN.readlines[1..-1].map(&:strip).map(&:split).inject(0) { |memo, w| memo + (w.find_all { |x| x=="1"}.size >= 2 ? 1 : 0) }

n = gets.to_i

peta = []
counter = 0
for i in 0..n-1
  peta[i] = gets.split.map(&:to_i)
  counter+=1 if peta[i].count(1)>=2
end

puts counter

cnt = 0
n = gets.chomp.to_i
n.times{cnt += 1 if gets.chomp.split.map(&:to_i).reduce(:+) > 1}
puts cnt

number = gets.strip.to_i

count = 0
number.times do
  input = gets.strip
  count += 1 if input.split(' ').count('1') >= 2
end

puts count

def chk(ans)
	n=0
	for i in ans
		if i==1
			n+=1
		end
	end
	return n
end
n = gets.to_i()
problem =0
while n>0
	n-=1
	ans = gets.split().map{|x| x.to_i}
	if chk(ans)>1
		problem+=1
	end
end
puts problem

n = gets.to_i
problems_solved = 0
n.times {
    arr = gets.split(" ").map(&:to_i)
    if arr.select{|a| a==1 }.count >=2
        problems_solved+=1
    end
}
puts problems_solved

n =  gets.to_i
f = []
s = []
l = []
arrs = []
implements  = 0

n.times do
  q = gets.strip.split(" ").map(&:to_i)
  f << q[0]
  s << q[1]
  l << q[2]
  arrs << q
end

arrs.each do |arr|
  implements += 1 if arr.count{|one| one == 1}  > 1
end
puts implements

n = gets.strip.to_i
count = 0
n.times do |i|
  arr = gets.strip.split(" ").map(&:to_i)
  count += 1 if arr.count{|elem| elem == 1} > 1
end
puts count

N = gets.to_i
as = N.times.map{gets.split.count{|bb| bb.to_i.zero? }}

puts as.count{|a| a < 2 }

N = gets.to_i
as = N.times.map{gets.chomp.gsub('0','')}
puts as.count{|a| a.size > 3 }

N = gets.to_i
as = N.times.map{gets.split.count{|bb| bb == '0' }}

puts as.count{|a| a < 2 }

res = 0
gets.to_i.times do
  input = gets.split.map(&:to_i)
  res += 1 if input.select { |item| item == 1 }.size >= 2
end
puts res

amountOfProblems = gets.chomp#get input on first line
amountOfProblems = amountOfProblems.to_i
willWorkSolution = 0
secondCounter = 0

amountOfProblems.times do
    willWorkSolution = gets.chomp # 1 0 1 - space separated other input
    willWorkSolution = willWorkSolution.split(" ") # array is: ["1", "0", "1"]
    counter = 0

    willWorkSolution.each do |one_or_zero|
        if (one_or_zero == "1")
           counter = counter + 1
            if (counter == 2)
                secondCounter = secondCounter + 1
            end
        end

    end
end

puts secondCounter

i = gets.to_i
counter = 0
(1..i).each do |x|
  counter += gets.split(' ').map(&:to_i).reduce(:+) >= 2 ? 1 : 0
end
print counter

n = gets.to_i
ans = 0
n.times do
  if gets.split.map(&:to_i).inject(:+) >= 2
    ans += 1
  end
end
puts ans

problemNum = gets.chomp.to_i

result = 0

(0...problemNum).each do |iter|
	input = gets.chomp.split(' ').map {|item| item.to_i}
	result += 1 if input.count(1) > 1
end

puts result

#!/usr/bin/env ruby

count = gets.to_i
sum = 0

(0..count-1).each do |i|
	row = gets.split(" ")
	row_sum = 0
	row.each do |r|
		row_sum += r.to_i
	end
	if row_sum > 1 then
		sum+=1
	end
end

puts sum

count = gets.to_i

res = 0
count.times do |i|
    res += 1 if gets.split(" ").map(&:to_i).inject(:+) > 1
end

p res

d=0
gets.to_i.times{
	a,b,c=gets.split.map{|e| e.to_i}
	d=d+(a+b+c>1?1:0)
}
puts d

require 'scanf'
n = $stdin.readline.to_i

counter = 0
n.times do
  a, b, c = $stdin.readline.scanf("%d%d%d")
  counter += 1 if a + b + c >=2
end

puts counter

count = 0

gets.strip.to_i.times do
  p, v, t = gets.strip.split(' ').map(&:to_i)
  count +=1 if p + v + t >= 2
end

puts count

n = gets.chomp.to_i
ans = 0
n.times{
  num = gets.chomp.split.map{|x| x.to_i }
  ans += 1 if num.inject{ |sum,x| sum + x } >= 2
}
puts ans

#my code goes here
a = []
k = 0
n = gets.to_i
for c in 1..n
for b in 0...3
s = 0
a[b] = gets.to_i
a.each do |x|
  s += x
end
end
if s >= 2
k = k + 1
else
k = k + 0
end
end
puts "#{k}"

gets.to_i.times.map{gets.split.map(&:to_i).inject(:+)}.select{|x| x >= 2}.size

n = gets.to_i
ans = 0

(1..n).each do |i|
  res = 0
  (1..3).each do |j|
    tmp = gets.to_i
    res += tmp
  end

  if res >= 2
    ans += 1
  end
end

puts ans

ans=0
a=[]
gets.to_i.times{|i|
	a<<gets.chomp.delete(' ')
}
a.each {|i|
	if i[/11/]=="11" then ans+=1 end	}
p ans

a = gets.to_i
count = 0
index = 0
loop do
	break if index > a -1
	b = gets.chomp(" ")
	temp = 0
	#puts b[0].to_s + " - This is it!"
	if b[0].to_i == 1
		temp = temp + 1
	end
	if b[1].to_i == 1
		temp = temp + 1
	end
	if b[2].to_i == 1
		temp = temp + 1
	end
	if temp>=2
		count = count + 1
	end
	index = index + 1
end
puts count

n = gets.to_i
l = []
(1..n).each { l << gets.split.map(&:to_i) }
puts l.transpose.map { |x| x.reduce(:+) }.reduce(0) { |res, x| res + (x > 1 ? 1 : 0) }

p = gets.chomp.to_i
s = 0

for i in 1..p do
    a = gets.chomp.split(" ").sort.join("")
    if a.to_i != 1
        s += 1
    end
end

puts s

gets.to_i.times do
    a = gets.split.map(&:to_i).inject(:+)
    a += a
end

n, *s = STDIN.read.split("\n") #gets.sFile.readlines('inp.txt')
puts s.length
ans = 0
s.each do |i|
	if i[0].to_i + i[2].to_i + i[4].to_i >= 2
		ans += 1
	end
end
puts ans

n, *s = gets.split('/n') #File.readlines('inp.txt')
ans = 0
s.each do |i|
	if i[0].to_i + i[2].to_i + i[4].to_i >= 2
		ans += 1
	end
end
puts ans

amountOfProblems = gets.chomp#get input on first line
amountOfProblems = amountOfProblems.to_i
willWorkSolution = 0
solutionsTeamWillWork = []

amountOfProblems.times do
    willWorkSolution = gets.chomp # 1 0 1 - space separated other input
    willWorkSolution = willWorkSolution.split(" ") # array is: ["1", "0", "1"]

    willWorkSolution.each do |one_or_zero|
        if (one_or_zero == "1")
           solutionsTeamWillWork.push(one_or_zero)
        end
    end
end

puts solutionsTeamWillWork.size / amountOfProblems

amountOfProblems = gets.chomp#get input on first line
amountOfProblems = amountOfProblems.to_i
willWorkSolution = 0
solutionsTeamWillWork = []

amountOfProblems.times do
    willWorkSolution = gets.chomp # 1 0 1 - space separated other input
    willWorkSolution = willWorkSolution.split(" ") # array is: ["1", "0", "1"]

    counter = 0

    willWorkSolution.each do |one_or_zero|

        if (one_or_zero == "1")
           counter = counter + 1
           if (counter >= 2)
              solutionsTeamWillWork.push(1)
           end
        end
    end
end

puts solutionsTeamWillWork.size

n = gets.chomp.to_i
a = []
res = 0
for i in 0...n
    a[i] = gets.chomp
    if a.count('1')>=2
        res+=1
    end
end
puts res


INF = 10 ** 20
n = gets.to_i
gets
d = [INF] * (n + 1)
gets.to_i.times do
	a, b, c = gets.split.map(&:to_i)
	d[b] = [d[b], c].min
end
s = d.inject(&:+) - 2 * INF
p s < INF ? s : -1

n = gets.chomp.to_i
q = gets.chomp.split(' ').map &:to_i; q.unshift nil
m = gets.chomp.to_i
result = {}
m.times do
  a, b, c = gets.chomp.split(' ').map &:to_i
  next if q[a] <= q[b]
  result[b] = result[b] ? [result[b], c].min : c
end
puts result.length == n - 1 ? result.values.inject(:+).to_i : -1

n=gets.chomp.to_i
gets
m=gets.chomp.to_i
max=100000000
memo=Array.new
n.times{memo << max}
m.times{
  tmp=gets.chomp.split()
  b=tmp[1].to_i()-1
  c=tmp[2].to_i
  if memo[b] > c
    memo[b]=c
  end
}
sum=0
cnt=0
n.times{|i|
  if memo[i]<max
    sum+=memo[i]
  else
    cnt+=1
  end
}
if cnt==1
  print(sum)
else
  print(-1)
end

#!/usr/bin/ruby

n = gets.to_i
qs = gets.chomp.split.map{|e| e.to_i}
m = gets.to_i

UNSET = 9999999

employees = Array.new(n){Array.new(n, UNSET)} # start from 0!

m.times do
  a, b, c = gets.chomp.split.map{|e| e.to_i}
  if (employees[b - 1][a - 1] > c)
    employees[b - 1][a - 1] = c
  end
end

acc = 0
root = 0

n.times {|i|
  min = employees[i].min
  if (min == UNSET)
    root += 1
    if (root > 1)
      puts -1
      exit
    end
  else
    acc += min
  end
}

puts acc

n = gets.chomp.to_i
q = gets.chomp.split(' ').map &:to_i; q.unshift nil
m = gets.chomp.to_i
result = {}
m.times do
  a, b, c = gets.chomp.split(' ').map &:to_i
  next if q[a] <= q[b]
  result[b] = result[b] ? [result[b], c].min : c
end
puts result.length == n - 1 ? result.values.inject(:+) : -1

n = gets.chomp.to_i
q = gets.chomp.split(' ').map &:to_i
m = gets.chomp.to_i
result = {}
m.times do
  a, b, c = gets.chomp.split(' ').map &:to_i
  result[b] = result[b] ? [result[b], c].min : c
end
puts result.length == n - 1 ? result.values.inject(:+) : -1


#!/usr/bin/ruby1.9

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

move = [0] + STDIN.readline.split.collect {|s| s.to_i }

curr = s
steps = 0

while true
	break if curr == t
	steps += 1
	curr = move[curr]
	if curr == s
		steps = -1
		break
	end
end

puts steps

n,s,t=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i).unshift('a')
d,k=s,0
while d!=t
	d=a[d]
	k+=1
	if k>n
		k=-1
		break
	end
end
puts k

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    n.times do |turn|
      if s == t
        puts turn
        exit
      elsif s == operation[s-1]
        puts -1
        exit
      else
        s = operation[s-1]
      end
    end
    puts -1
  end
end

b = B.new

n,s,t=gets.split.map(&:to_i)
p=gets.split.map(&:to_i)
p.unshift(0)
v=s
res=-1
0.upto(n){|x|
	if v == t then
		res=x
		break
	end
	v=p[v];
}
puts res

n,s,t = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
visited = Array.new(n,false)
ans = -1
i = s - 1
count = 0
if s == t
	ans = 0
else
	while true
		visited[i] = true
		if !visited[arr[i]-1]
			i = arr[i] - 1
			count += 1
		else
			# puts "breaking here #{arr[i]} #{visited}"
			break
		end
		if i == t - 1
			ans = count
			break
		end

	end
end
# puts "#{visited} #{i}"
puts ans

n,s,t = gets.chomp.split(" ").collect {|x| x.to_i}

arr = gets.chomp.split(" ").collect {|x| (x.to_i-1)}

aux = [0]*n

(0...n).each{ |v|
  aux[arr[v]] = v
}

q = 0
s-=1
t-=1

while s!=t and q<n
	t = aux[t]
	q += 1
end

if q==n
	puts -1
else
	puts q
end

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

k = 0

while s!=t do
    k+=1

    m = p[s-1]
    p[s-1] = -1
    s = m

    if s==-1
        k = -1
        break
    end
end

puts k

require 'set'

def read_next_line
  gets.split.map { |x| x.to_i }
end

_, s, t = read_next_line
p = read_next_line
w = Set.new << s
c = 0
while s != t
  s = p[s - 1]
  if w.include? s
    puts -1
    exit
  end
  w << s
  c += 1
end
puts c

#!/usr/bin/env ruby

n, s, t = gets.split.collect{ |i| i.to_i }
p = gets.split.collect{ |i| i.to_i - 1 }

s = s - 1
t = t - 1
n.times do |i|
	if s == t
		puts i
		exit
	end
	s = p[s]
end

puts -1

n, s, t = gets.strip.split.map(&:to_i)
ps = gets.strip.split.map(&:to_i)

# find cycles
cycles = []
#seq = (1..n).to_a.map{|x| [x,false]}
free = [true]*n
i = 0
while i < n
  j = i
  i += 1
  next unless free[j]

  cycle = []
  while free[j]
    free[j] = false
    cycle << (j + 1)
    j = ps[j] - 1
  end
  cycles << cycle
end

#p cycles

cycle = cycles.find{|c| c.include?(s)}
if cycle.include?(t)
  si = cycle.index s
  ti = cycle.index t
  result = (ti - si) % cycle.length
else
  result = -1
end

puts result

n, s, t = gets.split.map &:to_i
p = [0] + gets.split.map(&:to_i)

visited = {s => true}

shuffle_count = 0
current_s = s
until current_s == t do
  if visited.has_key? p[current_s]
    puts -1
    exit
  end
  current_s = p[current_s]
  shuffle_count += 1
  visited[current_s] = true
end

puts shuffle_count

n, s, t = gets.split.map &:to_i
p = gets.split.map &:to_i

visited = {s => true}

shuffle_count = 0
current_s = s
until current_s == t do
  if visited.has_key? p[current_s - 1]
    puts -1
    exit
  end
  current_s = p[current_s - 1]
  shuffle_count += 1
  visited[current_s] = true
end

puts shuffle_count

I =->{gets.split.map &:to_i}
def gao
	n, s, t = I[]; return 0 if s==t
	s -= 1; t -= 1; a = I[]; vis = Array.new(n, 0); s = a[s] - 1; res = 1; while (s != t) do
		vis[s] = 1; s = a[s]-1; res += 1
		return -1 if !vis[s].zero?
	end
	res
end
puts gao()

st = gets.split
n=st[0].to_i
s=st[1].to_i
t=st[2].to_i

mas = gets.split

count=0
path_exists=false
if s==t
  puts 0
else
  cur=s
  (n+1).times do
    cur=mas[cur-1].to_i
    count+=1
    if cur==t
      path_exists = true
      break
    end
  end
  if path_exists
    print(count)
  else
    print(-1)
  end
end

#!/usr/bin/env ruby -w

require 'set'

n_glasses, ball_start, ball_end = gets.split(' ').map(&:to_i)
ball_start -= 1
ball_end   -= 1

marbles = gets.split(' ').map(&:to_i).map(&:pred)

iterations   = 0

visited = Set.new

actual_index = ball_start
until actual_index == ball_end || iterations == -1
  if visited.include? actual_index
    iterations = -1
  else
    visited << actual_index
    actual_index = marbles[actual_index]
    iterations += 1
  end
end

print iterations

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

p = [0] + gets.split.map(&:to_i)

a = s;
count = 0
until a == t
  a = p[a]
  count += 1
  if a == s
    count = -1
	break
  end
end

puts count

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    n.times do |turn|
      if s == operation[s-1]
        puts -1
        break
      elsif s == t
        puts turn
        break
      else
        s = operation[s-1]
      end
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    turn = 0
    operation.each do |pos|
      turn += 1
      if s == pos
        diff += 1
      else
        s = pos
      end
      break if t == s
    end

    if t == s && n > turn
      puts turn - diff
    elsif diff > 0 && t == s
      puts n - diff
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    n.times do |turn|
      if s == t
        puts turn
        break
      elsif s == operation[s-1]
        puts -1
        break
      else
        s = operation[s-1]
      end
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    turn = 0
    operation.each do |pos|
      turn += 1
      if s == pos
        diff += 1
      else
        s = pos
      end
      break if t == s
    end

    if t == s && n >= turn
      if diff > 0
        puts "#{turn - diff}"
      else
        puts -1
      end
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    turn = 0
    operation.each do |pos|
      turn += 1
      if s == pos
        diff += 1
        s = pos
      else
        s = pos
      end
      break if t == s
    end

    if t == s && n > turn
      puts turn - diff
    elsif diff > 0 && t == s
      puts n - diff
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    list = []

    turn = 0
    operation.each do |pos|
      turn += 1
      if list.include?(pos)
        diff += 1
      else
        s = pos
        list << pos
      end
      break if t == s
    end

    if t == s && n >= turn
      if diff > 0
        puts "#{turn - diff}"
      else
        puts -1
      end
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    operation.each do |pos|
      if s == pos
        diff += 1
        s = pos
      else
        s = pos
      end
    end

    if diff > 0
      puts n-diff
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    turn = 0
    operation.each do |pos|
      turn += 1
      if s == pos
        diff += 1
        s = pos
      else
        s = pos
      end
      break if t == s
    end

    if diff > 0 && t == s && n > turn
      puts turn - diff
    elsif diff > 0 && t == s
      puts n - diff
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    n.times do |turn|
      if s == t
        puts turn
        break
      elsif s == operation[s-1]
        puts -1
        break
      else
        s = operation[s-1]
      end
    end
    puts -1
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    turn = 0
    operation.each do |pos|
      turn += 1
      if s == pos
        diff += 1
      else
        s = pos
      end
    end

    if t == s && n >= turn
      if diff > 0
        puts "#{turn - diff}"
      else
        puts -1
      end
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    operation.each do |pos|
      if s == pos
        diff += 1
        s = pos
      else
        s = pos
      end
    end

    if diff > 0 && t == s
      puts n-diff
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    elsif s == operation[0]
      puts n-1
    else
      puts -1
    end

  end
end

b = B.new

class B
  def initialize
    n, s, t = $stdin.gets.split(' ').map(&:to_i)
    operation = $stdin.gets.split(' ').map(&:to_i)

    if s == t
      puts 0
      return 0
    end

    diff = 0

    turn = 0
    operation.each do |pos|
      turn += 1
      if s == pos
        diff += 1
      else
        s = pos
      end
      break if t == s
    end

    if t == s && n > turn
      puts "#{turn - diff}"
    elsif diff > 0 && t == s
      puts "#{n - diff}"
    else
      puts -1
    end
  end
end

b = B.new


def in_range(i, j, n)
  i < n && j < n
end

gets.strip.to_i.times do |t|
  n = gets.strip.to_i
  board = []
  n.times do
    board << gets.strip
  end
  res = true
  (0..n-1).to_a.each do |i|
    (0..n-1).to_a.each do |j|
      if board[i][j] == '1'
        if in_range(i+1, j, n) && board[i+1][j] == '0' && in_range(i, j+1, n) && board[i][j+1] == '0'
          res = false
          break
        end
      end
    end
    break if res == false
  end

  puts res ? "YES" : "NO"
end

t = gets.chomp.to_i
t.times do |testcase|
  n = gets.chomp.to_i
  grid = []
  n.times do |i|
    grid << gets.chomp
  end
  flag = true
  n.times do |i|
    n.times do |j|
      if grid[i][j] == "1"
        if !(i==n-1 || j==n-1 || grid[i+1][j]=="1" || grid[i][j+1]=="1")
          flag = false
          break
        end
      end
    end
    if !flag
      break
    end
  end
  if flag
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.to_i
t.times do
  n = gets.to_i
  s = n.times.map { gets.chomp }
  ok = true
  (n - 2).downto(0) do |i|
    (n - 2).downto(0) do |j|
      if s[i][j] == "1"
        if !(s[i][j + 1] == "1" || s[i + 1][j] == "1")
          ok = false
          break
        end
      end
    end
    break if !ok
  end
  puts ok ? "YES" : "NO"
end

def func(a)
    a.map{|e| e.join.sub(/([o1]*)[o1]$/){|s|s=?o*$1.size+?o}.split('')}
end

def func2(a)
    a.map{|e| e.join.sub(/^(1+)o/){|s|s=?o*$1.size+?o}.split('')}
end

gets.to_i.times{
    n=gets.to_i
    a=(1..n).map{gets.chomp.chars}
    a=func(a)
    a=func(a.transpose)
    a=func2(a)
    a=func2(a.transpose.map(&:reverse))
    #a.map{|e|puts e.join}
    puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)
}

def func(a)
    a.map{|e| e.join.sub(/([o1]*)[o1]$/){|s|s=?o*$1.size+?o}.split('')}
end

def func2(a)
    a.map{|e| e.join.sub(/(1+)o/){|s|s=?o*$1.size+?o}.split('')}
end

gets.to_i.times{
    n=gets.to_i
    a=(1..n).map{gets.chomp.chars}
    a=func(a)
    a=func(a.transpose)
    a=func2(a)
    a=func2(a.transpose)
    puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)
    #a.map{|e|puts e.join}
}

def func(a)
    n=a.size
    q=[]
    (0...n).each do |i|
        if a[i][-1]==?1
            q << [i, n-1]
        elsif a[-1][i]==?1
            q << [n-1, i]
        end
    end

    while q[0]
        r, c=q.pop
        a[r][c]=?*
        [[0, -1], [-1, 0]].each do |dr, dc|
            nr=r+dr
            nc=c+dc
            next if nr<0 or nr>=n or nc<0 or nc>=n
            next if a[nr][nc]!=?1
            q << [nr, nc]
        end
    end
end

gets.to_i.times do
    n=gets.to_i
    a=(1..n).map{gets.chomp}
    func(a)

    puts (a.none?{|e|e[?1]} ? :YES : :NO)
    #a.map{|e|puts e}; puts
end

def func(a)
    n=a.size
    q=[]
    (0...n).each do |i|
        if a[i][-1]==?1
            q << [i, n-1]
        elsif a[-1][i]==?1
            q << [n-1, i]
        end
    end

    while q[0]
        r, c=q.shift
        a[r][c]=?*
        [[0, -1], [-1, 0]].each do |dr, dc|
            nr=r+dr
            nc=c+dc
            next if nr<0 or nr>=n or nc<0 or nc>=n
            next if a[nr][nc]!=?1
            q << [nr, nc]
        end
    end
end

gets.to_i.times do
    n=gets.to_i
    a=(1..n).map{gets.chomp}
    func(a)

    puts (a.none?{|e|e[?1]} ? :YES : :NO)
    #a.map{|e|puts e}; puts
end

def func(a)
    a.map{|e| e.join.sub(/([#1]*)[#1]$/){|s|s=?#*$1.size+?#}.split('')}
end

def func2(a)
    a.map{|e| e.join.gsub(/(1+)#/){|s|s=?#*$1.size+?#}.split('')}
end

gets.to_i.times{
    n=gets.to_i
    a=(1..n).map{gets.chomp.chars}
    a=func(a)
    a=func(a.transpose)
    a=func2(a)
    a=func2(a.transpose)
    puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)
    #a.map{|e|puts e.join}; puts
}

def func(a)
    a.map{|e| e.join.sub(/([#1]*)[#1]$/){|s|s=?#*$1.size+?#}.split('')}
end

def func2(a)
    a.map{|e| e.join.gsub(/(1+)#/){|s|s=?#*$1.size+?#}.split('')}
end

gets.to_i.times{
    n=gets.to_i
    a=(1..n).map{gets.chomp.chars}
    a=func(a)
    a=func(a.transpose)
    b=[]
    while a!=b
        b=func2(a)
        b=func2(b.transpose)
        a=b.dup
    end
    puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)
    #a.map{|e|puts e.join}; puts
}


n = gets.to_i
if n.even?
  k = n/2
  puts k
  puts ([2] * k).join(" ")
else
  k = 1
  n -= 3
  ps = [3]
  k += n/2
  ps << ([2] * (k-1))
  puts k
  puts ps.join(" ")
end

n=gets.to_i
if n==3
    puts "1\n3"
elsif n.even?
    puts n/2
    puts ([2]*(n/2))*' '
else
    puts n/2
    puts (([2]*(n/2-1))<<3)*' '
end

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i # n >= 2 and n <= 100_000
was_odd = n.odd?
ans_len = 0
if was_odd
    ans_len += 1
    n -= 3
end

ans_len += n / 2

puts ans_len
if was_odd
    print "3 "
end
(n/2).times {
    print "2 "
}
puts ""

class Memo
  attr_reader(:i)
  def initialize(i, state)
    @i =i
    @state = state
  end

  def yes!
    @state = true
  end

  def no!
    @state = false
  end

  def yes?
    @state
  end

  def to_s
    "#{@i} => #{@state}"
  end
end
class Sieve
  attr_reader(:n)
  def initialize(n)
    @n = n
    @memo = Array.new(n + 100){|i| Memo.new(i, true)}
    @run = false
  end

  def run!
    return if @run
    @run = true
    @memo[0].yes!
    @memo[1].yes!
    @memo[2].yes!
    @memo.drop(2).each do |m|
      if(m.yes?)
        start = m.i * m.i
        start.step(@n + 1, m.i) do |i|
          @memo[i].no!
        end
      end
    end
  end

  def primes
    @primes ||= run!.select{|m| m.yes? }
  end

  def prime_values
    @prime_values ||= primes.map(&:i)
  end

  def get_data
    run!
    @memo
  end
end

def main
  g = gets.strip.to_i
  n = g / 2
  puts n
  if(g % 2 == 0)
    puts Array.new(n){ 2 }.join(" ")
  else
    x = Array.new(n - 1){ 2 }
    x << 3
    puts x.join(" ")
  end
end

main

n = gets.chomp.to_i

if n%2 == 0
	puts n/2
	print "2 " * (n/2)
else
	if n == 3
		puts 1
		print 3
	else
		puts n/2
		print "2 " * ((n/2)-1)
		print 3
	end
end

#!/usr/bin/env ruby
n=gets.chomp.to_i
if n&1==0 then
  puts "#{n/2}"
  (n/2).times do
    print "2 "
  end
else puts "#{n/2 }"
  print "3 "
  ((n/2)-1).times do
    print "2 "
  end
end

n = gets.to_i
if n % 2 == 0 then
	puts n / 2
	puts "2 " * (n / 2 - 1) + "2"
else
	puts n / 2
	puts "2 " * (n / 2 - 1) + "3"
end

n = gets.to_i

result = Array.new(n.div(2)-1, 2)
result << (n.even? ? 2 : 3)

puts result.size
puts result.join(' ')

k = gets.chomp.to_i
n = k / 2
out = ["2"] * n
if k.odd?
	out.pop
	out += ["3"]
end
puts out.size
puts out.join(" ")

number = gets.strip.to_i
even = false

even = true if number % 2 == 0

if even
  output = ''
  (number/2).times { output << '2 ' }
  puts number/2
  puts output.strip
else
  if number == 3
    puts 1
    puts number
  else
    balance = number - 3
    output = ''
    (balance/2).times { output << '2 ' }
    output << '3'
    puts(balance/2 + 1)
    puts output
  end
end

n = gets.chomp.to_i

ans = []
if n.even?
  ans = [2] * (n / 2)
else
  ans = [2] * (n / 2 - 1) + [3]
end
puts ans.length
puts ans.join(" ")

n = gets.to_i

if(n%2==0)
    puts n/2
    (n/2).times{print 2.to_s + " "}
else
    puts (n-1)/2
    ((n/2)-1).times{print 2.to_s + " "}
    print 3.to_s

end

n = gets.to_i
ans = n / 2
puts ans
if (n % 2 == 1)
    print 3, " "
    n -= 3
end

(0...n / 2).each do
    print 2, " "
end

given_number = gets.chomp.to_i

k = given_number / 2

md = given_number % 2
ary = [2]
if md == 1
  ary *= k - 1
  ary << 3
else
  ary *= k
end

puts k
puts ary.join(' ')

n = gets.to_i
count3 = n%2
count2 = n/2 - count3
puts count2 + count3
(1..count2).each do
  print 2, ' '
end
print count3==1? 3 : ''

require 'prime'
require 'set'
a=["","","1\n2","1\n3","2\n2 2","2\n2 3"]
n=gets.to_i

if n<6 then
	puts a[n]
else
	if n%2==0 then
		puts n/2
		(n/2).times{|i|
			print " " if i>0
			print "2"
		}
		print "\n"
	else
		d=n-3
		puts d/2+1
		(d/2).times{|i|
			print " " if i>0
			print "2"
		}
		print " 3\n"
	end
end

n = gets.to_i

if (n % 2 == 1)
    print 3, " "
    n -= 3
end

(0...n / 2).each do
    print 2, " "
end

given_number = gets.chomp.to_i

k = given_number / 2

md = given_number % 2
ary = [2]
if md == 1
  ary *= k - 1
  ary << 3
else
  ary *= k
end

puts ary.join(' ')

n = gets.to_i
count3 = n%2
count2 = n/2 - count3
(1..count2).each do
  print 2, ' '
end
print count3==1? 3 : ''

require 'prime'
require 'set'
a=["","","1\n2","1\n3","2\n2 2","2\n2 3"]
x=Prime.each(10**5).to_a
y=SortedSet.new(x)
n=gets.to_i

if n<6 then
	puts a[n]
elsif y.member?(n) then
	puts "1\n#{n}"
else
	ok=false
	x.size.times{|i|
		i.upto(x.size-1){|j|
			t=n-x[i]-x[j]
			break if t<x[j]

			if (t==0) || y.member?(t) then
				if t==0 then
					puts 2
					puts [x[i],x[j]]*" "
				else
					puts 3
					puts [x[i],x[j],t]*" "
				end
				ok=true
				break
			end
		}
		break if ok==true
	}
end

k = gets.chomp.to_i
n = k / 2
out = ["2"] * n
out += ["3"] if k.odd?
puts out.size
puts out.join(" ")


def solve(presents, to_give)
  indexes = Array.new(presents.size)
  presents.each_with_index { |e, i| indexes[e] = i }

  p_idx = 0
  p = 0
  sum = 0
  to_give.each do |e|
    if indexes[e] <= p_idx
      sum += 1
      p += 1
    else
      sum += (indexes[e] - p) * 2 + 1
      p_idx = indexes[e]
      p += 1
    end
  end
  sum
end

# gets.strip.split(' ').map(&:to_i)
gets.strip.to_i.times do
  n, m = gets.strip.split(' ').map(&:to_i)
  presents = gets.strip.split(' ').map(&:to_i)
  to_give = gets.strip.split(' ').map(&:to_i)
  puts solve(presents, to_give)
end

def solve
    gets.strip
    n = gets.strip.split(' ').map(&:to_i)
    m = gets.strip.split(' ').map(&:to_i)
    pc = {}
    n.each_with_index { |e, i| pc[e] = i }
    sum = 0
    out = 0
    cur = -1
    m.each { |e|
        if pc[e] < cur
            sum += 1
        else
            cur = pc[e]
            sum += (2 * (cur - out) + 1)
        end
        out += 1
    }
    puts sum
end

gets.strip.to_i.times {
    solve
}


a, b, n, t, words = nil
File.open("input.txt") do |f|
  n, t = f.gets.split.map(&:to_i)
  a = []*(2*n)
  b = []*(2*n)
  n.times do |i|
    a[i], b[i], a[i+n], b[i+n] = f.gets.split.map(&:to_i)
  end

  words = []
  f.gets.to_i.times do
    words << [f.gets.strip, f.gets.to_i]
  end
end

result = Array.new(n){[]}
du = Hash.new(0)
th = 0
tm = t
while !words.empty?
  w, c = words.shift
  d = du[[w, th%n]]

  reqt = [1, c-(a[th]+b[(th+n)%(2*n)])-d].max
  if reqt <= tm
    result[th%n] << w
    tm -= reqt
  else
    du[[w, th%n]] += tm
    tm = 0
    words << [w, c]
  end

  if tm == 0
    tm = t
    th = (th+1)%(2*n)

  end
end

File.open("output.txt", "w") do |f|
  result.each do |a|
    f.puts [a.size, a]*" "
  end
end

a, b, n, t, words = nil
File.open("input.txt") do |f|
  n, t = f.gets.split.map(&:to_i)
  a = []*(2*n)
  b = []*(2*n)
  n.times do |i|
    a[i], b[i], a[i+n], b[i+n] = f.gets.split.map(&:to_i)
  end

  words = []
  f.gets.to_i.times do
    words << [f.gets.strip, f.gets.to_i]
  end
end

result = Array.new(n){[]}
du = Hash.new(0)
th = 0
tm = t
while !words.empty?
  w, c = words.shift
  d = du[[w, th%n]]

  reqt = [1, c-(a[th]+b[th])-d].max
  if reqt <= tm
    result[th%n] << w
    tm -= reqt
  else
    du[[w, th%n]] += tm
    tm = 0
    words << [w, c]
  end

  if tm == 0
    tm = t
    th = (th+1)%(2*n)

  end
end

File.open("output.txt", "w") do |f|
  result.each do |a|
    f.puts [a.size, a]*" "
  end
end


module AtCoder
  class FenwickTree
    def initialize(n = 0)
      @n = n
      @data = Array.new(n, 0).unshift(nil)
    end

    def add(idx, x)
      idx += 1
      while idx <= @n
        @data[idx] += x
        idx += idx & -idx
      end
      self
    end

    def sum(l, r)
      __sum__(r) - __sum__(l)
    end

    private

    def __sum__(r)
      s = 0
      while r > 0
        s += @data[r]
        r ^= r & -r
      end
      s
    end
  end
end

gets.to_i.times do

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

ft = AtCoder::FenwickTree.new(n)
s = Array.new(n)
a.each_with_index do |ai, i|
  ai >>= 1
  s[ai] = i - ft.sum(0, ai)
  ft.add(ai, 1)
end
x = s[0]; s.map! { |si| x = (x < si) ? x : si }
t = b.map.with_index do |bj, j|
  bj >>= 1
  bj -= 1
  j + s[bj]
end

puts t.min

end

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i).map{|x|x/2}
  b=gets.split.map(&:to_i).map{|x|x/2-1}
  l=[]
  i=0
  n.times do |j|
    while i<=b[j]
      l << j
      i+=1
    end
  end
  ans=2*n
  n.times do |j|
    if ans>j+l[a[j]]
      ans=j+l[a[j]]
    end
  end
  puts ans
end

module AtCoder
  class FenwickTree
    def initialize(n = 0)
      @n = n
      @data = Array.new(n, 0).unshift(nil)
    end

    def add(idx, x)
      idx += 1
      while idx <= @n
        @data[idx] += x
        idx += idx & -idx
      end
      self
    end

    def sum(l, r)
      __sum__(r) - __sum__(l)
    end

    private

    def __sum__(r)
      s = 0
      while r > 0
        s += @data[r]
        r ^= r & -r
      end
      s
    end
  end
end

gets.to_i.times do

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

ft = AtCoder::FenwickTree.new(n)
s = Array.new(n)
a.each_with_index do |ai, i|
  ai >>= 1
  s[ai] = i - ft.sum(0, ai)
  ft.add(ai, 1)
end
t = b.map.with_index do |bj, j|
  bj >>= 1
  bj -= 1
  j + s[bj]
end

puts t.min

end

gets.to_i.times do

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

b0 = b[0]
c = a.index { |ai| ai < b0 }

puts c

end


def solution(costs, questions)
  sorted_costs = costs.sort
  prefix_sorted_costs, prefix_costs = [0], [0]
  for i in 0...costs.size do
    prefix_sorted_costs << prefix_sorted_costs.last + sorted_costs[i]
    prefix_costs << prefix_costs.last + costs[i]
  end

  questions.each do |question|
    if question[0] == 1
      puts prefix_costs[question[2]] - prefix_costs[question[1]-1]
    else
      puts prefix_sorted_costs[question[2]] - prefix_sorted_costs[question[1]-1]
    end
  end
end

_ = gets
costs = gets.split.map(&:to_i)
m = gets.to_i
questions = []
m.times do
  questions << gets.split.map(&:to_i)
end

solution(costs, questions)

n = gets
stone = gets.split.map(&:to_i)
sorted = stone.sort
for i in 1...stone.length
  stone[i] += stone[i-1]
  sorted[i] += sorted[i-1]
end
stone << 0
sorted << 0
q = gets.to_i
for i in 1..q
  a, b, c = gets.split.map(&:to_i)
  b -= 2
  c -= 1
  sum = 0
  if a == 1
    sum = stone[c]-stone[b]
  else
    sum = sorted[c]-sorted[b]
  end
  puts sum
end

n=gets
stone=gets.split.map &:to_i
sorted = stone.sort
for i in 1...stone.length
  stone[i] += stone[i-1]
  sorted[i] += sorted[i-1]
end
stone << 0
sorted << 0
q=gets.to_i
for i in 1..q
  a,b,c = gets.split.map &:to_i
  b-=2
  c-=1
  sum=0
  if a==1
    sum=stone[c]-stone[b]
  else
    sum=sorted[c]-sorted[b]
  end
  puts sum

end

# coding: utf-8

# if __FILE__ = $0
# end

# Kuriyama Mirai? <피림이: 이뭔씹...>

n = gets.to_i
v = gets.split.map(&:to_i) # stones
temp = 0
sv = [0] + v.map { |i|
    temp += i
    temp
}
ov = [0] + v.sort # ordered
temp = 0
ov = ov.map { |i|
    temp += i
    temp
}
#p sv
#p ov
questions = gets.to_i
questions.times {
    type, l, r = gets.split.map(&:to_i)
    if type == 1
        puts sv[r] - sv[l-1]
    else
        puts ov[r] - ov[l-1]
    end
}

n = gets.to_i
v = [0] + gets.split.collect{|i| i.to_i}
u = v.sort
(1..n).each do |i|
    v[i] += v[i-1]
    u[i] += u[i-1]
end
m = gets.to_i
(0...m).each do
    type, l, r = gets.split.collect{|i| i.to_i}
    if type == 1
        puts v[r]-v[l-1]
    else
        puts u[r]-u[l-1]
    end
end

# 433B

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

n = gets.to_i
a = read_line
b = a.sort
sums = [nil, Array.new(n), Array.new(n)]
sums[1][0] = a[0]
sums[2][0] = b[0]
(1...n).each do |i|
  sums[1][i] = sums[1][i - 1] + a[i]
  sums[2][i] = sums[2][i - 1] + b[i]
end
gets.to_i.times do
  type, l, r = read_line
  puts sums[type][r - 1] - (l > 1 ? sums[type][l - 2] : 0)
end

n = gets.to_i
v = [0] + gets.split.collect{|i| i.to_i}
u = v.sort
(1..n).each do |i|
	v[i] += v[i - 1]
	u[i] += u[i - 1]
end
m = gets.to_i
(0...m).each do
	t, left, right = gets.split.collect{|i| i.to_i}
	if 1 == t
		puts v[right]-v[left - 1]
	else
		puts u[right]-u[left - 1]
	end
end


n = gets.chomp.to_i

arr = gets.split(" ")

arr = arr.map { |element|  element.to_i }

sum = arr.inject(0){|sum,x| sum + x }

sorted = arr.sort.reverse

answer = []

arr.each_with_index do |element,i|

	if element == sorted[0]
		mx = sorted[1]
	else
		mx = sorted[0]
	end
	answer<<(i+1) if sum - mx - element == mx

end

puts answer.length
puts answer.join(' ')

n = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
sum = a.inject(:+)
sorted = a.sort.reverse
nice = []
a.each_with_index do |e, i|
  mx = e == sorted.first ? sorted[1] : sorted.first
  nice << (i + 1) if (sum - e) - mx == mx
end
puts nice.size
puts nice.join(' ')

n = gets.chomp.to_i

arr = gets.split(" ")
arr = arr.map { |element|  element.to_i }

sum = arr.inject(0){|sum,x| sum + x }
max = arr.max
nums = []

temp = arr.uniq
temp.each_with_index do |element,i|
	nums<<[sum-2*element,i]
end

nums.delete_if { |x| x[0] > max || x[0] < 1 }
nums = nums.uniq
answer = []

nums.each do |element|
	temp_arr = arr.each_index.select { |i| arr[i] == element[0] && i!=element[1] }.compact
	answer.concat(temp_arr)
end

answer = answer.uniq

puts answer.length
answer.each { |x| print (x+1).to_s+" "}

n = gets.chomp.to_i

arr = gets.split(" ")
arr = arr.map { |element|  element.to_i }
sum = arr.inject(0){|sum,x| sum + x }
max = arr.max
nums = []
arr.each do |element|
	nums<<sum-2*element
end

nums.delete_if { |x| x > max || x < 1 }
nums = nums.uniq
answer = []
nums.each do |element|
	temp_arr = arr.each_index.select { |i| arr[i] == element}.compact
	answer.concat(temp_arr)
end

answer = answer.uniq

puts answer.length
answer.each { |x| print (x+1).to_s+" "}


n=gets.to_i
p n-1
c=[0]*100020
z=[]
1.upto(n-1){|i|
	x,y=gets.split.map &:to_i
	print "2 "+$_
	if c[x]==0
		c[x]=i
	else
		z<<[i,c[x]]*' '
	end
	if c[y]==0
		c[y]=i
	else
		z<<[i,c[y]]*' '
	end
}
puts z*"\n"


X = gets.to_i

def check(x)
  ans = []

  if x[0] == 0
    x += 1
    ans << 0
  end

  loop do
    flg = false
    l = x.to_s(2).size
    ml = 0

    (0...l).each do |i|
      if x[i] == 0
        ml = i + 1
        flg = true
      end
    end

    if flg
      mask = ('1' * ml).to_i(2)
      ans << ml

      x ^= mask
      x += 1
    else
      break
    end
  end

  ans
end

ans = check(X)

if ans.empty?
  puts 0
else
  puts ans.size * 2
  puts ans.join(' ')
end

def check(x,n,a,b)
    f = true
    for i in 0..n
        f = false if x[i] == 0
    end
    if f
        if b.empty?
            puts 0
        else
            puts a
            puts b.join(" ")
        end
        exit
    end
end

x = gets.to_i
a = 0
b = []
n = Math.log2(x).floor
while 1
    check(x,n,a,b)
    a += 1
    n.downto(0) do |i|
        if x[i] == 0
            x ^= ((1<<(i+1))-1)
            b << i+1
            check(x,n,a,b)
            break
        end
    end
    a += 1
    x += 1
    check(x,n,a,b)
end


w,h = gets.strip.split(" ").map(&:to_i)

matrix = Array.new

h.times do |k|
    matrix[k] = gets.strip.split("")
end

matrix = matrix.transpose

(2*w).times do |i|
    m = (i/2.0).floor
    (2*h).times do |j|
        k = (j/2.0).floor
        print matrix[m][k]
    end
    print "\n"
end

# http://codeforces.ru/contest/523/problem/A

def img_to_s image
    image.map(&:join).join("\n")
end

initial = ARGF.read.split("\n")

image = initial[1..-1].map(&:chars)

# flip 90 degrees

image = image.transpose

# mirror

image = image.map { |i| i.map(&:reverse) }

# scaling

image = image.reduce([]) do |res, r|
    scaled = r.reduce([]) { |i_res, c| i_res << c; i_res << c; i_res }
    res << scaled
    res << scaled
end

puts img_to_s(image)

(w, h) = gets.scan(/\d+/).map(&:to_i)
s = []
h.times do
  s << gets
end
w.times do |i|
  2.times do
    h.times do |j|
      print s[j][i], s[j][i]
    end
    print "\n"
  end
end

_, h = gets.split.map(&:to_i)
a = []
h.times do
  a << gets.chomp.split(//)
end
a = a.transpose.map { |x| x.zip(x).flatten }
puts a.zip(a).flatten(1).map(&:join)

a,b = gets.split.map{|x| x.to_i}
c = []
b.times do |x|
  c[x] = gets.chomp.split('')
end

for i in 0...2*a
  for j in 0...2*b
    print("#{c[j/2][i/2]}")
  end
  puts
end

initial_num = ARGF.read

result = ''

initial_num.chars.each_with_index do |c, index|
	i = c.to_i
	if i >= 5
		if i == 9 and index == 0
			result << c
		else
			result << (9-i).to_s
		end
	else
		result << c
	end
end

puts result


n, c, s, r = gets, Hash.new(0), 0, 0
gets.split.map(&:to_i).each_with_index {|a, i|
  r += a * i - s - c[a - 1] + c[a + 1]
  c[a] += 1
  s += a
}
p r

# cook your code here

n = gets.to_i

nums = gets.split.map{ |x| x.to_i }

numap = Hash.new(0)
sum = 0

for i in 0...n
    sum += nums[i]
    numap[nums[i]] += 1
end

minsum=0
greatsum=0
=begin
for i in 0...n
    puts i
    #puts nums[i]
    puts numap[nums[i]]
end
=end
for i in 0...n
    sum -= nums[i]
    minsum = sum - (n-1-i)*nums[i] - numap[nums[i]+1] + numap[nums[i]-1]
    greatsum += minsum
    numap[nums[i]] -= 1
    # puts (i+1).to_s + " " + nums[i].to_s + " " + sum.to_s + " " + minsum.to_s + " " + greatsum.to_s
end

puts greatsum

N = gets.to_i
A = gets.split.map(&:to_i)

sum, ans = 0, 0
mp = Hash.new(0)
N.times do |i|
    res = sum
    num = i
    ((A[i]-1)..(A[i]+1)).each do |v|
        res -= v*mp[v]
        num -= mp[v]
    end
    ans += num*A[i] - res
    sum += A[i]
    mp[A[i]] += 1
end
puts ans

N = $stdin.gets.chomp.to_i
arr = $stdin.gets.chomp.split(" ").map{ |a| a.to_i }.reverse
nod = {}
#p arr

ans = 0
s = 0
arr.each_with_index do |a, i|
  ans += (s - (a*i)) - ((nod[a-1] || 0) * (a-1 - a) + (nod[a+1] || 0) * (a+1 - a))
  s += a
  nod[a] = (nod[a] || 0) + 1
end
puts ans

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

n = gets.to_i
sum = 0
ans = 0
ary = gets.split.map(&:to_i)
hash = {}
ary.each do |v|
  sum += v
  hash[v] = hash[v].nil? ? 1 : hash[v] + 1
end
ary.each do |v|
  sum -= v
  n -= 1
  ans += (sum - n * v)
  hash[v] -= 1
  ans += hash[v - 1] unless hash[v - 1].nil?
  ans -= hash[v + 1] unless hash[v + 1].nil?
end
puts ans


t = gets.to_i

t.times do
  ln = gets.chomp
  if ln.downcase == "yes"
    puts "YES"
  else
    puts "NO"
  end
end

gets.to_i.times { puts gets.chomp.upcase == "YES" ? "YES" : "NO" }

max_iterates = gets.to_i
result = []
for i in 1..max_iterates do
  val = gets.chomp.downcase == "yes" ? "YES" : "NO"
  result << val
end

result.each { |str| puts str }

#!/usr/bin/env ruby

numtests=gets.to_i
i=0
(1..numtests).each do |i|
  l=gets.chomp
  if !l.nil? && l.downcase == "yes"
    puts "YES"
  else
    puts "NO"
  end
end

n = gets.chomp.to_i
inputs = []
(0..n-1).each do |i|
  inputs.push(gets.chomp)
end
inputs.each do |i|
  if i.downcase == "yes"
    puts "YES"
  else
    puts "NO"
  end
end


def solve

    n , m = readline.split(' ')
    n = n.to_i; m = m.to_i

    s = Array.new; h = Hash.new(0)

    n.times {
        s << gets
    }

    s_ = s.dup

    (0...n).each{|x|
        s_[x] = s[x].dup
    }

    (0...n).each{|x|

        h.clear

        (0...m).each{|y|
            h[s[x][y]] += 1
        }

        (0...m).each{|y|
             s_[x][y] = '_' if (h[s[x][y]] > 1)
        }

    }

    (0...m).each{|y|

        h.clear

        (0...n).each{|x|
            h[s[x][y]] += 1
        }

        (0...n).each{|x|
             s_[x][y] = '_' if (h[s[x][y]] > 1)
        }

    }

    res = String.new

    (0...n).each{|x|
        (0...m).each{|y|
            res = res + s_[x][y].chr if (s_[x][y] != '_')
        }
    }

    return res

end

#$stdin = File.open("in.txt", "r")
puts solve

n, m = $stdin.readline.split(" ").collect { |i| i.to_i }
arr = []
for i in 0...n
	arr << $stdin.readline.chomp
end
tmph = Hash.new(0)
arr.each do |s|
	for i in 0...m
		s[i] = s[i].upcase if tmph[s[i]] > 1
		tmph[s[i].downcase] += 1
	end
	for i in 0...m
		s[i] = s[i].upcase if tmph[s[i]] > 1
	end
	tmph = Hash.new(0)
end
for i in 0...m
	arr.each do |s|
		s[i] = s[i].upcase if tmph[s[i].downcase] > 1
		tmph[s[i].downcase] += 1
	end
	arr.each do |s|
		s[i] = s[i].upcase if tmph[s[i].downcase] > 1
	end
	tmph = Hash.new(0)
end
arr.each do |s|
	for i in 0...m
		print s[i] if s[i] == s[i].downcase
	end
end
puts "\n"

n, m = gets.split.map(&:to_i)
R, C = [n, m].map { |i| Array.new(i) { Hash.new { |hash,key| hash[key] = 0 } } }
F = Array.new
(0...n).each do |r|
  t = gets.chomp
  (0...m).each do |c|
    R[r][t[c]] += 1
    C[c][t[c]] += 1
  end
  F << t
end
ret = ""
(0...n).each do |r|
  (0...m).each do |c|
    ret += F[r][c] if R[r][F[r][c]] + C[c][F[r][c]] <= 2
  end
end

puts ret

#Fuck U Codeforces :D
n,m=gets.split.map(&:to_i)
c=n.times.map{gets.chomp.bytes.to_a}
d=c.transpose
n.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}
#Expected to be acc.
#YARAAAAAAB

n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
bd = Array.new(n)
bdf = Array.new(n)
for i in 0..n-1
	bd[i] = gets.chomp.split(//)
	bdf[i] = Array.new(m)
end

for i in 0..n-1
	for j in 0..m-1
		bdf[i][j] = true
	end
end

for i in 0..n-1
	h = Hash.new
	for j in 0..m-1
		if h.key?(bd[i][j])
			h[bd[i][j]] += 1
		else
			h[bd[i][j]] = 1
		end
	end
	for j in 0..m-1
		bdf[i][j] = false if h[bd[i][j]] > 1
	end
end

for j in 0..m-1
	h = Hash.new
	for i in 0..n-1
		if h.key?(bd[i][j])
			h[bd[i][j]] += 1
		else
			h[bd[i][j]] = 1
		end
	end
	for i in 0..n-1
		bdf[i][j] = false if h[bd[i][j]] > 1
	end
end

for i in 0..n-1
	for j in 0..m-1
		print bd[i][j] if bdf[i][j] == true
	end
end

puts ""

w=gets.to_i
m=[]
w.times{
    m.push(gets.split[0])
}
h=m[0].length
ans=""
w.times{|i|
    h.times{|j|
        ok=1
        w.times{|x|
            if i!=x && m[i][j]==m[x][j] then
                ok=0
                break
            end
        }
        h.times{|y|
            if j!=y && m[i][j]==m[i][y] then
                ok=0
                break
            end
        }
        if ok==1 then
            ans+=m[i][j]
        end
    }
}
puts ans

#!/usr/bin/env ruby
#$DEBUG = true
#$stdin = File.open '2.test' if $DEBUG
n, m = gets.split.map(&:to_i)
chars = [].tap{|chars| n.times{chars << gets.chomp.split("") } }
26.times { |c|
    c = (c+97).chr
    2.times {
        chars.each {|line|
            line.map!{|ci| ci == c ? "" : ci } if line.select{ |ci| ci == c || ci == "" }.size > 1
        }
        chars = chars.transpose
    }
    chars.each {|line| line.map!{|ci| ci == "" ? nil : ci } }
}
puts chars.flatten.compact.join

#=================================
# Better be sure than sorry.
#=================================
$stdin=File.open("./input.txt","r") if ENV["LOGNAME"]=="skydos"
require 'prime'
require 'mathn'
#=================================

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

board_main=[]
board_copy=[]
n.times{
	line = gets.strip.split(//)
	board_main << line
	board_copy << line.dup
}

for i in 0...n
	for j in 0...m
		ch = board_main[i][j]

		# rows
		count=0
		for row_index in 0...n
			count+=1 if board_main[row_index][j]==ch
		end

		if count!=1
			for row_index in 0...n
				board_copy[row_index][j]="" if board_main[row_index][j]==ch
			end
		end

		# cols
		count=0
		for col_index in 0...m
			count+=1 if board_main[i][col_index]==ch
		end

		if count!=1
			for col_index in 0...m
				board_copy[i][col_index]="" if board_main[i][col_index]==ch
			end
		end
	end
end

for i in 0...n
	for j in 0...m
		print board_copy[i][j]
	end
end

def solve

    n , m = readline.split(' ')
    n = n.to_i; m = m.to_i

    s = Array.new; h = Hash.new

    n.times {
        s << gets
    }

    s_ = s.dup
=begin
    (0...n).each{|x|
        s_[x] = s[x].dup
    }

    (0...n).each{|x|

        h = Hash.new

        (0...m).each{|y|
            h[s[x][y].chr] += 1
        }

        (0...m).each{|y|
            s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }

    (0...m).each{|y|

        h = Hash.new

        (0...n).each{|x|
            h[s[x][y].chr] += 1
        }

        (0...n).each{|x|
            s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }
=end
    res = String.new

    (0...n).each{|x|
        (0...m).each{|y|
            res = res + s_[x][y].chr if (s_[x][y] != 0)
        }
    }

    return res

end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    n , m = readline.split(' ')
    n = n.to_i; m = m.to_i

    s = Array.new; h = Hash.new(0)

    n.times {
        s << gets
    }

    s_ = s.dup

    (0...n).each{|x|
        s_[x] = s[x].dup
    }

    (0...n).each{|x|

        h.clear

        (0...m).each{|y|
            h[s[x][y]] += 1
        }

        (0...m).each{|y|
            # s_[x][y] = 0 if (h[s[x][y]] > 1)
        }

    }

    (0...m).each{|y|

        h.clear

        (0...n).each{|x|
            h[s[x][y]] += 1
        }

        (0...n).each{|x|
            # s_[x][y] = 0 if (h[s[x][y]] > 1)
        }

    }

    res = String.new

    (0...n).each{|x|
        (0...m).each{|y|
            res = res + s_[x][y].chr if (s_[x][y] != 0)
        }
    }

    return res

end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    n , m = readline.split(' ')
    n = n.to_i; m = m.to_i

    s = Array.new; h = Hash.new(0)

    n.times {
        s << gets
    }

    s_ = s.dup

    (0...n).each{|x|
        s_[x] = s[x].dup
    }

    (0...n).each{|x|

        h.clear

        (0...m).each{|y|
            #        h[s[x][y].chr] += 1
        }

        (0...m).each{|y|
            # s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }

    (0...m).each{|y|

        h.clear

        (0...n).each{|x|
            #      h[s[x][y].chr] += 1
        }

        (0...n).each{|x|
            #  s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }

    res = String.new

    (0...n).each{|x|
        (0...m).each{|y|
            res = res + s_[x][y].chr if (s_[x][y] != 0)
        }
    }

    return res

end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    n , m = readline.split(' ')
    n = n.to_i; m = m.to_i

    s = Array.new; h = Hash.new

    n.times {
        s << gets
    }

    s_ = s.dup

    (0...n).each{|x|
        s_[x] = s[x].dup
    }

=begin
    (0...n).each{|x|

        h = Hash.new

        (0...m).each{|y|
            h[s[x][y].chr] += 1
        }

        (0...m).each{|y|
            s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }

    (0...m).each{|y|

        h = Hash.new

        (0...n).each{|x|
            h[s[x][y].chr] += 1
        }

        (0...n).each{|x|
            s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }
=end
    res = String.new

    (0...n).each{|x|
        (0...m).each{|y|
            res = res + s_[x][y].chr if (s_[x][y] != 0)
        }
    }

    return res

end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    a , b = readline.split(' ')
    a = a.to_i; b = b.to_i

    x = 0; sa = 0; sb = 0

    while true do
        t = a - x % a; w = b - x % b;
        break if (t == a && w == b && x > 0)

        if (t < w || t == w && a > b) then
            sa += t; x += t
            else
            sb += w; x += w;
        end
    end

        return 'Dasha' if (sa > sb)
        return 'Masha' if (sa < sb)
        return 'Equal'
    end

    #$stdin = File.open("in.txt", "r")
    puts solve

def solve

    n , m = readline.split(' ')
    n = n.to_i; m = m.to_i

    s = Array.new; h = Hash.new(0)

    n.times {
        s << gets
    }

    s_ = s.dup

    (0...n).each{|x|
        s_[x] = s[x].dup
    }

    (0...n).each{|x|

        h.clear

        (0...m).each{|y|
            #          h[(s[x][y].chr)] += 1
        }

        (0...m).each{|y|
             s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }

    (0...m).each{|y|

        h.clear

        (0...n).each{|x|
            #  h[(s[x][y].chr)] += 1
        }

        (0...n).each{|x|
              s_[x][y] = 0 if (h[s[x][y].chr] > 1)
        }

    }

    res = String.new

    (0...n).each{|x|
        (0...m).each{|y|
            res = res + s_[x][y].chr if (s_[x][y] != 0)
        }
    }

    return res

end

#$stdin = File.open("in.txt", "r")
puts solve

#=================================
# Better be sure than sorry.
#=================================
$stdin=File.open("./input.txt","r") if ENV["LOGNAME"]=="skydos"
require 'prime'
require 'mathn'
#=================================

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

board_main=[]
board_copy=[]
n.times{
	line = gets.strip.split(//)
	board_main << line
	board_copy << line.dup
}

for i in 0...n
	for j in 0...m
		ch = board_main[i][j]

		# rows
		count=0
		for row_index in 0...n
			count+=1 if board_main[row_index][j]==ch
		end

		if count!=1
			for row_index in 0...n
				board_copy[row_index][j]="_" if board_main[row_index][j]==ch
			end
		end

		# cols
		count=0
		for col_index in 0...m
			count+=1 if board_main[i][col_index]==ch
		end

		if count!=1
			for col_index in 0...m
				board_copy[i][col_index]="_" if board_main[i][col_index]==ch
			end
		end
	end
end

for i in 0...n
	for j in 0...m
		print board_copy[i][j]
	end
	puts
end

n, m = $stdin.readline.split(" ").collect { |i| i.to_i }
arr = []
for i in 0...n
	arr << $stdin.readline.chomp
end
p arr
tmph = Hash.new(0)
arr.each do |s|
	for i in 0...m
		s[i] = s[i].upcase if tmph[s[i]] > 1
		tmph[s[i].downcase] += 1
	end
	for i in 0...m
		s[i] = s[i].upcase if tmph[s[i]] > 1
	end
	tmph = Hash.new(0)
end
for i in 0...m
	arr.each do |s|
		s[i] = s[i].upcase if tmph[s[i].downcase] > 1
		tmph[s[i].downcase] += 1
	end
	arr.each do |s|
		s[i] = s[i].upcase if tmph[s[i].downcase] > 1
	end
	tmph = Hash.new(0)
end
arr.each do |s|
	for i in 0...m
		print s[i] if s[i] == s[i].downcase
	end
end
puts "\n"


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

    n.times do
      check_list = Hash.new
      flag = true
      num = gets.chomp.split('').map(&:to_i).sort.uniq

      num.each do |val|
        check_list[val] = true if val <= k
      end

      answer += 1 if check_list.size == (k+1) && flag
    end

    puts answer
  end
end

a = A.new

n,k=gets.split.map &:to_i;p=(0..k).to_a.map &:to_s;q=p.length;p (((1..n).map{|i|((p&(gets.strip.split(''))).length >= q)?1:0}).count(1))

input = gets.chomp
nums = input.split
n = Integer(nums[0])
k = Integer(nums[1])
a =  Array.new
for i in 0...n
    input = gets.chomp
    a[i] = Array.new
    for j in 0...input.length
        a[i][j] = Integer(input[j])
    end
end

def good_number(x)
    x = x.sort
    y = Array.new
    y[0] = x[0]
    for i in 0...x.length - 1
        if x[i + 1] != x[i]
            y.push(x[i + 1])
        end
    end
    return y
end
ans = 0
for i in 0...n
    if good_number(a[i])[0] == 0
        if good_number(a[i]).length >= k + 1 && good_number(a[i])[k] == k
            ans += 1
        end
    end
end
puts ans

n, k = gets.chomp.split.map &:to_i
res = 0
n.times do
  s = gets.chomp
  # Check all digit in [0..k] appear in s
  t = true
  (0..k).to_a.each do |index|
    t = false and break if s.index((index + 48).chr).nil?
  end
  res += 1 if t
end
puts res

#!/usr/bin/ruby
n,k=gets.split.map(&:to_i)
p n.times.count{
	a=gets.chomp.split('').map(&:to_i)
	(0..k).all?{|e|a.include?(e)}
}

n, k = STDIN.readline.split.map {|s| s.to_i }
count = 0
n.times do
	s = STDIN.readline.strip
	okay = true
	(0..k).each do |d|
		if s.index(d.to_s) == nil
			okay = false
			break
		end
	end
	count += 1 if okay
end
puts count

n, k = gets.split.map(&:to_i)
tot = 0
n.times do
  arr = gets.split(//)[0..-2].map(&:to_i).uniq.sort
  tot += 1 if arr.size > k && arr[0..k] == (0..k).to_a
end
puts tot

ans=0
a=gets.chomp.split(" ")
inp=a[0].to_i
rng=a[1].to_i
inp.times do
a=gets.chomp
tempa=Hash.new(0)
a.each_char do |i|
tempa[i]+=1
end
trg=1
0.upto(rng) do |i|
if tempa["#{i}"]==0
trg=0
end
break if trg==0
end
if trg==1
ans+=1
end
end

puts "#{ans}"

t=0;n,k=gets.split.map &:to_i;p=(0..k).to_a;pl=p.length;n.times{m=gets.strip.split('').map &:to_i;t+=((p&m).length >= pl)?1:0};p t

n,k=gets.split.map &:to_i;p=(0..k).to_a.map &:to_s;p (((1..n).map{|i|((p&(gets.strip.split(''))).length >= p.length)?1:0}).count(1))

n,k=gets.split.map &:to_i;p=(0..k).to_a.map &:to_s;pl=p.length;p (((1..n).map{|i|((p&(gets.strip.split(''))).length >= pl)?1:0}).count(1))

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

    n.times do
      check_list = Hash.new
      num = gets.chomp.split('').map(&:to_i)

      num.each do |val|
        check_list[val] = true
      end

      answer += 1 if check_list.size == (k+1)
    end

    puts answer
  end
end

a = A.new

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

    n.times do
      check_list = Hash.new
      flag = true
      num = gets.chomp.split('').map(&:to_i).sort.uniq

      num.each do |val|
        flag = false if val > k
        check_list[val] = true
      end

      answer += 1 if check_list.size == (k+1) && flag
    end

    puts answer
  end
end

a = A.new

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

    n.times do
      num = gets.chomp.split('').map(&:to_i)

      answer += 1 if k == num.count{|val| val < k }
    end

    puts answer
  end
end

a = A.new

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

    n.times do
      check_list = Hash.new
      flag = true
      num = gets.chomp.split('').map(&:to_i)

      num.each do |val|
        flag = false if val > k
        check_list[val] = true
      end

      answer += 1 if check_list.size == (k+1) && flag
    end

    puts answer
  end
end

a = A.new

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

    n.times do
      num = gets.chomp.split('').map(&:to_i)

      answer += 1 if num.all?{|val| val <= k }
    end

    puts answer
  end
end

a = A.new

t=0;n,k=gets.split.map &:to_i;n.times{px=(0..k).to_a.join;mx=gets.strip;t+=((mx.tr(px,'')=='' and px.tr(mx,'')=='')?1:0)};p t

t=0;n,k=gets.split.map &:to_i
n.times{
	px=(0..k).to_a.join
	mx=gets.strip
	t+=(mx.tr(px,'')=='' and px.tr(mx,'')=='')?1:0
}
p t

input = gets.chomp
nums = input.split
n = Integer(nums[0])
k = Integer(nums[1])
a =  Array.new
for i in 0...n
    input = gets.chomp
    a[i] = Array.new
    for j in 0...input.length
        a[i][j] = Integer(input[j])
    end
end

def good_number(x)
    x = x.sort
    y = Array.new
    y[0] = x[0]
    for i in 0...x.length - 1
        if x[i + 1] != x[i]
            y.push(x[i + 1])
        end
    end
    return y
end
ans = 0
for i in 0...n
    if (good_number(a[i])).length >= k + 1
        ans += 1
    end
end
puts ans

input = gets.chomp
nums = input.split
n = Integer(nums[0])
k = Integer(nums[1])
a =  Array.new
for i in 0...n
    input = gets.chomp
    a[i] = Array.new
    for j in 0...input.length
        a[i][j] = Integer(input[j])
    end
end

def good_number(x)
    x = x.sort
    y = Array.new
    y[0] = x[0]
    for i in 0...x.length - 1
        if x[i + 1] != x[i]
            y.push(x[i + 1])
        end
    end
    return y
end
ans = 0
for i in 0...n
    if (good_number(a[i])).length == k + 1
        ans += 1
    end
end
puts ans

input = gets.chomp
nums = input.split
n = Integer(nums[0])
k = Integer(nums[1])
a =  Array.new
for i in 0...n
    input = gets.chomp
    a[i] = Array.new
    for j in 0...input.length
        a[i][j] = Integer(input[j])
    end
end

def good_number(x)
    x = x.sort
    y = Array.new
    y[0] = x[0]
    for i in 0...x.length - 1
        if x[i + 1] != x[i]
            y.push(x[i + 1])
        end
    end
    return y
end
ans = 0
for i in 0...n
    if k != 0
        if (good_number(a[i])).length >= k + 1
            ans += 1
        end
    else
        if good_number(a[i])[0] == 0
            ans += 1
        end
    end
end
puts ans

input = gets.chomp
nums = input.split
n = Integer(nums[0])
k = Integer(nums[1])
a =  Array.new
for i in 0...n
    input = gets.chomp
    a[i] = Array.new
    for j in 0...input.length
        a[i][j] = Integer(input[j])
    end
end

def good_number(x)
    x = x.sort
    y = Array.new
    y[0] = x[0]
    for i in 0...x.length - 1
        if x[i + 1] != x[i]
            y.push(x[i + 1])
        end
    end
    return y
end
ans = 0
for i in 0...n
    if good_number(a[i])[0] == 0
        if good_number(a[i]).length >= k + 1
            ans += 1
        end
    end
end
puts ans


class Array
  def r
    self.map { |q| q.to_i }
  end
end

def read_line
  STDIN.gets.split(" ")
end

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

s = read_to_array[0]
beg = false
ended = false
res = String.new
s.each_char do |c|
  if c == 'a'
    if beg
      beg = false
      ended = true
    end
  else
    if !ended
      beg = true
    end

    if beg
      c = c.ord - 1
    end
  end

  res << c
end

if beg == false && ended == false
  n = res[res.size - 1]
  res[res.size - 1] = 'z'
end

puts res

s=gets.chomp
n=s.size

i=s.index /[^a]/
unless i
    s[-1]=?z
    puts s
    exit
end

j=s.index(?a, i+1)||n

puts s[0, i]+s[i, j-i].tr!('b-z', 'a-y')+s[j, n-j]

class Array
  def r
    self.map { |q| q.to_i }
  end
end

def read_line
  STDIN.gets.split(" ")
end

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

s = read_to_array[0]
beg = false
ended = false
res = String.new
s.each_char do |c|
  if c == 'a'
    if beg
      beg = false
      ended = true
    end
  else
    if !ended
      beg = true
    end

    if beg
      c = (c.ord - 1)
    end
  end

  res << c
end

puts res

class Array
  def r
    self.map { |q| q.to_i }
  end
end

def read_line
  STDIN.gets.split(" ")
end

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

s = read_to_array[0]
beg = false
ended = false
res = String.new
s.each_char do |c|
  if c == 'a'
    if beg
      beg = false
      ended = true
    end
  else
    if !ended
      beg = true
    end

    if beg
      c = c.ord - 1
    end
  end

  res << c
end

if beg == false && ended == false
  n = res[res.size - 1]
  res[res.size - 1] = 'a'
end

puts res

s=gets.chomp
for i in 0..s.size-1
    next if s[i]==?a
    for j in i..s.size-1
        if s[j]!=?a
            s[j]=(s[j].ord-1).chr
        else
            break
        end
    end
    break
end
puts s