words = (0...3).map { gets.strip.tr('-;_', '').downcase }
gets
$<.map do |s|
  s = s.strip.tr('-;_', '').downcase
  puts ["WA", "ACC"][words.permutation.map { |ws| ws.join } .include?(s) ? 1 : 0]
end

def gao(s)
	_ = s.downcase.gsub(/[^a-z]/, '')
end

a = [gets, gets, gets].permutation.map{|_| gao(_ * '')}
gets.to_i.times do
	puts a.member?(gao(gets)) ? :ACC : :WA
end

arr = []
3.times do
	arr << gets.chomp.tr("-;_","").downcase
end
list = arr.permutation(3).map{|a| a.inject("",&:concat)}
# puts "#{list}"
n = gets.to_i
n.times do
	s = gets.chomp.tr("-;_","").downcase
	puts list.include?(s) ? "ACC" : "WA"
end

# http://codeforces.com/problemset/problem/61/B

init_str = []
3.times do
  init_str << gets.chomp.downcase.gsub(/[;_-]/, "")
end

students_ans = []
n = gets.chomp.to_i
n.times do
  students_ans << gets.chomp.downcase.gsub(/[;_-]/, "")
end

a, b, c = init_str
acc_ans = [a + b + c, a + c + b, b + a + c, b + c + a, c + a + b, c + b + a]

for i in 0..(n - 1)
  puts acc_ans.include?(students_ans[i]) ? "ACC" : "WA"
end

input=[gets.split[0],gets.split[0],gets.split[0]]
for i in 0..2
	input[i]=input[i].gsub(/[:;,_-]/,"").upcase
end
n=gets.to_i
n.times{
	data=gets.split[0]
	data=data.gsub(/[:;,_-]/,"").upcase
	ans=0
	input.permutation.to_a.each{|a|
		if data==a[0]+a[1]+a[2] then
			puts "ACC"
			ans=1
			break
		end
	}
	if ans==0 then
		puts "WA"
	end
}

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

Infinity = 1/0.0

alias p! p
def p(*objs)
    def onep(obj)
        case obj
        when Matrix
            puts"Matrix["
            obj.to_a.each{|row|p row}
            puts"]"
        else
            p! obj
        end
    end
    objs.each{|obj|
        onep obj
    }
end
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_ia;self.split.map(&:to_i);end;end
def getia;gets.to_ia;end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end

a=gets.chomp.gsub(/;|-|_/,"").upcase
b=gets.chomp.gsub(/;|-|_/,"").upcase
c=gets.chomp.gsub(/;|-|_/,"").upcase

d=[a+b+c,a+c+b,b+a+c,b+c+a,c+a+b,c+b+a]

n=geti
n.times{
    s=gets.chomp.gsub(/;|-|_/,"").upcase
    puts d.include?(s) ? "ACC" : "WA"
}

s = []
3.times { |i| s[i] = gets.downcase.delete "-;_\n" }
a = s.permutation.to_a.map { |a,b,c| a + b + c }
gets.to_i.times do
    x = gets.downcase.delete "-;_\n"
    puts (a.include?(x) ? "ACC" : "WA")
end


t = gets.to_i
t.times do
  n, k = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)
  hist = Array.new(n, 0)
  count = 0
  for i in 1..(n - 2) do
    count += 1 if a[i - 1] < a[i] && a[i] > a[i + 1]
    hist[i] = count
  end
  hist[n - 1] = hist[n - 2]
  #  p hist
  s_max = 0
  for l in 0..(n - k) do
    s = hist[l + k - 2] - hist[l] + 1
    if s > s_max
      s_max = s
      l_ans = l
    end
  end
  puts "#{s_max} #{l_ans + 1}"
end

# coding: utf-8

t = gets.to_i
t.times {
    n, k = gets.split.map(&:to_i)
    peaks = []
    l = 0
    max_peaks = 0
    a = gets.split.map(&:to_i)
    for i in 2...n
        if a[i-2] < a[i-1] && a[i-1] > a[i]
            #p "add peak #{i-1}"
            peaks << i-1
        end

        if i - (k-1) >= 0
            while !peaks.empty? && peaks.first <= i-(k-1)
                #p "===> #{i}"
                #p peaks
                peaks.shift
            end

            if max_peaks < (peaks.count)
                #p "updating #{i}"
                max_peaks = peaks.count
                l = i-(k-1) #zero-based
            end
        end
    end
    #p peaks
    # [l, l+k -1] <=> (l-1, l+k)
    puts "#{max_peaks + 1} #{l + 1}" #l+1 -> one-based
}

t = gets.chomp.to_i
t.times do
	n, k = gets.chomp.split(" ").map(&:to_i)
	arr = gets.chomp.split(" ").map(&:to_i)
	ngo = Array.new(n, 0)
	(1..arr.length-2).each do |a|
		if arr[a-1] < arr[a] && arr[a] > arr[a+1] then
			# ngo[a-1] = 1
			# ngo[a] = 2
			# ngo[a+1] = 1
			ngo[a]=1
		end
	end
	#puts "ppp"
	#p ngo
	sum = ngo.slice(1,k - 2).inject(:+)
	max = sum
	ind = 1
	(2..n - k + 2).each do |i|
		#puts "i:#{i}"
		sum = sum - ngo[i - 1] + ngo[i + k - 2 - 1]
		if max < sum then
			#p ngo.slice(i, k - 2)
			#p arr.slice(i, i + k-1)
			max = sum
			ind = i
		end
	end
	puts "#{max + 1} #{ind}"
end

def solve arr, k
  peak = []
  peak[0] = 0
  (1...arr.length-1).each do |i|
    if arr[i] > arr[i - 1] && arr[i] > arr[i + 1]
      peak[i] = peak[i - 1] + 1
    else
      peak[i] = peak[i - 1]
    end
  end
  peak[arr.length - 1] = peak[arr.length - 2]
  res = [-1, 1000000]
  (0..arr.length - k).each do |i|
    if peak[i + k - 2] - peak[i] > res[0]
      res[0] = peak[i + k - 2] - peak[i]
      res[1] = i
    end
  end
  [res[0] + 1, res[1] + 1]
end

t = STDIN.gets.chomp.to_i

t.times do |i|
  n, k = STDIN.gets.chomp.split.map {|i| i.to_i}
  arr = STDIN.gets.chomp.split.map {|i| i.to_i}
  res = solve arr, k
  puts res.join(" ")
end


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

count = [0] * (data.max + 1)
data.each { |i| count[i] += 1 }

puts ([1] + (2...count.length).map { |i|
    (i...count.length).step(i).map { |j|
        count[j]
    }.reduce(0, :+)
}).max

N = 1e5

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(N + 1, 0)
a.each{|x| c[x] += 1}
amx = a.max
ans = 1
(2..amx).each do |i|
  cnt = 0
  (i..amx).step(i){|j| cnt += c[j]}
  ans = [ans, cnt].max
end
p ans

N = 10 ** 5

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(N + 1, 0)
a.each{|x| c[x] += 1}
ans = 1
2.upto(N) do |i|
  cnt = 0
  i.step(N, i){|j| cnt += c[j]}
  ans = [ans, cnt].max
end
p ans

N = 10 ** 5

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(N + 1, 0)
a.each{|x| c[x] += 1}
ans = 1
(2..N).each do |i|
  cnt = 0
  i.step(N, i){|j| cnt += c[j]}
  ans = [ans, cnt].max
end
p ans

#!/usr/bin/ruby
#require 'prime'
h=Hash.new 0
gets
gets.split.each{|_|
	if false
		_.to_i.prime_division.each{|n,p|h[n]+=1}
	else
		n=_.to_i
		i=2
		while i*i<=n
			if n%i==0
				h[i]+=1
				n/=i while n%i==0
			end
			i+=1
		end
		h[n]+=1 if n>1
	end
}
p h.values.max||1

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

count = [0] * (data.max + 1)
data.each { |i| count[i] += 1 }

puts (2...count.length).map { |i|
    (i...count.length).step(i).map { |j|
        count[j]
    }.reduce(0, :+)
}.max

N = 1e5

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(N + 1, 0)
a.each{|x| c[x] += 1}
amx = a.max
ans = 0
(2..amx).each do |i|
  cnt = 0
  (i..amx).step(i){|j| cnt += c[j]}
  ans = [ans, cnt].max
end
p n == 1 ? 1 : ans


gets
l=gets.split.map(&:to_i)
m=(1..3).map{|i|(0..l.size).select{|j|i==l[j]}}
p n=m.map(&:size).min
n.times{|i|puts m.map{|a|a[i]+1}*" "}

n = gets.to_i
a = gets.split(" ").map(&:to_i)
print [a.count(1), a.count(2), a.count(3)].min
print "\n"
[a.count(1), a.count(2), a.count(3)].min.times {
  p = a.index(1)
  print p+1
  a[p] = 0
  print ' '
  p = a.index(2)
  print p+1
  a[p]= 0
  print ' '
  p = a.index(3)
  print p+1
  a[p] = 0
  print ' '
  print "\n"
}

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

n = gets.to_i
s = gets.split.map(&:to_i)
i = 1
a = []
s.each do |g|
  a << [i, g]
  i += 1
end

one   = a.select { |i| i[1] == 1 }
two   = a.select { |i| i[1] == 2 }
three = a.select { |i| i[1] == 3 }

if one.length == 0 or two.length == 0 or three.length == 0
  puts 0
  exit 0
end

ans = []
until one.length == 0 or two.length == 0 or three.length == 0
  o = one.shift
  tw = two.shift
  th = three.shift

  ans << "#{o[0]} #{tw[0]} #{th[0]}"
end

puts "#{ans.length}"
ans.each do |an|
  puts an
end

n = gets.to_i

ps = []
ms = []
pes = []

gets.split.map(&:to_i).each_with_index do |t, i|
  if t == 1
    ps << i + 1
  elsif t == 2
    ms << i + 1
  else
    pes <<i + 1
  end
end

ans = 0
list = []
while !ps.empty? && !ms.empty? && !pes.empty? do
  ans += 1
  list << [ps.shift, ms.shift, pes.shift].join(" ")
end
puts ans
puts list

gets.chomp.to_i
students = gets.chomp.split(/ /).map(&:to_i)
has_teams = true
teams = []
while has_teams
    temp = []
    (1..3).each do |skill|
        index = students.index( skill )
        if index.nil?
            has_teams = false
            break
        end
        temp << index + 1
        students[index] = -1
    end
    teams << temp if temp.length == 3
end
puts teams.length
teams.each do |team|
    puts team.join(" ")
end

n = gets.to_i
a = gets.split.map(&:to_i)
pr = []
m = []
s = []
for i in 0...a.size
	if a[i] == 1
		pr << i + 1
	elsif a[i] == 2
		m << i + 1
	else
		s << i + 1
	end
end
w = [pr.size, m.size,s.size].min
puts w
0.upto(w - 1) do |i|
	print pr[i],' ',m[i],' ',s[i],"\n"
end

n = gets.to_i
students = gets.split(" ").map(&:to_i)
gift = {1 => [], 2 => [] , 3 => []}
students.each_with_index do |s,i|
gift[s] << i + 1
end
# puts "#{gift}"
arr = []
gift.each { |k,v|
 arr << v.count
}
min_teams = arr.min
puts min_teams
(0..min_teams - 1).each { |i|
  puts "#{gift[1][i]} #{gift[2][i]} #{gift[3][i]}"
}

n = gets.to_i
a = [nil] + gets.split.map(&:to_i)
b = [nil] + Array.new(3, 0)
for i in 1..n
  b[a[i]] += 1
end
w = b[1..3].min
puts w
if w != 0
  x = 0
  y = 0
  z = 0
  ans = Array.new(w){Array.new(3)}
  for i in 1..n
    if a[i] == 1 && x < w
      ans[x][0] = i
      x += 1
      a[i] = 0
    elsif a[i] == 2 && y < w
      ans[y][1] = i
      y += 1
      a[i] = 0
    elsif a[i] == 3 && z < w
      ans[z][2] = i
      z += 1
      a[i] = 0
    end
  end
  w.times do |i|
    puts "#{ans[i][0]} #{ans[i][1]} #{ans[i][2]}"
  end
end

n = gets.to_i
a = gets.split.map{|x| x.to_i - 1}
ans = [[], [], []]
a.each.with_index(1){|x, i| ans[x] << i}
w = ans.map(&:size).min
puts w
puts ans.map{|x| x.take(w)}.transpose.map{|x| x.join(' ')}

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

result = {
  1 => [],
  2 => [],
  3 => []
}

a.each_with_index { |v, i| result[v] << i+1 }

result = result[1].zip(result[2]).zip(result[3]).map(&:flatten).select { |w| w.none?(&:nil?) }

puts result.size
result.each { |w| puts w.join(' ') }

Person = Struct.new(:index, :subj) do
  def <=> (other)
    self.subj <=> other.subj
  end
end

n = gets.to_i
arr = gets.split(" ").each_with_index.map { |x, i| Person.new(i + 1, x.to_i) }.group_by { |p| p.subj }.values

if arr.length != 3
  puts 0
else
  len = arr.min_by { |x| x.length }.length

  puts len
  for i in 0...len
    puts "#{arr[0][i].index} #{arr[1][i].index} #{arr[2][i].index} "
  end
end

a=Array.new
b=Array.new
c=Array.new
d=Array.new
i=j=k=0
n=gets.to_i
str=gets.to_s
d=str.split
for r in 0..n-1
    h=d[r].to_i
#    print h
    if h==1
      a[i]=r+1
      i+=1
    elsif h==2
      b[j]=r+1
      j+=1
    elsif h==3
      c[k]=r+1
      k+=1
    end
end
#puts i
#puts j
#puts k
if i<=j and i<=k
  h=i
elsif j<=i and j<=k
  h=j
elsif k<=i and k<=j
  h=k
end
puts h
for r in 0..h-1
  print a[r]
  print " "
  print b[r]
  print " "
  puts c[r]
end

# 490A

gets.to_i
g = [[], [], []]
gets.chomp.split.each_with_index do |x, i|
  g[x.to_i - 1] << i + 1
end
m = g.map { |a| a.size }.min
puts m
m.times { |i| puts "#{g[0][i]} #{g[1][i]} #{g[2][i]}" }

n, t = 2.times.map{gets.split.map(&:to_i)}
m = 3.times.map{|x| t.each_index.select{|i| t[i]==x+1}}
puts s = m.map(&:size).min
s.times.each{|i| puts 3.times.map{|j| (m[j][i] + 1).to_s}.join(" ")}

n = gets.to_i
t = gets.split.map(&:to_i)
m = (1..3).to_a.map{ |x|
            t.each_index.select{ |i|
                            t[i] == x
                                    }
                                        }
                                        s = m.map(&:size).min
                                        puts s
                                        s.times.each{ |i|
                                                puts 3.times.map{ |j|
                                                            "#{m[j][i] + 1}"
                                                                }.join(" ")
                                        }

n = Integer gets
arry = gets
arry = arry.split(" ")
for i in 0..n-1
  arry[i] = Integer arry[i]
end

a=[[],[],[]]

for i in 1..n
  a[arry[i-1]-1] << i
end

size = a[0].size()
if a[1].size()<size
  size = a[1].size()
end

if a[2].size()<size
  size = a[2].size()
end

puts size
for i in 0..size-1
  printf("#{a[0][i]} #{a[1][i]} #{a[2][i]}\n")
end

n = gets.to_i
children_skills = gets.split.map &:to_i

children_by_skill = Array.new(3) {Array.new}

children_skills.each_index do |i|
  children_by_skill[children_skills[i] - 1] << (i + 1)
end

max_number_teams = [children_by_skill[0].length, children_by_skill[1].length, children_by_skill[2].length].min
puts max_number_teams
max_number_teams.times do
  puts "#{children_by_skill[0].shift} #{children_by_skill[1].shift} #{children_by_skill[2].shift}"
end

#!/usr/bin/ruby
gets
h=gets.split.map(&:to_i).each_with_index.group_by{|e,i|e}
h.default=[]
a=(1..3).map{|e|h[e].map{|_|_.last+1}}
siz=a.map(&:size).min
p siz
siz.times{|i|puts a.map{|e|e[i]}*' '}

n=gets.to_i
s=gets.chomp.split.map(&:to_i)
x=0;y=0;z=0;a=[];b=[];c=[]
s.each_index do |i|
	#puts i
	if s[i]==1
		a[x]=i+1
		x+=1
	elsif s[i]==2
		b[y]=i+1
		y+=1
	else
		c[z]=i+1
		z+=1
	end
end
m=x
if y<m
	m=y
end
if z<m
	m=z
end
puts m
m.times do |i|
	print a[i].to_s+' '+b[i].to_s+' '+c[i].to_s
	puts
end

def run
  n = $stdin.gets
  t = $stdin.gets.split.map(&:to_i)

  a = []
  t.each_with_index do |x, i|
    a << [x, i]
  end

  b = a.group_by { |x| x[0] }
  if b.length != 3
    puts "0"
    return
  end

  r = []
  b[1].zip(b[2], b[3]).each do |v|
    if v[0] != nil and v[1] != nil and v[2] != nil
      r << v.map {|x| x[1] + 1 }.join(" ")
    else
      break
    end
  end

  puts r.length
  puts r.join("\n")
end

run if $0 == __FILE__

n = gets.chomp.to_i
a = [[] , [] , []]           #list of lists
arr= gets.split.map(&:to_i)
arr.each_with_index do |val,ind|
  a[val-1] << ind + 1
end
size = a.map{|i| i.size}.min
puts size
size.times { |i|

  puts "#{a[0][i]} #{a[1][i]} #{a[2][i]}"
}

n = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
num = [[],[],[]]
arr.size.times do |i|
  num[arr[i]-1].push(i+1)
end
small = [num[0].size , num[1].size , num[2].size].min
puts small

while !num[0].empty? and !num[1].empty? and !num[2].empty?
  puts "#{num[0][0]} #{num[1][0]} #{num[2][0]}"
  num[0].shift; num[1].shift; num[2].shift
end

n = gets.to_i
a = gets.split(" ").map(&:to_i)
print [a.count(1), a.count(2), a.count(3)].min
print "\n"
[a.count(1), a.count(2), a.count(3)].min.times {
  print a.index(1)+1
  a[a.index(1)] = 0
  print ' '
  print a.index(2)+1
  a[a.index(2)] = 0
  print ' '
  print a.index(3)+1
  a[a.index(3)] = 0
  print "\n"
}
print "\n"
print a

n = gets.to_i
a = gets.split(" ").map(&:to_i)
print [a.count(1), a.count(2), a.count(3)].min
print "\n"
[a.count(1), a.count(2), a.count(3)].min.times {
  p = a.index(1)+1
  print p
  p = 0
  print ' '
  p = a.index(2)+1
  print p
  p = 0
  print ' '
  p = a.index(3)+1
  print p
  p = 0
  print ' '
  print "\n"
}

n = gets.to_i
a = [nil] + gets.split.map(&:to_i)
b = [nil] + Array.new(3, 0)
for i in 1..n
  b[a[i]] += 1
end
w = b[1..3].min
puts w
if w != 0
  x = 0
  y = 0
  z = 0
  ans = Array.new(w){Array.new(3)}
  for i in 1..n
    if a[i] == 1 && x < w
      ans[x][0] = i
      x += 1
      a[i] = 0
    elsif a[i] == 2 && y < w
      ans[y][1] = i
      y += 1
      a[i] = 0
    elsif a[i] == 3 && y < w
      ans[z][2] = i
      z += 1
      a[i] = 0
    end
  end
  w.times do |i|
    puts "#{ans[i][0]} #{ans[i][1]} #{ans[i][2]}"
  end
end

Person = Struct.new(:index, :subj) do
  def <=> (other)
    self.subj <=> other.subj
  end
end

n = gets.to_i
arr = gets.split(" ").each_with_index.map { |x, i| Person.new(i, x.to_i) }

arr = arr.group_by { |x| x.subj }

one = arr[1].map { |x| x.index }
scn = arr[2].map { |x| x.index }
thr = arr[3].map { |x| x.index }

stop = [one.length, scn.length, thr.length].min

puts stop
for i in 0...stop
  puts "#{one[i]} #{scn[i]} #{thr[i]}"
end


n, d, h = gets.split(" ").map(&:to_i)
d += 1
h += 1

if n < d
    p -1
    exit
end

if d >= 2*h
    p -1
    exit
end

if d == h && h >= 3
    (h-1).times do |i|
        puts "#{i+1} #{i+2}"
    end

    (n-h).times do |i|
        b = h
        puts "2 #{b+i+1}"
    end

    exit
end

if d == h && h < 3
    if n != d
        p -1
        exit
    end

    (h-1).times do |i|
        puts "#{i+1} #{i+2}"
    end

    exit
end

(h-1).times do |i|
    puts "#{i+1} #{i+2}"
end
#puts "."
puts "1 #{h+1}"
(d-h-1).times do |i|
    b = h
    puts "#{b+i+1} #{b+i+2}"
end
#puts "."

(n-d).times do |i|
    b = d
    puts "1 #{b+i+1}"
end

buf=gets.split(" ")
n, d, h=buf[0].to_i, buf[1].to_i, buf[2].to_i
if n<=d || d>h*2 || n>2 && d==1
	puts -1
else
	for i in 2..h+1
		puts "#{i-1} #{i}"
	end
	last=1
	for i in 1..d-h
		puts "#{last} #{i+h+1}"
		last=i+h+1
	end
	for i in 1..n-d-1
		puts "#{h} #{i+d+1}"
	end
end

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

if n < d
    p -1
    exit
end

if d >= 2*h
    p -1
    exit
end

if d == h && h >= 3
    (h-1).times do |i|
        puts "#{i+1} #{i+2}"
    end

    (n-h).times do |i|
        b = h
        puts "2 #{b+i+1}"
    end

    exit
end

if d == h && h < 3
    if n != d
        p -1
        exit
    end

    (h-1).times do |i|
        puts "#{i+1} #{i+2}"
    end

    exit
end

(h-1).times do |i|
    puts "#{i+1} #{i+2}"
end
puts "1 #{h+1}"
(d-h-1).times do |i|
    b = h+1
    puts "#{b+i+1} #{b+i+2}"
end

(n-d).times do |i|
    b = d+1
    puts "1 #{b+i+1}"
end

buf=gets.split(" ")
n, d, h=buf[0].to_i, buf[1].to_i, buf[2].to_i
if n<=d || d>h*2
	puts -1
else
	for i in 2..h+1
		puts "#{i-1} #{i}"
	end
	last=1
	for i in 1..d-h
		puts "#{last} #{i+h+1}"
		last=i+h+1
	end
	for i in 1..n-d-1
		puts "#{h} #{i+d+1}"
	end
end


gets.chomp.to_i.times do |_i|
  a, b, c = gets.split.map(&:to_i)
  puts a + b + c - 1
end

T = gets.to_i

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

  puts Math.sqrt(b ** 2 + (c - a) ** 2).ceil
end


T = gets.to_i
T.times do |i|
    n = gets.to_i
    str = gets.chomp
    result = true
    (n/2).times do |i|
        diff = str[i].ord - str[n-i-1].ord
        unless [0, 2, -2].include?(diff)
            result = false
            break
        end
    end
    puts result ? 'YES' : 'NO'
end

gets.to_i.times{gets;s=gets.chomp
	puts [s.bytes,s.bytes.reverse].transpose.all?{|x,y|[-2,0,2].include?(x-y)} ? :YES : :NO
}

t=gets.to_i

chars = ('a'..'z').to_a
hsh = {}
0.upto(25).each do |pi|
  hsh[chars[pi]] = []
  hsh[chars[pi]] << chars[pi-1] if pi>0
  hsh[chars[pi]] << chars[pi+1] if pi<25
end

t.times do
  n=gets.to_i
  str=gets.strip
  hn=n/2
  possible = true
  0.upto(hn-1).each do |i|
    break unless possible
    j = n-1 - i
    # puts hsh[str[i]].inspect
    # puts hsh[str[j]].inspect
    possible = possible && (hsh[str[i]] & hsh[str[j]]).any?
  end
  puts (possible ? "YES" : "NO")
end

A = 'a'.ord
Z = 'z'.ord

def g(c)
  c > -1 ? c.chr : c
end

def f
  n = gets.chomp.to_i
  s = gets.chomp

  (0..(n/2)).each do |i|
    j = -1 - i
    a = s[i].ord
    b = s[j].ord

    amin = (a - 1) < A ? -1 : a - 1
    amax = (a + 1) > Z ? -1 : a + 1
    bmin = (b - 1) < A ? -1 : b - 1
    bmax = (b + 1) > Z ? -1 : b + 1
    # puts [i, j, s[i], s[j], a, b].inspect
    # puts [g(amin), g(amax), g(bmin), g(bmax)].inspect
    aminbmax = bmax != -1 && amin != -1 && bmax == amin
    amaxbmax = bmax != -1 && amax != -1 && bmax == amax
    aminbmin = bmin != -1 && amin != -1 && bmin == amin
    amaxbmin = bmin != -1 && amax != -1 && bmin == amax

    # puts [aminbmax, amaxbmax, aminbmin, amaxbmin].inspect

    if aminbmax || amaxbmax || aminbmin || amaxbmin
    else
      puts 'NO'
      return
    end
  end

  puts 'YES'
end

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

def palindrome(x)
  return x==x.reverse
end

t = gets.to_i

larr = []
arr = []

t.times do
  larr.push gets.to_i
  arr.push gets.to_s
end

t.times do |i|
  num = larr[i]
  string = arr[i]

#  if palindrome(string)
#    puts "YES"
#    next
#  end
  f = false

  (0..num/2-1).each do |c|
    asdf = (string[c].ord-string[num-c-1].ord).abs
    if !(asdf == 0 or asdf == 2)
#      puts (string[c].ord-string[num-c-1].ord).abs
      puts "NO"
      f = true
      break
    end
  end

  next if f

  puts "YES"
end

i = gets.to_i

def checkpal(sa,sb)
    for i in 0..sa.length-1 do
        if sa[i].ord == sb[i].ord || (sa[i].ord-sb[i].ord).abs == 2
            next
        else
            return puts 'NO'
        end
    end
    return puts 'YES'
end

i.times do
    k = gets.to_i
    s = gets.chomp
    sa = s[0..(k/2)-1]
    sb = s[k/2..k].reverse
    checkpal(sa,sb)
end

gets.to_i.times{gets;s=gets.chomp
	puts [s.bytes,s.bytes.reverse].transpose.all?{|x,y|[-2,0,2].include?(x-y)} ? :Yes : :No
}

T = gets.to_i
T.times do |i|
    n = gets.to_i
    str = gets.chomp
    result = true
    (n/2).times do |i|
        diff = (str[i].ord - str[n-i-1].ord) % 26
        unless [0, 2, 24].include?(diff)
            result = false
            break
        end
    end
    puts result ? 'YES' : 'NO'
end


n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)
cnt = 0
a.each do |x|
  cnt += (x != 1) ? 1 : 0
end
if (cnt < n)
  print cnt
  exit
end
g = a[0]
a[1..-1].each do |x|
  g = g.gcd(x)
end
if (g > 1)
  print -1
  exit
end
ans = n
(0..n - 1).each do |i|
  g = a[i]
  (i..n - 1).each do |j|
    g = g.gcd(a[j])
    if (g == 1)
      ans = [ans, j - i + 1].min
      break
    end
  end
end
print ans - 1 + n - 1

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

k = a.dup
flag = false
count = 0
while(a.size > 0)
	if a.count(1) != 0
		flag = true
		break
	end
	b = []
	for i in 0..(a.size-2)
		b << (a[i].gcd a[i+1])
	end
	a = b
	count += 1
end
if flag
	if k.count(1) > 1
		print(n - k.count(1) + count)
	else
		print(n - 1 + count)
	end
else
	print('-1')
end

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

k = a.dup
flag = false
count = 0
while(a.size > 0)
	if a.count(1) != 0
		flag = true
		break
	end
	b = []
	for i in 0..(a.size-2)
		b << (a[i].gcd a[i+1])
	end
	a = b
	count += 1
end
if flag
	print(n - a.count(1) + count)
else
	print('-1')
end


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

max = arr.max
puts arr.map { |a| max - a }.inject(0, :+)

gets;a=gets.split.map &:to_i;p a.max*a.size-(a.inject :+)

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
a = gets.split.map(&:to_i)
top = a.max
fullfill = 0
for i in a
    fullfill += top - i
end
puts fullfill

class A
  attr_reader(:arr)
  def initialize(arr)
    @arr = arr
  end

  def max
    arr.max
  end

  def diffs
    m = max
    arr.map{|a| m - a}
  end

  def out
    diffs.inject(:+)
  end
end

_ = gets
xs = gets.split(" ").map(&:to_i)
puts A.new(xs).out

citizens = gets.chomp.to_i
w = gets.chomp
welfare = w.split

count = 0
for x in (0..citizens-1)
	for i in (0..citizens-1)
		if x != i
			if welfare[x].to_i < welfare[i].to_i
				count += (welfare[i].to_i - welfare[x].to_i)
				welfare[x] = welfare[i]
			end
		end
	end
end

print count

gets
arr = gets.split().map &:to_i
sum = 0
arr.each {|i| sum+= (arr.max - i)}
puts sum

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

max = pieces.max
p pieces.reduce(0) {|acc, n| n < max ? acc + max - n : acc }

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

puts a.map { |x| a.max - x }.inject(&:+)

n = gets.chomp.to_i
container = gets.chomp.split().map { |e| e.to_i }
maxEle = container.max
answer = 0
for i in container
    answer += maxEle - i
end

puts answer

require 'mathn'

n = gets.to_i
sum = 0

aaa = Array.new(n)
aab = Array.new(n)
aaa = gets.chomp.split

max = aaa[0].to_i

for i in 0...n
  aab[i] = aaa[i].to_i
  sum += aab[i]
  max = aab[i] if max < aab[i]
end

# puts sum

sum = max*n - sum

puts sum

n = gets.to_i
a = []
a = gets.split.map &:to_i
sum = 0
(0...n).each do |i|
    sum += a[i]
end
puts a.max * n - sum

n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
t = 0
for i in 0...n
	t += a[-1] - a[i]
end
puts t

n = gets.to_i
as = gets.split.map(&:to_i)
puts as.map { |a| as.max - a }.inject(:+)

n = gets.strip.to_i
a = gets.strip.split.map(&:to_i).sort
max_val = a[n - 1]
ans = 0
n.times do |i|
    ans += max_val - a[i]
end
puts ans

gets
x=gets.split.map{|e| e.to_i}
m=x.max
ans=0
x.each{|e|
	ans+=m-e
}
puts ans

t = gets.chomp.to_i
arr = gets.chomp.split.map(&:to_i)
mx = arr.max
co = 0
t.times.each do |i|
  # p [mx,arr[i]]
  co += (mx-arr[i])
end
puts co

gets
a=gets.split.map &:to_i
res=0
a.each{|e| res+=a.max-e}
puts res

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

max = pieces.max
p pieces.reduce(0) {|acc, n| n < max ? acc + max - n : acc }


command = []
i = 0
traffic = 0
members = 0

while line = gets do
    command[i] = line.chomp
    i += 1
end

command.size.times do |i|
    if command[i].start_with? "+" then
        members += 1
    elsif command[i].start_with? "-" then
        members -= 1
    else
        k = command[i].index(":")
        traffic += (command[i][k+1..-1].size * members)
    end
end

puts traffic

#!/usr/bin/ruby
h=Hash.new
n=0
while s=gets do
  s.chomp!
  if s[0,1]=="+" then h[s[1..-1]]=true
  elsif s[0,1]=="-" then h.delete(s[1..-1])
  else
    x=s.split(":")[1]
    if x then n+=x.length*h.size end
  end
end
puts n

hash = Hash.new
bytes = 0
while line = gets
	line.chomp!
	hash.store(line, 0) if line.start_with?('+')
	if line.start_with?('-')
		line[0] = '+'
		hash.delete(line)
	end
	if line.include?(':')
		msg = line.split(':')
		if msg.size == 1
			len = 0
		else
			len = msg[1].length
		end
		bytes += len * hash.size
	end
end
puts bytes

member=0
byte=0
while line = gets
  line.strip!
  if line[0..0]=="+"
    member+=1
  elsif line[0..0]=="-"
    member-=1
  else
    pos=line.index ":"
    byte+=(line.size-pos-1)*member
  end
end
puts byte

bytes=0
names={}
while line=STDIN.gets do
	line=line.chomp
	if line[0,1]=="+" then names[line[1,line.length-1]]=1
	elsif line[0,1]=="-" then names.delete(line[1,line.length-1])
	else
		name,message=line.split(":")
		if message!=nil then bytes+=message.length*names.length end
	end
end
print(bytes,"\n")

traffic = num = 0
while input = gets
    input.strip!
    if input[0] == "+"
        num = num + 1
    elsif input[0] == "-"
        num = num - 1
    else
        traffic = traffic + (input.split(':', 2)[1].length) * num
    end
end
puts traffic

nl=Hash.new
sum=0
while line=gets
  line.chomp!
  if line[0] == "+"
    nl[line[1..(line.length-1)]]=true
  elsif line[0] == "-"
    nl.delete(line[1..(line.length-1)])
  else
    tmp=line.split(":")
    if tmp.length!=1
      sum+=tmp[1].length*nl.length
    end
  end
end
puts sum

ans=0
active=0

while a=gets do
a=a.chomp
if a[0]=="+"
active+=1
elsif a[0]=="-"
active-=1
else
l=a.length-a.index(":")-1
ans+=l*active
end
end

puts"#{ans}"

res = 0
sankasha=0
while line=gets do
  if line =~ /^\+/
    sankasha += 1
  elsif line =~ /^\-/
    sankasha -= 1
  else
    line.chomp =~ /:(.*)\z/
    res += ($1.size)*sankasha
  end
end
puts res

users = 0
bytes = 0

while line = gets do
  if line[0,1] == "+" then
    users += 1
  elsif line[0,1] == "-" then
    users -= 1
  else
    line = /[:](.*)/.match(line)
    if line then bytes += line[1].to_s.length * users end
  end
end

p bytes

count = 0
traffic = 0
while (command = gets)
	command.strip!
	case command[0]
	when "+"
	 count += 1
	when "-"
		count -= 1
	else
		message = command.split(":", 2)[1]
		traffic += message.length * count
	end
end

puts traffic

#!/usr/bin/env ruby

$users = Array.new
$bytes_sent = 0

def handle_input(chat_command)

	if chat_command.start_with? '+'
		chat_command.slice!(0)
		$users << chat_command
	elsif chat_command.start_with? '-'
		chat_command.slice!(0)
		$users.delete chat_command
	else
		message = chat_command.split(':')[1].to_s.strip
		$bytes_sent += $users.count * message.length
	end
end

ARGF.each do |line|
	handle_input(line)
end

puts $bytes_sent

num=0
sum=0
while STDIN.gets != nil ;
	s = $_
	if s[0] == ?+ ;
		num += 1
	elsif s[0] == ?- ;
		num -= 1
	else
		# print s.split(?:)
		sum += num*(s.split(?:)[1].size-1)
		# print "#{num}, #{sum}\n"
	end
end
print sum

#!/usr/bin/env ruby
count = 0
traffic = 0
while l = gets
  l.strip!
  case l[0]
  when ?+
    count += 1
  when ?-
    count -= 1
  else
    traffic += count * l.split(':', 2)[1].length
  end
end

puts traffic

traffic = count = 0

while l = gets
  l.strip!
  case l[0]
  when "+"
    count += 1
  when "-"
    count -= 1
  else
    traffic += count * l.split(':', 2)[1].length
  end
end

puts traffic

require 'set'
lines = gets(nil)
user = Set.new
ans = 0
lines.split("\n").each do |line|
    if line[0] == "+"
        user.add(line[1,line.size-1])
    elsif line[0] == "-"
        user.delete(line[1,line.size-1])
    else
        ans += user.size * line.split(":")[1].size if line.split(":")[1]
    end
end
puts ans

cnt = 0
res = 0
while s = gets do
  next if s.empty?
  s.strip!
  cnt += 1 if s[0, 1] == '+'
  cnt -= 1 if s[0, 1] == '-'
  pos = s.index(':')
  next if pos.nil?
  res += cnt * s[pos + 1, s.length].length
end
puts res

peoples, traffic = 0, 0
while line = gets do
  case line.strip
    when /\+\w+/ then peoples += 1
    when /\-\w+/ then peoples -= 1
    when /\w+:(.+)/ then traffic += peoples * $1.size
  end
end
puts traffic

require 'set'

ret = 0
persons = Set.new
while line = gets
  line.chomp!
  if line[0] == '+'
    persons.add(line[1..line.size])
  elsif line[0] == '-'
    persons.delete(line[1..line.size])
  else
    name, text = line.split(":")
    if text
      ret += text.size * persons.size
    end
  end
end

puts ret

input, names, traffic = STDIN.read.split("\n"), [], 0

input.each do |i|
	if i[0] == '+'
		names.push i[1..-1]
	elsif i[0] == '-'
		names.delete i[1..-1]
	else
		msg = i.split(':')
		traffic += names.length * msg[1].length unless msg[1].nil?
	end
end

p traffic

#!/usr/bin/ruby
h=Hash.new
n=0
while s=gets do
  s.chomp!
  if s[0,1]=="+" then h[s[1,-1]]=true
  elsif s[0,1]=="-" then h.delete(s[1,-1])
  else n+=s.split(":")[1].length*h.size end
end
puts n

member=0
byte=0
while line = gets
  line.strip!
  if line[0..0]=="+"
    member+=1
  elsif line[0..0]=="-"
    member-=1
  else
    pos=line.index ":"
    byte+=line.size-pos-1
  end
end
puts byte

member=0
byte=0
while line = gets
  if line[0..0]=="+"
    member+=1
  elsif line[0..0]=="-"
    member-=1
  else
    pos=line.index ":"
    byte+=line.size-pos-1
  end
end
puts byte

ans=0
active=0

while a=gets do

if a[0]=="+"
active+=1
elsif a[0]=="-"
active-=1
else
l=a.length-a.index(":")-1
ans+=l*active
end
end

puts"#{ans}"

res = 0
while line=gets do
  if line =~ /^\+/
  elsif line =~ /^\-/
  else
    res += line.split(':')[1].size
  end
end
puts res

res = 0
while line=gets do
  if line =~ /^\+/
  elsif line =~ /^\-/
  else
    res += line.chomp.split(':')[1].size
  end
end
puts res

users = 0
bytes = 0
loop {
  line = gets().chomp!

  if line[0,1] == "+" then
    users += 1
  elsif line[0,1] == "-" then
    users -= 1
  else
    line = /[:](.*)/.match(line)
    bytes += line[1].to_s.length * users
  end

  if users <= 1 then break end
}
p bytes

cnt = 0
res = 0
100.times do
  s = gets
  next if s.nil? || s.empty?
  s.chomp!
  cnt += 1 if s[0] == '+'
  cnt -= 1 if s[0] == '-'
  pos = s.index(':')
  i = 0
  while i <= s.length && s[i] != ':' do
    i += 1
  end
#  if i != pos then
#    while true do
#      i += 1
#    end
#  end
  res += cnt * (s.length - i - 1)
end
puts res

cnt = 0
res = 0
while s = gets do
  next if s.empty?
  cnt += 1 if s[0] == '+'
  cnt -= 1 if s[0] == '-'
  pos = s.index(':')
  next if pos.nil?
  res += cnt * s[pos + 1, s.length].chomp.length
end
puts res

cnt = 0
res = 0
100.times do
  s = gets
  next if s.nil? || s.empty?
  cnt += 1 if s[0] == '+'
  cnt -= 1 if s[0] == '-'
  pos = s.index(':')
  next if pos.nil?
  res += cnt * s[pos + 1, s.length].chomp.length
end
puts res

cnt = 0
res = 0
while s = gets do
  cnt += 1 if s[0] == '+'
  cnt -= 1 if s[0] == '-'
  pos = s.index(':')
  next if pos.nil?
  res += cnt * s[pos + 1, s.length].chomp.length
end
puts res

cnt = 0
res = 0
while true do
  s = gets
  break if s.nil?
  cnt += 1 if s[0] == '+'
  cnt -= 1 if s[0] == '-'
  pos = s.index(':')
  next if pos.nil?
  res += cnt * s[pos + 1, s.length].chomp.length
end
puts res

cnt = 0
res = 0
while s = gets do
  next if s.empty?
  s.strip!
  cnt += 1 if s[0] == '+'
  cnt -= 1 if s[0] == '-'
  pos = s.index(':')
  next if pos.nil?
  res += cnt * s[pos + 1, s.length].length
end
puts res

cnt = 0
res = 0
100.times do
  s = gets
  next if s.nil? || s.empty?
  s.chomp!
  cnt += 1 if s[0] == '+'
  cnt -= 1 if s[0] == '-'
  pos = s.index(':')
  next if pos.nil?
  i = 0
  while i <= s.length && s[i] != ':' do
    i += 1
  end
#  if i != pos then
#    while true do
#      i += 1
#    end
#  end
  res += cnt * (s.length - i - 1)
end
puts res


gets
a=gets.split.map(&:to_i).sort
[[a.shift],a.last==0?a.shift(2):[a.pop],a].each{|c|puts"#{c.size} #{c.join' '}"}

gets.chomp.to_i
arrs = gets.chomp.split(/ /).map(&:to_i)
arr_1 = []
arr_2 = []
arr_3 = []
arrs.each do |a|
    arr_1 << a if a < 0
    arr_2 << a if a > 0
    arr_3 << a if a == 0
end
arr_3 << arr_1.shift if arr_1.length.even?
if arr_2.length == 0 && arr_1.length > 2
    arr_2 << arr_1.delete_at(0)
    arr_2 << arr_1.delete_at(0)
end
puts "#{arr_1.length} #{arr_1.sort.join(" ")}"
puts "#{arr_2.length} #{arr_2.sort.join(" ")}"
puts "#{arr_3.length} #{arr_3.sort.join(" ")}"

def g1(xs)
	print xs.size
	while xs.empty? ==false
		print " ",xs.shift
	end
	print "\n"
end
def f()
n=gets.to_i
xs=gets.split.map{|e| e.to_i}
xsU=xs.select{|e|
	e>0
}
xsD=xs.select{|e|
	e<0
}
xs0=xs.select{|e|
	e==0
}
if xsU.size==0
	xsU<<xsD.shift
	xsU<<xsD.shift
end
if xsD.size%2==0 then
	xs0<<xsD.shift
end
g1(xsD)
g1(xsU)
g1(xs0)
end
f()

n=gets.to_i
a=gets.chomp.split.map(&:to_i).sort!
puts "1 #{a[0]}"
if a[2]<0 && a[1]<0
	puts "2 #{a[1]} #{a[2]}"
	print "#{n-3}"
	for i in 3..n-1
		print " #{a[i]}"
	end
else
	puts "1 #{a[n-1]}"
	print n-2
	for i in 1..n-2
		print " #{a[i]}"
	end
end
puts

n=gets.to_i
a=gets.split.map(&:to_i)
x=[];y=[];z=[]
a.each {|v|
    x<<v if v<0
    y<<v if v>0
    z<<v if v==0
}
if y.empty? then
    y<<x.pop
    y<<x.pop
end
if x.size.even? then
    z<<x.pop
end
puts "#{x.size} #{x*' '}"
puts "#{y.size} #{y*' '}"
puts "#{z.size} #{z*' '}"

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
b = a.select { |i|  i > 0}
c = a.select { |i|  i < 0}
d = a.select { |i|  i == 0 }
if c.size % 2 == 0
    d.push c.pop
end
if b.size == 0
    2.times { b.push c.pop }
end

puts "#{c.size} " + c.join(' ')
puts "#{b.size} " + b.join(' ')
puts "#{d.size} " + d.join(' ')

n = Integer(gets)
input = gets.chomp
nums = input.split
a = Array.new
for i in 0...n
    a[i] = Integer(nums[i])
end
num1 = -1
for i in 0...n
    if a[i] < 0
        num1 = i
        break
    end
end
positive = Array.new
for i in 0...n
    if a[i] > 0
        positive.push(a[i])
    end
end
class2 = Array.new
if positive.length == 0
    for i in 0...n
        if a[i] < 0 && i != num1
            class2.push(a[i])
        end
        if class2.length == 2
            break
        end
    end
end

if positive.length != 0
    b = a.reject{|x| x == a[num1] || x == positive[0]}
    print 1," ", a[num1]
    puts
    print 1, " ", positive[0]
    puts
    print (n-2)," ",b.join(" ")
else
    b = a.reject{|x| x == a[num1] || x == class2[0] || x == class2[1]}
    print 1," ", a[num1]
    puts
    print 2," ", class2.join(" ")
    puts
    print (n-3)," ",b.join(" ")
end

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }.sort
c = [a.shift]
b = a[-1] > 0 ? [a.pop()] : a.shift(2)
[c, b, a].each do |arr| puts "#{arr.size} " + arr.join(' ') end

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
b = a.select { |i|  i > 0}
c = a.select { |i|  i < 0}
d = a.select { |i|  i == 0 }
if c.size % 2 == 0
    d.push c.pop
end
if b.size == 0
    2.times { b.push c.pop }
end
[c, b, d].each do |arr|
    puts "#{arr.size} " + arr.join(' ')
end

def pick_minus(a)
  a.delete(a.find {|x| x < 0})
end

def pick_plus(a)
  a.delete(a.find {|x| x > 0})
end

def str(n)
  if n.is_a?(Array)
    "#{n.length} #{n.join(" ")}\n"
  else
    "1 #{n}\n"
  end
end

def run
  n = $stdin.gets.to_i
  a = $stdin.gets.split.map(&:to_i)

  n1 = pick_minus(a)
  unless n2 = pick_plus(a)
    n2 = [pick_minus(a), pick_minus(a)]
  end

  puts str(n1) , str(n2) , str(a)
end

run if $0 == __FILE__

n = gets.to_i

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

zero_array = arr.select{|i| i == 0}
zero_count = zero_array.length

pos_array = arr.select{|i| i > 0}
pos_count = pos_array.length

neg_count = n - zero_count - pos_count

neg_array = arr - pos_array - zero_array

if pos_count == 0
	pos_array << neg_array.pop
	pos_array << neg_array.pop
	neg_count -= 2
	pos_count += 2
end

if neg_count.even?
	zero_array << neg_array.pop
	zero_count += 1
	neg_count -= 1
end

puts "#{neg_count} #{neg_array.join(' ')}"
puts "#{pos_count} #{pos_array.join(' ')}"
puts "#{zero_count} #{zero_array.join(' ')}"

n = gets

a= gets.split.map(&:to_i).sort
b= [[a.shift]]            # there's atleast one negative element
a.last == 0 ? b<< a.shift(2) : b<< [a.pop]  # there's no positive so combine two neg
b<< a
b.each do |val|
    puts "#{val.size()} #{val.join(' ')}"
end

gets
l = gets.split.map(&:to_i)
n = l.find { |x| x < 0 }
l.delete(n)
p = l.find { |x| x > 0 }
if !p
	t1 = l.find { |x| x < 0 }
	l.delete(t1)
	t2 = l.find { |x| x < 0 }
	l.delete(t2)

	p = "2 #{t1} #{t2}"
else
	l.delete(p)
	p = "1 #{p}"
end
puts "1 #{ n }"
puts "#{ p }"
puts "#{ l.size } #{ l.join(" ") }"

# cook your code here

def solve(arr)
    first = []
    second= []
    third= []
    arr.each do |val|
        if val ==0
            third.push(0)
        elsif val >0
            second << val
        else
            first << val
        end
    end

    if second.size == 0
        second<< first.pop
        second << first.pop
    end
    if first.size % 2 ==0
        third<< first.pop
    end
    return first,second,third

end

n = gets.to_i
a = gets.split(" ").map(&:to_i)
ans = solve(a)
print "#{ans[0].size} "
ans[0].each do |val|
    print "#{val} "
end
puts
print "#{ans[1].size} "
ans[1].each do |val|
    print "#{val} "
end
puts
print"#{ans[2].size} "
ans[2].each do |val|
    print "#{val} "
end

gets
a=gets.split.map(&:to_i).sort
b=[[a.shift]]
a.last==0?b<<a.shift(2):b<<[a.pop]
b<<a
b.each{|c|puts"#{c.size} #{c.join' '}"}

gets
a=gets.split.map(&:to_i).sort
puts"1 #{a.shift}"
a.last==0?(puts"2 #{a.shift(2).join' '}"):(puts"1 #{a.pop}")
puts"#{a.size} #{a.join' '}"

gets
a=gets.split.map(&:to_i).sort
puts"1 #{a.shift}"
if a.last==0 then puts"2 #{a.shift(2).join' '}" else puts"1 #{a.pop}" end
puts"#{a.size} #{a.join' '}"

gets.chomp # Skip the first line
seq = gets.chomp.split.map { |x| x.to_i }
neg = seq.select { |x| x < 0 }
pos = seq.select { |x| x > 0 }
zeros = [0]
if pos.empty?
  pos.concat(neg[-2..-1])
  neg = neg[0..-3]
end
first_neg, *rest_neg = *neg
(neg.size.even? ? zeros : pos).concat(rest_neg) unless rest_neg.first.nil?
puts "1 #{first_neg}"
puts "#{pos.size} #{pos.join(' ')}"
puts "#{zeros.size} #{zeros.join(' ')}"

n = gets[0..-2].to_i
arr = gets[0..-2].split(' ')
a = Array.new(arr.size) { |index| arr[index].to_i }
a.sort!
puts "1 #{a[0]}"
nPositive = a.count { |obj| obj > 0 }
if nPositive == 0#There is no positive numbers
  puts "2 #{a[1]} #{a[2]}"
  print "#{n - 3} "
  3.upto(n - 1) { |i| print "#{a[i]} " }
else #There is atleast one positive numbers
  puts "1 #{a[n-1]}"
  print "#{n - 2} "
  1.upto(n - 2) { |i| print "#{a[i]} " }
end

#!/usr/bin/env ruby

n = gets.to_i
x = gets.split.collect{|i| i.to_i}
a, b, c = [], [], []
x.each do |i|
	if i < 0 && a.size == 0
		a << i
	elsif i > 0 && b.size == 0
		b << i
	else
		c << i
	end
end

if b.size == 0
	2.times do
		c.each do |i|
			if b.size < 2 && i < 0
				b << i
				c.delete(i)
				break
			end
		end
	end
end

puts "#{a.size} #{a.join(" ")}"
puts "#{b.size} #{b.join(" ")}"
puts "#{c.size} #{c.join(" ")}"

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    list.each do |num|
      if num < 0
        m_list << num
      elsif num > 0
        p_list << num
      else
        zero_list << num
      end
    end
    m_count = list.count{|n| n < 0}
    p_count = list.count{|n| n > 0}
    z_count = list.count{|n| n == 0}

    if m_count > 2
      if(m_count.odd?)
        list1 << m_list.pop
        list2 += m_list
      else
        list1 << m_list.pop
        list2 << m_list.pop
        list2 << m_list.pop
        list3 += m_list
      end
      m_list = []
    else
      list1 << m_list.pop
      list3 += m_list
    end

    list2 += p_list
    list3 += [0]*z_count

    puts "#{list1.size} " + list1.join(' ')
    puts "#{list2.size} " + list2.join(' ')
    puts "#{list3.size} " + list3.join(' ')
  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = [1]
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    list.each do |num|
      if num < 0
        m_list << num
      elsif num > 0
        p_list << num
      else
        zero_list << num
      end
    end
    m_count = list.count{|n| n < 0}

    m_c = (m_count/2.0).ceil
    if m_c.even?
      list1 << -1
    else
      list1 << 1
    end

    if (m_count-m_c).even?
      list2 << 1
    else
      list2 << -1
    end

    m_c.times do
      list1 << m_list.pop
    end

    list2 += m_list + p_list
    list3 += zero_list

    puts list1.join(' ')
    puts list2.join(' ')
    puts list3.join(' ')
  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    0.upto(n-2) do |i|
      1.upto(n-1) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts "#{list1.size} " + list1.join(' ')
          puts "#{list2.size} " + list2.join(' ')
          puts "#{list3.size} " + list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = [1]
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    0.upto(97) do |i|
      1.upto(98) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts "#{list1.max.abs} " + list1.join(' ')
          puts "#{list2.max.abs} " + list2.join(' ')
          puts "#{[list3.max.abs,1].max} " + list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = [1]
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    0.upto(97) do |i|
      1.upto(98) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts list1.join(' ')
          puts list2.join(' ')
          puts list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    list.each do |num|
      if num < 0
        m_list << num
      elsif num > 0
        p_list << num
      else
        zero_list << num
      end
    end
    m_count = list.count{|n| n < 0}
    p_count = list.count{|n| n > 0}
    z_count = list.count{|n| n == 0}

    if p_count == 0
      list2 << m_list.pop
      list2 << m_list.pop
      list1 += m_list
    else
      list1 << m_list.pop
      list2 += m_list
    end

    list2 += p_list
    list3 += [0]*z_count + m_list

    puts "#{list1.size} " + list1.join(' ')
    puts "#{list2.size} " + list2.join(' ')
    puts "#{list3.size} " + list3.join(' ')
  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    list.each do |num|
      if num < 0
        m_list << num
      elsif num > 0
        p_list << num
      else
        zero_list << num
      end
    end
    m_count = list.count{|n| n < 0}
    p_count = list.count{|n| n > 0}
    z_count = list.count{|n| n == 0}

    if m_count > 2
      if(m_count.odd?)
        list2 << m_list.pop
        list2 << m_list.pop
        list1 += m_list
      else
        list1 << m_list.pop
        list2 += m_list
      end
      m_list = []
    else
      list1 << m_list.pop
    end

    list2 += p_list
    list3 += [0]*z_count + m_list

    puts "#{list1.size} " + list1.join(' ')
    puts "#{list2.size} " + list2.join(' ')
    puts "#{list3.size} " + list3.join(' ')
  end
end

a = A.new

gets.chomp.to_i
arrs = gets.chomp.split(/ /).map(&:to_i)
arr_1 = []
arr_2 = []
arr_3 = []
arrs.each do |a|
    arr_1 << a if a < 0
    arr_2 << a if a > 0
    arr_3 << a if a == 0
end
arr_2 << arr_1.shift if arr_1.length.even?
if arr_2.length == 0 && arr_1.length > 2
    arr_2 << arr_1.delete_at(0)
    arr_2 << arr_1.delete_at(0)
end
puts "#{arr_1.length} #{arr_1.sort.join(" ")}"
puts "#{arr_2.length} #{arr_2.sort.join(" ")}"
puts "#{arr_3.length} #{arr_3.sort.join(" ")}"

def g1(xs)
	print xs.size
	while xs.empty? ==false
		print " ",xs.shift
	end
	print "\n"
end
def f()
n=gets.to_i
xs=gets.split.map{|e| e.to_i}
xsU=xs.select{|e|
	e>0
}
xsD=xs.select{|e|
	e<0
}
xs0=xs.select{|e|
	e==0
}
if xsU.size==0
	xsU<<xsD.shift
	xsU<<xsD.shift
end
g1(xsU)
g1(xsD)
g1(xs0)
end
f()

def g1(xs)
	print xs.size
	while xs.empty? ==false
		print " ",xs.shift
	end
	print "\n"
end
def f()

n=gets.to_i
xs=gets.split.map{|e| e.to_i}
(1..n).each{|i|
	xs1=xs.take(i)
	xa=xs.drop(i)
	(1..(xa.size)).each{|j|
		xs2=xa.take(j)
		xs3=xa.drop(j)
		if xs2.empty? ==true || xs3.empty? == true
			next
		end
		n1=xs1.reduce(:*)
		n2=xs2.reduce(:*)
		n3=xs3.reduce(:*)
		if n1<0 && n2>0 && n3==0 then
			g1(xs1)
			g1(xs2)
			g1(xs3)
			return
		end
	}
}
end
f()

n = Integer(gets)
input = gets.chomp
nums = input.split
a = Array.new
for i in 0...n
    a[i] = Integer(nums[i])
end
num1 = -1
for i in 0...n
    if a[i] < 0
        num1 = i
        break
    end
end
positive = Array.new
for i in 0...n
    if a[i] > 0
        positive.push(a[i])
    end
end
class2 = Array.new
if positive.length == 0
    for i in 0...n
        if a[i] < 0 && i != num1
            class2.push(a[i])
        end
        if class2.length == 2
            break
        end
    end
end

if positive.length != 0
    b = a.reject{|x| x == a[num1] || x == positive[0]}
    print 1," ", a[num1]
    puts
    print 1, " ", positive[0]
    puts
    print (n-2)," ",b.join(" ")
else
    b = a.reject{|x| x == a[num1] || x == class2[0] || x == class2[1]}
    print 1," ", a[num1]
    puts
    print 1," ", class2.join(" ")
    puts
    print (n-3)," ",b.join(" ")
end

gets
a=gets.split.map(&:to_i)
puts "1 #{a.min}","1 #{a.max}","#{a.size-2} #{(a-[a.min,a.max]).join' '}"

gets.chomp # Skip the first line
seq = gets.chomp.split.map { |x| x.to_i }
neg = seq.select { |x| x < 0 }
pos = seq.select { |x| x > 0 }
zeros = [0]
first_neg, *rest_neg = *neg
(neg.size.even? ? zeros : pos) << rest_neg
puts "1 #{first_neg}"
puts "#{pos.size} #{pos.join(' ')}"
puts "#{zeros.size} #{zeros.join(' ')}"

gets.chomp # Skip the first line
seq = gets.chomp.split.map { |x| x.to_i }
first_neg = seq.find { |x| x < 0 }
pos = seq.select { |x| x > 0 || (x < 0 && x != first_neg) }
puts "1 #{first_neg}"
puts "#{pos.size} #{pos.join(' ')}"
puts '1 0'

gets.chomp # Skip the first line
seq = gets.chomp.split.map { |x| x.to_i }
neg = seq.select { |x| x < 0 }
pos = seq.select { |x| x > 0 }
zeros = [0]
first_neg, *rest_neg = *neg
(neg.size.even? ? zeros : pos).concat(rest_neg) unless rest_neg.first.nil?
puts "1 #{first_neg}"
puts "#{pos.size} #{pos.join(' ')}"
puts "#{zeros.size} #{zeros.join(' ')}"

n = gets[0..-2].to_i
arr = gets[0..-2].split(' ')
a = Array.new(arr.size) { |index| arr[index].to_i }
a.sort!
puts "1 #{a[0]}"
nPositive = a.count { |obj| obj > 0 }
if nPositive == 0#There is no positive numbers
  puts "2 #{a[1]} #{a[2]}"
  print "#{n - 3} "
  3.upto(n - 1) { |i| print "#{a[i] }" }
else #There is atleast one positive numbers
  puts "1 #{a[n-1]}"
  print "#{n - 2} "
  1.upto(n - 2) { |i| print "#{a[i]} " }
end

n = gets[0..-2].to_i
arr = gets[0..-2].split(' ')
a = Array.new(arr.size) { |index| arr[index].to_i }
a.sort!
puts("1 " + a[0].to_s)
print n - 2
1.upto(n - 2) { |i| print " ", a[i]}
puts
print 1, " ", a[-1]

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
for i in 1...n-1
    for j in i+1...n
        array = [a[0, i], a[i, j - 1], a[j, n - j]]
        m1 = a[0, i].inject { |val, temp| val *= temp}
        m2 = a[i, j - i].inject { |val, temp| val *= temp}
        m3 = a[j, n - j].inject { |val, temp| val *= temp}
        if m1 < 0 && m2 > 0 && m3 == 0
            puts "#{array[0].size} " + array[0].join(' ')
            puts "#{array[1].size} " + array[1].join(' ')
            puts "#{array[2].size} " + array[2].join(' ')
        end
    end
end

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }.sort
c = [a.shift]
b = a[-1] > 0 ? [a.pop()] : [a.shift(2)]
[c, b, a].each do |a| puts "#{a.size} " + a.join(' ') end

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }.sort
c = [a.shift]
b = a[-1] > 0 ? [a.pop()] : [a.shift(2)]
[c, b, a].each do |arr| puts "#{arr.size} " + arr.join(' ') end

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    list.each do |num|
      if num < 0
        m_list << num
      elsif num > 0
        p_list << num
      else
        zero_list << num
      end
    end
    m_count = list.count{|n| n < 0}
    p_count = list.count{|n| n > 0}
    z_count = list.count{|n| n == 0}

    if m_count > 2
      if(m_count.odd?)
        list1 << m_list.pop
        list2 += m_list
      else
        list1 << m_list.pop
        list2 << m_list.pop
        list2 << m_list.pop
      end
      m_list = []
    else
      list1 << m_list.pop
    end

    list2 += p_list
    list3 += [0]*z_count + m_list

    puts "#{list1.size} " + list1.join(' ')
    puts "#{list2.size} " + list2.join(' ')
    puts "#{list3.size} " + list3.join(' ')
  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    list.each do |num|
      if num < 0
        m_list << num
      elsif num > 0
        p_list << num
      else
        zero_list << num
      end
    end
    m_count = list.count{|n| n < 0}
    p_count = list.count{|n| n > 0}
    z_count = list.count{|n| n == 0}

    if m_count > 2
      if(m_count.odd?)
        list1 << m_list.pop
        list2 += m_list
      else
        list1 << m_list.pop
        list1 << m_list.pop
        list2 += m_list
      end
      m_list = []
    else
      list1 << m_list.pop
    end

    list2 += p_list
    list3 += [0]*z_count + m_list

    puts "#{list1.size} " + list1.join(' ')
    puts "#{list2.size} " + list2.join(' ')
    puts "#{list3.size} " + list3.join(' ')
  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = [1]
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    0.upto(97) do |i|
      1.upto(98) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts "1 " + list1.join(' ')
          puts "1 " + list2.join(' ')
          puts "1 " + list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = [1]
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    0.upto(97) do |i|
      1.upto(98) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts "#{list1.size} " + list1.join(' ')
          puts "#{list2.size} " + list2.join(' ')
          puts "#{list3.size} " + list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []

    n = gets.chomp.to_i

    list = gets.chomp.split(' ').map(&:to_i)
    z_c = list.count(0)
    list.delete(0)
    list.sort!

    list += [0]*z_c

    0.upto(n-3) do |i|
      1.upto(n-2) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts "#{list1.size} " + list1.join(' ')
          puts "#{list2.size} " + list2.join(' ')
          puts "#{list3.size} " + list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []

    n = gets.chomp.to_i

    list = gets.chomp.split(' ').map(&:to_i)
    z_c = list.count(0)
    list.delete(0)
    list.sort!

    list += [0]*z_c

    0.upto(n-2) do |i|
      1.upto(n-1) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts "#{list1.size} " + list1.join(' ')
          puts "#{list2.size} " + list2.join(' ')
          puts "#{list3.size} " + list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []
    zero_list = []
    m_list = []
    p_list = []
    n = gets.chomp.to_i

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

    list.each do |num|
      if num < 0
        m_list << num
      elsif num > 0
        p_list << num
      else
        zero_list << num
      end
    end
    m_count = list.count{|n| n < 0}
    p_count = list.count{|n| n > 0}
    z_count = list.count{|n| n == 0}

    if m_count > 2
      if(m_count.odd?)
        list2 << m_list.pop
        list2 << m_list.pop
        list1 += m_list
      else
        list1 << m_list.pop
        list2 += m_list
      end
    else
      list1 << m_list.pop
    end

    list2 += p_list
    list3 += [0]*z_count + m_list

    puts "#{list1.size} " + list1.join(' ')
    puts "#{list2.size} " + list2.join(' ')
    puts "#{list3.size} " + list3.join(' ')
  end
end

a = A.new

class A
  def initialize
    list1 = []
    list2 = []
    list3 = []

    n = gets.chomp.to_i

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

    list.sort!

    0.upto(n-2) do |i|
      1.upto(n-1) do |j|
        list1 = list[0..i]
        list2 = list[i+1..j]
        list3 = list[j+1..n-1]

        if list1.inject(:*) < 0 && list2.inject(:*) > 0 && list3.include?(0)
          puts "#{list1.size} " + list1.join(' ')
          puts "#{list2.size} " + list2.join(' ')
          puts "#{list3.size} " + list3.join(' ')
          exit
        end
      end
    end

  end
end

a = A.new


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

lhash = {}
rhash = Hash.new(0)

N.times do |i|
    rhash[as[i]] += 1
end
rsum = rhash.keys.length

ans = 0
N.times do |i|
    rhash[as[i]] -= 1
    rsum -= 1 if rhash[as[i]] == 0
    next if lhash[as[i]]
    lhash[as[i]] = true
    ans += rsum
end
puts ans

require 'set'

n = STDIN.readline.strip.to_i
numbers = STDIN.readline.strip.split(/\s+/).map(&:to_i)

# robot 2
n_rev = numbers.reverse
r2_seen = Set.new
n_seen = 0
seen_map = {}
seen_map[n] = 0
n_rev.each_with_index do |x2, i|
  if !r2_seen.include? x2
    r2_seen << x2
    n_seen += 1
  end
  seen_map[n - i - 1] = n_seen
end

# robot 1
count = 0
seen = Set.new
numbers.each_with_index do |x1, i|
  if !seen.include?(x1)
    count += seen_map[i + 1]
    seen << x1
  end
end

puts count

require 'set'

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

n = input[0]
a = input

s = Set.new
ns = 0
s_map = {}
s_map[n] = 0
a.reverse.each_with_index do |x, i|
	unless s.include?(x)
		s << x
		ns += 1
	end
	s_map[n-i-1] = ns
end

# p s_map
s = Set.new
ac = 0
a.each_with_index do |x, i|
	unless s.include?(x)
		s << x
		ac += s_map[i+1]
	end
end

print ac

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

n = input[0]
a = input

store = {}
next_round = 0
ac = 0
a << -1 if a[-1] == a[-2]
a.each_with_index do |x, i|

	ac += next_round
	# puts "#{x}, #{next_round}, #{store[x]}"
	if store[x]
		next_round = -1 * store[x]
		store[x] = i
	else
		next_round = n - i - 1
		store[x] = i
	end
	# p ac
end

print ac

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

n = input[0]
a = input

store = {}
next_round = 0
ac = 0
a.each_with_index do |x, i|

	ac += next_round
	# puts "#{x}, #{next_round}, #{store[x]}"
	if store[x]
		next_round = -1 * store[x]
		store[x] = i
	else
		next_round = n - i - 1
		store[x] = i
	end
	# p ac
end

print ac


def time_spent_on_tree(pre_height, height, nxt_height)
  (height - pre_height) + (height - nxt_height) + 1#cost of eating nuts
end

n = gets.to_i

tree_heights = [0]
n.times do
  tree_heights << gets.to_i
end

total_time = 0

(1 .. tree_heights.length - 2).each do |i|
  height = tree_heights[i]
  pre_height = [tree_heights[i - 1], height].min
  nxt_height = [height, tree_heights[i + 1]].min

  total_time += time_spent_on_tree pre_height, height, nxt_height
  total_time += 1 #cost of jumping between trees
end
last_tree_height = tree_heights[-1]
total_time += time_spent_on_tree tree_heights[-2 .. -1].min, last_tree_height, last_tree_height

puts total_time

#!/usr/bin/env ruby

n, x, tot = gets.to_i, [], 0
n.times { x << gets.to_i }
x.each_index { |i| tot = tot + (x[i] - x[i+1]).abs if i + 1 < n }
puts tot + n + n - 1 + x[0]

BEGIN{
	def gint()
		return gets.split.map(&:to_i)
	end
	def main()
		n = gint[0]
		a = []
		n.times do |i|
			a << gint[0]
		end
		ans = n + a[0]
		a.each_with_index do |item, i|
			break if i == a.length - 1
			if item > a[i + 1] then
				ans += item - a[i + 1] + 1
			elsif
				ans += 1 + a[i + 1] - item
			end
		end
		puts ans
	end
	Test_case = 1
	Test_case.times do
		main()
	end
}

n = gets.to_i
r = 0
h = 0

first = true

n.times do |i|
	if not first
		r += 1
	end
	first = false
	t = gets.to_i
	r += (t-h).abs + 1
	h = t
end

puts r

n = gets.to_i
prev = gets.to_i
res = prev
for i in 0...n-1
	curr = gets.to_i
	res += (prev-curr).abs
	res += 2
	prev = curr
end
puts (res+1).to_s

t,ans=0,-1
gets.to_i.times do
	a=gets.to_i
	ans+=(a - t).abs+2
	t=a
end
puts ans

EAT = 1; JUMP = 1

def run
  n = ARGF.readline.to_i
  trees = ARGF.readlines.map(&:to_i)

  sec = 0
  height = 0
  cur_tree = 0

  trees.length.times do |i|
    if i > 0
      sec += JUMP

      diff = trees[i] - trees[i - 1]
      if diff < 0
        sec += -diff
        height = trees[i]
      end
    end

    sec += trees[i] - height + EAT
    height = trees[i]
  end

  puts sec
end

run if $0 == __FILE__

n,ac = gets.to_i,0
tot = 2*n-1
for i in (0...n)
    t = gets.to_i
    tot+=(t-ac).abs
    ac = t
end
puts tot

n=gets.to_i-1
l=r=gets.to_i
n.times{
    t=gets.to_i
    r+=(l-t).abs+2
    l=t
}
puts r+1

n=gets.to_i
h = []
n.times do |i|
	h[i] = gets.to_i
end

t = h[0] + n

(n-1).times do |i|
	if h[i+1] > h[i]
		t += h[i+1] - h[i] + 1
	else
		t += h[i] - h[i+1] + 1
	end
end

puts t

n = gets.chomp.to_i
h1,s = 0,n
for i in 0...n
    h2 = gets.chomp.to_i
    s+=(h2-h1).abs
    h1=h2
end
s+=n-1
puts s

n = gets.to_i
arr = []
n.times do
	arr << gets.to_i
end
time = arr[0]
prev_height = arr[0]
time += 1 # To eat
(1..n-1).each do |i|
	if arr[i] < prev_height
		#puts "He gets down to #{arr[i]}"#
		time += prev_height - arr[i]
		#puts "He jumps"
		time += 1
	else

		#puts "He jumps"
		time += 1 # Time to jump
		#puts "He Climbs to top in #{arr[i] - prev_height}"
		time += arr[i] - prev_height
	end
	#puts "He eats"
	time += 1

	#puts "Current time #{time}"
	prev_height = arr[i]
end

puts time

n = gets.chomp.to_i
a = 0
p = 0
(1..n).each { |k|
	b = gets.chomp.to_i
	p += (b-a).abs
	a = b
}

puts (p+2*n-1)

n=gets.to_i

sum=gets.to_i
last=sum

(n-1).times{
  t=gets.to_i
  sum+=2+(last-t).abs
  last=t
}

puts sum+1

n = gets.to_i
arr = []
n.times do
	arr << gets.to_i
end
time = arr[0]
prev_height = arr[0]
time += 1 # To eat
(1..n-1).each do |i|
	if arr[i] < prev_height
		time += prev_height
		time += arr[i]
	else
		time += arr[i] - prev_height
		time += 1 # Time to jump
	end
	time += 1
	prev_height = arr[i]
end

puts time


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

matches = (n * (n - 1)/2).to_i
matrix = []
n.times { |i| matrix[i] = [] }

t = {}
n.times do |i|
  team = gets.strip
  matrix[i] = [0, 0, 0, team]
  t[team] = i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matches.times do
  result = gets
  ts, res = result.split

  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':').map { |v| v.to_i }
  score1, score2 = get_score.call(goal1, goal2)

  # by score
  matrix[t[team1]][0] += score1
  matrix[t[team2]][0] += score2

  # by diff
  matrix[t[team1]][1] += goal1 - goal2
  matrix[t[team2]][1] += goal2 - goal1

  # by goals
  matrix[t[team1]][2] += goal1
  matrix[t[team2]][2] += goal2
end

matrix = matrix.sort_by { |team| [ team[0], team[1], team[2] ] }

puts (n/2 .. n-1).inject([]) { |res, i| res << matrix[i][-1] }.sort

class Team
	def initialize(nm)
		@name = nm
		@point = 0
		@scored = 0
		@missed = 0
	end
	attr_accessor :name, :point, :scored, :missed
end

n = gets.chomp.to_i
h = Hash.new(n)
for i in 0..n-1
	s = gets.chomp
	h[s] = Team.new(s)
end

for i in 0..n*(n-1)/2-1
	a = gets.chomp.split(/ /)
	teams = a[0].split(/\-/)
	score = a[1].split(/\:/).map!{|x| x.to_i}
	h[teams[0]].scored += score[0]
	h[teams[0]].missed += score[1]
	h[teams[1]].scored += score[1]
	h[teams[1]].missed += score[0]
	if score[0] == score[1]
		h[teams[0]].point += 1
		h[teams[1]].point += 1
	elsif  score[0] > score[1]
		h[teams[0]].point += 3
	else
		h[teams[1]].point += 3
	end
end

qu = h.values.sort!{|a,b|
	if a.point == b.point
		if (a.scored - a.missed) == (b.scored-b.missed)
			b.scored <=> a.scored
		else
			(b.scored - b.missed) <=> (a.scored-a.missed)
		end
	else
		b.point <=> a.point
	end
}

qu = qu.slice(0..n/2-1)
qu.sort!{|a,b| a.name <=> b.name}

for i in 0..n/2-1
	puts qu[i].name
end

class SoccerData
  attr_accessor :win
  attr_accessor :lose
  attr_accessor :point
  attr_accessor :name
  def initialize(name)
    @name=name
    @win=0
    @lose=0
    @point=0
  end
  include Comparable
  def <=>(other)
    if @point!=other.point
      return other.point-@point
    elsif (@win-@lose)!=(other.win-other.lose)
      return (other.win-other.lose)-(@win-@lose)
    elsif (@win!=other.win)
      return other.win-@win
    else
      return @name<=>other.name
    end
  end
end
n=gets.chomp!.to_i
data=Array.new(n)
t=SoccerData.new("test")
memo=Hash.new
n.times do |i|
  name=gets.chomp!
  data[i]=SoccerData.new(name)
  memo[name]=data[i]
end
(n*(n-1)/2).times do
  tmp1=gets.chomp!.split(" ")
  tmp2=tmp1[0].split("-")
  d1=memo[tmp2[0]]
  d2=memo[tmp2[1]]
  tmp2=tmp1[1].split(":")
  w1=tmp2[0].to_i
  w2=tmp2[1].to_i
  if w1>w2
    d1.point+=3
  elsif w1<w2
    d2.point+=3
  else
    d1.point+=1
    d2.point+=1
  end
  d1.win+=w1
  d1.lose+=w2
  d2.win+=w2
  d2.lose+=w1
end
data.sort!
str=Array.new(n/2)
(n/2).times do |i|
  str[i]=data[i].name
end
str.sort!
str.each do |s|
  puts(s)
end

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

=begin
nA. World Football Cup
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations:

    * the final tournament features n teams (n is always even)
    * the first n / 2 teams (according to the standings) come through to the knockout stage
    * the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals
    * it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity.

You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.
Input

The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following n·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 — names of the teams; num1, num2 (0 ≤ num1, num2 ≤ 100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.
Output

Output n / 2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
=end

N = gets.to_i
teams = Hash.new
N.times do
  teams[gets.chomp] = [0, 0, 0]
end

(N * (N - 1) / 2).times do
  tmp = gets.chomp.split
  team1, team2 = tmp[0].split('-')
  score1, score2 = tmp[1].split(':').map{|e| e.to_i}
  if score1 > score2
    teams[team1][0] += 3
  elsif score1 == score2
    teams[team1][0] += 1
    teams[team2][0] += 1
  else
    teams[team2][0] += 3
  end

  teams[team1][1] += score1 - score2
  teams[team2][1] += score2 - score1

  teams[team1][2] += score1
  teams[team2][2] += score2
end

#p teams

teams = teams.to_a
teams.sort!{|l_e, r_e|
  l = l_e[1]
  r = r_e[1]
  if l[0] > r[0]
    -1
  elsif l[0] < r[0]
    1
  elsif l[1] > r[1]
    -1
  elsif l[1] < r[1]
    1
  elsif l[2] > r[2]
    -1
  elsif l[2] < r[2]
    1
  else
    0
  end
}

#p teams

passed = teams[0, (N / 2)].sort{|l, r|
  l[0] <=> r[0]
}

passed.each {|e|
  puts e[0]
}

n = gets.to_i
teams = Hash.new { |hash, key| hash[key] = [0, 0, 0]}
n.times{ gets }
((n * (n - 1) / 2)).times do
  line = gets.chomp
  line =~ /(\w+)\-(\w+) (\d+):(\d+)/
  t_an, t_bn, t_aw, t_bw = $1, $2, $3.to_i, $4.to_i
  if t_aw > t_bw
    teams[t_an][0] += 3
  elsif t_aw < t_bw
    teams[t_bn][0] += 3
  else
    teams[t_an][0] += 1
    teams[t_bn][0] += 1
  end
  teams[t_an][1] += t_aw - t_bw; teams[t_an][2] += t_aw
  teams[t_bn][1] += t_bw - t_aw; teams[t_bn][2] += t_bw
end

puts teams.sort { |a, b| b[1] <=> a[1] }.map{ |u| u[0]}[0, n / 2].sort

class Team
  attr_accessor :name, :point, :net_score, :score

  def initialize(name)
    self.name = name
    self.point = self.net_score = self.score = 0
  end

  def add(x, y)
    self.point += [0, 1, 3][(x <=> y) + 1]
    self.net_score += x - y
    self.score += x
  end

  def <=>(o)
    [:point, :net_score, :score].each do |t|
      ret = self.send(t) <=> o.send(t)
      return ret unless ret == 0
    end
  end
end

n = gets.to_i
teams = Hash.new
n.times do
  name = gets.strip
  teams[name] = Team.new(name)
end
(n * (n - 1) / 2).times do
  a, b, x, y = gets.strip.split(/[\:\- ]/)
  x = x.to_i
  y = y.to_i
  teams[a].add(x, y)
  teams[b].add(y, x)
end
winner = teams.values.sort[-n / 2..-1].map(&:name).sort
puts winner

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][2] += g1
	teams[b][2] += g2

	teams[a][1] -= g2 - g1
	teams[b][1] -= g1 - g2

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

t = teams.sort { |a, b| b[1] <=> a[1] }.first(n / 2).map { |data| data[0] }.sort
t.each { |e| puts e }

class Team
	def initialize(nm)
		@name = nm
		@point = 0
		@scored = 0
		@missed = 0
	end
	attr_accessor :name, :point, :scored, :missed
end

n = gets.chomp.to_i
h = Hash.new(n)
for i in 0..n-1
	s = gets.chomp
	h[s] = Team.new(s)
end

for i in 0..n*(n-1)/2-1
	a = gets.chomp.split(/ /)
	teams = a[0].split(/\-/)
	score = a[1].split(/\:/).map!{|x| x.to_i}
	h[teams[0]].scored += score[0]
	h[teams[0]].missed += score[1]
	h[teams[1]].scored += score[1]
	h[teams[1]].missed += score[0]
	if score[0] == score[1]
		h[teams[0]].point += 1
		h[teams[1]].point += 1
	elsif  score[0] > score[1]
		h[teams[0]].point += 3
	else
		h[teams[1]].point += 3
	end
end

qu = h.values.sort!{|a,b|
	if a.point == b.point
		if (a.scored - a.missed) == (b.scored-b.missed)
			b.scored <=> a.scored
		else
			(a.scored - a.missed) <=> (b.scored-b.missed)
		end
	else
		b.point <=> a.point
	end
}

qu = qu.slice(0..n/2-1)
qu.sort!{|a,b| a.name <=> b.name}

for i in 0..n/2-1
	print " " if i > 0
	print qu[i].name
end
puts

class Team
	def initialize(nm)
		@name = nm
		@point = 0
		@scored = 0
		@missed = 0
	end
	attr_accessor :name, :point, :scored, :missed
end

n = gets.chomp.to_i
h = Hash.new(n)
for i in 0..n-1
	s = gets.chomp
	h[s] = Team.new(s)
end

for i in 0..n*(n-1)/2-1
	a = gets.chomp.split(/ /)
	teams = a[0].split(/\-/)
	score = a[1].split(/\:/).map!{|x| x.to_i}
	h[teams[0]].scored += score[0]
	h[teams[0]].missed += score[1]
	h[teams[1]].scored += score[1]
	h[teams[1]].missed += score[0]
	if score[0] == score[1]
		h[teams[0]].point += 1
		h[teams[1]].point += 1
	elsif  score[0] > score[1]
		h[teams[0]].point += 3
	else
		h[teams[1]].point += 3
	end
end

qu = h.values.sort!{|a,b|
	if a.point == b.point
		if (a.scored - a.missed) == (b.scored-b.missed)
			b.scored <=> a.scored
		else
			(a.scored - a.missed) <=> (b.scored-b.missed)
		end
	else
		b.point <=> a.point
	end
}

qu = qu.slice(0..n/2-1)
qu.sort!{|a,b| a.name <=> b.name}

for i in 0..n/2-1
	puts qu[i].name
end

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

t = {}
y = {}
number.times do |i|
  team = gets.strip
  t[team] = i
  y[i] = team
end

matches = (number * (number - 1)/2).to_i
matrix = []
number.times { |i| matrix[i] = Array.new(0) }

matches.times do
  result = gets
  ts, res = result.split
  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':')

  matrix[t[team1]][t[team2]] = goal1.to_i
  matrix[t[team2]][t[team1]] = goal2.to_i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matrix_r = matrix
matrix.each_with_index do |team, i|
  sum = team.inject(0) { |r, v| r+v.to_i }

  diff = sum
  number.times do |j|
    diff -= matrix[j][i] || 0
  end

  matrix_r[i] << sum << diff << y[i]
end

matrix_sorted = matrix_r.sort_by do |team|
  [ team[-3], team[-2] ]
end

# output results
(number / 2).times do |i|
  puts matrix_sorted[number/2 + i][-1]
end

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

matches = (number * (number - 1)/2).to_i
matrix = []
number.times { |i| matrix[i] = [] }

t = {}
number.times do |i|
  team = gets.strip
  matrix[i] = [0, 0, 0, team]
  t[team] = i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matches.times do
  result = gets
  ts, res = result.split

  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':').map { |v| v.to_i }
  score1, score2 = get_score.call(goal1, goal2)

  matrix[t[team1]][0] += score1
  matrix[t[team2]][0] += score2

  matrix[t[team1]][1] += goal1 - goal2
  matrix[t[team2]][1] += goal2 - goal1

  matrix[t[team1]][2] += goal1
  matrix[t[team2]][2] += goal2
end

matrix = matrix.sort_by do |team|
  [ team[0], team[1], team[2] ]
end

(number / 2).times do |i|
  puts matrix[number/2 + i][-1]
end

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

t = {}
y = {}
number.times do |i|
  team = gets.strip
  t[team] = i
  y[i] = team
end

matches = (number * (number - 1)/2).to_i
matrix = []
number.times { |i| matrix[i] = Array.new(0) }

matches.times do
  result = gets
  ts, res = result.split
  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':')

  matrix[t[team1]][t[team2]] = goal1.to_i
  matrix[t[team2]][t[team1]] = goal2.to_i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matrix_r = matrix
matrix.each_with_index do |team, i|
  sum = team.inject(0) { |r, v| r+v.to_i }

  diff = sum
  number.times do |j|
    diff -= matrix[j][i] || 0
  end

  matrix_r[i] << sum << diff << y[i]
end

matrix_sorted = matrix_r.sort_by do |team|
  [ team[-3], team[-2] ]
end.reverse

# output results
(number / 2).times do |i|
  puts matrix_sorted[i][-1]
end

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

matches = (number * (number - 1)/2).to_i
matrix = []
number.times { |i| matrix[i] = [] }

t = {}
number.times do |i|
  team = gets.strip
  matrix[i] = [0, 0, 0, team]
  t[team] = i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matches.times do
  result = gets
  ts, res = result.split

  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':').map { |v| v.to_i }
  score1, score2 = get_score.call(goal1, goal2)

  matrix[t[team1]][0] += score1
  matrix[t[team2]][0] += score2

  matrix[t[team1]][1] += goal1 - goal2

  matrix[t[team1]][2] += goal1
end

matrix = matrix.sort_by do |team|
  [ team[0], team[1], team[2] ]
end

(number / 2).times do |i|
  puts matrix[number/2 + i][-1]
end

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

matches = (number * (number - 1)/2).to_i
matrix = []
number.times { |i| matrix[i] = [] }

t = {}
number.times do |i|
  team = gets.strip
  matrix[i] = [0, 0, 0, team]
  t[team] = i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matches.times do
  result = gets
  ts, res = result.split

  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':').map { |v| v.to_i }
  score1, score2 = get_score.call(goal1, goal2)

  matrix[t[team1]][0] += score1
  matrix[t[team2]][0] += score2

  matrix[t[team1]][1] += goal1 - goal2
  matrix[t[team2]][1] += goal2 - goal1

  matrix[t[team1]][2] += goal1
  matrix[t[team2]][2] += goal2
end

matrix = matrix.sort_by do |team|
  [ team[0], team[1], team[2] ]
end

p matrix

(number / 2).times do |i|
  puts matrix[number/2 + i][-1]
end

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

matches = (number * (number - 1)/2).to_i
matrix = []
number.times { |i| matrix[i] = [] }

t = {}
number.times do |i|
  team = gets.strip
  matrix[i] = [0, 0, team]
  t[team] = i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matches.times do
  result = gets
  ts, res = result.split

  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':').map { |v| v.to_i }
  score1, score2 = get_score.call(goal1, goal2)

  matrix[t[team1]][0] += score1
  matrix[t[team2]][0] += score2

  matrix[t[team1]][1] += goal1 - goal2
end

matrix_sorted = matrix.sort_by do |team|
  [ team[-3], team[-2] ]
end

# output results
qual = number==1 ? 1 : number / 2
qual.times do |i|
  puts matrix_sorted[number/2 + i][-1]
end

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

matches = (n * (n - 1)/2).to_i
matrix = []
n.times { |i| matrix[i] = [] }

t = {}
n.times do |i|
  team = gets.strip
  matrix[i] = [0, 0, 0, team]
  t[team] = i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matches.times do
  result = gets
  ts, res = result.split

  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':').map { |v| v.to_i }
  score1, score2 = get_score.call(goal1, goal2)

  # by score
  matrix[t[team1]][0] += score1
  matrix[t[team2]][0] += score2

  # by diff
  matrix[t[team1]][1] += goal1 - goal2
  matrix[t[team2]][1] += goal2 - goal1

  # by goals
  matrix[t[team1]][2] += goal1
  matrix[t[team2]][2] += goal2
end

matrix = matrix.sort_by { |team| [ team[0], team[1], team[2], team[3] ] }

(n/2 .. n-1).each { |i| puts matrix[i][-1] }

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

matches = (n * (n - 1)/2).to_i
matrix = []
n.times { |i| matrix[i] = [] }

t = {}
n.times do |i|
  team = gets.strip
  matrix[i] = [0, 0, 0, team]
  t[team] = i
end

get_score = lambda do |goal1, goal2|
  if goal1 > goal2
    [3, 0]
  elsif goal1 < goal2
    [0, 3]
  else
    [1, 1]
  end
end

matches.times do
  result = gets
  ts, res = result.split

  team1, team2 = ts.split('-')
  goal1, goal2 = res.split(':').map { |v| v.to_i }
  score1, score2 = get_score.call(goal1, goal2)

  # by score
  matrix[t[team1]][0] += score1
  matrix[t[team2]][0] += score2

  # by diff
  matrix[t[team1]][1] += goal1 - goal2
  matrix[t[team2]][1] += goal2 - goal1

  # by goals
  matrix[t[team1]][2] += goal1
  matrix[t[team2]][2] += goal2
end

matrix = matrix.sort_by { |team| [ team[0], team[1], team[2] ] }

(n / 2 .. n-1).each { |i| puts matrix[i][-1] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][2] += g1
	teams[b][2] += g2

	teams[a][1] += (g1 - g2).abs
	teams[a][1] += (g2 - g1).abs

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

teams.sort { |a, b| a[1] <=> b[1] }.reverse.first(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][1] += g1
	teams[b][1] += g2

	teams[a][2] += g2
	teams[a][2] += g1

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

teams.sort { |a, b| a[1] <=> b[1] }.last(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][2] += g1
	teams[b][2] += g2

	teams[a][1] += (g1 - g2).abs
	teams[a][1] += (g2 - g1).abs

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

t = teams.sort { |a, b| b[1] <=> a[1] }.map { |data| data[0] }.sort
t.first(n / 2).each { |e| puts e }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][1] += g1
	teams[b][1] += g2

	teams[a][2] += (g1 - g2).abs
	teams[a][2] += (g2 - g1).abs

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

teams.sort { |a, b| a[1] <=> b[1] }.last(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][2] += g1
	teams[b][2] += g2

	teams[a][1] += (g1 - g2).abs
	teams[a][1] += (g2 - g1).abs

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

t = teams.sort { |a, b| b[1] <=> a[1] }.first(n / 2).map { |data| data[0] }.sort
t.each { |e| puts e }

n, teams = gets.to_i, Hash.new(0); n.times { gets }
while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i
	teams[a] += g1
	teams[b] += g2
end

p teams

teams.sort { |a, b| a[1] <=> b[1] }.last(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][2] += g1
	teams[b][2] += g2

	teams[a][1] += (g1 - g2).abs
	teams[a][1] += (g2 - g1).abs

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

teams.sort { |a, b| a[1] <=> b[1] }.last(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][1] += g1
	teams[b][1] += g2

	teams[a][2] += g1 - g2
	teams[a][2] += g2 - g1

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

teams.sort { |a, b| a[1] <=> b[1] }.last(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][2] += g1
	teams[b][2] += g2

	teams[a][1] += (g1 - g2).abs
	teams[a][1] += (g2 - g1).abs

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

teams.sort { |a, b| b[1] <=> a[1] }.first(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, Hash.new(0); n.times { gets }
while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i
	teams[a] += g1
	teams[b] += g2
end
teams.sort { |a, b| a[1] <=> b[1] }.last(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, {}
n.times { teams[gets.strip.to_sym] = [0, 0, 0] }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)
	a, b, g1, g2 = g[0].to_sym, g[1].to_sym, g[2].to_i, g[3].to_i

	teams[a][1] += g1
	teams[b][1] += g2

	teams[a][2] -= g2
	teams[a][2] -= g1

	if g1 == g2
		teams[a][0] += 1
		teams[b][0] += 1
	elsif g1 > g2
		teams[a][0] += 3
	else
		teams[b][0] += 3
	end

end

teams.sort { |a, b| a[1] <=> b[1] }.last(n / 2).each { |data| puts data[0] }

n, teams = gets.to_i, Hash.new(0); n.times { gets }

while g = gets
	g = g.strip.split(/[:\-[:space:]]/)

	 a = g[0].to_sym
	 b = g[1].to_sym
	as = g[2].to_i
	bs = g[3].to_i

	if as == bs
		score = [1, 1]
	elsif as > bs
		score = [3, -1]
	else
		score = [-1, 3]
	end

	teams[a] = score[0]
	teams[b] = score[1]

end

teams.sort_by { |team, score| score }.last(n / 2).each { |data| puts data[0] }


n,m = STDIN.readline.strip.split(/\s+/).map(&:to_i)

coordinates = []
m.times do
  coordinates << STDIN.readline.strip.split(/\s+/).map(&:to_i)
end

flower = '1'
string = ''
n.times do
  string += flower
  if flower == '1'
    flower = '0'
  else
    flower = '1'
  end
end

puts string

n, d = gets.chomp.split.map(&:to_i)
x = ""
0.upto(n-1) { |n| x += (n&1).to_s }
print x

n, m = gets.chomp.split
n = n.to_i
m = m.to_i
m.times do |m|
  gets.chomp
end

ans = ""
n.times do |i|
  ans << (i % 2).to_s
end
puts ans

N, M = gets.to_i
puts N.times.map{|i| i%2==0 ? 1 : 0}.join

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

str = ""
n.times do |i|
  str << (i%2).to_s
end
puts str

n, m = gets.split.map &:to_i
puts (0...n).map { |i| i.even? ? ?0 : ?1 } .join


def root(n)
  until n <= 9
    n = n.to_s.split('').map(&:to_i).inject(:+)
  end
  n
end

def solve(s)
end

gets.strip.to_i.times do
  k, x = gets.strip.split(' ').map(&:to_i)
  puts (9 * (k - 1)) + x
end

n = gets.to_i
n.times do
    k,x = gets.split.map(&:to_i)
    puts 9*(k-1)+x
end

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

n = gets.to_i
n.times do
  kx = gets.chomp.split(" ").map(&:to_i)
  k = kx[0]
  x = kx[1]

  puts 9*(k-1) + x
end

q=gets.to_i
for _ in 1..q
  a,b=gets.split.map &:to_i
  puts b+9*a-9
end


MAXLEN = 6
AAA = 'a' * MAXLEN
require 'set'
t = gets.to_i
t.times.each do
  n = gets.to_i
  s = gets.chomp
  seen = Set.new
  (0...s.length).each do |i|
    (i + 1..s.length).each do |j|
      seen << s[i, j - i]
    end
  end

  base = 'a'.ord - 1
  key = nil
  (1..2000000).each do |s|
    key = ''
    ok = true
    until s == 0 do
      if s % 27 == 0
        ok = false
        break
      end
      c = (base + (s % 27)).chr
      key.concat(c)
      s /= 27
    end
    key.reverse!

    break if ok && !seen.include?(key)
  end
  puts key
end

require 'set'
t = gets.to_i
t.times.each do
  n = gets.to_i
  s = gets.chomp
  seen = Set.new
  (0...s.length).each do |i|
    (i + 1..s.length).each do |j|
      seen << s[i, j - i]
    end
  end
  cur = 'a'
  while seen.include?(cur) do
    pos = cur.length - 1
    while cur[pos] == 'z' && pos >= 0 do
      pos -= 1
    end
    if pos < 0
      cur = 'a' * (cur.length + 1)
    else
      cur[pos] = (cur[pos].ord + 1).chr
    end
  end
  puts cur
end


gets.to_i.times{
    gets
    x = gets.split.map &:to_i
    a = x.max
    b = x.max(2)[1]
    x.size.times{|i|
        if x[i]==a
            print(a-b,' ')
        else
            print(x[i]-a,' ')
        end
    }
    puts ""
}

main = -> {
  n = gets.to_i
  s = gets.split.map(&:to_i)

  strongs = s.max(2)

  ans = n.times.map {|i|
    if s[i] == strongs[0]
      s[i] - strongs[1]
    else
      s[i] - strongs[0]
    end
  }

  ans.join(" ")
}

t = gets.to_i
puts t.times.map{ main.call }.join("\n")

=begin

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

=end

t=gets.to_i
for i in 1..t
    n =gets.to_i
    s=gets
    maks=s.split.map{|x|x.to_i}
    s1=maks.dup
    s2=maks.dup
    for j in 1..(n-1)
        s1[j]=[s1[j-1], s1[j]].max
        s2[n-j-1]=[s2[n-j-1], s2[n-j]].max
    end
    print(maks[0]-s2[1], " ")
    for j in 1..(n-2)
        print(maks[j]-[s1[j-1], s2[j+1]].max, " ")
    end
    puts maks[n-1]-s1[n-2]
end


include Math

n,k = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
val = ((-1 + sqrt(1+8*k))/2).ceil
ans = k - ((val-1)*val)/2

puts arr[ans-1]

n,k,*a=$<.read.split.map(&:to_i)
i=1
(k-=i;i+=1)while k>i
p a[k-1]

n, k = readline.split(' ').collect(&:to_i)
arr = readline.split(' ').collect(&:to_i)
k -= 1
1.upto(n) do |x|
  if k >= x
    k -= x
  else
    puts arr[k]
    break
  end
end

a=gets.chomp.split.map{|i|i.to_i}
b=gets.chomp.split
if a[1]==1
puts "#{b[0]}"
else
n=1
loop do
p=(n*(n+1))/2
if p>=a[1]
n-=1
break
else
n+=1
end
end
a[1]-=((n*(n+1))/2)
puts"#{b[a[1]-1]}"
end

include Math
n,k = gets.split.map(&:to_i)
arr_n = gets.split.map(&:to_i)

k1 = ((-1 + sqrt(1+8*k))/2).ceil
keq = k - ((k1-1)*k1)/2

# puts "k1 #{k1}"
# puts "k1(k1+1)/2 #{k1*(k1+1)/2}"
# puts "Number of ids in k1 robots turn #{(k1*(k1+1) - (k1-1)*k1)/2}"
# puts keq
puts arr_n[keq-1]

def f num
	((num + 1) * num) / 2
end

input = gets.chomp.split(' ').map { |i| i.to_i }
n, k = input[0], input[1]

ident = []

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

input.each do |i|
	ident << i
end

max = f(n)
nearest = 0

until f(nearest) >= k
	nearest += 1
end

nearest -= 1
k -= f(nearest)

#puts k
puts ident[k - 1] if k > 0
puts ident[0] if k <= 0

parse_int = lambda { gets.split.map{|x| x.to_i} }

n,k = parse_int.call
nums = parse_int.call

i = 1
while true
  break if k-i<1

  k-=i
  i+=1

end

print nums[k-1]

def f num
	((num + 1) * num) / 2
end

input = gets.chomp.split(' ').map { |i| i.to_i }
n, k = input[0], input[1]

ident = []

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

input.each do |i|
	ident << i
end

max = f(n)
nearest = 0

until f(nearest) > k
	nearest += 1
end

nearest -= 1
k -= f(nearest)

puts input[k - 1]

input = gets.chomp.split(' ').map { |i| i.to_i }
n, k = input[0], input[1]

ident = []

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

input.each do |i|
	ident << i
end

target = 0

if k > n
	target = (k % n) - 1
	target = (n - 1) if target < 0
else
	target = k - 1
end

puts (n - target)


t = gets.chomp.to_i
t.times do
  a, b = gets.chomp.split(' ').map(&:to_i)
  a_binary = a.to_s(2).reverse
  b_binary = b.to_s(2).reverse
  l = [a_binary.length, b_binary.length].min
  x = ""
  l.times do |i|
    if a_binary[i] == "1" && b_binary[i] == "1"
      x << "1"
    else
      x << "0"
    end
  end
  x = x.reverse.to_i(2)
  puts (a ^ x) + (b ^ x)
end

1.upto(gets.to_i){a,b=gets.split(' ').map(&:to_i)
puts a^b}

1.upto(gets.to_i){
  a,b=gets.split(' ').map(&:to_i)
  puts a^b
}

(1..(gets.to_i)).each{
  a,b=gets.split(' ').map(&:to_i)
  puts a^b
}

gets.to_i.times{a,b=gets.split(' ')
puts a.to_i^b.to_i}

T = gets.to_i

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

  bl = [a.bit_length, b.bit_length].max
  x = 0

  bl.times do |i|
    if a[i] == 1 && b[i] == 1
      x |= (1 << i)
    end
  end

  puts (a ^ x) + (b ^ x)
end


num = gets.to_i
colors = gets
possible_colors = ["R", "G", "B"]
colors = colors.split("")
num_possibilites = 0
(1..num).each do |index|
	color = colors[index]
	prev_color = colors[index-1] || ""
	next_color = colors[index+1] || ""
	if color == prev_color
		possibilites = (possible_colors-[prev_color, next_color])
		num_possibilites += 1
		colors[index] = possibilites.first
	end
end
puts num_possibilites
puts colors.join("")

def solution
  read_int
  gs = read_string.chars

  colors = ["R", "G", "B"]

  moves = 0

  for i in (0...gs.size)
    prv = i > 0 ? gs[i - 1] : nil
    crt = gs[i]
    nxt = gs[i + 1]

    if prv == crt
      moves += 1
      gs[i] = colors.find { |c| c != prv && c != nxt }
    end

  end

  puts moves
  puts gs.join('')

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']


line_one = $stdin.gets
line_two= $stdin.gets

h1, a1, c1 = line_one.split.map(&:to_i)
h2, a2 = line_two.split.map(&:to_i)

actions = Array.new
while h2 > 0
  if h1 > a2 || a1 >= h2
    actions << 'STRIKE'
    h2 -= a1
  else
    actions << 'HEAL'
    h1 += c1
  end
  h1 -= a2
end

puts actions.length
puts actions

h1, a1, c1 = gets.split.map(&:to_i)
h2, a2 = gets.split.map(&:to_i)
s = (h2 - 1) / a1
h = a2 * s < h1 ? 0 : (a2 * s - h1) / (c1 - a2) + 1
p s + h + 1
h.times {puts 'HEAL'}
(s + 1).times {puts 'STRIKE'}

h, a, c=gets.split.map &:to_i
g, b=gets.split.map &:to_i
res=[]
while g>0
    if h-b<=0 && g-a>0
        res<<"HEAL"
        h+=c
    else
        res<<"STRIKE"
        g-=a
    end
    h-=b
end
puts res.size, res

h,a,c=gets.split.map &:to_i
g,b=gets.split.map &:to_i
k=(g+a-1)/a
n=[(b*(k-1)-h)/(c-b)+1,0].max
puts n+k,[:HEAL]*n,[:STRIKE]*k

hac = gets.chomp.split.map(&:to_i)
h = hac[0]
a = hac[1]
c = hac[2]
ha = gets.chomp.split.map(&:to_i)
h2 = ha[0]
a2 = ha[1]

arr = []
while h2-a > 0
	if h - a2 > 0
		arr << "STRIKE"
		h2 -= a
	else
		arr << "HEAL"
		h += c
	end
	if h2 > 0
		h -= a2
	end
end

puts arr.size+1
for n in arr
	puts n
end
print "STRIKE"

#!/usr/bin/env ruby

h1, a1, c1 = gets.split.map{|x| x.to_i}
h2, a2 = gets.split.map{|x| x.to_i}

$cnt = 0
$moves = []
while h2 > 0 do
	$cnt += 1
	if h1 <= a2 and a1 < h2 then
		$moves.push("HEAL")
		h1 += c1
	else
		$moves.push("STRIKE")
		h2 -= a1
	end

	h1 -= a2
end

puts $cnt
puts $moves.join("\n")

line_one = $stdin.gets
line_two= $stdin.gets

h1, a1, c1 = line_one.split.map(&:to_i)
h2, a2 = line_two.split.map(&:to_i)

actions = Array.new
while h2 > 0
  if h1 > a2
    actions << 'STRIKE'
    h2 -= a1
  else
    actions << 'HEAL'
    h1 += c1
  end
  h1 -= a2
end

puts actions.length
puts actions

line_one = $stdin.gets
line_two= $stdin.gets

h1, a1, c1 = line_one.split.map(&:to_i)
h2, a2 = line_two.split.map(&:to_i)

actions = Array.new
while h2 > 0
  if h1 > a2
    actions << 'STRIKE'
    h2 -= a1
  else
    actions << 'HEAL'
    h1 += c1
  end
  if h2 <= 0
    break
  end
  h1 -= a2
end

puts actions.length
puts actions

h, a, c=gets.split.map &:to_i
g, b=gets.split.map &:to_i
res=[]
while g>0
    if h-b<=0
        res<<"HEAL"
        h+=c
    else
        res<<"STRIKE"
        g-=a
    end
    h-=b
end
puts res.size, res

hac = gets.chomp.split.map(&:to_i)
h = hac[0]
a = hac[1]
c = hac[2]
ha = gets.chomp.split.map(&:to_i)
h2 = ha[0]
a2 = ha[1]

arr = []
while h2 > 0
	if h - a2 > 0
		arr << "STRIKE"
		h2 -= a
		h -= a2
	else
		arr << "HEAL"
		h += c
		h -= a
	end
end

puts arr.size
for n in arr
	puts n
end

hac = gets.chomp.split.map(&:to_i)
h = hac[0]
a = hac[1]
c = hac[2]
ha = gets.chomp.split.map(&:to_i)
h2 = ha[0]
a2 = ha[1]

arr = []
while h2 > 0
	if h/a2 >= h2/a && h - a2 > 0
		arr << "STRIKE"
		h2 -= a
		h -= a2
	else
		arr << "HEAL"
		h += c
		h -= a2
	end
end

puts arr.size
for n in arr
	puts n
end

hac = gets.chomp.split.map(&:to_i)
h = hac[0]
a = hac[1]
c = hac[2]
ha = gets.chomp.split.map(&:to_i)
h2 = ha[0]
a2 = ha[1]

arr = []
while h2 > 0
	if h/a2 > h2/a || h - a2 > 0
		arr << "STRIKE"
		h2 -= a
		h -= a2
	else
		arr << "HEAL"
		h += c - a2
	end
end

puts arr.size
for n in arr
	puts n
end

#!/usr/bin/env ruby

h1, a1, c1 = gets.split.map{|x| x.to_i}
h2, a2 = gets.split.map{|x| x.to_i}

$cnt = 0
$moves = []
while h2 > 0 do
	$cnt += 1
	if h1 <= a2 then
		$moves.push("HEAL")
		h1 += c1
	else
		$moves.push("STRIKE")
		h2 -= a1
	end

	h1 -= a2
end

puts $cnt
puts $moves.join("\n")

h1, a1, c1 = gets.split.map(&:to_i)
h2, a2 = gets.split.map(&:to_i)
s = (h2 - 1) / a1
h = a2 * s < h1 ? 0 : (a2 * s - h1) / c1 + 1
p s + h + 1
h.times {puts 'HEAL'}
(s + 1).times {puts 'STRIKE'}


require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  a,b,c = gets.chomp.split.map(&:to_i).sort.reverse
  ans = 0
  if a > 0
    ans += 1
    a-=1
  end
  if b > 0
    ans += 1
    b-=1
  end
  if c > 0
    ans += 1
    c-=1
  end

  if a > 0 && b > 0
    ans += 1
    a-=1
    b-=1
  end
  if a > 0 && c > 0
    ans += 1
    a-=1
    c-=1
  end
  if b > 0 && c > 0
    ans += 1
    b-=1
    c-=1
  end

  if a > 0 && b > 0 && c > 0
    ans += 1
  end
  puts ans
end

def Solve()
    a,b,c=gets.chomp.split.map(&:to_i).sort.reverse
    ans=0
    if a>=1
        ans+=1
        a-=1
    end
    if b>=1
        ans+=1
        b-=1
    end
    if c>=1
        ans+=1
        c-=1
    end

    if a>=1  && b>=1
        ans+=1
        a-=1
        b-=1
    end
    if b>=1 && c>=1
        ans+=1
        b-=1
        c-=1
    end
    if  c>=1 && a>=1
        ans+=1
        c-=1
        a-=1
    end

    if a>=1 && b>=1 && c>=1
        ans+=1
    end
    puts ans
end

gets.to_i.times do
    Solve()
end

def try_consume(a,b,c)
  return 0 if $a < a || $b < b || $c < c
  $a -= a
  $b -= b
  $c -= c
  1
end

def solve
  $a, $b, $c = gets.split.map{|x| x.to_i}.sort
  ans = 0
  ans += try_consume(1,0,0)
  ans += try_consume(0,1,0)
  ans += try_consume(0,0,1)
  ans += try_consume(1,0,1)
  ans += try_consume(0,1,1)
  ans += try_consume(1,1,0)
  ans += try_consume(1,1,1)
  puts ans
end

t = gets.to_i
t.times { solve }

T = gets.to_i

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

  cnt = 0

  if a > 0
    a -= 1
    cnt += 1
  end

  if b > 0
    b -= 1
    cnt += 1
  end

  if c > 0
    c -= 1
    cnt += 1
  end

  nums = [a, b, c].sort

  if nums[0] > 0 && nums[2] > 0
    nums[0] -= 1
    nums[2] -= 1
    cnt += 1
  end

  if nums[1] > 0 && nums[2] > 0
    nums[1] -= 1
    nums[2] -= 1
    cnt += 1
  end

  if nums[0] > 0 && nums[1] > 0
    nums[0] -= 1
    nums[1] -= 1
    cnt += 1
  end

  if nums.all? { |n| n > 0 }
    cnt += 1
  end

  puts cnt
end

t = gets.to_i
t.times do
  ans = 0
  a, b, c = gets.split.map(&:to_i).sort.reverse
  if a >= 1
    ans += 1
    a -= 1
  end
  if b >= 1
    ans += 1
    b -= 1
  end
  if c >= 1
    ans += 1
    c -= 1
  end
  if a >= 1 && b >= 1
    ans += 1
    a -= 1
    b -= 1
  end
  if b >= 1 && c >= 1
    ans += 1
    b -= 1
    c -= 1
  end
  if c >= 1 && a >= 1
    ans += 1
    c -= 1
    a -= 1
  end
  if a >= 1 && b >= 1 && c >= 1
    ans += 1
    a -= 1
    b -= 1
    c -= 1
  end
  puts ans
end

t = gets.to_i
t.times do
  d = gets.chomp.split.map(&:to_i).sort.reverse

  ans = 0
  [1, 2, 4, 3, 5, 6, 7].each do |i|
    f = true
    3.times do |j|
      if i & (1 << j) > 0 && d[j] == 0
        f = false
        break
      end
    end

    if f
      ans += 1

      3.times do |j|
        d[j] -= 1 if i & (1 << j) > 0
      end
    end
  end
  puts ans
end


n, m = gets.chomp.split(" ").map(&:to_i)
x = 1000000000
for i in 0..m-1
  li, ri = gets.chomp.split(" ").map(&:to_i)
  x = [ri - li+1, x].min
end
puts x
for i in 0..n-1
  print (i%x), " "
end

# be name khoda ...
def read_ints; STDIN.gets.split.map(&:to_i); end
n,m = read_ints
a = Array.new
min = -1
m.times{
  l,r = read_ints
  if(min == -1 || r - l + 1 < min)
    min = r-l+1
  end
}
puts min
cur = 0
n.times {
  if(cur >= min)
    cur = 0
  end
  print "#{cur} "
  cur+=1
}

n, m = gets.chomp.split(" ").map(&:to_i)
x = 1000000000
for i in 0..m-1
  li, ri = gets.chomp.split(" ").map(&:to_i)
  x = [ri - li+1, x].min
end
puts x
for i in 0..n-1
  print (i%x)
end


n = gets.to_i
n.times do
  num = 0
  a = gets.split.map(&:to_i)
  a.reverse! if a[0] > a[1]
  until a[0] == 0
    num += a[1]/a[0]
    a[1] %= a[0]
    a.reverse!
  end
  puts num
end

def num (a, b)
    result = 0
    while a > 0 && b > 0 do
        if b > a then
            result += b/a
            b -= b - (b % a)
        else
            result += a/b
            a -= a - (a % b)
        end
    end
    return result
end

n = gets.to_i

pair = []
n.times do |i|
    pair << gets.split.map{|j| j.to_i}
end

n.times do |i|
    result = num(pair[i][0],pair[i][1])
    puts result
end

def run
  n = ARGF.readline
  ARGF.readlines.each do |s|
    a, b = s.chomp.split.map(&:to_i)

    cnt = 0
    while a > 0 and b > 0
      a, b = b, a if a > b
      cnt += b / a
      b = b % a
    end

    puts cnt
  end
end

run if $0 == __FILE__

n = gets.chomp.to_i
(1..n).each { |k|
	a,b = gets.chomp.split(" ").collect! {|x| x.to_i}
	p = 0
	if a<b
		aux = a
		a = b
		b = aux
	end
	while a>0 and b>0
		p += a/b
		aux = a%b
		a = b
		b = aux
	end
	puts p
}

gets.to_i.times {
  a,b=gets.split.map(&:to_i)
  a,b=[a,b].min,[a,b].max

  count=0
  while a!=0
    count+=b/a;
    a,b=b%a,a
  end
  puts count
}

def solve(a,b)
	return 0 if a == 0 or b == 0
	return solve(b,a) if a > b
	b/a + solve(b % a, a)
end

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

n = gets.to_i

n.times do |i|
	t = gets.split.collect{|x| x.to_i}

	a = t.max
	b = t.min
	i = 0
	while a > 0 and b > 0
		i+=a / b
		k = a%b
		a = [k,b].max
		b = [k,b].min
	end
	puts i
end