n = gets.strip.to_i
prev_stops = []
results = gets.strip.split.map(&:to_i)
results.shift
(n-1).times do
    current_stops = gets.strip.split.map(&:to_i)
    current_stops.shift
    results = results & current_stops
end
print results.join(" ")

n = gets.to_i
h = {}
n.times do
    r, *a = gets.split.map{|x| x.to_i}
    a.each do |x|
        h[x] = 0 unless h[x]
        h[x] += 1
    end
end
ans = []
h.each{|x| ans << x[0] if x[1] == n}
puts ans.join(' ')

n = gets.to_i
stops = []
arr = gets.chomp.split(" ").map(&:to_i)
arr.shift
(n-1).times do
  newarr = (gets.chomp.split(" ").map(&:to_i))
  newarr.shift
  arr = arr&newarr
end

arr.each do |x|
  print "#{x} "
end

puts

n = gets.to_i
stops = []
arr = gets.chomp.split(" ").map(&:to_i)
(n-1).times do
  arr = arr&(gets.chomp.split(" ").map(&:to_i))
end

arr.each do |x|
  print "#{x} "
end
puts

n = gets.to_i
stops = []
arr = gets.chomp.split(" ").map(&:to_i)
(n-1).times do
  newarr = (gets.chomp.split(" ").map(&:to_i))
  newarr.shift
  arr = arr&newarr
end

arr.each do |x|
  print "#{x} "
end

puts

n = gets.to_i
stops = []
arr = gets.chomp.split(" ").map(&:to_i)
(n-1).times do
  newarr = (gets.chomp.split(" ").map(&:to_i))
  arr = arr&newarr
end

arr.each do |x|
  print "#{x} "
end

puts


def solve
  n = gets.strip.to_i
  h = Hash.new(0)
  a = gets.strip.split(' ').map(&:to_i).each { |e| h[e] += 1 }
  ans = 0
  (2..n*2).each { |i|
    cnt = 0
    (1...(i / 2.0).ceil).each { |j| cnt += [h[j], h[i - j]].min }
    cnt += (h[(i / 2.0).to_i] / 2.0).to_i if i.even?
    ans = [ans, cnt].max
  }
  puts ans
end

gets.strip.to_i.times { solve }

gets.to_i.times do
    n = gets.to_i
    w = gets.split.map &:to_i
    cnt = [0] * (n + 1)
    w.each do |i|
        cnt[i] += 1
    end
    res = 0
    (2..2 * n).each do |s|
        cur = 0
        (1...(s + 1) / 2).each do |i|
            next if s - i > n
            cur += [cnt[i], cnt[s - i]].min
        end
        cur += cnt[s / 2] / 2 if s % 2 == 0
        res = [res, cur].max
    end
    puts res
end

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  w = gets.chomp.split(" ").map(&:to_i).sort
  hash = Hash.new(0)
  w.each do |x|
    hash[x] += 1
  end
  min = w.min
  max = w.max
  maxans = 0
  (min*2).upto(max*2) do |i|
    cnt = 0
    ((i-1)/2).times do |j|
      cnt += [hash[j+1],hash[i-j-1]].min
    end
    if i % 2 == 0
      cnt += hash[i/2] / 2
    end
    maxans = cnt if cnt > maxans
  end
  ans << maxans
end
ans.each do |x|
  puts x
end


n = gets.chomp.to_i
na = gets.chomp.split(/ /).map!{|x| x.to_i}
na.sort!
wk = 10
cnt = 0
pen = 0
for i in 0..n-1
    wk += na[i]
    if wk > 720
        break
    end
    cnt += 1
    if wk > 360
        pen += (wk - 360)
    end
end
puts cnt.to_s + " " + pen.to_s

gets

mins = gets.split.map{|_|_.to_i}.sort

t = 10
solved = 0
pen = 0

mins.each do |m|
  t += m
  if t > 720
    break
  end
  solved += 1
  if t > 360
    pen += t - 360
  end
end

puts "#{solved} #{pen}"


#!/usr/bin/ruby

n, k, l, r, s_all, s_k = gets.split.map(&:to_i)

high_base = s_k / k
low_base = ((k == n) ? 0 : (s_all - s_k) / (n - k))

high_count = k
low_count = n - k

high_elevated_count = s_k - k * high_base
low_elevated_count = (s_all - s_k) - (n - k) * low_base

r = []
high_elevated_count.times { r << (high_base + 1) }
(high_count - high_elevated_count).times { r << high_base }
low_elevated_count.times { r << (low_base + 1) }
(low_count - low_elevated_count).times { r << low_base }

puts r.join(' ')

def populate_scores(a, x, k, s)
  d = k - x
  t = s / d
  rem = s % d
  for i in x...x + rem
    a[i] = t + 1
  end
  for i in x + rem...k
    a[i] = t
  end
end

n, k, _, _, s_all, s_k = gets.split.map { |x| x.to_i }

a = Array.new(n)

populate_scores(a, 0, k, s_k)
s_rem = s_all - s_k
populate_scores(a, k, n, s_rem) if s_rem > 0
puts a * ' '


parse_int = lambda { gets.split.map{|x| x.to_i} }
$stdout.sync = true

chars = []
for lt in (0...10)
  puts lt.to_s*4
  $stdout.flush

  chars << lt unless parse_int.call == [0, 0]
end

for perm in chars.permutation
  puts perm.join
  $stdout.flush

  break if parse_int.call == [4, 0]
end

parse_int = lambda { gets.split.map{|x| x.to_i} }
$stdout.sync = true

chars = []
for lt in (0...10)
  puts lt.to_s*4

  chars << lt if gets.chomp == '1 0'
end

for vv in chars.permutation
  puts vv.join

  break if gets.chomp == '4 0'
end


gets.to_i.times do
	s = gets.chomp
	cnt = %w[U D L R].map { s.count _1 }
	ud, lr = cnt[0, 2].min, cnt[2, 2].min
	ud = 1 if lr == 0 && ud != 0
	lr = 1 if ud == 0 && lr != 0
	ans = 'U' * ud + 'L' * lr + 'D' * ud + 'R' * lr
	puts ans.size, ans
end

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

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

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q=gi
q.times do
  s=gs
  lc=s.count("L")
  rc=s.count("R")
  uc=s.count("U")
  dc=s.count("D")

  h_c=min(lc,rc)
  v_c=min(uc,dc)
  if v_c==0 && h_c==0
    puts 0
    puts ''
    next
  elsif v_c==0
    puts 2
    puts 'RL'
    next
  elsif h_c==0
    puts 2
    puts 'UD'
    next
  end

  ans=(("R"*h_c)<<("U"*v_c)<<("L"*h_c)<<("D"*v_c))
  puts 2*(v_c+h_c)
  puts ans
end

gets.to_i.times do
	s = gets.chomp
	cnt = %w[U D L R].map { s.count _1 }
	ud, lr = cnt[0, 2].min, cnt[2, 2].min
	ud = 1 if lr == 0 && ud != 0
	lr = 1 if ud == 0 && lr != 0
	ans = 'U' * ud + 'L' * lr + 'U' * ud + 'R' * lr
	puts ans.size, ans
end

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

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

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q=gi
q.times do
  s=gs
  lc=s.count("L")
  rc=s.count("R")
  uc=s.count("U")
  dc=s.count("D")

  h_c=min(lc,rc)
  v_c=min(uc,dc)
  if v_c==0 && h_c==0
    puts 0
    next
  elsif v_c==0
    puts 2
    puts 'RL'
    next
  elsif h_c==0
    puts 2
    puts 'UD'
    next
  end

  ans=(("R"*v_c)<<("U"*h_c)<<("L"*v_c)<<("D"*h_c))
  puts 2*(v_c+h_c)
  puts ans
end

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

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

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q=gi
q.times do
  s=gs
  lc=s.count("L")
  rc=s.count("R")
  uc=s.count("U")
  dc=s.count("D")

  h_c=min(lc,rc)
  v_c=min(uc,dc)
  if v_c==0 && h_c==0
    puts 0
    puts ''
    next
  elsif v_c==0
    puts 2
    puts 'RL'
    next
  elsif h_c==0
    puts 2
    puts 'UD'
    next
  end

  ans=(("R"*v_c)<<("U"*h_c)<<("L"*v_c)<<("D"*h_c))
  puts 2*(v_c+h_c)
  puts ans
end


n,k=gets.split.map &:to_i;a=gets.split.map &:to_i;b=a.rindex{|i|i!=a[k-1]}||-1;p b<k-1?b+1:-1

require 'set'

def solve(k, a)
  t = a[k - 1]

  # kより後ろの配列
  other = a[k - 1 .. -1]
  other.each do |i|
    if i != t
      return -1
    end
  end

  c = 0
  (k - 1).downto(0) do |idx|
    if a[idx - 1] != t
      return idx
    end
  end
  return c
end

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

#!/usr/bin/ruby

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

if arr[(k-1)..-1].uniq.length == 1
	x = arr[k-1]
	pos = k-1
	pos -= 1 while pos >= 0 and arr[pos] == x
	puts pos+1
else
	puts -1
end

#!/bin/ruby
n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
t=a[k-1..a.size]
if !t.all?{|e|e==t[0]} then
  puts -1
else
  while a.size!=0&&a[-1]==t[0]
    a.pop
  end
  puts a.size
end

n,k= gets.chomp.split.map(&:to_i)
a=gets.chomp.split(' ').map(&:to_i)
if a[k-1..-1].uniq.length == 1
    count=k-2
    while count >= 0 && a[count]==a[-1]
        count -= 1
    end
    puts count+1
else
    puts -1
end

n,k= gets.chomp.split(' ').map(&:to_i)
a=gets.chomp.split(' ').map(&:to_i)
if a[k-1..-1].uniq.length == 1
    count=k-2
    while count >= 0 && a[count]==a[-1]
        count -= 1
    end
    puts count+1
else
    puts -1
end

n, k = gets.split.map(&:to_i)
k -= 1
a = gets.split.map(&:to_i)
if (k..n-1).map { |i| a[i] }.uniq.size == 1
  el = (k..n-1).map { |i| a[i] }.uniq[0]
  c = -1
  c -= 1 while a[c] == el
  puts n+c+1
else
  puts(-1)
end

n, k = gets.split
n = n.to_i
k = k.to_i
a = gets.split
i = a.size - 1
while i >= 0 && a[i] == a[a.size - 1] do
  i -= 1
end

if i >= k - 1 then
    puts -1
else
    puts i + 1
end

a=gets.chomp.split(" ")
k=a[1].to_i-1
n=a[0].to_i
b=gets.chomp.split(" ")
ans=k
t=0

if (b[k..n-1].uniq.length==1)

t=1
(k-1).downto(0) do |i|
if b[i]!=b[k]
break
else
ans-=1
end
end
end

 if t==1
 puts "#{ans}"
 else
 puts "-1"
 end

I =->{gets.split.map &:to_i}
n, k = I[]; a = I[]; a.unshift(0)
b = a.rindex{|ai| ai != a[k]}
puts b<k ? b:-1

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

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

if data[(k-1)...n].to_set.count == 1 then
  e = data.last
  index = n
  data.reverse.each{|value|
    if value != e then
      puts index
      exit
    end

    index-=1
  }

  puts 0
else
  puts -1
end

n, k = gets.split.collect{|i| i.to_i}
a = Array.new(gets.split)
if a[k-1..-1].uniq.length == 1
    ans = k-2
    while ans >= 0 and a[ans] == a[-1]
        ans -= 1
    end
    puts ans+1
else
    puts -1
end

n, k = gets.split.collect{|i| i.to_i}
a = Array.new(gets.split)
if a[k-1..-1].uniq.length == 1
    if k == 1
        puts 0
        exit
    end
    ans = k-2
    while ans >= 0 and a[ans] == a[-1]
        ans -= 1
    end
    puts ans+1
else
    puts -1
end

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

n, k = read_next_line
a = read_next_line
d = k - 1

for i in k...n
  if a[i] != a[d]
    puts -1
    exit
  end
end

i = k - 2
while i >= 0 && a[i] == a[d]
  i -= 1
end
puts i + 1

fl = gets.split.map(&:to_i)
n, k = fl[0] ,fl[1] - 1
seq = gets.split.map(&:to_i)

ps = seq[k]

k +=1

if !!seq.drop(k).find {|x| x != ps}
  puts (-1)
else
  aux = seq.zip((1..n).to_a).select { |x,y| x != ps}
  puts (aux.empty? ? 0 : aux.last[1])
end

def f
	n, k = gets.split.map(&:to_i)
	a = gets.split.map(&:to_i)

	i = k - 1; while (i < n - 1)
		return -1 if (a[i] != a[i+1])
		i += 1
	end

	i = k - 2; while (i >= 0)
		return i+1 if (a[i] != a[k-1])
		i -= 1
	end
	return 0
end

puts f

#!/usr/bin/env ruby

n, k = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
subseq = as[k-1...as.size]
if subseq.any?{|v| v != subseq.first}
  puts -1
  exit
end
pos = k-2
turn = k-1
while pos >= 0 && as[pos] == subseq.first
  pos -= 1
  turn -= 1
end
puts turn

I =->{gets.split.map &:to_i}[]
n, k = I; a = I; a.unshift(0)
b = a.rindex{|ai| ai != a[k]}
puts b<k ? b:-1

require 'set'

def op(k, a)
  tmp = a[k - 1]
  a.shift
  a << tmp

  a
end

def is_same(a)
  set = Set.new a
  return set.count == 1 ? true : false
end

n, k = gets.chomp.split(" ").map{|e| e.to_i}
a = gets.chomp.split(" ").map{|e| e.to_i}
c = 0
old = [a]

loop do
  old << a.clone
  if is_same(a)
    puts  c
    break
  end

  a = op(k, a)
  if old.include? a
    puts -1
    break
  end
  c += 1
end

n, k = gets.split.map(&:to_i)
k -= 1
a = gets.split.map(&:to_i)

if (k..n-1).map { |i| a[i] }.uniq.size == 1
  puts k
else
  puts -1
end

n, k = gets.split.map(&:to_i)
k -= 1
a = gets.split.map(&:to_i)
if (k..n-1).map { |i| a[i] }.uniq.size == 1
  el = (k..n-1).map { |i| a[i] }.uniq[0]
  c = -1
  c -= 1 while a[c] == el
  p n+c
else
  puts(-1)
end

a=gets.chomp.split(" ")
k=a[1].to_i-1
b=gets.chomp.split(" ")
sub=b[k..b.length-1]
l=sub.length
if l==sub.count(sub[0])
ans=a[0].to_i-l
puts "#{ans}"
else
puts "-1"
end

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

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

if data[(k-1)...n].to_set.count == 1 then
  e = data[n-1]
  index = n
  data.reverse.each{|value|
    if value != e then
      puts index
      exit
    end

    index-=1
  }
else
  puts -1
end

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

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

if data[(k-1)...n].to_set.count == 1 then
  e = data[k-1]
  index = k
  data.reverse.each{|value|
    if value == e then
      puts index
      exit
    end

    index-=1
  }
else
  puts -1
end

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

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

if data[(k-1)...n].to_set.count == 1 then
  e = data[n-1]
  index = n
  data.reverse.each{|value|
    if value == e then
      puts index
      exit
    end

    index-=1
  }
else
  puts -1
end

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

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

if data.to_set.count == 1 then
  e = data[k-1]
  index = 1
  data.reverse.each{|value|
    if value == e then
      puts index
      exit
    end

    index+=1
  }
else
  puts -1
end

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

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

if data[k...n].to_set.count == 1 then
  e = data[k-1]
  index = 1
  data.reverse.each{|value|
    if value == e then
      puts index
      exit
    end

    index+=1
  }
else
  puts -1
end

fl = gets.split.map(&:to_i)
n, k = fl[0] ,fl[1] - 1
seq = gets.split.map(&:to_i)

ps = seq[k]

k +=1

if !!seq.drop(k).find {|x| x != ps}
  puts (-1)
else
  aux = seq.zip((1..n).to_a).select { |x,y| x != ps}
  puts (aux.empty? ? 0 : aux.last[0])
end

fl = gets.split.map(&:to_i)
n, k = fl[0] ,fl[1] - 1
seq = gets.split.map(&:to_i)

ps = seq[k]

k +=1

if !!seq.drop(k).find {|x| x != ps}
  puts (-1)
else
  aux = seq.zip((1..n).to_a).select { |x,y| x != ps}
  puts (!!aux ? 0 : aux.last[1])
end

fl = gets.split.map(&:to_i)
n, k = fl[0] - 1,fl[1] - 1
seq = gets.split.map(&:to_i)

ps = seq[k]

k +=1

if !!seq.drop(k).find {|x| x != ps}
  puts (-1)
else
  puts seq.take_while{|x| x != ps}.length
end


def run
  n, k = gets.chomp.split(" ").map(&:to_i)
  a = []
  b = []
  ab = []
  n.times do
    x, y, z = gets.chomp.split(" ").map(&:to_i)
    if y == 0 && z == 0
      next
    end
    if y == z
      ab << x
      next
    end
    if y == 1
      a << x
    end
    if z == 1
      b << x
    end
  end

  a.sort!
  b.sort!
  ab.sort!

  i = 0
  min_t = 0
  while i < k
    if (ab.first && a.first && b.first && ab.first <= a.first + b.first) || (ab.first && (a.first == nil || b.first == nil))
      min_t += ab.first
      ab.shift
    elsif (ab.first && a.first && b.first && ab.first > a.first + b.first) || (ab.first == nil && a.first && b.first)
      min_t += a.first + b.first
      a.shift
      b.shift
    else
      puts -1
      return
    end

    i += 1
  end

  puts min_t
end

run

n, k = gets.split.map &:to_i
books_alice_likes = []
books_bob_likes = []
books_both_like = []
n.times do
	t, a, b = gets.split.map &:to_i
	if a == 1
		if b == 1
			books_both_like << t
		else
			books_alice_likes << t
		end
	elsif b == 1
		books_bob_likes << t
	end
end

books_alice_likes.sort!
books_bob_likes.sort!
books_both_like.sort!

books_read = 0
time = 0
while books_read < k
	if books_both_like.empty?
		if books_alice_likes.empty? or books_bob_likes.empty?
			puts "-1"
			exit
		end

		time += books_alice_likes.shift + books_bob_likes.shift
		books_read += 1

	else

		if not (books_alice_likes.empty? or books_bob_likes.empty?) and books_alice_likes[0] + books_bob_likes[0] < books_both_like[0]
			time += books_alice_likes.shift + books_bob_likes.shift
		else
			time += books_both_like.shift
		end
		books_read += 1
	end
end

puts time

n, k = gets.split.map &:to_i
books_alice_likes = []
books_bob_likes = []
books_both_like = []
n.times do
	t, a, b = gets.split.map &:to_i
	if a == 1
		if b == 1
			books_both_like << t
		else
			books_alice_likes << t
		end
	elsif b == 1
		books_bob_likes << t
	end
end

books_alice_likes.sort!
books_bob_likes.sort!
books_both_like.sort!

books_read = 0
time = 0
while books_read < k
	if books_both_like.empty?
		if books_alice_likes.empty? or books_bob_likes.empty?
			puts "-1"
			exit
		end

		time += books_alice_likes.shift + books_bob_likes.shift
		books_read += 1

	else

		#if not (books_alice_likes.empty? or books_bob_likes.empty?) and books_alice_likes[0] + books_bob_likes[0] < books_both_like[0]
		#	time += books_alice_likes.shift + books_bob_likes.shift
		#else
			time += books_both_like.shift
		#end
		books_read += 1
	end
end

puts time

def run
  n, k = gets.chomp.split(" ").map(&:to_i)
  tab = []
  n.times do
    x, y, z = gets.chomp.split(" ").map(&:to_i)
    if y == 0 && z == 0
      next
    end
    tab << [x, y, z]
  end

  tab.sort_by!{|t, a, b| a == 1 ? [0, t] : [1, 0]}

  i = 0
  b_i = 0
  min_t = 0

  while i < k

    if tab[i][1] == 1
      min_t += tab[i][0]
      if tab[i][2] == 1
        b_i += 1
        tab[i][3] = true
      end
    else
      puts -1
      return
    end

    i += 1
  end

  tab.sort_by!{|t, a, b| b == 1 ? [0, t] : [1, 0]}

  i = 0
  while i < k - b_i
    if tab[i][2] == 1
      unless tab[i][3]
        min_t += tab[i][0]
      end
    else
      puts -1
      return
    end

    i += 1
  end

  puts min_t

end

run


n=gets.to_i
h=gets.split.map &:to_i
g=Hash.new{[]}
a=->b{s=[];b.zip(0..).map{|x,i|g[i]<<=s.shift while s[0]&&b[s[0]]<x;s[0]&&g[i]<<=x==b[s[0]]?s.shift: s[0];s.unshift i}}
a[h];a[h.map &:-@]
i=0;d=[n-1];s={}
(i+=1;d=d.map{|x|g[x].filter{|y|!s[y]&&s[y]=0}}.flatten)until d.include?0
p i

n=gets.to_i
h=gets.split.map &:to_i
g=Hash.new{[]}
a=->b{s=[];b.zip(0..).map{|x,i|g[i]<<=s.shift while s[0]&&b[s[0]]<x;s[0]&&g[i]<<=x==b[s[0]]?s.shift: s[0];s.unshift i}}
a[h];a[h.map{|x|-x}]
i=0;d=[n-1];s={}
(i+=1;d=d.map{|x|g[x].filter{|y|!s[y]&&s[y]=0}}.flatten)until d.include?0
p i

n=gets.to_i
h=gets.split.map &:to_i
g=Hash.new{[]}
a=->b{s=[];b.zip(0..).map{|x,i|g[i]<<=s.shift while s[0]&&b[s[0]]<x;s[0]&&g[i]<<=x==b[s[0]]?s.shift: s[0];s.unshift i}}
a[h];a[h.map{|x|-x}]
i=0;d=[n-1];s={}
(i+=1;d=d.map{|x|g[x].filter{|y|!s[y]&&s[y]=0}}.flatten)while !d.include?0
p i


s1 = gets.chomp
s2 = gets.chomp
puts (s1.length - s1.gsub(s2, '').length) / s2.length

def function
a=gets.chomp
b=gets.chomp
a=a.gsub(b,'#')
puts a.count('#')
end
function

corp1 = gets.to_s
corp2 = gets.chomp

p corp1.gsub(/#{corp2}/, '#').split(//).count('#')

A=gets
B=gets.chop
p A.scan(/#{B}/).size

goog = gets.strip
pin = gets.strip
puts goog.scan(pin).size

gogol = gets.chomp
pineapple = gets.chomp

puts gogol.scan(pineapple).length

def function
a=gets.chomp
b=gets.chomp
puts=a.gsub(b,'#').length
end
function

def function
a=gets.chomp
b=gets.chomp
a=a.gsub(b,'#')
z=a.split('#')
print z.length-1
end
function

s1 = gets.chomp
s2 = gets.chomp

puts s1.chars.each_cons(s2.length).map(&:join).count(s2)


def dfs(f, b, v)
	until b[v] do
		b[v] = 1
		v = f[v]
	end
	v
end

n = gets.to_i
f = [0] + gets.split.map(&:to_i)
a = [nil]*(n+1)
b = [nil]*(n+1)
r = []

f.each{|i| a[i] = 1}
[*1..n].each{|i| r << [i, dfs(f, b, i)] unless a[i]}
[*1..n].each{|i| r << [i, dfs(f, b, i)] unless b[i]}
r = [] if a[1..n].all? && r.size == 1
p r.size
r.each_index{|i| puts "#{r[i - 1][1]} #{r[i][0]}"}

def solve(a)
    n = a.size
    begins = (1..n).to_a - a
    begins.map! { |x| x - 1 }

    used = Array.new(n, false)
    pairs = []

    begins.each do |x|
        e = x
        while !used[e]
            used[e] = true
            e = a[e] - 1
        end

        pairs << [x, e]
    end

    used.each_with_index do |u, i|
        next if u
        e = i
        while !used[e]
            used[e] = true
            e = a[e] - 1
        end
        pairs << [i, i]
    end

    ans = []
    return ans if pairs.size == 1 && pairs[0][0] == pairs[0][1]

    pairs.flatten!
    pairs << pairs.shift

    0.step(pairs.size-1, 2) do |i|
        ans <<= [pairs[i]+1, pairs[i+1]+1]
    end

    ans
end

gets
a = gets.split(' ').map { |x| x.to_i }
#a = [2, 3, 1, 3, 4, 4, 1]
ans = solve a

puts ans.size
ans.each do |pair|
    puts pair.join(' ')
end


n, m = gets.split.map(&:to_i)
a = [0] + gets.split.map(&:to_i)
b = Array.new(100010, 0)
bb = Array.new(100010, 0)
c = 0
for i in 1..n
  b[a[i]] += 1
  c += 1 if bb[a[i]] == 0
  bb[a[i]] = 1
end

s = []
for i in 1..n
  s[i] = c
  b[a[i]] -= 1
  c -= 1 if b[a[i]] == 0
end
ans = []
m.times do |i|
  l = gets.to_i
  ans[i] = s[l]
end
puts ans

def solution(arr, positions)
  mask = Array.new(10**5+10, false)
  distinct_counter = [0]*arr.size

  n = arr.size - 1
  prev = 0
  arr.reverse.each do |value|
    unless mask[value]
      mask[value] = true
      prev += 1
    end
    distinct_counter[n] = prev
    n -= 1
  end

  positions.each do |position|
    puts distinct_counter[position-1]
  end
end

n , m = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
positions = m.times.map { |i| gets.to_i }

solution(arr, positions)

gets

result = []
unvisited = Array.new(100010, true)
different = 0
gets.split(/\s+/).reverse.each do |n|
  n = n.to_i
  if(unvisited[n])
    unvisited[n] = false
    different += 1
  end
  result << different
end

result.reverse!

# puts result.inspect

while n = gets
  puts result[n.to_i-1]
end

n, m = gets.split.map(&:to_i)
a = [0] + gets.split.map(&:to_i)
b = Array.new(100010, 0)
c = 0
for i in 1..n
  c += 1 if b[a[i]] == 0
  b[a[i]] += 1
end
s = []
for i in 1..n
  s[i] = c
  b[a[i]] -= 1
  c -= 1 if b[a[i]] == 0
end

m.times do |i|
  l = gets.to_i
  puts s[l]
end

n, m = gets.split.map(&:to_i)
a = [0] + gets.split.map(&:to_i)
b = Array.new(100010, 0)
c = 0
s = []
n.downto 1 do |i|
  c += 1 if b[a[i]] == 0
  b[a[i]] = 1
  s[i] = c
end
m.times do |i|
  l = gets.to_i
  puts s[l]
end

n, m = gets.split.map(&:to_i)
a = [nil] + gets.split.map(&:to_i)
b = []
c = 0
s = []
n.downto(1) do |i|
  c += 1 if !b[a[i]]
  b[a[i]] = true
  s[i] = c
end
m.times do
  puts s[gets.to_i]
end

require 'set'

n, m = gets.split(' ').map(&:to_i)
a = gets.split(' ').map(&:to_i).reverse
b = [0]
c = Set.new

a.each do |l|
  b << b.last + (c.include?(l) ? 0 : 1)
  c << l
end

1.upto(m) { puts b[-gets.to_i] }

require 'set'

n, m = gets.split(" ").map{|s| s.to_i}
a =  gets.split(" ").map{|s| s.to_i}
maxuniq = a.uniq.size
l = Hash::new()
m.times{|i| l[i] = gets.to_i-1}
ls = l.sort{|a,b| a[1]<=>b[1]}.reverse
ret = Array::new(m)
aset = Set::new()
b = -1;
stm = m
m.times{|i|
	aset.merge(a[ls[i][1]..b])
	ret[ls[i][0]]=aset.size
	if (aset.size == maxuniq)
		stm = i+1;
		break
	end
	b = ls[i][1]-1
}
stm.upto(m-1) {|i|
	ret[ls[i][0]] = maxuniq
}
ret.each{|r| puts r}

require 'set'
n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}

ls=[]
m.times{|e|
	ls<<gets.to_i
}
ss=ls.sort
s1=SortedSet.new
i=n-1
h={}
while i>=0 && !ss.empty?
	e1=ss.last-1
	if e1<i then
		s1.add(x[i])
		i-=1
	elsif e1==i then
		s1.add(x[i])
		h[i]=s1.size
		ss.pop
	else
		ss.pop
	end
end

ls.each{|e|
	puts h[e-1]
}

# coding: utf-8

require 'set'

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
unique_counter_reversed = []
s = Set.new
a.reverse_each { |i|
    s << i
    unique_counter_reversed << s.length
}

m.times {
    l = gets.to_i
    # calculated index: (n-1) - (l-1)
    # <=> n - l -1 + 1
    # <-> n - l Q.E.D
    puts unique_counter_reversed[n - l]
}

require 'set'

n, m = gets.split(" ").map{|s| s.to_i}
a =  gets.split(" ").map{|s| s.to_i}
l = Hash::new()
m.times{|i| l[i] = gets.to_i-1}
ls = l.sort{|a,b| a[1]<=>b[1]}.reverse
ret = Array::new(m)
aset = Set::new()
b = -1;
a.each{|el| puts el}
m.times{|i|
	aset.merge(a[ls[i][1]..b])
	ret[ls[i][0]]=aset.size
	aset.each{|el| puts el}
	b = ls[i][1]-1
}
ret.each{|r| puts r}

require 'set'

n, m = gets.split(" ").map{|s| s.to_i}
a =  gets.split(" ").map{|s| s.to_i}
l = Hash::new()
m.times{|i| l[i] = gets.to_i-1}
ls = l.sort{|a,b| a[1]<=>b[1]}.reverse
ret = Array::new(m)
aset = Set::new()
b = -1;
a.each{|el| puts el}
m.times{|i|
	aset.merge(a[ls[i][1]..b])
	puts ls[i][1]
	ret[ls[i][0]]=aset.size
	aset.each{|el| puts el}
	b = ls[i][1]-1
}
ret.each{|r| puts r}

def solution(fence, take)

  prefix_sum = fence.inject([0]) { |v, i| v << v.last + i }

  min_cost = Float::INFINITY
  index = -1

  for i in 1...fence.size-take do
    if min_cost > prefix_sum[i+take-1] - prefix_sum[i-1]
      min_cost = prefix_sum[i+take-1] - prefix_sum[i-1]
      index = i
    end
  end
  index
end

tables, take = gets.split.map(&:to_i)
fence = gets.split.map(&:to_i)

puts solution(fence, take)

gets
require 'set'

prev = Set.new
current = 0
longest = 0
gets.split(/\s+/).each_with_index do |n,i|
  n = n.to_i

  if prev.member?(n)
    prev = Set.new
    current = 1
  else
    prev.add(n)
    current += 1
  end

  longest = current if current > longest
end

while n = gets
  n = n.to_i
  if n < longest
    puts longest
  else
    x = 2*longest - n - 1
    puts x >= 0 ? x : 0
  end
end

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = []
m.times do |i|
  l = gets.to_i
  ans[i] = a[l...n].uniq.size
end
puts ans

n, m = gets.split.map(&:to_i)
a = [0] + gets.split.map(&:to_i)
b = Array.new(100010, 0)
bb = Array.new(100010, 0)
c = 0
for i in 1..n
  b[a[i]] += 1
  c += 1 if bb[a[i]] == 0
  bb[a[i]] = 1
end

s = []
for i in 1..n
  s[i] = c
  b[a[i]] -= 1
  c -= 1 if b[a[i]] == 0
end
ans = []
m.times do |i|
  l = gets.to_i
  ans[i] = s[i + 1]
end
puts ans


gets.to_i.times do
	gets
	puts gets.split.map(&:to_i).uniq.size
end

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

=begin
=end

def solve
	n = gets.to_i
	xs = gets.split.map{|x| x.to_i }
	puts xs.uniq.size
end

gets.to_i.times{ solve }

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

T = gets.to_i

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

  puts a.uniq.size
end

1.upto(gets.to_i) do
  gets
  puts gets.split(' ').map(&:to_i).uniq.size
end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  arr = gets.chomp.split.map(&:to_i).uniq
  puts arr.size
end


# http://codeforces.com/problemset/problem/381/A

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

l, r = 0, n - 1
sereja_move = true
result1 = 0
result2 = 0

while l < n && r >= 0 && l <= r
  if list[l] < list[r]
    max = list[r]
    r -= 1
  else
    max = list[l]
    l += 1
  end

  sereja_move ? result1 += max : result2 += max
  sereja_move = !sereja_move
end

print result1, " ", result2

def solution(n, cards)
  l, sereja, dima, count = 0, 0, 0, 0
  r = n-1
  while (l <= r ) do
    if cards[l] > cards [r]
      sereja += cards[l]
      l += 1
      count += 1
    else
      sereja += cards[r]
      r -= 1
      count += 1
    end
    if count <= n/2
      if cards[l] > cards[r]
        dima += cards[l]
        l += 1
      else
        dima += cards[r]
        r -= 1
      end
    end
  end
  puts "#{sereja} #{dima}"
end

solution(gets.chomp.to_i, gets.split.map(&:to_i))

def solution(n, cards)
  l, sereja, dima, count = 0, 0, 0, 0
  r = n-1
  while (l <= r ) do
    if cards[l] > cards [r]
      sereja += cards[l]
      l += 1
      count += 1
    else
      sereja += cards[r]
      r -= 1
      count += 1
    end
    if r >= 0 && count <= n/2
      if cards[l] > cards[r]
        dima += cards[l]
        l += 1
      else
        dima += cards[r]
        r -= 1
      end
    end
  end
  puts "#{sereja} #{dima}"
end

solution(gets.chomp.to_i, gets.split.map(&:to_i))

# http://codeforces.com/problemset/problem/381/A

n = gets.chomp.to_i

list = gets.chomp.split.map(&:to_i)

l, r = 0, n - 1

sereja_move = true
result = 0
while l < n && r >= 0 && l <= r
  max = [list[l], list[r]].max

  if sereja_move
    result += max
  end

  if list[l] < list[r]
    r -= 1
  else
    l += 1
  end
  sereja_move = !sereja_move
end

print result, " ", list.inject(:+) - result

# http://codeforces.com/problemset/problem/381/A

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

l, r = 0, n - 1
sereja_move = true
result = 0

while l < n && r >= 0 && l <= r
  if list[l] < list[r]
    max = list[r]
    r -= 1
  else
    max = list[l]
    l += 1
  end

  result += max if sereja_move
  sereja_move = !sereja_move
end

print result, " ", list.inject(:+) - result

gets
a = gets.split(' ').map(&:to_i)
s = [0, 0]

0.upto(a.size-1) do |i|
  s[i % 2] += [a.first, a.last].max

  a.__send__(a.first > a.last ? :shift : :pop)
end

puts s.join(' ')

gets
cards = gets.split.map(&:to_i)
result = [0, 0]
i = 0
until cards.empty?
  if cards.first > cards.last
    result[i] += cards.shift
  else
    result[i] += cards.pop
  end
  i += 1
  i %= 2
end
puts result.join ' '

n = gets().to_i
arr = gets.split.map(&:to_i)
l = 0
r = n-1
one = 0
two = 0
turn = true
while l <= r do
    if arr[l] >= arr[r]
        if turn
            one += arr[l]
        else
            two += arr[l]
        end
        l+=1
    else
        if turn
            one += arr[r]
        else
            two += arr[r]
        end
        r-=1
    end
    turn = !turn
end
print one," ",two,"\n"

cardNum = gets.chomp.to_i
cards = gets.chomp.split(' ').map {|i| i.to_i}

players = Array.new(2) {|i| i = 0}
nowTurn = 0

until cards.length == 0
	if cards[0] > cards[cards.length - 1]
		players[nowTurn] += cards[0]
		cards.delete_at(0)
	else
		players[nowTurn] += cards[cards.length - 1]
		cards.delete_at(cards.length - 1)
	end

	nowTurn = (nowTurn + 1) % players.length
end

puts "#{players[0]} #{players[1]}"

n = gets.to_i
card = gets.split(' ').map(&:to_i)
change = {Sereja: :Dima, Dima: :Sereja}
player = :Sereja
point = {Sereja: 0, Dima: 0}
until card.length == 0
  if card.first > card.last
    point[player] += card.delete_at(0)
  else
    point[player] += card.delete_at(-1)
  end
  player = change[player]
end
puts "#{point[:Sereja]} #{point[:Dima]}"

#!/usr/bin/ruby
gets
a=gets.split.map(&:to_i)
r=[0,0]
f=0
while !a.empty?
	if a.first>a.last
		r[f]+=a.shift
	else
		r[f]+=a.pop
	end
	f^=1
end
puts r*' '

n = gets.chomp.to_i
arr = gets.chomp.split(' ').map{|x| x.to_i}
l = 0
r = n-1
b = [0, 0]
p = 0
while l <= r
	if arr[l] >= arr[r]
		b[p] += arr[l]
		l += 1
	elsif
		b[p] += arr[r]
		r -= 1
	end
	p = 1-p
end
print b[0], " ", b[1]

n = gets.to_i
a = gets.split.map &:to_i
l = 0
r = n - 1
s = [0, 0]
turn = 0
while l <= r do
    if a[l] >= a[r] then
        s[turn] += a[l]
        l += 1
    else
        s[turn] += a[r]
        r -= 1
    end
    turn = 1 - turn
end
puts s[0].to_s + ' ' + s[1].to_s

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

i, j = 0, n - 1;
ans = [0, 0];
count = 0;

n.times {
  if (arr[i] > arr[j])
    ans[count] += arr[i];
    i += 1;
  else
    ans[count] += arr[j];
    j -= 1;
  end
  count = 1 - count;
}
# print ans , "\n";
puts ans;

n = gets.to_i
cards = gets.split.map(&:to_i)
sereja = 0
dima = 0
turn = 1
until cards.size == 0
	if turn == 1
		if cards[0] > cards[-1]
			sereja += cards[0]
			cards.shift
		else
			sereja += cards[-1]
			cards.pop
		end
	else
		if cards[0] > cards[-1]
			dima += cards[0]
			cards.shift
		else
			dima += cards[-1]
			cards.pop
		end
	end
	turn *= -1
end

puts "#{sereja} #{dima}"

#!/usr/bin/ruby

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

seq = []
while a.size > 0
  if a.first > a.last
    seq << a.shift
  else
    seq << a.pop
  end
end

if seq.size % 2 == 1
  seq << 0
end

s1 = 0
s2 = 0
while seq.size > 0
  s1 += seq.shift
  s2 += seq.shift
end

puts "#{s1} #{s2}"

n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)
ans = Array.new(2, 0)
l, r, turn = 0, n - 1, 0
while l <= r do
  if a[l] > a[r]
    ans[turn] += a[l]
    l += 1
  else
    ans[turn] += a[r]
    r -= 1
  end
  turn = 1 - turn
end
puts [ans[0], ans[1]].join(' ')

n=gets.to_i
a=gets.split.map(&:to_i)
s=[0]*2
c=0
while c<n
 s[c%2]+=a[0]>a[-1] ? a.shift : a.pop
 c+=1
end
puts s*' '

def run
  n = $stdin.gets.to_i
  a = $stdin.gets.split.map(&:to_i)
  s = [0, 0]
  c = 0
  while a.length > 0
    s[c % 2] += a[0] > a[-1] ? a.shift : a.pop
    c += 1
  end
  puts s.join(" ")
end

run if $0 == __FILE__

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
ans = [0,0]
toggle_bit = 0
until arr.empty?
    if (arr.first > arr.last)
        ans[toggle_bit] += arr.shift
    else
        ans[toggle_bit] += arr.pop
    end
    toggle_bit ^=1
end

puts "#{ans[0]} #{ans[1]}"

# cook your code here
n = gets.to_i
arr = gets.split(" ").map(&:to_i)
sereja = 0
dima = 0
count =0
val =0
len = arr.size-1
last_ind = len
firt_ind = 0
while firt_ind <= last_ind
    if arr[firt_ind] < arr[last_ind]
        val  = arr[last_ind]
        last_ind -=1
    else
        val = arr[firt_ind]
        firt_ind +=1
    end

    sereja += val if count ==0
    dima += val if count == 1
    if count ==0
        count = 1
    else
        count =0
    end

end

puts "#{sereja} #{dima}"

class A
  def initialize
    n = gets.chomp.to_i
    cards = gets.chomp.split(' ').map(&:to_i)
    sere = 0
    dima = 0
    turn = 0
    val = 0

    until cards.size.zero? do
      val = ( cards.first < cards.last )? cards.pop : cards.shift
      if turn.odd?
        dima += val
      else
        sere += val
      end
      turn += 1
    end
    puts "#{sere} #{dima}"
  end
end

A.new

a=gets.chomp.to_i
b=gets.chomp.split(" ")
if a%2==0
s=0
r=0
(a/2).times do
if b[0].to_i > b[ b.length-1].to_i
s+=b[0].to_i
b.delete_at(0)
else
s+=b[b.length-1].to_i
b.delete_at(b.length-1)
end
if b[0].to_i>b[b.length-1].to_i
r+=b[0].to_i
b.delete_at(0)
else
r+=b[b.length-1].to_i
b.delete_at(b.length-1)
end
end
else
s=0
r=0
(a/2).times do
if b[0].to_i > b[ b.length-1].to_i
s+=b[0].to_i
b.delete_at(0)
else
s+=b[b.length-1].to_i
b.delete_at(b.length-1)
end
if b[0].to_i>b[b.length-1].to_i
r+=b[0].to_i
b.delete_at(0)
else
r+=b[b.length-1].to_i
b.delete_at(b.length-1)
end
end
s+=b[0].to_i
end

puts "#{s} #{r}"

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

s = 0
d = 0
t = 0
while !cs.empty? do
  if cs[0] > cs[-1]
    c = cs.shift
  else
    c = cs.pop
  end

  if t % 2 == 0
    s += c
  else
    d += c
  end
  t += 1
end

puts "#{s} #{d}"

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

i=0
a=0
b=0
while !x.empty?
	t=0
	if x[0]<x.last then
		t=x.last
		x.pop
	else
		t=x[0]
		x.shift
	end
	if i==0 then
		a+=t
	else
		b+=t
	end
	i=1-i
end
puts [a,b]*" "

numberOfCards = gets.chomp.to_i
cards = gets.chomp.split(' ').map { |x| x.to_i }
player = 0
scores = [0, 0]

while not cards.count.zero?
  if cards.first > cards.last
    scores[player] += cards.shift
  else
    scores[player] += cards.pop
  end
  player = (player + 1) % 2
end

puts "#{scores[0]} #{scores[1]}"

# coding : utf-8

# if __FILE__ == $0
# end

n = gets.to_i
cards = gets.split.map(&:to_i)
sereja, dima = 0, 0 # sereja goes first

while !cards.empty?
    # serja
    if cards.first > cards.last
        sereja += cards.shift
    else
        sereja += cards.pop
    end

    unless cards.empty?
        # dima
        if cards.first > cards.last
            dima += cards.shift
        else
            dima += cards.pop
        end
    end
end

puts "#{sereja} #{dima}"

n = $stdin.gets.to_i
cards = $stdin.gets.chomp.split(' ').map{|e| e.to_i}

pls = [0, 0]
plnum = 0
until cards.empty?
    d = [cards.first, cards.last].max
    pls[plnum] += d
    cards.delete d
    plnum = plnum == 0 ? 1 : 0
end

puts pls.join ' '

n = gets.to_i
a = gets.split.map(&:to_i)
sol = [0, 0]
n.times do |i|
   if a.first > a.last
     sol[i % 2] += a.first
     a.shift
   else
     sol[i % 2] += a.last
     a.pop
   end
end
puts sol.join(" ")

n = gets.to_i
a = gets.split.map(&:to_i)
npoints, i = [0, 0], 0
while a.size > 0
  if a[0] > a[-1]
    npoints[i%2] += a[0]
    a = a[1..-1]
  else
    npoints[i%2] += a[-1]
    a = a[0..-2]
  end
  i += 1
end
puts npoints.join(' ')

n=gets.to_i
a=gets.split.map(&:to_i)
c=0
b=[0,0]
while a.size>0
  if a[0]>a[-1]
    b[c%2]+=a[0]
    a.shift
  else
    b[c%2]+=a[-1]
    a.pop
  end
  c+=1
end
puts b*' '

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
arr.sort!
sereja = 0
dima = 0
count =0
arr.reverse.each do |a|
    sereja += a if count == 0
    dima +=a    if count ==1
    if count == 0
        count = 1
    else
        count =0
    end

end
puts "#{sereja} #{dima}"

# cook your code here
n = gets.to_i
arr = gets.split(" ").map(&:to_i)
arr.sort!
sereja = 0
dima = 0
arr.each_with_index do |a,ind|
    if ind.even?
        dima += a
    else
        sereja +=a
    end

end
puts "#{dima} #{sereja}"

def solution(n, cards)
  l, sereja, dima = 0, 0, 0
  r = n-1
  while (l <= r ) do

    if cards[l] > cards [r]
      sereja += cards[l]
      l += 1
    else
      sereja += cards[r]
      r -= 1
    end
    if cards[l] > cards[r]
      dima += cards[l] if l < n
      l += 1
    else
      dima += cards[r] if r >0
      r -= 1
    end
  end
  puts "#{sereja} #{dima}"
end

solution(gets.chomp.to_i, gets.split.map(&:to_i))

gets
cards = gets.split.map(&:to_i).sort.each_slice(2)
result = cards.inject { |a, b|
  a[0] += b[0]
  a[1] += b[1].to_i
  a
}
# p result
puts result.sort.reverse.join ' '

gets
cards = gets.split.map(&:to_i)
result = [0, 0]
i = 0
until cards.empty?
  if cards.first > cards.last
    result[i] += cards.shift
  else
    result[i] += cards.pop
  end
  i += 1
  i %= 2
end
puts result.sort.reverse.join ' '


n, m = gets.split.map { |v| v.to_i }
m = m % (2 * n)

x, y = gets.split.map { |v| v.to_i }
a = Array.new(n) { gets.split.map { |v| v.to_i } }

(0 ... m).each do |index|
  x = 2 * a[index % n][0] - x
  y = 2 * a[index % n][1] - y
end

puts "#{x} #{y}"

n, m = gets.split.map { |v| v.to_i }
m = m % (2 * n - 1)

x, y = gets.split.map { |v| v.to_i }
a = Array.new(n) { gets.split.map { |v| v.to_i } }

(0 ... m).each do |index|
  x = 2 * a[index % n][0] - x
  y = 2 * a[index % n][1] - y
end

puts "#{x} #{y}"

n, m = gets.split.map { |v| v.to_i }
m = m % 2 * n

x, y = gets.split.map { |v| v.to_i }
a = Array.new(n) { gets.split.map { |v| v.to_i } }

(0 ... m).each do |index|
  x = 2 * a[index % n][0] - x
  y = 2 * a[index % n][1] - y
end

puts "#{x} #{y}"


n,m,x,y = gets.split.map(&:to_i)
u = gets.split.map(&:to_i)
v = gets.split.map(&:to_i)

ans = 0
now = 0
v.each do |vest|
  while now < u.size && (u[now]+y < vest)
    now += 1
  end
  break if now == u.size
  if u[now]-x <= vest && vest <= u[now]+y
    ans+=1
    now+=1
  end
end
p ans

now = 0
tmp = 0
v.each do |vest|
  while now < u.size && (u[now]+y < vest)
    now += 1
  end
  break if now == u.size
  if u[now]-x <= vest && vest <= u[now]+y
    ans+=1
    now+=1
    puts "#{now} #{tmp+1}"
  end
  tmp += 1
end

n, m, x, y = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)

res = []
j = 0
(0...n).each do |i|
  while j < m and b[j] <= a[i] + y do
    b[j] < a[i] - x ? j += 1 : break
  end
  break if j == m
  if a[i] - x <= b[j] and  b[j] <= a[i] + y
    res.push [i+1, j+1]
    j += 1
  end
end
puts res.size
res.each { |e| puts e*' ' }

# http://codeforces.com/problemset/problem/161/A
n, m, x, y = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)
b = gets.chomp.split.map(&:to_i)

i, j = 0, 0
result = []

while i < n && j < m
  if b[j] >= a[i] - x && b[j] <= a[i] + y
    result << [i + 1, j + 1]
    i += 1
    j += 1
  elsif b[j] < a[i] + y
    j += 1
  else
    i += 1
  end
end

puts result.length
result.each do |pair|
  print pair[0], " ", pair[1], "\n"
end

n, m, x, y = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)

res = []
(0...n).each do |i|
  res.push (0...m).find_all {|j| a[i]-x <= b[j] and b[j] <= a[i]+y }
end
taken = []
puts [res.flatten.uniq.size, m].min
res.each_with_index do |e, i|
  next if e.size.zero?
  jj = (0...e.size).find { |j| !taken.include?(e[j]) }
  next if jj.nil? or e[jj].nil?
  puts "#{i+1} #{e[jj]+1}"
  taken.push e[jj]
end


# ProblemA.rb

n, a, b = gets.split().map {|s| s.to_i}
chores = gets.split().map {|s| s.to_i}

chores = chores.sort

vasya = chores[0...b]
petya = chores[b..-1]

if petya.index(vasya[-1]) || vasya.index(petya[0])
  puts "0"
  exit
end

puts petya[0] - vasya[-1]

chore_number, petya_chore_number, vasya_chore_number = gets.split(' ').map(&:to_i)

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

puts chores[chore_number - petya_chore_number] - chores[vasya_chore_number - 1]

I = ->{gets.split.map(&:to_i)}
n, a, b = I[]
c = I[].sort
p c[b] - c[b - 1]

n, a, b = gets.rstrip.split(' ').map(&:to_i)
arr = gets.rstrip.split.map(&:to_i)

sorted_arr = arr.sort

b_last_chore = b - 1
b_chore = sorted_arr[b_last_chore]

a_chores = []
i = b_last_chore + 1
a.times do
  a_chores.push sorted_arr[i]
  i += 1
end

diff = a_chores.first - b_chore
puts diff

def chores
  chores = STDIN.gets.split(' ').map{|i| i.to_i}
  c = STDIN.gets.split(' ').map{|i| i.to_i}
  c.sort!

  max = c[c.size-chores[1]]

  n = max
  (c.size-chores[1]).downto(0) do |i|
    if c.select{|j| j > c[i]}.size > chores[1]
      break
    else
      n = c[i]
    end
  end
  puts max - n
end

#plant

chores

n, a, b = gets.split.collect{|i| i.to_i}
values = gets.split.collect{|i| i.to_i}.sort

if values[b-1] == values[b]
  puts 0
else
  puts values[b] - values[b-1]
end

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

list[n - a]
list[n - a - 1]

x = list[n - a] - list[n - a - 1]
puts x > 0 ? x : 0

s = gets.chomp.split(" ")
kolvo = s[0].to_i
a = s[1].to_i
b = s[2].to_i
p = gets.chomp.split(" ")
massiv = Array.new(kolvo){}
for i in(0..kolvo-1)
    massiv[i] = p[i].to_i
end
massiv = massiv.sort
if massiv[b]-massiv[b-1]!=0 then
    k = massiv[b]-massiv[b-1]
    puts k
else
    puts 0
end

def execute(n, a, b, h)
  h.sort! {|x, y| x <=> y }
  x = h[n - a] - h[b - 1]
end

# puts execute(5, 2, 3, %w(6 2 3 100 1).map(&:to_i))
# puts execute(7, 3, 4, %w(1 1 9 1 1 1 1).map(&:to_i))
n, a, b = gets.chomp.split(' ').map(&:to_i)
h = gets.chomp.split(' ').map(&:to_i)
puts execute(n, a, b, h)

n,a,b = gets.chomp.split(/ /).map!{|x| x.to_i}
h = gets.chomp.split(/ /).map!{|x| x.to_i}
h.sort!()
ret = h[b] - h[b-1]
puts ret

I = ->{gets.split.map(&:to_i)}
n, a, b = I[]
c = I[].sort
p c[b, a].min - c[0, b].max

def checks(n_a_b, h)
	n_a_b = n_a_b.split.map(&:to_i)
	n = n_a_b[0]
	a = n_a_b[1]
	b = n_a_b[2]

	h = h.split.map(&:to_i)

	h.sort!

	n_a = h.last(a)
	n_e = h.first(b)

	puts n_a.first - n_e.last
end

checks(gets, gets)

n,b,c=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i)
i=1
while i<n
	j=i-1
	while j>=0
		if a[j]<a[j+1]
			a[j],a[j+1]=a[j+1],a[j]
		end
		j-=1
	end
	i+=1
end
puts a[b-1]-a[b]

s = gets.chomp.split(" ")
kolvo = s[0].to_i
a = s[1].to_i
b = s[2].to_i
p = gets.chomp.split(" ")
massiv = Array.new(kolvo){}
for i in(0..kolvo-1)
    massiv[i] = p[i].to_i
end
massiv = massiv.sort
puts massiv
if massiv[b]-massiv[b-1]!=0 then
    k = massiv[b]-massiv[b-1]
    puts k
else
    puts 0
end

def execute(n, a, b, h)
  h.sort! {|x, y| x <=> y }
  puts h.inspect # [1, 2, 3, 6, 100]
  x = h[n - a] - h[b - 1]
end

# puts execute(5, 2, 3, %w(6 2 3 100 1).map(&:to_i))
# puts execute(7, 3, 4, %w(1 1 9 1 1 1 1).map(&:to_i))
n, a, b = gets.chomp.split(' ').map(&:to_i)
h = gets.chomp.split(' ').map(&:to_i)
puts execute(n, a, b, h)

def checks(n_a_b, h)
	n_a_b = n_a_b.split.map(&:to_i)
	n = n_a_b[0]
	a = n_a_b[1]
	b = n_a_b[2]

	h = h.split.map(&:to_i)

	h.sort!

	n_a = h.last(a)
	n_e = h.first(b)

	x = n_a.first - 1

	count = 0
	for i in (0..n - 1)
		if x < n_a.first && x >= n_e.last
			x -=1
		end
	end

	puts 4
end

checks(gets, gets)

def checks(n_a_b, h)
	n_a_b = n_a_b.split.map(&:to_i)
	n = n_a_b[0]
	a = n_a_b[1]
	b = n_a_b[2]

	h = h.split.map(&:to_i)

	h.sort!

	n_a = h.last(a)
	n_e = h.first(b)

	x = n_a.first - 1

	count = 0
	for i in (0..n - 1)
		if x < n_a.first && x >= n_e.last
			x -=1
			count += 1
		end
	end

	puts count
end

checks(gets, gets)

def checks(n_a_b, h)
	n_a_b = n_a_b.split.map(&:to_i)
	n = n_a_b[0]
	a = n_a_b[1]
	b = n_a_b[2]

	h = h.split.map(&:to_i)

	h.sort!

	puts h[n - a] - h[a]
end

checks(gets, gets)

def chores
  chores = STDIN.gets.split(' ').map{|i| i.to_i}
  c = STDIN.gets.split(' ').map{|i| i.to_i}
  c.sort!

  max = c[c.size-chores[1]]
  min = max

  (c.size-chores[1]).downto(0) do |i|
    if c[i] > min
      break
    else
      min -= 1
    end
  end
  puts max-min-1
end

chores


def ans(aa)
  bb = {}
  for v in aa
    bb[v] = 1
  end

  tt = []
  for v in (0..aa.length)
    bb[v].nil? and tt << v
  end

  t =aa.first
  ret = []
  ti = 0
  aa.each {|e|
    if e != t
      ret << t
    else
      ret << tt[ti]
      ti+=1
    end
    t =e
  }

  ret
end

gets
a = gets.chomp.split(' ').map &:to_i
puts ans(a).join(' ')

def ans(aa)
  bb = {}
  for v in aa
    bb[v] = 1
  end

  tt = []
  for v in (0..aa.length)
    bb[v].nil? and tt << v
  end

  t = 0
  ret = []
  ti = 0
  aa.each {|e|
    if e != t
      ret << t
    else
      ret << tt[ti]
      ti+=1
    end
    t =e
  }

  ret
end

gets
a = gets.chomp.split(' ').map &:to_i
puts ans(a).join(' ')

def ans(aa)
  ret = []
  v = aa.first
  vi = 0
  if aa.first == 0
    ret << 1
  elsif aa.first == 1
    ret << 0
  else
    return -1
  end
  retMax = ret.max
  aa.each_with_index {|e,i|
    if e != v
      ret[i]= v
      retMax = [retMax, v].max
      t = ret[vi]
      #printf("ret:%s i:%d e:%d\n", ret.to_s, i, e)
      if e-v >= i - vi
        t = v + 1
        t == e and t+=1
      end
      (vi+1...i).each { |j|
        ret[j] = [t, e-1].min
        retMax = [retMax, ret[j]].max
        t+=1
        t == e and t+=1
      }
      retMax < e-1 and return -1
      v = e
      vi = i
    end
  }

  while ret.length < aa.length
    ret << ret.last
  end
  return ret
end

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


n = gets.to_i
a = []
n.times do |i|
    s = gets.chomp
    if a.include?(s)
        puts "YES"
    else
        puts "NO"
        a.push(s)
    end
end

require 'set'

read = Set.new

gets.to_i.times do
	name = gets.chomp;

	puts read.include?(name) ? "YES" : "NO"

	read.add(name)
end

n = gets.to_i
s = [gets.chomp]
puts 'NO'
(n - 1).times do
  flag = FALSE
  t = gets.chomp
  s.each do |name|
    if t == name
      puts 'YES'
      flag = TRUE
      break
    end
  end
  if !flag
    puts 'NO'
    s << t
  end
end

h={}
gets.to_i.times{s=gets.chomp;puts h[s]?:YES: :NO;h[s]=1}

n = gets.to_i
names = []
n.times do
  s = gets.chomp
  if names.include?(s)
    puts 'YES'
  else
    puts 'NO'
  end
  names << s
end

n = gets.strip.to_i
list = {}
n.times do
    s = gets.strip
    if list.has_key? s
        puts "YES"
    else
        list[s] = 1
        puts "NO"
    end
end

n = gets.to_i
name = {}
n.times do
    s = gets
    if name[s]
        puts 'YES'
    else
        puts 'NO'
        name[s] = true
    end
end

t = gets.chomp.to_i
arr = []
t.times do
	t = gets.chomp
	if arr.include? t
		puts "yes"
	else
		puts "no"
		arr << t
	end
end

n = gets

a={}
b=[]
for i in 0..n.to_i-1 do
    value = gets
    b[i]=value
end

for i in 0..n.to_i-1 do
    value = b[i]
    if a[value]
        print "YES"
        print "\n" if i!=n.to_i-1
    else
        print "NO"
        a[value]=1
        print "\n" if i!=n.to_i-1
    end
end

n = gets

a={}
for i in 1..n.to_i do
    value = gets
    if a[value]
        p "YES"
    else
        p "NO"
        a[value]=1
    end
end


n, m = *gets.split(' ').map { |s| s.to_i }
a = []
n.times do
    a << gets.chomp
end

def ok?(a, x1, y1, x2, y2)
    x1.upto(x2) do |i|
        y1.upto(y2) do |j|
            return false if a[i][j] == ?1
        end
    end
    return true
end

perimeter = -1
0.upto(n-1) do |x2|
    0.upto(m-1) do |y2|
        0.upto(x2) do |x1|
            0.upto(y2) do |y1|
                p = x2+y2-x1-y1
                next if p <= perimeter
                next unless ok?(a, x1, y1, x2, y2)
                perimeter = p
            end
        end
    end
end

puts perimeter * 2 + 4

n,m = gets.split.map(&:to_i)
r = Array.new(n){gets.split ''}
a = 4
for i in 0...n
for j in 0...m
    next if r[i][j]=='1'
    for ii in i...n
        break if r[ii][j]=='1'
per = 2*(2+ii-i)
a=per if per>a
        for jj in (j+1)...m
            break unless for iii in i..ii
                break if r[iii][jj]=='1'
            end
            per = 2*((1+ii-i)+(1+jj-j))
            a=per if per>a
        end
    end
end
end
p a

n,m = gets.split.map(&:to_i)
r = Array.new(n){gets.split ''}
a = 4
for i in 0...n
for j in 0...m
    next if r[i][j]=='1'
    for ii in (i+1)...n
        break if r[ii][j]=='1'
        for jj in (j+1)...m
            break unless for iii in i..ii
                break nil if r[iii][jj]=='1'
            end
            per = 2*((1+ii-i)+(1+jj-j))
            a=per if per>a
        end
    end
end
end
p a

n,m = gets.split.map(&:to_i)
r = Array.new(n){gets.split ''}
a = 4
for i in 0...n
for j in 0...m
    next if r[i][j]=='1'
    for ii in (i+1)...n
per = 2*((1+ii-i)+1)
a=per if per>a
        break if r[ii][j]=='1'
        for jj in (j+1)...m
            break unless for iii in i..ii
                break nil if r[iii][jj]=='1'
            end
            per = 2*((1+ii-i)+(1+jj-j))
            a=per if per>a
        end
    end
end
end
p a


a=gets.chomp.to_i
b=[]
a.times do
temp=gets.chomp.split(" ")
c=(temp[2].to_i-temp[0].to_i+1)*(temp[3].to_i-temp[1].to_i+1)
b<<c
end
ans=0
b.each {|i| ans=ans+i}
puts "#{ans}"

ans=0
gets.to_i.times{|e|
	x1,y1,x2,y2=gets.split.map{|e| e.to_i}
	ans+=((x1-x2).abs+1)*((y1-y2).abs+1)
}
puts ans

n = gets.to_i
ans = n.times.map do |i;x1,x2,y1,y2|
	x1,y1,x2,y2 = gets.split.map(&:to_i)
	(x2 - x1 + 1) * (y2 - y1 + 1)
end
puts ans.reduce(:+)

n = gets.to_i
d = n.times.map{gets.split.map(&:to_i)}
table = Array.new(100){Array.new(100){0}}
d.each do |x1,y1,x2,y2|
    i = x1
    while(i <= x2)
        j = y1
        while(j <= y2)
            table[j-1][i-1] += 1
            j+=1
        end
        i+=1
    end
end
p table.flatten.reduce(&:+)

n = gets.to_i
ans = 0
n.times do
    x1, y1, x2, y2 = gets.split.collect{|i| i.to_i}
    ans += (x2-x1+1)*(y2-y1+1)
end
puts ans

n = gets.to_i
ans = 0
n.times do
  a, b, c, d = gets.split.map(&:to_i)
  ans += (c - a + 1) * (d - b + 1)
end
puts ans

#!/usr/bin/env ruby

num = ARGF.gets.chomp.to_i
count = 0

num.times do
    x1, y1, x2, y2 = ARGF.gets.chomp.split(' ').map { |s| s.to_i }
    count += (y2 - y1 + 1) * (x2 - x1 + 1)
end

puts count

n = gets().chomp().to_i
count = 0
n.times do
        x1, y1, x2, y2= gets().split(' ').map{ |p| p.to_i}

        count += (y2 - y1 +1) *(x2-x1+1)
end
puts count

n = gets.to_i
ans = 0
n.times do
	x1, y1, x2, y2 = gets.split.map { |x| x.to_i }
	ans += (x2 - x1 + 1) * (y2 - y1 + 1)
end
puts ans

n = gets.to_i
result = 0
n.times do
   x1, y1, x2, y2 = gets.split.map(&:to_i)
   result += (x2 - x1 + 1) * (y2 - y1 + 1)
end

p result

# http://codeforces.com/contest/552/problem/A
n = gets.chomp.to_i
hash = Hash.new(0)
n.times do
  x1, y1, x2, y2 = gets.chomp.split.map(&:to_i)
  (x1..x2).each do |x|
    (y1..y2).each do |y|
      hash[[x, y]] += 1
    end
  end
end

puts hash.values.inject(0) { |a, e| a + e }

#!/usr/bin/ruby
# ==> Made With LOVE <3 <==

n = gets.to_i
sum=0

n.times do
	x1, x2, y1, y2 = gets.split.map(&:to_i)
	sum += (y1 - x1 + 1) * (y2 - x2 + 1)
end

puts sum

rectangleNum = gets.chomp.to_i
rectangle = []

(0...rectangleNum).each do |iter|
	rectangle << gets.chomp.split(' ').map {|item| item.to_i}
end

result = 0
rectangle.each do |rec|
	result += (rec[2] - rec[0] + 1) * (rec[3] - rec[1] + 1)
end

puts result

rectangleNum = gets.chomp.to_i

firstRec = gets.chomp.split(' ').map {|item| item.to_i}
secondRec = gets.chomp.split(' ').map {|item| item.to_i}

result = 0

maxX = if firstRec[2] > secondRec[2]
	firstRec[2]
else
	secondRec[2]
end

maxY = if firstRec[3] > secondRec[3]
	firstRec[3]
else
	secondRec[3]
end

(1..maxY).each do |y|
	(1..maxX).each do |x|
		if x >= firstRec[0] && x <= firstRec[2] && y >= firstRec[1] && y <= firstRec[3]
			result += 1
		end

		if x >= secondRec[0] && x <= secondRec[2] && y >= secondRec[1] && y <= secondRec[3]
			result += 1
		end
	end
end

puts result


gets.to_i.times do
  a, b = gets.split.map(&:to_i)
  if a > b
    puts "10" * b + "0" * (a - b)
  else
    puts "01" * a + "1" * (b - a)
  end
end

# gets.to_i
# gets.split.map(&:to_i)

gets.to_i.times do
  a, b = gets.split.map(&:to_i)
  if a > b
    ans = Array.new(a+b, 0)
    i = 0
    while b > 0
      ans[i] = 1
      i += 2
      b -= 1
    end
  else
    ans = Array.new(a+b, 1)
    i = 0
    while a > 0
      ans[i] = 0
      i += 2
      a -= 1
    end
  end

  puts ans.join
end

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

  s = ""
  while a + b > 0
    if a > 0
      s += '0'
      a -= 1
    end

    if b > 0
      s += '1'
      b -= 1
    end
  end

  puts s
end

gets.to_i.times do
  a, b = gets.split.map(&:to_i)
  d = [(a - b).abs, 1].max

  s = ""
  while a + b > 0
    ta = a - [a - d, 0].max
    tb = b - [b - d, 0].max
    s += '0' * ta
    s += '1' * tb
    a -= ta
    b -= tb
  end

  puts s
end


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

t = file.gets.to_i

while t != 0
  n = file.gets.to_i
  ans = n / 2
  if ans == 0 || ans == 1
    puts n == 2 ? 0 : ans
  else
    res = if n.even?
      (ans - 1) <= 0 ? 0 : ans-1
    else
      ans
    end
    puts res
  end
  t -= 1
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  n-=1 if n%2 == 0
  puts n/2
end

gets.to_i.times do
	n = gets.to_i
	puts (n - 1) / 2
end

t = gets.to_i
t.times do
	n = gets.to_i
	puts n-n/2-1
end

t = gets.to_i
for i in 1..t do
  n = gets.to_i
  puts n - (n/2 + 1)
end

t = gets.chomp.to_i

(1..t).each{
    n = gets.chomp.to_i

    puts (n-1)/2
}

# https://codeforces.com/problemset/problem/1335/A
def solution(n)
  return (n / 2.0).ceil - 1
end

t = gets.chomp.to_i

while t > 0 do
  puts solution(gets.chomp.to_i)
  t -= 1
end

gets.to_i.times do
  n = gets.to_i
  puts (n - 1)/2
end

t = gets.to_i

1.upto(t) do
  n = gets.to_i
  puts n.div(2) - 1 + n.modulo(2)
end

t = gets.to_i
(1).upto(t) do
  n = gets.to_i
  puts (n-1)/2
end

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

t = file.gets.to_i

while t != 0
  n = file.gets.to_i
  ans = n / 2
  if ans == 0 || ans == 1
    puts n == 2 ? 0 : ans
  else
    res = if ans.even?
      (ans - 1) <= 0 ? 0 : ans-1
    else
      ans
    end
    puts res
  end
  t -= 1
end

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

t = file.gets.to_i

while t != 0
  n = file.gets.to_i
  ans = n / 2
  res = if ans.even?
    (ans - 1) <= 0 ? 0 : ans-1
  else
    ans
  end
  puts res
  t -= 1
end


n = gets.to_i
sum = 0

arr = gets.split(" ").map(&:to_i).sort
area = arr.map { |e| e * e * Math::PI }

if n.even?
	start = 1
else
	sum += area[0]
	start = 2
end

(start..(n-1)).step(2).each do |i|
	sum += area[i] - area[i-1]
end
puts sum

n = gets.to_i
a = Array.new
s = gets.split(' ')
for i in 0..n-1
  a[i] = s[i].to_i
end
a = a.sort
a = a.reverse
ans = 0
0.step(n-2, 2) { |i|
  ans += a[i] * a[i] - a[i+1] * a[i+1]
}
if n % 2 == 1
  ans += a[n-1] * a[n-1]
end
puts ans * Math::PI

n = gets.to_i
a = gets.split.map(&:to_i).sort.reverse
ans = 0
0.step(n-2, 2) { |i|
  ans += a[i] * a[i] - a[i+1] * a[i+1]
}
ans += a[n-1] * a[n-1] if n % 2 == 1
p ans * Math::PI

def area rad
	Math::PI * rad * rad
end

n = STDIN.gets.chomp.to_i
radius = STDIN.gets.chomp.split(' ').map{|ch| ch.to_i}.sort
total = 0.0
num = 0
(n - 1).downto(0).each do |i|
	if num % 2 == 0
		total += area radius[i]
	else
		total -= area radius[i]
	end
	num += 1
end

puts total

def area rad
	Math::PI * rad * rad
end

n = STDIN.gets.chomp.to_i
radius = STDIN.gets.chomp.split(' ').map{|ch| ch.to_i}.sort
total = 0.0
num = 0
(n - 1).downto(0).each do |i|
	total += (num % 2 == 0) ? area(radius[i]) : -(area radius[i])
	num += 1
end

puts total

n = gets.to_i
r = gets.split.map &:to_i
r << 0 if r.size.odd?
s = 0
r.sort.each_slice(2) do |x, y|
  s += y**2 - x**2
end
p s * Math::PI

n = gets.to_i
rads = gets.split.map{|i| i.to_i}.sort

s = 0
if rads.size % 2 == 0 then
    i = 0
    while 2*i < rads.size do
        s += (rads[2*i+1]**2 - rads[2*i]**2)
        i += 1
    end
else
    i = 1
    s += rads[0]**2
    while 2*i < rads.size
        s += (rads[2*i]**2 - rads[2*i-1]**2)
        i += 1
    end
end

s *= Math::PI

puts s

gets
p Math::PI * gets.split.map{|_| _.to_f ** 2}.sort.inject{|i, j| j - i}

n = gets.to_i
r = gets.split.map(&:to_i).sort.reverse

back = 0
ans = 0
r.each do |x|
        if back<x
                ans += x*x
                back = x
        else
                ans -= x*x
                back = 0
        end
end
p ans*3.14159265358979323846

n = gets.chomp.to_i
r = gets.chomp.split(" ").map{|e| e.to_i}
r.sort!{|a, b| -(a <=> b)}

sum = 0.0
for i in 0..(n - 1) do
  if (i % 2 != 0)
    next
  end

  if (i + 1 >= n)
    sum += r[i] ** 2 * Math::PI
  else
    sum += (r[i] ** 2 - r[i + 1] ** 2) * Math::PI
  end
end
puts sum

n=gets.to_i
r=gets.chomp.split.map(&:to_i).sort.reverse
i,sign,ans=0,1,0
while i<n
	ans+=sign*r[i]*r[i]
	i+=1
	sign*=-1
end
puts 3.1415926536*ans

n = gets.to_i
a = gets.split.map(&:to_i)
s = a.sort!.reverse!.map! {|x| x**2 }.each_index { |i| a[i] *= -1 if i.odd? }.inject(:+)
puts s *= Math::PI

n = gets.to_i

a = gets.split.map &:to_f
a.sort!

ans = a.inject(0) do |x,y|
    y*y - x
end

p Math::PI * ans

n = gets.to_i
r = gets.split.collect{|i| i.to_i}.sort.reverse
r.push(0)
ans = 0
for i in (0...n).step(2)
    ans += Math::PI*(r[i]*r[i]-r[i+1]*r[i+1])
end
puts ans

n = gets.to_i
r = gets.split.collect{|i| i.to_i}.sort.reverse
r.push(0)
ans = 0
outer_r = r[0]
color = 'r'
r[1,n].each do |inner_r|
    if color == 'r'
        ans += Math::PI*(outer_r*outer_r-inner_r*inner_r)
        color = 'g'
    else
        color = 'r'
    end
    outer_r = inner_r
end
puts ans

n  = gets.chomp.to_i
r  = gets.split.collect{|x| x.to_f}.sort
pi = 3.1415926
red = 0
(0..n-1).each{|i|
  if i % 2 == n % 2
    red -= (pi * r[i] * r[i])
  else
    red += (pi * r[i] * r[i])
  end
}

puts red

n=gets.to_i
a=gets.chomp.scan(/\w+/)
for i in 0..n-1 do
  a[i]=a[i].to_i
end
a.sort!
a.reverse!
k=0
for i in 0..n-1 do
  (i%2==0)?k+=(Math::PI)*a[i]*a[i]:k-=(Math::PI)*a[i]*a[i]
end
puts k

n = gets.to_i
radii = gets.split.map{|e| e.to_f}
radii.sort!.reverse!

PI = 3.141592653589793238462643

red = 0.0
prev = true
for i in 0..n-1
	if prev
		red += (radii[i] * radii[i] * PI)
		prev = false
	else
		red -= (radii[i] * radii[i] * PI)
		prev = true
	end
end

puts red

n = gets.to_i
a = gets.split.map { |x| x.to_i }.sort
m = a.size % 2
s = 0
for i in (n - 1).step(m + 1, -2)
  s += a[i] ** 2 - a[i - 1] ** 2
end
s += a[0] ** 2 if m == 1
puts(s * Math::PI)

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

sum = 0
lists.each_with_index do |v, i|
  sum += (i % 2 == 0) ? v * v : - v * v
end

puts (sum * Math::PI)

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

radii.sort!

area = 0
red = true
for i in 0...(n-1)
  if red
    curr = radii[n - 1 - i]
    prev = radii[n - 2 - i]
    diff = Math::PI * curr * curr - Math::PI * prev * prev
    area += diff
  end
  red = !red
end

if red
  area += Math::PI * radii[0] * radii[0]
end

puts area

gets
p gets.split.map { |i| i.to_i }.sort! { |a, b| b<=>a }.map { |r| Math::PI*r*r }.each_with_index.inject(0){ |sum, (value,index)| index.even? ? sum + value : sum - value }

gets
p Math::PI * gets.split.map{|_| _.to_f ** 2}.sort.reverse.inject{|i, j| j - i}

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

back = 0
ans = 0
r.each do |x|
        if back<x
                ans += x*x
                back = x
        else
                ans -= x*x
                back = 0
        end
end
p ans*3.14159265358979323846

n=gets.to_i
a=gets.chomp.scan(/\w+/)
for i in 0..n-1 do
  a[i]=a[i].to_i
end
a.sort!
k=0
for i in 0..n-1 do
  (i%2==0)?k+=(Math::PI)*a[i]*a[i]:k-=(Math::PI)*a[i]*a[i]
end
puts k

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

sum = 0
lists.each_with_index do |v, i|
  p sum
  sum += (i % 2 == 0) ? v * v : - v * v
end
p lists
p sum
puts (sum * Math::PI)

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

radii.sort!

area = 0
red = true
for i in 0...(n-1)
  if red
    curr = radii[n - 1 - i]
    prev = radii[n - 2 - i]
    puts curr
    puts prev
    diff = Math::PI * curr * curr - Math::PI * prev * prev
    area += diff
  end
  red = !red
end

if red
  area += Math::PI * radii[0] * radii[0]
end

puts area


s = gets.chomp
st = []
seq = []
rev = {}
rev[')'] = '('
rev[']'] = '['
i = 0
s.each_char do |c|
    if (c == '(' || c == '[')
        st.push([i, c])
    elsif (st.size() > 0 && (c == ')' || c == ']') && rev[c] == st.last[1])
        seq.push(st.last)
        seq.push([i, c])
        st.pop
    else
        while (st.size() > 0)
            st.pop
        end
    end
    i += 1
end
seq.sort!
res = "", mx = -1, prv = -1, c = 0, t = ""
for i in seq
    # puts "#{i[0]} #{i[1]}"
    if (i[0] - prv == 1)
        c += 1 if (i[1] == '[')
        t << i[1]
    else
        if (mx < c)
            mx = c
            res = t.dup
        end
        t.clear
        c = 0
        c += 1 if (i[1] == '[')
        t << i[1]
    end
    prv = i[0]
end

if (mx < c)
    mx = c
    res = t.dup
end
puts mx, res

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	#puts 'left = %d, right = %d, count = %d, brackets = %s, parens = %s' % [
	#		left, right, count, brackets, parens]
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		if parens.length == 0 and brackets.length == 0
			if count > best_count
				best_count, best_left, best_right = count, left, right
			end
		end
		right += 1
	else
		next_left = right+1

		# find the longest correct substring preceding the error
		right -= 1
		seek = right
		b, p, count = 0, 0, 0
		while seek >= left
			ch = s[seek]
			if ch == '['
				b -= 1
				count += 1
			elsif ch == ']'
				b += 1
			elsif ch == '('
				p -= 1
			elsif ch == ')'
				p += 1
			end
			if b == 0 and p == 0 and count > best_count
				best_count, best_left, best_right = count, seek, right
			end
			if b < 0 or p < 0
				b, p, count = 0, 0, 0
				right = seek-1
			end
			seek -= 1
		end

		parens, brackets = [], []
		left = right = next_left
		count = 0
	end
end
puts best_count
puts s[best_left..best_right]

s = gets.chomp
st = []
seq = []
rev = {}
rev[')'] = '('
rev[']'] = '['
i = 0
s.each_char do |c|
    if (c == '(' || c == '[')
        st.push([i, c])
    elsif (st.size() > 0 && (c == ')' || c == ']') && rev[c] == st.last[1])
        seq.push(st.last)
        seq.push([i, c])
        st.pop
    else
        while (st.size() > 0)
            st.pop
        end
    end
    i += 1
end
seq.sort!
res = "", mx = -1, prv = -1, c = 0, t = ""
for i in seq
    if (i[0] - prv == 1)
        c += 1 if (i[1] == '[')
        t.concat(i[1])
    else
        if (mx < c)
            mx = c
            res = t.dup
        end
        t.clear
        c = 0
        c += 1 if (i[1] == '[')
        t.concat(i[1])
    end
    prv = i[0]
end

if (mx < c)
    mx = c
    res = t.dup
end
puts mx, res

A,B,C,D=[?[,?],?(,?)].map(&:ord)
def match(c,d)
	return 1 if (c==A)&&(d==B)
	return 0 if (c==C)&&(d==D)
	false
end
t=gets.chomp
s=t.bytes.each_with_index.map.to_a
size=s.size
table=Array.new(size+1)
stack=[]
s.each{|chr|
	ord,idx=chr
	if (ord==A)||(ord==C)||stack.empty?
		stack.push chr
	else
		if m=match(stack.last.first,ord)
			_ord,_idx=stack.pop
			table[idx]=0
			table[_idx]=m
		else
			stack.push chr
		end
	end
}
max=0
sum=0
_l=l=r=0
table.each_with_index{|n,idx|
	if n
		sum+=n
	else
		if sum>max
			max=sum
			l=_l
			r=idx-1
		end
		sum=0
		_l=idx+1
	end
}
puts max
puts max==0 ? "" : t[l..r]

s = gets.chomp
st = []
seq = []
rev = {}
rev[')'] = '('
rev[']'] = '['
i = 0
s.each_char do |c|
    if (c == '(' || c == '[')
        st.push([i, c])
    elsif (st.size() > 0 && (c == ')' || c == ']') && rev[c] == st.last[1])
        seq.push(st.last)
        seq.push([i, c])
        st.pop
    else
        while (st.size() > 0)
            st.pop
        end
    end
    i += 1
end
seq.sort!
res = "", mx = -1, prv = -1, c = 0, t = ""
for i in seq
    # puts "#{i[0]} #{i[1]}"
    if (i[0] - prv == 1)
        c += 1 if (i[1] == '[')
        t << i[1]
    else
        if (mx < c)
            mx = c
            res = t.dup
        end
        t = ""
        c = 0
        c += 1 if (i[1] == '[')
        t += i[1]
    end
    prv = i[0]
end

if (mx < c)
    mx = c
    res = t.dup
end
puts mx, res

s = gets.chomp
st = []
seq = []
rev = {}
rev[')'] = '('
rev[']'] = '['
i = 0
s.each_char do |c|
    if (c == '(' || c == '[')
        st.push([i, c])
    elsif (st.size() > 0 && (c == ')' || c == ']') && rev[c] == st.last[1])
        seq.push(st.last)
        seq.push([i, c])
        st.pop
    else
        while (st.size() > 0)
            st.pop
        end
    end
    i += 1
end
seq.sort!
res = "", mx = -1, prv = -1, c = 0, t = ""
for i in seq
    # puts "#{i[0]} #{i[1]}"
    if (i[0] - prv == 1)
        c += 1 if (i[1] == '[')
        t << i[1]
    else
        if (mx < c)
            mx = c
            res = t.dup
        end
        t.clear
        c = 0
        c += 1 if (i[1] == '[')
        t += i[1]
    end
    prv = i[0]
end

if (mx < c)
    mx = c
    res = t.dup
end
puts mx, res

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
best_count = 0
best_left, best_right = 1, 0
while right < s.length
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		right += 1
	else
		next_left = right+1
		seek = right = right-1
		b, p, count = 0, 0, 0
		while seek >= left
			ch = s[seek]
			if ch == '['
				b -= 1
				count += 1
			elsif ch == ']'
				b += 1
			elsif ch == '('
				p -= 1
			elsif ch == ')'
				p += 1
			end
			if b == 0 and p == 0 and count > best_count
				best_count, best_left, best_right = count, seek, right
			end
			if b < 0 or p < 0
				b, p, count = 0, 0, 0
				right = seek-1
			end
			seek -= 1
		end
		parens, brackets = [], []
		left = right = next_left
	end
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		if parens.length == 0 and brackets.length == 0
			if count > best_count
				best_count, best_left, best_right = count, left, right
			end
		end
		right += 1
	else
		while left != right
			if parens.length == 0 and brackets.length == 0
				if count > best_count
					best_count, best_left, best_right = count, left, right-1
				end
			end
			if s[left] == '('
				parens.shift
			elsif s[left] == '['
				brackets.shift
				count -= 1
			end
			left += 1
		end
		parens = []
		brackets = []
		left = right = right+1
		count = 0
	end
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		right += 1
	else
		while left != right
			if parens.length == 0 and brackets.length == 0
				if count > best_count
					best_count, best_left, best_right = count, left, right-1
				end
			end
			if s[left] == '('
				parens.shift
			elsif s[left] == '['
				brackets.shift
				count -= 1
			end
			left += 1
		end
		left = right = right+1
		count = 0
	end
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	#puts 'left = %d, right = %d, count = %d, brackets = %s, parens = %s' % [
	#		left, right, count, brackets, parens]
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		if parens.length == 0 and brackets.length == 0
			if count > best_count
				best_count, best_left, best_right = count, left, right
			end
		end
		right += 1
	else
		next_left = right+1

		# find the longest correct substring preceding the error
		seek = right-1
		b, p, count = 0, 0, 0
		while seek >= left
			ch = s[seek]
			if ch == '['
				b -= 1
				count += 1
			elsif ch == ']'
				b += 1
			elsif ch == '('
				p -= 1
			elsif ch == ')'
				p += 1
			end
			if b == 0 and p == 0 and count > best_count
				best_count, best_left, best_right = count, seek, right
			end
			if b < 0 or p < 0
				b, p, count = 0, 0, 0
			end
			seek -= 1
		end

		parens, brackets = [], []
		left = right = next_left
		count = 0
	end
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '('
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	ch = s[right]
	complete, error = false, false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
			complete = true if parens.length == 0 and brackets.length == 0
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
			complete = true if parens.length == 0 and brackets.length == 0
		end
	end
	if not complete and not error
		right += 1
		next
	end
	if complete
		if count > best_count
			best_count, best_left, best_right = count, left, right
		end
	elsif error
		while left != right
			if parens.length == 0 and brackets.length == 0
				if count > best_count
					best_count, best_left, best_right = count, left, right-1
				end
			end
			if s[left] == '('
				parens.shift
			elsif s[left] == '['
				brackets.shift
				count -= 1
			end
			left += 1
		end
	end
	left = right = right+1
	count = 0
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		if parens.length == 0 and brackets.length == 0
			if count > best_count
				best_count, best_left, best_right = count, left, right
			end
		end
		right += 1
	else
		while left != right
			if parens.length == 0 and brackets.length == 0
				if count > best_count
					best_count, best_left, best_right = count, left, right-1
				end
				break
			end
			if s[left] == '('
				parens.shift
			elsif s[left] == '['
				brackets.shift
				count -= 1
			end
			left += 1
		end
		parens = []
		brackets = []
		left = right+1
		right = right+1
		count = 0
	end
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	#puts 'left = %d, right = %d, count = %d, brackets = %s, parens = %s' % [
	#		left, right, count, brackets, parens]
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		if parens.length == 0 and brackets.length == 0
			if count > best_count
				best_count, best_left, best_right = count, left, right
			end
		end
		right += 1
	else
		next_left = right+1

		# find the longest correct substring preceding the error
		right -= 1
		right -= 1 while right != left and s[right] != ']'
		if right != left
			b, p, count = 0, 0, 0
			seek = right
			while seek >= left
				ch = s[seek]
				if ch == '['
					b -= 1
					count += 1
				elsif ch == ']'
					b += 1
				elsif ch == '('
					p -= 1
				elsif ch == ')'
					p += 1
				end
				if b == 0 and p == 0 and count > best_count
					best_count, best_left, best_right = count, seek, right
				end
				break if b < 0 or p < 0
				seek -= 1
			end
		end

		parens, brackets = [], []
		left = right = next_left
		count = 0
	end
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
best_case = nil
while right < s.length
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		if parens.length == 0 and brackets.length == 0
			if count > best_count
				best_count, best_left, best_right = count, left, right
				best_case = 0
			end
		end
		right += 1
	else
		while left != right
			if parens.length == 0 and brackets.length == 0
				if count > best_count
					best_count, best_left, best_right = count, left, right-1
					best_case = 1
				end
				break
			end
			if s[left] == '('
				parens.shift
			elsif s[left] == '['
				brackets.shift
				count -= 1
			end
			left += 1
		end
		parens = []
		brackets = []
		left = right+1
		right = right+1
		count = 0
	end
end
if s[best_left..best_right] == ']][[[[[]'
	puts 'best_case == %d' % best_case
end
puts best_count
puts s[best_left..best_right]

#!/usr/bin/ruby

s = STDIN.readline.strip + '$'
parens, brackets = [], []
left, right = 0, 0
count, best_count = 0, 0
best_left, best_right = 1, 0
while right < s.length
	ch = s[right]
	error = false
	if ch == '('
		parens.push(right)
	elsif ch == '['
		brackets.push(right)
		count += 1
	elsif ch == ')'
		if parens.length == 0
			error = true
		elsif brackets.length != 0 and brackets[-1] > parens[-1]
			error = true
		else
			parens.pop
		end
	elsif ch == ']'
		if brackets.length == 0
			error = true
		elsif parens.length != 0 and parens[-1] > brackets[-1]
			error = true
		else
			brackets.pop
		end
	else
		error = true
	end
	if not error
		if parens.length == 0 and brackets.length == 0
			if count > best_count
				best_count, best_left, best_right = count, left, right
			end
		end
		right += 1
	else
		while left != right
			if parens.length == 0 and brackets.length == 0
				if count > best_count
					best_count, best_left, best_right = count, left, right-1
				end
			end
			if s[left] == '('
				parens.shift
			elsif s[left] == '['
				brackets.shift
				count -= 1
			end
			left += 1
		end
		left = right = right+1
		count = 0
	end
end
puts best_count
puts s[best_left..best_right]

s = gets.chomp
st = []
seq = []
rev = {}
rev[')'] = '('
rev[']'] = '['
i = 0
s.each_char do |c|
    if (c == '(' || c == '[')
        st.push([i, c])
    elsif (st.size() > 0 && (c == ')' || c == ']') && rev[c] == st.last[1])
        seq.push(st.last)
        seq.push([i, c])
        st.pop
    else
        while (st.size() > 0)
            st.pop
        end
    end
    i += 1
end
seq.sort!
res = "", mx = -1, prv = -1, c = 0, t = ""
for i in seq
    # puts "#{i[0]} #{i[1]}"
    if (i[0] - prv == 1 || prv == -1)
        c += 1 if (i[1] == '[')
        t += i[1]
    else
        if (mx < c)
            mx = c
            res = t.dup
            t = ""
            c = 0
            c += 1 if (i[1] == '[')
            t += i[1]
        end
    end
    prv = i[0]
end

if (mx < c)
    mx = c
    res = t.dup
end
puts mx, res


n = gets.to_i
x = gets.split.map(&:to_i)
v = x[0]
c = [0,0,0,0,0]
n.times do |i|
  c[x[i]-v+2] += 1
end
maxidx = minidx = 2
5.times do |i|
  if c[i] != 0
    minidx = i if i < minidx
    maxidx = i if i > maxidx
  end
end
diff = maxidx - minidx
if diff != 2
  p n
  puts x.join(" ")
else
  num = []
  cnt = [c[minidx],c[minidx+1],c[minidx+2]]
  a = [cnt[0],cnt[2]].min * 4 #incrase cnt[1]
  b = cnt[1]/2 * 4 #decrease cnt[1]
  if a > b
    k = [cnt[0],cnt[2]].min
    num[0] = cnt[0]-k
    num[2] = cnt[2]-k
    num[1] = cnt[1] + k*2
    ans = num[0] + num[1] +num[2] - k*2
  else
    k = cnt[1]/2
    num[0] = cnt[0] + k
    num[1] = cnt[1] - k*2
    num[2] = cnt[2] + k
    ans = num[0] + num[1] +num[2] - k*2
  end
  p ans
  res = []
  3.times do |i|
    res += [v+minidx-2+i] * num[i] if num[i] > 0
  end
  puts res.join(" ")
end
#p c
#p cnt
#p num

n = gets.chomp.to_i
arr = gets.chomp.split(" ").map {|e| e.to_i}
mi = arr.min
ma = arr.max

out = n
mac = 0
mic = 0
if ma - mi > 1
	arr.each do |e|
		if e == mi
			mic +=1
		elsif e == ma
			mac +=1
		end
	end
end
mmc = (n - mic - mac)

if mic > mac
	mic = mac
else
	mac = mic
end
if mmc % 2 != 0
	mmc = mmc - 1
end
if mmc <= (2*mic)
	out -= mic*2
	arr = arr.map do |e|
		if e == mi && mic > 0
			mic -= 1
			e + 1
		elsif e == ma && mac > 0
			mac -= 1
			e-1
		else
			e
		end
	end
elsif (ma - mi) > 1
	out -= mmc
	mm = mi + 1
	i = 1
	arr = arr.map do |e|
		if e == mm && mmc > 0
			i = -i
			mmc -= 1
			e + i
		else
			e
		end
	end
end
puts out
puts arr.join(" ")

n = gets.to_i
x = gets.split.map(&:to_i)
v = x[0]
c = [0,0,0,0,0]
n.times do |i|
  c[x[i]-v+2] += 1
end
maxidx = minidx = 2
5.times do |i|
  if c[i] != 0
    minidx = i if i < minidx
    maxidx = i if i > maxidx
  end
end
diff = maxidx - minidx
if diff != 2
  p 0
  puts x.join(" ")
else
  num = []
  cnt = [c[minidx],c[minidx+1],c[minidx+2]]
  a = [cnt[0],cnt[2]].min * 4 #incrase cnt[1]
  b = cnt[1]/2 * 4 #decrease cnt[1]
  if a > b
    k = [cnt[0],cnt[2]].min
    num[0] = cnt[0]-k
    num[2] = cnt[2]-k
    num[1] = cnt[1] + k*2
    ans = num[0] + num[1] +num[2] - k*2
  else
    k = cnt[1]/2
    num[0] = cnt[0] + k
    num[1] = cnt[1] - k*2
    num[2] = cnt[2] + k
    ans = num[0] + num[1] +num[2] - k*2
  end
  p ans
  m = num[2] - 1 < 0 ? 0 : num[2] - 1
  print "#{v+minidx-2} " * num[0] +  "#{v+minidx-1} " * num[1] +  "#{v+minidx} " * m
  print "#{v+minidx}" if num[2] > 0
end
#p c
#p cnt
#p num

n = gets.to_i
x = gets.split.map(&:to_i)
v = x[0]
c = [0,0,0,0,0]
n.times do |i|
  c[x[i]-v+2] += 1
end
maxidx = minidx = 2
5.times do |i|
  if c[i] != 0
    minidx = i if i < minidx
    maxidx = i if i > maxidx
  end
end
diff = maxidx - minidx
if diff != 2
  p 0
  puts x.join(" ")
else
  num = []
  cnt = [c[minidx],c[minidx+1],c[minidx+2]]
  a = [cnt[0],cnt[2]].min * 4 #incrase cnt[1]
  b = cnt[1]/2 * 4 #decrease cnt[1]
  if a > b
    k = [cnt[0],cnt[2]].min
    num[0] = cnt[0]-k
    num[2] = cnt[2]-k
    num[1] = cnt[1] + k*2
    ans = num[0] + num[1] +num[2] - k*2
  else
    k = cnt[1]/2
    num[0] = cnt[0] + k
    num[1] = cnt[1] - k*2
    num[2] = cnt[2] + k
    ans = num[0] + num[1] +num[2] - k*2
  end
  p ans
  res = []
  3.times do |i|
    res += [v+minidx-2+i] * num[i] if num[i] > 0
  end
  puts res.join(" ")
end
#p c
#p cnt
#p num

n = gets.chomp.to_i
arr = gets.chomp.split(" ").map {|e| e.to_i}
mi = arr.min
ma = arr.max

out = n
mac = 0
mic = 0
if ma - mi > 1
	arr.each do |e|
		if e == mi
			mic +=1
		elsif e == ma
			mac +=1
		end
	end
end
if mic > mac
	mic = mac
else
	mac = mic
end
mic = mic
out -= mic*2
i = 0
arr = arr.map do |e|
	if e == mi && mic > 0
		mic -= 1
		e + 1
	elsif e == ma && mac > 0
		mac -= 1
		e-1
	else
		e
	end
end
puts out
puts arr.join(" ")


gets.to_i.times do
  n = gets.to_i.to_s(2).chars.select { |i| i == ?1 } .size
  puts 2**n
end

gets.to_i.times do
  p 2**gets.to_i.to_s(2).chars.select { |i| i == ?1 } .size
end

t = gets.to_i
t.times do
  a = gets.to_i
  puts 2**a.to_s(2).count('1')
end


n = gets.to_i
h = Hash.new(0)
gets.split.map(&:to_i).each_with_index do |i, j|
	k = i - [j, n - 1 - j].min
	h[k] += 1 if k > 0
end
p n - h.max{|a, b| a[1] <=> b[1]}[1]

N  = gets.to_i
a = gets.split(/\s+/).map {|i| i.to_i}
d = {}
d.default = 0
i, j = 0, N - 1
while i <= j
    if a[i] - i > 0
        d[a[i] - i] += 1
    end
    if i != j && a[j] - i > 0
        d[a[j] - i] += 1
    end
    i += 1
    j -= 1
end

puts N - [1, d.values.max].max

N  = gets.to_i
a = gets.split(/\s+/).map {|i| i.to_i}
d = Array.new(100001, 0)
i, j = 0, N - 1
while i <= j
    d[a[i] - i] += 1 if a[i] - i > 0
    d[a[j] - i] += 1 if i != j && a[j] - i > 0
    i += 1
    j -= 1
end
puts N - [1, d.max].max

N = gets.to_i
a = gets.split(/\s+/).map {|i| i.to_i}
d = Hash.new(0)
i, j = 0, N - 1
while i <= j
    d[a[i] - i] += 1 if a[i] - i > 0
    d[a[j] - i] += 1 if i != j && a[j] - i > 0
    i += 1
    j -= 1
end
puts N - [1, d.values.max].max

n = gets.to_i
h = Hash.new(0)
gets.split.map(&:to_i).each_with_index do |i, j|
	h[i - [j, n - 1 - j].min] += 1
end
p n - h.max{|a, b| a[1] <=> b[1]}[1]


info = gets.chomp.to_i
info.times do
  input  = gets.chomp.to_i
  if input.positive?
    print -(input-1), ' ', input
    puts
  elsif input.negative?
    print input, ' ', -(input)-1
    puts
  else
    print 0, ' ', 0
    puts
  end
end

def i(x)
	x.to_i
end

t = gets
for k in 1..i(t)
	n = gets
	puts "#{-i(n) + 1} #{n}"
end

def i(x)
	x.to_i
end

t = gets
for k in 1..i(t)
	n = gets
	if i(n) == 1
		puts "0 1"
		next
	end
	puts "-#{i(n) - 1} #{n}"
end

def i(x)
	x.to_i
end

t = gets
for k in 1..i(t)
	n = gets
	if n == 1
		puts "0 1"
		next
	end
	puts "-#{i(n) - 1} #{n}"
end


#I could just assign 0 to some graph[i][j] instead of using path
def DFS(v,p)
	#puts v+1
	p.push(v)
	$colour[v] = :grey
	$n.times do |i|
		if $graph[v][i] == 1
			if $colour[i] == :white
				DFS(i,p)
				#p = Array.new
			elsif $colour[i] == :grey
				unless p[p.length-2] == i
					if $cycle == :void
						$cycle = true
						if p.length % 2 == 1
							$students += 1 #p.length - 2
							#puts "ALALAH!: " + v.to_s + ", " + i.to_s
						end
					end
					break
				end
			end
		end
	end
	$colour[v] = :black
	if $cycle == :void
		$cycle = :false
	end
end

#input = File.open('input.txt','r')
$n, m = gets.split#input.
$n = $n.to_i
m = m.to_i
$graph = Array.new($n)#,Array.new(n,0))
$n.times do |i|
	$graph[i] = Array.new($n,0)
end
m.times do |i|
	a, b = gets.split#input.
	#print(a,' ',b,"\n")
	#puts graph[a.to_i-1][b.to_i-1]
	$graph[a.to_i-1][b.to_i-1] = $graph[b.to_i-1][a.to_i-1] = 1
	#puts graph[a.to_i-1][b.to_i-1]
end
# $graph.each do |i|
	# i.each do |j|
		# print j
		# print ' '
	# end
	# puts
# end
$colour = Array.new($n,:white)
$cycle = :void
$students = 0
$n.times do |i|
	if $colour[i] == :white
		path = Array.new
		DFS(i,path)
		$cycle = :void
	end
end
# if $cycle
	# puts "No"
# else
	# puts "Yes"
# end
if ($n - $students) % 2 == 1
	$students += 1
end
puts $students
#assign a value to a variable only if that variable isn't already set:
#@variable ||= "default value"

#x, y = y, x will interchange the values of x and y

#names2 = %w{  ann richard william susan pat }

#puts "Enrollments will now Stop" if participants > 2500

#Negative index values count from the end of the array

n, m = STDIN.readline.split.map {|s| s.to_i }
enemies = {}
(1..n).each {|i| enemies[i] = [] }
m.times do
	a, b = STDIN.readline.split.map {|s| s.to_i }
	enemies[a].push(b)
	enemies[b].push(a)
end
value_side = {}
sides = [[], [], []]
def add(i, target, value_side, sides, enemies)
	okay = true
	value_side[i] = target
	sides[target].push(i)
	next_target = (target == 0 ? 1 : 0)
	enemies[i].each do |j|
		if not value_side[j]
			add(j, next_target, value_side, sides, enemies)
		elsif value_side[j] == target
			sides[target].delete i
			value_side[i] = 2
			sides[2].push(i)
		end
	end
end
(1..n).each do |i|
	next if value_side[i]
	target = (sides[0].length <= sides[1].length ? 0 : 1)
	add(i, target, value_side, sides, enemies)
end
puts sides[2].length + (sides[0].length + sides[1].length)%2

#I could just assign 0 to some graph[i][j] instead of using path
def DFS(v,p)
	#puts v+1
	p.push(v)
	$colour[v] = :grey
	$n.times do |i|
		if $graph[v][i] == 1
			if $colour[i] == :white
				DFS(i,p)
				#p = Array.new
			elsif $colour[i] == :grey
				unless p[p.length-2] == i
					if $cycle == :void
						$cycle = true
						if p.length % 2 == 1
							$students += 1 #p.length - 2
							#puts "ALALAH!: " + v.to_s + ", " + i.to_s
						end
					end
					break
				end
			end
		end
	end
	$colour[v] = :black
	if $cycle == :void
		$cycle = :false
	end
end

#input = File.open('input.txt','r')
$n, m = gets.split#input.
$n = $n.to_i
m = m.to_i
$graph = Array.new($n)#,Array.new(n,0))
$n.times do |i|
	$graph[i] = Array.new($n,0)
end
m.times do |i|
	a, b = gets.split#input.
	#print(a,' ',b,"\n")
	#puts graph[a.to_i-1][b.to_i-1]
	$graph[a.to_i-1][b.to_i-1] = $graph[b.to_i-1][a.to_i-1] = 1
	#puts graph[a.to_i-1][b.to_i-1]
end
# $graph.each do |i|
	# i.each do |j|
		# print j
		# print ' '
	# end
	# puts
# end
$colour = Array.new($n,:white)
$cycle = :void
$students = 0
$n.times do |i|
	if $colour[i] == :white
		path = Array.new
		DFS(i,path)
	end
end
# if $cycle
	# puts "No"
# else
	# puts "Yes"
# end
if ($n - $students) % 2 == 1
	$students += 1
end
puts $students
#assign a value to a variable only if that variable isn't already set:
#@variable ||= "default value"

#x, y = y, x will interchange the values of x and y

#names2 = %w{  ann richard william susan pat }

#puts "Enrollments will now Stop" if participants > 2500

#Negative index values count from the end of the array

#I could just assign 0 to some graph[i][j] instead of using path
def DFS(v,p)
	#puts v+1
	p.push(v)
	$colour[v] = :grey
	$n.times do |i|
		if $graph[v][i] == 1
			if $colour[i] == :white
				DFS(i,p)
			elsif $colour[i] == :grey
				unless p[p.length-2] == i
					$cycle = true
					if p.length % 2 == 1
						$students += 1 #p.length - 2
					end
					break
				end
			end
		end
	end
	$colour[v] = :black
end

#input = File.open('input.txt','r')
$n, m = gets.split#input.
$n = $n.to_i
m = m.to_i
$graph = Array.new($n)#,Array.new(n,0))
$n.times do |i|
	$graph[i] = Array.new($n,0)
end
m.times do |i|
	a, b = gets.split#input.
	#print(a,' ',b,"\n")
	#puts graph[a.to_i-1][b.to_i-1]
	$graph[a.to_i-1][b.to_i-1] = $graph[b.to_i-1][a.to_i-1] = 1
	#puts graph[a.to_i-1][b.to_i-1]
end
# $graph.each do |i|
	# i.each do |j|
		# print j
		# print ' '
	# end
	# puts
# end
$colour = Array.new($n,:white)
$cycle = false
$students = 0
$n.times do |i|
	if $colour[i] == :white
		path = Array.new
		DFS(i,path)
	end
end
# if $cycle
	# puts "No"
# else
	# puts "Yes"
# end
if ($n - $students) % 2 == 1
	$students += 1
end
puts $students
#assign a value to a variable only if that variable isn't already set:
#@variable ||= "default value"

#x, y = y, x will interchange the values of x and y

#names2 = %w{  ann richard william susan pat }

#puts "Enrollments will now Stop" if participants > 2500

#Negative index values count from the end of the array

#I could just assign 0 to some graph[i][j] instead of using path
def DFS(v,p)
	#puts v+1
	p.push(v)
	$colour[v] = :grey
	$n.times do |i|
		if $graph[v][i] == 1
			if $colour[i] == :white
				DFS(i,p)
			elsif $colour[i] == :grey
				unless p[p.length-2] == i
					$cycle = true
					$students += p.length - 2
					break
				end
			end
		end
	end
	$colour[v] = :black
end

#input = File.open('input.txt','r')
$n, m = gets.split#input.
$n = $n.to_i
m = m.to_i
$graph = Array.new($n)#,Array.new(n,0))
$n.times do |i|
	$graph[i] = Array.new($n,0)
end
m.times do |i|
	a, b = gets.split#input.
	#print(a,' ',b,"\n")
	#puts graph[a.to_i-1][b.to_i-1]
	$graph[a.to_i-1][b.to_i-1] = $graph[b.to_i-1][a.to_i-1] = 1
	#puts graph[a.to_i-1][b.to_i-1]
end
# $graph.each do |i|
	# i.each do |j|
		# print j
		# print ' '
	# end
	# puts
# end
$colour = Array.new($n,:white)
$cycle = false
$students = 0
$n.times do |i|
	if $colour[i] == :white
		path = Array.new
		DFS(i,path)
	end
end
# if $cycle
	# puts "No"
# else
	# puts "Yes"
# end
if ($n - $students) % 2 == 1
	$students += 1
end
puts $students
#assign a value to a variable only if that variable isn't already set:
#@variable ||= "default value"

#x, y = y, x will interchange the values of x and y

#names2 = %w{  ann richard william susan pat }

#puts "Enrollments will now Stop" if participants > 2500

#Negative index values count from the end of the array

#I could just assign 0 to some graph[i][j] instead of using path
def DFS(v,p)
	#puts v+1
	p.push(v)
	$colour[v] = :grey
	$n.times do |i|
		if $graph[v][i] == 1
			if $colour[i] == :white
				DFS(i,p)
			elsif $colour[i] == :grey
				unless p[p.length-2] == i
					$cycle = true
					$students += p.length - 2
					break
				end
			end
		end
	end
	$colour[v] = :black
end

#input = File.open('input.txt','r')
$n, m = gets.split#input.
$n = $n.to_i
m = m.to_i
$graph = Array.new($n)#,Array.new(n,0))
$n.times do |i|
	$graph[i] = Array.new($n,0)
end
m.times do |i|
	a, b = gets.split#input.
	#print(a,' ',b,"\n")
	#puts graph[a.to_i-1][b.to_i-1]
	$graph[a.to_i-1][b.to_i-1] = $graph[b.to_i-1][a.to_i-1] = 1
	#puts graph[a.to_i-1][b.to_i-1]
end
# $graph.each do |i|
	# i.each do |j|
		# print j
		# print ' '
	# end
	# puts
# end
$colour = Array.new($n,:white)
$cycle = false
$students = 0
$n.times do |i|
	if $colour[i] == :white
		path = Array.new
		DFS(i,path)
	end
end
# if $cycle
	# puts "No"
# else
	# puts "Yes"
# end
puts $students
#assign a value to a variable only if that variable isn't already set:
#@variable ||= "default value"

#x, y = y, x will interchange the values of x and y

#names2 = %w{  ann richard william susan pat }

#puts "Enrollments will now Stop" if participants > 2500

#Negative index values count from the end of the array

#I could just assign 0 to some graph[i][j] instead of using path
def DFS(v,p)
	#puts v+1
	p.push(v)
	$colour[v] = :grey
	$n.times do |i|
		if $graph[v][i] == 1
			if $colour[i] == :white
				DFS(i,p)
			elsif $colour[i] == :grey
				unless p[p.length-2] == i
					$cycle = true
					$students += p.length - 2
					break
				end
			end
		end
	end
	$colour[v] = :black
end

#input = File.open('input.txt','r')
$n, m = gets.split#input.
$n = $n.to_i
m = m.to_i
$graph = Array.new($n)#,Array.new(n,0))
$n.times do |i|
	$graph[i] = Array.new($n,0)
end
m.times do |i|
	a, b = gets.split#input.
	#print(a,' ',b,"\n")
	#puts graph[a.to_i-1][b.to_i-1]
	$graph[a.to_i-1][b.to_i-1] = $graph[b.to_i-1][a.to_i-1] = 1
	#puts graph[a.to_i-1][b.to_i-1]
end
# $graph.each do |i|
	# i.each do |j|
		# print j
		# print ' '
	# end
	# puts
# end
$colour = Array.new($n,:white)
$cycle = false
$students = 0
$n.times do |i|
	if $colour[i] == :white
		path = Array.new
		DFS(i,path)
	end
end
# if $cycle
	# puts "No"
# else
	# puts "Yes"
# end
if $students % 2 == 1
	$students -= 1
end
puts $students
#assign a value to a variable only if that variable isn't already set:
#@variable ||= "default value"

#x, y = y, x will interchange the values of x and y

#names2 = %w{  ann richard william susan pat }

#puts "Enrollments will now Stop" if participants > 2500

#Negative index values count from the end of the array


N = gets.to_i
A = gets.chomp
B = gets.chomp

n00, n01, n10, n11 = 0, 0, 0, 0

N.times do |i|
    if A[i] == '0'
        if B[i] == '0'
            n00 += 1
        else
            n01 += 1
        end
    else
        if B[i] == '0'
            n10 += 1
        else
            n11 += 1
        end
    end
end

puts (n00+n01) * (n10+n11) - n01*n11

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

azerocount = 0
bzerocount = 0

b.each_char.with_index do |c,i|
  if c=="0"
    if a[i] == "0"
      azerocount+=1
    end
    bzerocount += 1
  end
end

aone = a.count('1')

#p azerocount
#p bzerocount
#p aone
puts azerocount*aone+(bzerocount-azerocount)*(n-aone-azerocount)


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

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

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q = gi
ans = []
base = ["0","1","2"]
q.times do |i|
  n, k = gsmi
  # RGBは012に変換
  s = gs.gsub(/R/,"0").gsub(/G/,"1").gsub(/B/,"2")
  last =
    if s.length%3==1
      "0"
    elsif s.length%3==2
      "01"
    else
      ""
    end
  ex = base.join * (s.length/3) + last

  #RGBRGB...との差分(距離)をとる
  diff = []
  ex.chars.each.with_index do |str,j|
    d = str.to_i - s[j].to_i
    d = d+3 if d<0
    diff << d
  end
  #debug 'diff', diff.to_s
  # 0,1,2のうちいずれかがk回続けばいい
  a0 = diff[0..(k-1)]
  h=Hash.new(0)
  a0.each{|j|h[j]+=1}
  ar = h.to_a
  #puts ar.to_s
  max = ar.each.map{|j|j[1]}.max
  prev = h
  1.upto(n-k) do |idx|
    prev_i = idx-1
    en = idx+k-1
    h[diff[prev_i]]-=1
    h[diff[en]]+=1
    ar = h.to_a
    #puts ar.to_s
    cnt = ar.each.map{|j|j[1]}.max
    max = cnt if cnt>max
    prev = h
  end

  ans << k-max
end
puts ans

S = "RGB"
gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  s = gets
  a = []
  (0...n - k + 1).each do |i|
    (0...3).each do |j|
      cur = 0
      (0...k).each do |shift|
        cur += 1 if s[i+shift] != S[(j + shift) % 3]
      end
      a << cur
    end
  end
  puts a.min
end

S = "RGB"
gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  s = gets
  a = []
  (0...3).each do |shift|
    d = [0] * n
    cur = 0
    (0...n).each do |i|
      d[i] = (s[i] != S[(i + shift) % 3]) ? 1 : 0
      cur += d[i]
      cur -= d[i - k] if i >= k
      a << cur if i >= k - 1
    end
  end
  puts a.min
end


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

if k == 1
    p 0
    exit
end

k.times {
    a = gets.split(" ")[1..-1].map(&:to_i)
    if a[0] == 1
        i = 0
        i += 1 while i < a.size && a[i] == i + 1
        puts 2 * (n - i) - k + 1
        exit
    end
}

n,m = gets.split.map{ |x| x.to_i }

v = Array.new(n) { [0,0] }
cost,merge = 0,0
real_start = 0
m.times do |i|
  a = gets.split.map{ |x| x.to_i }
  start = 1
  while (a[start] == start)
    start += 1
  end
  if start == 1
    cost += a.size - 1 - start
  else
    cost += a.size - start
    real_start = start
  end
end

merge = n-real_start+1;
#puts "split = #{cost}"
#puts "merge = #{merge}"
puts "#{cost + merge}"

n, k = gets.split.map(&:to_i)
a = k.times.map{gets.split.map(&:to_i)}
a = a.sort_by{|x| x[1]}
accumulate, c = 1, 0
a[0][1..-1].each do |x|
  if x == accumulate
    c += 1
    accumulate += 1
  else
    break
  end
end
ans = (a[0].size - 1 - c) * 2
a[1..-1].each{|x| ans += 2 * x.size - 3}
puts ans

n, k = gets.split.map(&:to_i)
a = k.times.map{gets.split.map(&:to_i)}.sort_by{|x| x[1]}
i = 1
i += 1 while a[0][i + 1] == i + 1
ans = (a[0][1..-1].size - i) * 2
a[1..-1].each{|x| ans += 2 * x[1..-1].size - 1}
puts ans

n, k = gets.split.map(&:to_i)
a = k.times.map{gets.split.map(&:to_i)}.sort_by{|x| x[1]}
accumulate, c = 1, 0
a[0][1..-1].each do |x|
  break if x != accumulate
  c += 1
  accumulate += 1
end
ans = (a[0][1..-1].size - c) * 2
a[1..-1].each{|x| ans += 2 * x[1..-1].size - 1}
puts ans

n, k = gets.split.map(&:to_i)
a = k.times.map{gets.split.map(&:to_i).drop(1)}.find{|x| x[0] == 1}
puts n + n - k + 1 - 2 * a.map.with_index(1).take_while{|x, i| x == i}.size

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

if m == 1
  puts 0
  exit
end

m.times do
  a = gets.split.map(&:to_i)
  a = a[1..-1]

  if a[0] == 1
    i = 0
    i += 1 while i < a.size && a[i] == i + 1

    puts((n - i) + (n - i - (m - 1)))
    exit
  end
end

n, k = gets.split.map(&:to_i)
a = k.times.map{gets.split.map(&:to_i)}
accumulate, c = 1, 0
a[0][1..-1].each do |x|
  if x == accumulate
    c += 1
    accumulate += 1
  else
    break
  end
end
ans = (a[0].size - 1 - c) * 2
a[1..-1].each{|x| ans += 2 * x.size - 3}
puts ans