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

s,b = Hash.new(0),Hash.new(0)

n.times do
  d,p,q = gets.split(' ').map{|e| e}
  p,q = p.to_i,q.to_i

  s[p] += q if d == 'S'
  b[p] += q if d == 'B'
end

s.keys.sort.first(k).reverse_each{|e| puts "S #{e} #{s[e]}"}
b.keys.sort.last(k).reverse_each{ |e| puts "B #{e} #{b[e]}"}

n,s=gets.split.map &:to_i
B={}
S={}
$<.map{|l|
  d,p,q=l.split
  (d==?B ? B : S)[p.to_i] ||= 0
  (d==?B ? B : S)[p.to_i] += q.to_i
}
S.sort_by{|k,v|k}[0,s].reverse.each{|k,v|puts"S #{k} #{v}"}
B.sort_by{|k,v|k}.reverse[0,s].each{|k,v|puts"B #{k} #{v}"}

#!/usr/bin/ruby

B = {}
S = {}

def algo
  n, s = gets.split(" ").map { |e| e.to_i }
  n.times.each do
    d, p, v = gets.split(" ")
    p = p.to_i
    v = v.to_i
    eval(d)[p] ||= 0
    eval(d)[p] += v
  end
  ks = S.keys.sort.take(s).reverse
  kb = B.keys.sort.reverse.take(s)
  ks.each { |e| puts "S #{e} #{S[e]}" }
  kb.each { |e| puts "B #{e} #{B[e]}" }
end

algo

n,s = gets.split(" ").map(&:to_i)
sell = Hash.new(0)
buy = Hash.new(0)
n.times{
    ccc,p,q = gets.split(" ")
    p = p.to_i
    q = q.to_i
    if ccc == 'B'
        buy[p]+=q
    else
        sell[p]+=q
    end
}
ans = sell.keys.sort.take(s).reverse.map{|x| "S #{x} #{sell[x]}"}
ans += buy.keys.sort.last(s).reverse.map{|x| "B #{x} #{buy[x]}"}
puts ans

# http://codeforces.com/contest/572/problem/B
n, m = gets.chomp.split.map(&:to_i)
b = Hash.new { 0 }
s = Hash.new { 0 }

n.times do
  d, p, q = gets.chomp.split
  p = p.to_i
  q = q.to_i
  b[p] += q if d == 'B'
  s[p] += q if d == 'S'
end

s.keys.sort.first(m).reverse.each do |key|
  puts ['S', key, s[key]].join(' ')
end

b.keys.sort.last(m).reverse.each do |key|
  puts ['B', key, b[key]].join(' ')
end

n, k = gets.split.map(&:to_i)
s = Hash.new(0)
b = Hash.new(0)
n.times do
    d, p, q = gets.split(' ')
    p = p.to_i
    q = q.to_i
    s[p] += q if (d=="S")
    b[p] += q if (d=="B")
end
s.sort.first(k).reverse_each{|x|
	puts "S #{x.first} #{x.last}"
}
b.sort.last(k).reverse_each{|x|
    puts "B #{x.first} #{x.last}"
}

n, k = gets.split.map(&:to_i)
s = Hash.new(0)
b = Hash.new(0)
n.times do
    d, p, q = gets.split(" ")
    p = p.to_i
    q = q.to_i
    s[p] += q if (d=="S")
    b[p] += q if (d=="B")
end
s.sort.first(k).reverse.each{|x|
	puts "S #{x.first} #{x.last}"
}
b.sort.last(k).reverse.each{|x|
    puts "B #{x.first} #{x.last}"
}

n, k = gets.split.map(&:to_i)
s = Hash.new(0)

b = Hash.new(0)
n.times do
    d, p, q = gets.split(" ")
    p = p.to_i
    q = q.to_i
    s[p] += q if (d=="S")
    b[p] += q if (d=="B")
end
s.sort.first(k).reverse_each{|x|
	puts "S #{x.first} #{x.last}"
}
b.sort.last(k).reverse_each{|x|
    puts "B #{x.first} #{x.last}"
}

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

s = Hash.new(0)
b = Hash.new(0)

n.times do
  d, p, q = gets.split
  p = p.to_i
  q = q.to_i
  s[p] += q if d == 'S'
  b[p] += q if d == 'B'
end
s.keys.sort.first(k).reverse_each { |e| puts "S #{e} #{s[e]}" }
b.keys.sort.last(k).reverse_each { |e| puts "B #{e} #{b[e]}" }

total_num, depth = *gets.chomp.split(" ").map(&:to_i)

aggregated = {
  'B' => {},
  'S' => {}
}

total_num.times do |index|
  input = gets.chomp.split(" ")
  direction, price, amount = input[0], input[1].to_i, input[2].to_i
  aggregated[direction][price] = aggregated[direction][price] ? aggregated[direction][price] + amount : amount
end

%w(S B).each do |direction|
  prices = aggregated[direction].keys.sort
  prices = direction == 'B' ? prices.reverse.first(depth) : prices.reverse.last(depth)
  prices.each { |price| puts "#{direction} #{price} #{aggregated[direction][price]}" }
end

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

ag_buy = {}
ag_sell = {}

n.times {
  dir, price, vol = gets.split(' ')
  price = price.to_i
  vol = vol.to_i
  ag = dir == "S" ? ag_sell : ag_buy
  ag[price] ||= 0
  ag[price] += vol
}

ar = ag_sell.keys.sort.take(s).reverse.map { |p| "S #{p} #{ag_sell[p]}" }
ar += ag_buy.keys.sort.last(s).reverse.map { |p| "B #{p} #{ag_buy[p]}" }

puts ar

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

s = Hash.new(0)
b = Hash.new(0)

n.times do
  d, p, q = gets.split.map{|e| e}
  p = p.to_i
  q = q.to_i

  s[p] += q if d == 'S'
  b[p] += q if d == 'B'
end

s.keys.sort.first(k).reverse_each{ |e| puts "S #{e} #{s[e]}" }
b.keys.sort.last(k).reverse_each{ |e| puts "B #{e} #{b[e]}" }

n, k = gets.split.map(&:to_i)
d, p, q = 0, 0, 0

s = Hash.new
b = Hash.new
s.default, b.default = 0, 0

for i in 1..n
  d, p, q = gets.split.map{|e| e}
  p = p.to_i
  q = q.to_i

  s[p] += q if d == 'S'
  b[p] += q if d == 'B'
end

out = ''

for i in 1..[k, s.size].min
  min = s.keys.min
  out = "S #{min} #{s[min]}\n" + out
  s.delete(min)
end

print out

for i in 1..[k, b.size].min
  max = b.keys.max
  puts "B #{max} #{b[max]}"
  b.delete(max)
end

n, k = gets.split.map(&:to_i)
d, p, q = 0, 0, 0

s = Hash.new
b = Hash.new

for i in 1..n
  d, p, q = gets.split.map{|e| e}
  p = p.to_i
  q = q.to_i

  if d == 'S'
    if s.has_key?(p)
      s[p] += q
    else
      s[p] = q
    end
  end

  if d == 'B'
    if b.has_key?(p)
      b[p] += q
    else
      b[p] = q
    end
  end
end

out = ''

for i in 1..[k, s.size].min
  min = s.keys.min
  out = "S #{min} #{s[min]}\n" + out
  s.delete(min)
end

print out

for i in 1..[k, b.size].min
  max = b.keys.max
  puts "B #{max} #{b[max]}"
  b.delete(max)
end

n, s = gets.chomp.split(' ').map(&:to_i)
ds = []
ps = []
qs = []
n.times do
  d, p, q = gets.chomp.split(' ')
  ds << d
  ps << p.to_i
  qs << q.to_i
end

buy = Hash.new(0)
bp = []
sell = Hash.new(0)
sp = []
for i in 0..n-1
  if ds[i] == 'B'
    buy[ps[i]] += qs[i]
    bp << ps[i]
  else
    sell[ps[i]] += qs[i]
    sp << ps[i]
  end
end

bp.sort!.uniq!
sp.sort!.uniq!

ns = ([] << s << sp.size).min
for i in 0..ns - 1
  puts "S #{sp[ns - i - 1]} #{sell[sp[ns - i - 1]]}"
end

nb = ([] << s << bp.size).min
for i in 0..nb - 1
  puts "B #{bp[bp.size - i - 1]} #{buy[bp[bp.size - i - 1]]}"
end

n, s = gets.split.map(&:to_i)
a = n.times.map{gets.split.tap{|x| x[1], x[2] = x[1].to_i, x[2].to_i}}.sort_by{|x| x[1]}
buy, sell = a.partition{|x| x[0] == 'B'}
def f(a)
  a.group_by{|x| x[1]}
    .map{|_, x| [x[0][0], x[0][1], x.map(&:last).inject(:+)]}
end
buy = f(buy).reverse.take(s)
sell = f(sell).take(s).reverse

puts (sell + buy).map{|x| x.join(' ')}

n, s = gets.split.map(&:to_i)
a = n.times.map{gets.split}.sort_by{|x| x[1].to_i}
a.each{|x| x[1], x[2] = x[1].to_i, x[2].to_i}
buy, sell = a.partition{|x| x[0] == 'B'}
def f(a)
  b = a.group_by{|x| x[1]}
  b.map{|x| [x[1][0][0], x[1][0][1], x[1].reduce(0){|sum, y| sum + y[2]}]}
end
buy = f(buy).reverse[0...[s, buy.size].min]
sell = f(sell)[0...[s, sell.size].min].reverse

puts (sell + buy).map{|x| x.join(' ')}

n,s = gets.split(" ").map(&:to_i)
sell = Hash.new(0)
buy = Hash.new(0)
n.times{
    ccc,p,q = gets.split(" ")
    p = p.to_i
    q = q.to_i
    if ccc == 'B'
        buy[p]+=q
    else
        sell[p]+=q
    end
}
ans = sell.values.sort.last(s).reverse.map{|x| "S #{sell.index(x)} #{x}"}
ans += buy.values.sort.last(s).reverse.map{|x| "B #{buy.index(x)} #{x}"}
puts ans

n,s = gets.split(" ").map(&:to_i)
buy = Hash.new(0)
sell = Hash.new(0)
for i in 1..n
   ccc,p,q = gets.split(" ")
   p = p.to_i
   q = q.to_i
   if ccc=='B'
       buy[p]+=q
   else
       sell[p]+=q
   end
end
buy = buy.sort_by { |x, y| [ -Integer(y), x ] }
sell = sell.sort_by { |x, y| [ -Integer(y), x ] }
i=0
sell.each do |key,value|
    puts "S #{key} #{value}"
    i+=1
    if i==s || i==sell.size
        break
    end
end
i=0
buy.each do |key,value|
    puts "B #{key} #{value}"
    i+=1
    if i==s || i==buy.size
        break
    end
end

# http://codeforces.com/contest/572/problem/B
n, m = gets.chomp.split.map(&:to_i)
b = Hash.new { 0 }
s = Hash.new { 0 }

n.times do
  d, p, q = gets.chomp.split
  p = p.to_i
  q = q.to_i

  if d == 'B'
    b[p] += q
  else
    s[p] += q
  end
end

b = b.to_a.sort_by { |x, y| -(x * y) }
s = s.to_a.sort_by { |x, y| x * y }

(0...m).each do |idx|
  puts (['S'] + s[idx]).join(' ')
end

(0...m).each do |idx|
  puts (['B'] + b[idx]).join(' ')
end

n, k = gets.split.map(&:to_i)
s = Hash.new(0)
b = Hash.new(0)
n.times do
    d, p, q = gets.split(" ")
    p = p.to_i
    q = q.to_i
    s[p] += q if (d=="S")
    b[p] += q if (d=="B")
end
s.sort.first(k).each{|x|
    print "S "
    puts "#{x.first} #{x.last}"
}
b.sort.last(k).each{|x|
    print "B "
    puts "#{x.first} #{x.last}"
}

total_num, depth = *gets.chomp.split(" ").map(&:to_i)

aggregated = {
  'B' => {},
  'S' => {}
}

total_num.times do |index|
  input = gets.chomp.split(" ")
  direction, price, amount = input[0], input[1].to_i, input[2].to_i
  aggregated[direction][price] = aggregated[direction][price] ? aggregated[direction][price] + amount : amount
end

%w(S B).each do |direction|
  prices = aggregated[direction].keys.sort
  prices = prices.reverse if direction == 'B'
  prices.first(depth).each do |price|
    puts "#{direction} #{price} #{aggregated[direction][price]}"
  end
end

total_num, depth = *gets.chomp.split(" ").map(&:to_i)

aggregated = {
  'B' => {},
  'S' => {}
}

total_num.times do |index|
  input = gets.chomp.split(" ")
  direction, price, amount = input[0], input[1].to_i, input[2].to_i
  aggregated[direction][price] = aggregated[direction][price] ? aggregated[direction][price] + amount : amount
end

%w(S B).each do |direction|
  aggregated[direction].keys.sort.reverse.first(depth).each do |price|
  	puts "#{direction} #{price} #{aggregated[direction][price]}"
  end
end

n, k = gets.split.map(&:to_i)
d, p, q = 0, 0, 0

s = Hash.new
b = Hash.new

for i in 1..n
  d, p, q = gets.split.map{|e| e}
  q = q.to_i

  if d == 'S'
    if s.has_key?(p)
      s[p] += q
    else
      s[p] = q
    end
  end

  if d == 'B'
    if b.has_key?(p)
      b[p] += q
    else
      b[p] = q
    end
  end
end

out = ''

for i in 1..[k, s.size].min
  min = s.keys.min
  out = "S #{min} #{s[min]}\n" + out
  s.delete(min)
end

print out

for i in 1..[k, b.size].min
  max = b.keys.max
  puts "B #{max} #{b[max]}"
  b.delete(max)
end

n, k = gets.split.map(&:to_i)
d, p, q = 0, 0, 0

s = Hash.new
b = Hash.new

for i in 1..n
  d, p, q = gets.split.map{|e| e}
  q = q.to_i

  if d == 'S'
    if s.has_key?(p)
      s[p] += q
    else
      s[p] = q
    end
  end

  if d == 'B'
    if b.has_key?(p)
      b[p] += q
    else
      b[p] = q
    end
  end
end

out = ''

for i in 1..[k, s.size].min
  min = s.keys.min
  out = "S #{min} #{s[min]}\n" + out
  s.delete(min)
end

puts out

for i in 1..[k, b.size].min
  max = b.keys.max
  puts "B #{max} #{b[max]}"
  b.delete(max)
end

n, s = gets.chomp.split(' ').map(&:to_i)
ds = []
ps = []
qs = []
n.times do
  d, p, q = gets.chomp.split(' ')
  ds << d
  ps << p.to_i
  qs << q.to_i
end

buy = Hash.new(0)
bp = []
sell = Hash.new(0)
sp = []
for i in 0..n-1
  if ds[i] == 'B'
    buy[ps[i]] += qs[i]
    bp << ps[i]
  else
    sell[ps[i]] += qs[i]
    sp << ps[i]
  end
end

bp.sort!.uniq!
sp.sort!.uniq!

for i in 0..([] << s << sp.size).min - 1
  puts "S #{sp[sp.size-i-1]} #{sell[sp[s-i-1]]}"
end

for i in 0..([] << s << bp.size).min - 1
  puts "B #{bp[bp.size-i-1]} #{buy[bp[bp.size-i-1]]}"
end

n, s = gets.chomp.split(' ').map(&:to_i)
ds = []
ps = []
qs = []
n.times do
  d, p, q = gets.chomp.split(' ')
  ds << d
  ps << p.to_i
  qs << q.to_i
end

buy = Hash.new(0)
bp = []
sell = Hash.new(0)
sp = []
for i in 0..n-1
  if ds[i] == 'B'
    buy[ps[i]] += qs[i]
    bp << ps[i]
  else
    sell[ps[i]] += qs[i]
    sp << ps[i]
  end
end

bp.sort!.uniq!
sp.sort!.uniq!

for i in 0..([] << s << sp.size).min - 1
  puts "S #{sp[s-i-1]} #{sell[sp[s-i-1]]}"
end

for i in 0..([] << s << bp.size).min - 1
  puts "B #{bp[bp.size-i-1]} #{buy[bp[bp.size-i-1]]}"
end

n, s = gets.chomp.split(' ').map(&:to_i)
ds = []
ps = []
qs = []
n.times do
  d, p, q = gets.chomp.split(' ')
  ds << d
  ps << p.to_i
  qs << q.to_i
end

buy = Hash.new(0)
bp = []
sell = Hash.new(0)
sp = []
for i in 0..n-1
  if ds[i] == 'B'
    buy[ps[i]] += qs[i]
    bp << ps[i]
  else
    sell[ps[i]] += qs[i]
    sp << ps[i]
  end
end

bp.sort!.uniq!
sp.sort!.uniq!

for i in 0..([] << s << sp.size).min - 1
  puts "S #{sp[sp.size-i-1]} #{sell[sp[sp.size-i-1]]}"
end

for i in 0..([] << s << bp.size).min - 1
  puts "B #{bp[bp.size-i-1]} #{buy[bp[bp.size-i-1]]}"
end

n,s=gets.split.map &:to_i
B={}
S={}
$<.map{|l|
  d,p,q=l.split
  (d==?B ? B : S)[p.to_i] ||= 0
  (d==?B ? B : S)[p.to_i] += q.to_i
}
S.sort_by{|k,v|k}.reverse[0,s].each{|k,v|puts"S #{k} #{v}"}
B.sort_by{|k,v|k}.reverse[0,s].each{|k,v|puts"B #{k} #{v}"}

n,s=gets.split.map &:to_i
B={}
S={}
$<.map{|l|
  d,p,q=l.split
  (d==?B ? B : S)[p.to_i] ||= 0
  (d==?B ? B : S)[p.to_i] += q.to_i
}
S.sort_by{|k,v|v}.reverse[0,s].each{|k,v|puts"S #{k} #{v}"}
B.sort_by{|k,v|v}.reverse[0,s].each{|k,v|puts"B #{k} #{v}"}

#!/usr/bin/ruby

B = {}
S = {}

def algo
  n, s = gets.split(" ").map { |e| e.to_i }
  n.times.each do
    d, p, v = gets.split(" ")
    p = p.to_i
    v = v.to_i
    eval(d)[p] ||= 0
    eval(d)[p] += v
  end
  ks = S.keys.sort.reverse.take(s)
  kb = B.keys.sort.reverse.take(s)
  ks.each { |e| puts "S #{e} #{S[e]}" }
  kb.each { |e| puts "B #{e} #{B[e]}" }
end

algo

#!/usr/bin/ruby

B = {}
S = {}

def algo
  n, s = gets.split(" ").map { |e| e.to_i }
  n.times.each do
    d, p, v = gets.split(" ")
    p = p.to_i
    v = v.to_i
    eval(d)[p] ||= 0
    eval(d)[p] += v
  end
  ks = S.keys.sort.take(s)
  kb = B.keys.sort.reverse.take(s)
  ks.each { |e| puts "S #{e} #{S[e]}" }
  kb.each { |e| puts "B #{e} #{B[e]}" }
end

algo


STDIN.reopen 'input.txt'
STDOUT.reopen 'output.txt'
N, K = gets.split.map &:to_i
A = gets.split.map &:to_i
p A.map { |v| v - [v / K, 3].min * K }.reduce :+

f = File.open('input.txt', 'r') or die "Unable to open file..."
input = []  # start with an empty array
  f.each_line {|line|
    input.push line
  }

k = input[0].scan(/\w+/)[1].to_i
jars = input[1].scan(/\w+/).map!(&:to_i)
total = jars.inject(0) do |give, jar|
 nom = 0
 until jar < k or nom == 3 do
 jar = jar - k
 nom += 1
 end

 if jar < k or nom == 3
 give += jar
 end

end

 File.open('output.txt', 'w') { |o| o.puts total}

if File.exist?('input.txt')
  input = IO.readlines("input.txt")
  input.each do |word|
    word.delete!("\n")
  end
end
input1 = input[0]
input2 = input[1]

# a = []

k = input1.split(" ")[1].to_i

piglet_honey = 0

input2.split(" ").each do |e|

  if e.to_i > 3*k
    piglet_honey += e.to_i - 3*k
  else
    piglet_honey += e.to_i%k
  end
end

output = File.open( "output.txt","w" )
output << piglet_honey
output.close

$stdin.reopen 'input.txt'
$stdout.reopen 'output.txt'
N, K = gets.split.map &:to_i
A = gets.split.map &:to_i
p A.map { |v| v - [v / K, 3].min * K }.reduce :+

$stdin.reopen('input.txt')
$stdout.reopen('output.txt')
N, K = gets.split.map &:to_i
A = gets.split.map &:to_i
p A.map { |v| v - [v / K, 3].min * K }.reduce :+

n, k, a = nil
File.open("input.txt") do |f|
  n, k = f.gets.split.map(&:to_i)
  a = f.gets.split.map(&:to_i)
end

s = a.inject(0){|r, x| r+(x>=3*k ? x-3*k : x%k)}

File.open("output.txt", "w") do |f|
  f.puts s
end

require 'scanf'

input = open("input.txt")

line = input.gets
l1 = line.scanf("%d %d")
n = l1[0]
k = l1[1]

line = input.gets
a = line.scan(/[\d]+/)

ret = 0

a.each do |i|
  ret += [i.to_i % k, i.to_i - 3 * k].max
end

output = open("output.txt", "w")
output.printf "%d\n" % [ret]


$h = Hash.new(0)
$l = 0
$r = 0

def solve
  type, id = gets.strip.split(' ')
  id = id.to_i
  if type == "L"
    $l += 1
    $h[[id, 0]] = $l
  elsif type == "R"
    $r += 1
    $h[[id, 1]] = $r
  else
    flg_left = false; flg_right = false
    if $h[[id, 0]].zero? && !$h[[id, 1]].zero?
      ok = $h[[id, 1]]
      flg_right = true
    elsif !$h[[id, 0]].zero? && $h[[id, 1]].zero?
      flg_left = true
      ok = $h[[id, 0]]
    end
    if flg_left == true
      s = [$l - ok, ok - 1 + $r].min
      puts s
    elsif flg_right == true
      s = [$r - ok, ok - 1 + $l].min
      puts s
    end
  end
end

gets.strip.to_i.times { solve }

def solution
  n = read_int

  left = 500_000
  right = left + 1
  shelf = {}

  n.times do
    command, id = read_string.split
    case command
    when ?L
      shelf[id] = left
      left -= 1
    when ?R
      shelf[id] = right
      right += 1
    when ??
      pos = shelf[id]
      puts [pos - left - 1, right - pos - 1].min
    end

  end

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']

$h = Hash.new(0)
$l = 0
$r = 0

def solve
  type, id = gets.strip.split(' ')
  id = id.to_i
  if type == "L"
    $l += 1
    $h[[id, 0]] = $l
  elsif type == "R"
    $r += 1
    $h[[id, 1]] = $r
  else
    ok = -1
    flg_left = false; flg_right = false
    if $h[[id, 0]].zero? && !$h[[id, 1]].zero?
      ok = $h[[id, 1]]
      flg_right = true
    elsif !$h[[id, 0]].zero? && $h[[id, 1]].zero?
      flg_left = true
      ok = $h[[id, 0]]
    end
    if flg_left == true
      s = [$l - ok, $l - ok + $r].min
      puts s
    elsif flg_right == true
      s = [$r - ok, $r - ok + $l].min
      puts s
    end
  end
end

gets.strip.to_i.times { solve }


def count(a,d)
  tot = 0
  if  a > d/2
    tot = d-a
  else
    # as the doors opened are fewer we need to count that amount of time to close them
    tot = a
  end
  tot
end

def calculate(n,l,r)
  # I choose to count 1s and then do the calculation for both sides
  lopens = l.count(1)
  ropens = r.count(1)
  tot_sec = 0

  tot_sec += count(lopens,n)
  tot_sec += count(ropens,n)

  tot_sec
end

n = gets.to_i
r = []
l = []
(1..n).each do
  a,b = gets.split(/\s/).map(&:to_i)
  l << a
  r << b
end

puts calculate(n,l,r)

def min(x, y)
    if x < y then
        return x
    else
        return y
    end
end

n = gets.to_i

left_open = 0
left_close = 0
right_open = 0
right_close = 0
n.times do |i|
    s = gets.split.collect{|j| j.to_i}
    if s[0] == 1 then
        left_open += 1
    else
        left_close += 1
    end
    if s[1] == 1 then
        right_open += 1
    else
        right_close += 1
    end
end
right = min(right_close, right_open)
left = min(left_close, left_open)
t = right + left
puts t

n=gets.to_i
a,b=0,0
n.times{
	c,d=gets.split.map(&:to_i)
	a += c
	b += d
}
puts [a,n-a].min + [b,n-b].min

n,a = gets.to_i ,Array.new{|el| el=Array.new(2)}
n.times{a<<gets.split.map(&:to_i)}
puts n-[a.find_all{|el| el[0]==0}.size,a.find_all{|el| el[0]==1}.size].max+(n-[a.find_all{|el| el[1]==0}.size,a.find_all{|el| el[1]==1}.size].max)

n = gets.chomp.to_i
right = 0
left = 0

n.times do
  l, r = gets.chomp.split(" ").map{|n| n.to_i}
  left += 1 if l == 0
  right += 1 if r == 0
end

puts [(n - left), left].min + [(n - right), right].min

n = gets.to_i
left = 0
right = 0
n.times do
  l, r = gets.split.map(&:to_i)
  left += l
  right += r
end
print [left, n-left].min + [right, n-right].min

n=gets.chomp.to_i
l1=0
l2=0
r1=0
r2=0
n.times do
	l,r=gets.chomp.split.map(&:to_i)
	if l!=0
		l1+=1
	else
		l2+=1
	end
	if r!=0
		r1+=1
	else
		r2+=1
	end
end
puts [l1,l2].min+[r1,r2].min

n = gets.to_i
a = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}.transpose
puts [a[0].find_all {|x| x==0}.size, a[0].find_all {|x| x==1}.size].min +
    [a[1].find_all {|x| x==0}.size, a[1].find_all {|x| x==1}.size].min

n = gets.to_i
ret = 0
left =  [0,0]
right = [0,0]
for i in 0 .. n - 1
  line = gets
  if line.split(" ")[0].to_i == 0
    left[0] += 1
  else
    left[1] += 1
  end
  if line.split(" ")[1].to_i == 0
    right[0] += 1
  else
    right[1] += 1
  end
end

if left[0] < left[1]
  ret += left[0]
else
  ret += left[1]
end
if right[0] < right[1]
  ret += right[0]
else
  ret += right[1]
end

puts ret

n = gets.strip.to_i
left_open = left_close = right_open = right_close = 0
n.times do
    l, r = gets.strip.split.map(&:to_i)
    left_close += 1 if l == 0
    left_open += 1 if l == 1
    right_close += 1 if r == 0
    right_open += 1 if r == 1
end
ans = [left_open, left_close].min + [right_open, right_close].min
puts ans

a = gets.chomp.to_i
s = []
y = []; z=[]
rez=0
(0..a-1).each do |i|
  s = gets.chomp.split(' ')
  y[i]=s[0].to_i; z[i]=s[1].to_i
end

sum1 = y.inject(0){|sum, t| sum + t }
sum2 = z.inject(0){|sum, t| sum + t }

if a%2 != 0
  a1=a+1
else
  a1=a
end

if sum1>=a1/2
  rez+=a-sum1
else
  rez+=sum1
end

if sum2>=a1/2
  rez+=a-sum2
else
  rez+=sum2
end
puts rez

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

n = gets.to_i
data = []

left_open = 0
right_open = 0
n.times{
  left, right = gets.split.map(&:to_i)

  left_open+=left
  right_open+=right
}

left_closed = n - left_open
right_closed = n - right_open

print [left_open, left_closed].min + [right_open, right_closed].min

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

n = gets.to_i
data = []

n.times{
  data << gets.split.map(&:to_i)
}

left_open = 0
right_open = 0
data.each{|left, right|
  left_open+=left
  right_open+=right
}

left_closed = n - left_open
right_closed = n - right_open

print [left_open, left_closed].min + [right_open, right_closed].min

n=gets.to_i
a=b=0
n.times {
	l,r=gets.split.map(&:to_i)
	a+=1 if l==0
	b+=1 if r==0
}
puts ([a,n-a].min + [b,n-b].min)

n=gets.to_i
a,b=0,0
n.times{
	c,d=gets.split.map(&:to_i)
	a +=1 if c == 1
	b +=1 if d == 1
}
puts [a,n-a].min + [b,n-b].min

state_amount = 2
col_amount = 2
row_amount = gets.strip.to_i

state_count = Array.new(col_amount) { Array.new(state_amount, 0) }

1.upto(row_amount).each do |row|
  row_states = gets.strip.split(' ')
  row_states.each_with_index do |state, col|
    state_count[col][state.to_i] += 1
  end
end

movement_count = 0
1.upto(col_amount) do |col|
  movement_count += row_amount - state_count[col - 1].max
end

puts movement_count

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

amount_0 = 0
amount_1 = 0
for i in 0...n
    if a[i][0] == 0
        amount_0 += 1
    end
end
for i in 0...n
    if a[i][1] == 0
        amount_1 += 1
    end
end
ans1 = 0
if amount_0 <= n - amount_0
    ans1 = amount_0
else
    ans1 = n - amount_0
end
ans2 = 0
if amount_1 <= n - amount_1
    ans2 = amount_1
else
    ans2 = n - amount_1
end
puts ans1 + ans2

#file_contents = File.read('test.in')
n = gets.to_i

left_count = right_count = 0
n.times do
  l, r = gets.split.map(&:to_i)
  left_count += l
  right_count += r
end
puts [left_count, n - left_count].min + [right_count, n - right_count].min

zeros = [0, 0]
n = gets.chomp.to_i
for _ in 1..n
  gets.chomp.split.each_with_index { |x, i| zeros[i] += 1 if x == '0' }
end
puts zeros.map { |z| [z, n - z].min }.inject(:+)

n = gets.to_i

l = Array.new(n)
r = Array.new(n)

n.times do |i|
	t = gets.split.collect{|x| x.to_i}
	l[i] = t[0]
	r[i] = t[1]
end

lr = l.inject { |s,x| s+x }
rr = r.inject { |s,x| s+x}

puts [n - lr, lr].min + [n - rr, rr].min

n = gets.chomp.to_i
right = 0
left = 0

n.times do
  l, r = gets.chomp.split(" ").map{|n| n.to_i}
  left += 1 if l == 0
  right += 1 if r == 0
end

puts [left, right].max

n = gets.to_i
ret = 0
for i in 0 .. n - 1
  line = gets
  for j in 0 .. 1
    if line.split(" ")[j].to_i == 1
      ret += 0.5
    end
  end
end

puts ret.to_i

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

left1 = 0
left2 = 0
for i in 0...n
    if a[i][0] == 0
        left1 += 1
    end
end
for i in 0...n
    if a[i][1] == 1
        left2 += 1
    end
end
left = left1 + left2
if left >= 2*n - left
    puts 2*n - left
else
    puts left
end

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

left1 = 0
left2 = 0
for i in 0...n
    if a[i][0] == 0
        left1 += 1
    end
end
for i in 0...n
    if a[i][1] == 1
        left2 += 1
    end
end
left = left1 + left2
if left1 == n - left2 && left1 == n
    puts 0
elsif left1 == n - left2 && left1 == 0
    puts 0
elsif

    if left >= 2*n - left
        puts 2*n - left
    else
        puts left
    end
end

#file_contents = File.read('test.in')
numbers = gets.scan(/\d+/)
numbers.collect! &:to_i

n = numbers.shift
left_count = right_count = 0
numbers.each_index {|index|
  if index.even?
    left_count += numbers[index]
  else
    right_count += numbers[index]
  end
}
puts [left_count, n - left_count].min + [right_count, n - right_count].min

numbers = gets.chomp.scan(/\d+/)
numbers.collect! &:to_i

n = numbers.shift
left_count = right_count = 0
numbers.each_index {|index|
  if index.even?
    left_count += numbers[index]
  else
    right_count += numbers[index]
  end
}
puts [left_count, n - left_count].min + [right_count, n - right_count].min


lines = gets.chomp.to_i
lines.times do
    input = (1..gets.chomp.to_i).to_a
    for i in (0..input.size-2).step 2 do
       aux = input[i]
       input[i] = input[i+1]
       input[i+1] = aux
    end
    if input.size % 2 == 1 and input.size > 1
        aux = input[-1]
        input[-1] = input[-2]
        input[-2] = aux
    end
    puts input * " "
end

def main
  t = inp.to_i
  t.times do
    n = inp.to_i
    if n & 1 == 0
      1.upto(n) do |i|
        print (i & 1 == 0)? (i - 1) : (i + 1)
        print " " if i != n
      end
    else
      print 1
      2.upto(n) do |i|
        print " "
        print (i & 1 == 0)? (i + 1) : (i - 1)
      end
    end
    puts ""

  end

end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

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

main

lines = gets.chomp.to_i
lines.times do
    input = (1..gets.chomp.to_i).to_a
    for i in (0..input.size-2).step 2 do
       aux = input[i]
       input[i] = input[i+1]
       input[i+1] = aux
    end
    if input.size % 2 == 1 and input.size > 1
        aux = input[-1]
        input[-1] = input[-2]
        input[-2] = aux
    end
    puts input * ""
end


gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  go = ->x { x -= 1; x == 0 ? 0 : x % 2 == 1 ? 1 : 2 }
  steps = go[n] + go[m]
  puts steps % 2 == 0 ? "Tonya" : "Burenka"
end

def main
  t = inp.to_i
  t.times do
    n, m = mapint(inarr)
    if n % 2 == 0
      puts (m % 2 == 0)? "Tonya": "Burenka"
    else
      puts (m % 2 == 1)? "Tonya": "Burenka"
    end

  end
end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

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

main


gets.to_i.times do
    n=gets.to_i
    if n==1
        puts 3
    elsif n%2==1
        puts 1
    else
        t=n
        while t%2==0 do
            t/=2
        end
        if t==1
            puts n+1
        else
            puts n/t
        end
    end
end

gets.to_i.times do
  x = gets.to_i
  start = 1 << x.to_s(2).reverse.index("1")
  p (start..).find { |y| x & y > 0 && x ^ y > 0 }
end


def solve n, xs
  xs.sort!
  ans = [1, xs[0], n+1-xs[xs.size-1]].max
  (xs.size - 1).times do |i|
    ans = [ans, (xs[i+1] - xs[i] + 2)/ 2].max
  end
  p ans
end

q = gets.to_i
q.times do
  n, m = gets.chomp.split.map(&:to_i)
  xs = gets.chomp.split.map(&:to_i)
  solve n, xs
end

t = gets.chomp.to_i
t.times do
  beds = gets.chomp.split(' ').map{|x| x.to_i}[0]
  taplist = gets.chomp.split(' ').map{|x| x.to_i - 1}
  taplist = [-1] + taplist + [beds]

  longest = 0
  (0..taplist.size-2).each do |i|
    time = 0
    if i == 0 then
      time = taplist[i+1] - taplist[0]
      # puts "time between #{taplist[0]} and #{taplist[i+1]} is #{time}"
    elsif i == taplist.size-2 then
      time = taplist[-1] - taplist[i]
      # puts "time between #{taplist[-1]} and #{taplist[i]} is #{time}"

    else
      if (taplist[i+1] - taplist[i]) == 1
        time = 1
      else
        time = (taplist[i+1] - taplist[i])/2 + 1
      end
      # puts "time between #{taplist[i+1]} and #{taplist[i]} is #{time}"

    end

    longest = time if time > longest
  end
  puts longest
end

gets.to_i.times{
	n,k=gets.split.map &:to_i
	a=gets.split.map &:to_i
	p [a[0],n+1-a[-1],*a.each_cons(2).map{|x,y|(y-x+2)/2}].max
}

def solve
  n, k = gets.strip.split.map(&:to_i)
  a = gets.strip.split.map(&:to_i)
  mx = [a.first, n - a.last + 1].max
  (1...(a.size)).each do |i|
    mx = [mx, ((a[i] - a[i-1] - 1) / 2.0).ceil + 1].max
  end
  mx
end

gets.strip.to_i.times do
  puts solve
end

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  a = gets.chomp.split.map(&:to_i)
  ans = 0
  (1..n).each do |i|
    tmp = 300
    a.each do |j|
      tmp = [tmp, (i-j).abs + 1].min
    end
    ans = [ans, tmp].max
  end
  puts ans
end

inp = gets.chomp.to_i
otv = []

inp.times{ |w|
count = 0
map = []

row = gets.chomp.split(" ")
gryadok = row[0]

(0..gryadok.to_i-1).each {|w| map[w] = 0}

crans = gets.chomp.split(" ")

startup = []
startdown = []

crans.size.times{|e|
startup[e] = crans[e].to_i-1
startdown[e] = crans[e].to_i-1
}

loop do

crans.size.times{|e|

map[startup[e]] = 1
map[startdown[e]] = 1
if startup[e] > 0
startup[e]-=1
end
if startdown[e] < (gryadok.to_i-1)
startdown[e]+=1
end

}

#puts map.to_s

count+=1
if map.index(0).nil? then otv.push(count);break; end

end

}

otv.size.times{|w|
puts otv[w]
}

t = gets.chomp.to_i
t.times do
  beds = gets.chomp.split(' ').map{|x| x.to_i}[0]
  taplist = gets.chomp.split(' ').map{|x| x.to_i - 1}
  taplist = [-1] + taplist + [beds]

  longest = 0
  (0..taplist.size-2).each do |i|
    time = 0
    if i == 0 then
      time = taplist[i+1] - taplist[0] + 1
    elsif i == taplist.size-2 then
      time = taplist[-1] - taplist[i] + 1
    else
      time = (taplist[i+1] - taplist[i])/2
    end

    longest = time if time > longest
  end
  puts longest - 1
end

gets.to_i.times{
	n,k=gets.split.map &:to_i
	a=gets.split.map &:to_i
	p [n+1-a[-1],*a.each_cons(2).map{|x,y|(y-x+2)/2}].max
}

def solve
  n, k = gets.strip.split.map(&:to_i)
  a = gets.strip.split.map(&:to_i)
  mx = [a.first, n - a.last + 1].max
  (1...(a.size)).each do |i|
    mx = [mx, ((a[i] - a[i-1] - 1) / 2).ceil + 1].max
  end
  mx
end

gets.strip.to_i.times do
  puts solve
end

inp = gets.chomp.to_i
otv = []

inp.times{ |w|
count = 0

row = gets.chomp.split(" ")
gryadok = row[0]

crans = gets.chomp.split(" ")
crans.each{|w| count+=w.to_i}

if (gryadok.to_i - count.to_i) < 0

otv.push(1)

else

otv.push(gryadok.to_i - count.to_i + 1)

end

}

otv.size.times{ |w|
puts otv[w]
}

def solve n, xs
  xs.sort!
  ans = [1, xs[0], n+1-xs[xs.size-1]].max
  (xs.size - 1).times do |i|
    ans = [ans, (xs[i+1] - xs[i] + 1)/ 2].max
  end
  p ans
end

q = gets.to_i
q.times do
  n, m = gets.chomp.split.map(&:to_i)
  xs = gets.chomp.split.map(&:to_i)
  solve n, xs
end


DBG = true
t = gets.to_i
for i in 0...t
  n = gets.to_i
  lr = []
  for j in 0...n
    ll,rr = gets.split.map{|z| z.to_i}
    lr << [ll,rr]
  end
  slr = lr.sort_by{|x| x[0]}
  rmax = slr[0][1]
  bdr = -1
  for j in 1...n
    if rmax < slr[j][0]
      bdr = rmax
      break
    end
    rmax = [rmax, slr[j][1]].max
  end
  if bdr == -1
    puts "-1"
  else
    for j in 0...n
      if lr[j][0] <= bdr
        print "1 "
      else
        print "2 "
      end
    end
    puts ""
  end
end

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

t = gets.to_i

t.times do
  n = gets.to_i
  arr = []

  (0..n-1).each do |i|
    arr.push gets.chomp.split(" ").map(&:to_i)+[i]
  end

#  p arr

  arr.sort_by!{|x| [x[0],x[1]]}

#  p arr

  newarr = []

  z = 1

=begin
  done = false
  arr.each_with_index do |x,i|
  	if i+2<n and x[1]>=arr[i+2][0]
  		puts -1
  		done = true
  		break
  	end
  	newarr.push (x+[z])
  	if i<n-1 and not x[1]>=arr[i+1][0]
	  	z = 3-z
	end
  end
=end

  hasone = false
  hastwo = false

  maxtillnow = -1.0/0.0

  arr.each_with_index do |x,i|
    newarr.push (x+[z])
    if z==1
    	hasone = true
    else
    	hastwo = true
    end
    maxtillnow = x[1] if x[1]>maxtillnow
  	if i+1<n and maxtillnow<arr[i+1][0]
#  		good = true
	  	z = 3-z
	end
  end

#  next if done

#  p newarr
  newarr.sort_by!{|x| x[2]}
#  p newarr
#  p hasone
#  p hastwo

  if not (hasone and hastwo)
  	puts -1
  	next
  end

  newarr.each do |x|
  	print x[3]
  	print " "
  end
  puts
end

DBG = true
t = gets.to_i
for i in 0...t
  n = gets.to_i
  l = []
  r = []
  for i in 0...n
    a,b = gets.split.map{|z| z.to_i}
    l << a
    r << b
  end
  ll = l.sort!
  rr = r.sort!
  lmax = ll[-1]
  rmin = rr[0]
  if lmax <= rmin
    puts "-1"
    next
  end
  for i in 0...n
    if r[i] < lmax
      print "1 "
    else
      print "2 "
    end
  end
  puts ""
end

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

t = gets.to_i

t.times do
  n = gets.to_i
  arr = []

  (0..n-1).each do |i|
    arr.push gets.chomp.split(" ").map(&:to_i)+[i]
  end

  arr.sort_by{|x| [x[0],x[1]]}

  newarr = []

  z = 1
  done = false
  arr.each_with_index do |x,i|
  	if i+2<n and x[1]>=arr[i+2][0]
  		puts -1
  		done = true
  		break
  	end
  	newarr.push (x+[z])
  	z = 3-z
  end

  next if done

  newarr.sort_by{|x| x[2]}
  newarr.each do |x|
  	print x[3]
  	print " "
  end
  puts
end

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

t = gets.to_i

t.times do
  n = gets.to_i
  arr = []

  (0..n-1).each do |i|
    arr.push gets.chomp.split(" ").map(&:to_i)+[i]
  end

#  p arr

  arr.sort_by!{|x| [x[0],x[1]]}

#  p arr

  newarr = []

  z = 1

=begin
  done = false
  arr.each_with_index do |x,i|
  	if i+2<n and x[1]>=arr[i+2][0]
  		puts -1
  		done = true
  		break
  	end
  	newarr.push (x+[z])
  	if i<n-1 and not x[1]>=arr[i+1][0]
	  	z = 3-z
	end
  end
=end

  arr.each_with_index do |x,i|
    newarr.push (x+[z])
  	if i<n-1 and not x[1]>=arr[i+1][0]
#  		good = true
	  	z = 3-z
	end
  end

#  next if done

#  p newarr
  newarr.sort_by!{|x| x[2]}
#  p newarr

  hasone = false
  hastwo = false
  newarr.each do |x|
  	if x[3]==1
  		hasone = true
  	else
  		hastwo = true
  	end
  	if hasone and hastwo
  		break
  	end
  end

#  p hasone
#  p hastwo

  if not (hasone and hastwo)
  	puts -1
  	next
  end

  newarr.each do |x|
  	print x[3]
  	print " "
  end
  puts
end

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

t = gets.to_i

t.times do
  n = gets.to_i
  arr = []

  (0..n-1).each do |i|
    arr.push gets.chomp.split(" ").map(&:to_i)+[i]
  end

#  p arr

  arr.sort_by!{|x| [x[0],x[1]]}

#  p arr

  newarr = []

  z = 1

=begin
  done = false
  arr.each_with_index do |x,i|
  	if i+2<n and x[1]>=arr[i+2][0]
  		puts -1
  		done = true
  		break
  	end
  	newarr.push (x+[z])
  	if i<n-1 and not x[1]>=arr[i+1][0]
	  	z = 3-z
	end
  end
=end

  arr.each_with_index do |x,i|
    newarr.push (x+[z])
  	if i<n-1 and not x[1]>=arr[i+1][0]
	  	z = 3-z
	end
  end

#  next if done

#  p newarr
  newarr.sort_by!{|x| x[2]}
#  p newarr

  hasone = false
  hastwo = false
  newarr.each do |x|
  	if x[3]==1
  		hasone = true
  	else
  		hastwo = true
  	end
  	if hasone and hastwo
  		break
  	end
  end

#  p hasone
#  p hastwo

  if not (hasone and hastwo)
  	puts -1
  	next
  end

  newarr.each do |x|
  	print x[3]
  	print " "
  end
  puts
end

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

t = gets.to_i

t.times do
  n = gets.to_i
  arr = []

  (0..n-1).each do |i|
    arr.push gets.chomp.split(" ").map(&:to_i)+[i]
  end

#  p arr

  arr.sort_by!{|x| [x[0],x[1]]}

#  p arr

  newarr = []

  z = 1
  done = false
  arr.each_with_index do |x,i|
  	if i+2<n and x[1]>=arr[i+2][0]
  		puts -1
  		done = true
  		break
  	end
  	newarr.push (x+[z])
  	z = 3-z
  end

  next if done

  newarr.sort_by!{|x| x[2]}
  newarr.each do |x|
  	print x[3]
  	print " "
  end
  puts
end

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

t = gets.to_i

t.times do
  n = gets.to_i
  arr = []

  (0..n-1).each do |i|
    arr.push gets.chomp.split(" ").map(&:to_i)+[i]
  end

  arr.sort_by{|x| [x[0],x[1]]}

  newarr = []

  z = 1
  done = false
  arr.each_with_index do |x,i|
  	if i+2<n and x[1]>arr[i+2][0]
  		puts -1
  		done = true
  		break
  	end
  	newarr.push (x+[z])
  	z = 3-z
  end

  next if done

  newarr.sort_by{|x| x[2]}
  newarr.each do |x|
  	print x[3]
  	print " "
  end
  puts
end


valid_pos = [[0, 4], [4, 0], [4, 4]]

n = gets.to_i
for k in 1..n
  kings = []
  for i in 0..7
    s = gets.chomp.split('')
    for j in 0..7
      kings << [i, j] if s[j] == 'K'
    end
  end
  puts(valid_pos.include?([(kings[1][0] - kings[0][0]).abs, (kings[1][1] - kings[0][1]).abs]) ? 'YES' : 'NO')
  gets if k < n
end

t = gets.to_i
for i in 0...t
	xs = []
	ys = []
	for y in 0...8
		gets.split('').each.with_index do |c, x|
			if c == 'K'
				xs << x % 4
				ys << y % 4
			end
		end
	end
	if xs[0] == xs[1] and ys[0] == ys[1]
		puts 'YES'
	else
		puts 'NO'
	end
	if i < t-1
		gets
	end
end

t = gets.to_i
for i in 0...t
	sum = []
	diff = []
	for y in 0...8
		gets.split('').each.with_index do |c, x|
			if c == 'K'
				sum << (x + y) % 4
				diff << (x - y) % 4
			end
		end
	end
	if sum[0] == sum[1] and diff[0] == diff[1]
		puts 'YES'
	else
		puts 'NO'
	end
	if i < t-1
		gets
	end
end

valid_pos = [[2, 2], [0, 4], [4, 0], [4, 4], [5, 2], [2, 6], [6, 6]]

n = gets.to_i
for k in 1..n
  kings = []
  for i in 0..7
    s = gets.chomp.split('')
    for j in 0..7
      kings << [i, j] if s[j] == 'K'
    end
  end
  puts(valid_pos.include?([(kings[1][0] - kings[0][0]).abs, (kings[1][1] - kings[0][1]).abs]) ? 'YES' : 'NO')
  gets if k < n
end

valid_pos = [[2, 2], [0, 4], [4, 0], [4, 4], [4, 2], [2, 6], [6, 6]]

n = gets.to_i
for k in 1..n
  kings = []
  for i in 0..7
    s = gets.chomp.split('')
    for j in 0..7
      kings << [i, j] if s[j] == 'K'
    end
  end
  puts(valid_pos.include?([(kings[1][0] - kings[0][0]).abs, (kings[1][1] - kings[0][1]).abs]) ? 'YES' : 'NO')
  gets if k < n
end

valid_pos = [[2, 2], [0, 4], [4, 0], [4, 4]]

n = gets.to_i
for k in 1..n
  kings = []
  for i in 0..7
    s = gets.chomp.split('')
    for j in 0..7
      kings << [i, j] if s[j] == 'K'
    end
  end
  puts(valid_pos.include?([(kings[1][0] - kings[0][0]).abs, (kings[1][1] - kings[0][1]).abs]) ? 'YES' : 'NO')
  gets if k < n
end


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

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

power = gets.chomp.split(" ").map(&:to_i)
schools = gets.chomp.split(" ").map(&:to_i)
chosen = gets.chomp.split(" ").map(&:to_i)

schoolsarr = []
m.times {schoolsarr.push []}

(0..n-1).each do |i|
	schoolsarr[schools[i]-1].push power[i]
end

(0..m-1).each do |i|
	schoolsarr[i].sort!
end

ans = 0

#p schoolsarr

chosen.each do |z|
	ind = z-1
#	p ind
#	p schools[ind]
	if (power[ind]!=schoolsarr[schools[ind]-1][-1])
		ans+=1
	end
end

puts ans

a = gets.rstrip.split.map(&:to_i)
n = a[0]
m = a[1]
k = a[2]

powers = gets.rstrip.split.map(&:to_i)
schools = gets.rstrip.split.map(&:to_i)
chosen = gets.rstrip.split.map(&:to_i)
count = 0
chosen.each do |chosen_one|
	school = schools[chosen_one - 1]
	max_power = 0
	schools.each_with_index do |student, i|
		next unless student == school
		max_power = powers[i] if max_power < powers[i]
	end
	count += 1 if powers[chosen_one - 1] < max_power
end

p count

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

powers  = gets.split(" ").map(&:to_i)
schools = gets.split(" ").map(&:to_i)
choosens = gets.split(" ").map(&:to_i)

made_up_cnt = 0
choosens.each do |student|
  schools.each_with_index do |school, index|
    if student-1 != index and schools[student-1] == school and powers[student-1] < powers[index]
      made_up_cnt += 1
      break
    end
  end
end

puts made_up_cnt

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

powers  = gets.split(" ").map(&:to_i)
schools = gets.split(" ").map(&:to_i)
choosens = gets.split(" ").map(&:to_i)

made_up_cnt = 0
choosens.each do |student|
  schools.each_with_index do |school, index|
    if schools[student-1] == school and powers[student-1] < powers[index]
      made_up_cnt += 1
    end
  end
end

puts made_up_cnt

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

powers  = gets.split(" ").map(&:to_i)
schools = gets.split(" ").map(&:to_i)
choosens = gets.split(" ").map(&:to_i)

made_up_cnt = 0
choosens.each do |student|
  schools.each_with_index do |school, index|
    if student-1 != index and schools[student-1] == school and powers[student-1] < powers[index]
      made_up_cnt += 1
    end
  end
end

puts made_up_cnt


#!/usr/bin/env ruby

class MeetingPlace
  attr_accessor :n, :x, :v, :zip, :result

  def get_data
    # fl=File.new "test0.txt"
    # @n,@x,@v = fl.readlines.map {|x| x.split.map(&:to_i)}
    @n, @x, @v = $stdin.readlines.map { |x| x.split.map(&:to_i) }
    @n = @n[0]
  end

  def output
    puts @result
  end

  def init
    @zip = @x.zip @v
    @x, @v = nil, nil
    @zip = @zip.sort { |a, b| a[0]<=>b[0] }
    @zipUp = @zip.each
    @zipDown = @zip.reverse_each
    @upper = @zipDown.next
    @lower =@zipUp.next
    @upperPrev = @upper
    @lowerPrev = @lower
    @direction = nil
    @result = (@upper[0] - @lower[0]).to_f / (@upper[1] + @lower[1])
    @lowerPtr = 0
    @upperPtr = @zip.length - 1
  end

  def getNext
    return false if @lowerPtr == @upperPtr
    if @upper[1] > @lower[1]
      @upper = @zipDown.next
      @direction = :down
      @upperPtr-=1
    else
      @lower = @zipUp.next
      @direction = :up
      @lowerPtr+=1
    end
    true
  end

  def get_result
    init
    while getNext
      if @direction == :down
        tmp = (@upper[0] - @lowerPrev[0]).to_f / (@upper[1] + @lowerPrev[1])
      else
        tmp = (@upperPrev[0] - @lower[0]).to_f / (@upperPrev[1] + @lower[1])
      end
      if tmp > @result
        @result = tmp
        if @upper[1] != @upperPrev[1]
          @upperPrev = @upper
        else
          @lowerPrev = @lower
        end
      end
    end
    @result = @result.round(6)
  end
end

if __FILE__ == $0
  mp = MeetingPlace.new
  mp.get_data
  mp.get_result
  mp.output
end

gets
xv = gets.split.map(&:to_f).zip(gets.split.map(&:to_f)).sort
aa, res = (bb = []), [0.0]
begin
  u = 2e9
  xv.each { |x, v|
    if u > v
      u = v
      bb << [x, v]
    end
  }
  break if !aa.equal?(bb)
  bb = []
  xv.reverse!
end while true
aa.each { |x, v|
  bb.each { |y, u|
    break if y <= x
    res << (y - x) / (u + v)
  }
}
p res.max

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

n = gets.to_i
a = read_ints.zip(read_ints)

l = 0.0
r = 1e9
100.times do
  c = (l + r) / 2
  mx = a.map{|x, v| x - v * c}.max
  mi = a.map{|x, v| x + v * c}.min
  if mx > mi
    l = c
  else
    r = c
  end
end
p (l + r) / 2

#!/usr/bin/env ruby

class MeetingPlace
  attr_accessor :n, :x, :v, :zip, :result

  def get_data
    # fl=File.new "test0.txt"
    # @n,@x,@v = fl.readlines.map {|x| x.split.map(&:to_i)}
    @n, @x, @v = $stdin.readlines.map { |x| x.split.map(&:to_i) }
    @n = @n[0]
  end

  def output
    puts @result
  end

  def init
    @zip = @x.zip @v
    @x, @v = nil, nil
    @zip = @zip.sort { |a, b| a[0]<=>b[0] }
    @zipUp = @zip.each
    @zipDown = @zip.reverse_each
    @upper = @zipDown.next
    @lower =@zipUp.next
    @upperPrev = @upper
    @lowerPrev = @lower
    @result = (@upper[0] - @lower[0]).to_f / (@upper[1] + @lower[1])
  end

  def getNext
    return false if @upper[0] == @lower[0]
    if @upper[1] > @lower[1]
      @upper = @zipDown.next
    else
      @lower = @zipUp.next
    end
    true
  end

  def get_result
    init
    while getNext
      tmp = (@upper[0] - @lower[0]).to_f / (@upper[1] + @lower[1])
      @result = tmp if tmp > @result
    end
    @result = @result.round(6)
  end
end

if __FILE__ == $0
  mp = MeetingPlace.new
  mp.get_data
  mp.get_result
  mp.output
end


#!/usr/bin/env ruby
# encoding: utf-8
n = gets.to_i
a = gets.split.map(&:to_i)
g = Array.new(n) { [] }

(n-1).times do
    u, v = gets.split.map(&:to_i)
    u, v = u-1, v-1
    g[u] << v
    g[v] << u
end

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

dp = Array.new(n) { Array.new(2) { [] } }
dp[0] = [[a[0]], [0]]
par = [-1]*n

st = [0]
until st.empty?
    u = st.pop
    g[u].each do |v|
        next if v == par[u]
        par[v] = u
        st.push(v)
        dp[v][0] += dp[u][0].map{|x| GCD(x,a[v])}
        dp[v][1] += dp[u][0]
        dp[v][1] += dp[u][1].map{|x| GCD(x,a[v]) }
        2.times do |i|
            dp[v][i].uniq!
        end
    end
end

print a[0]
for u in 1...n do
    print " #{dp[u][1].max}"
end

n = gets.to_i
a = gets.split.map &:to_i
g = Array.new(n) { [] }

(n-1).times do
    u, v = gets.split.map &:to_i
    u, v = u-1, v-1
    g[u] << v
    g[v] << u
end

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

dp = Array.new(n) { Array.new(2) { [] } }
dp[0] = [[a[0]], [0]]
par = [-1]*n

q = [0]
until q.empty?
    u = q.shift
    g[u].each do |v|
        next if v == par[u]
        par[v] = u
        q.unshift(v)
        dp[u][0].each do |x|
            dp[v][0] << GCD(x, a[v]);
            dp[v][1] << x
        end
        dp[u][1].each do |x|
            dp[v][1] << GCD(x, a[v])
        end
        for i in 0..1 do
            dp[v][i].uniq!
        end
    end
end

print a[0]
for u in 1...n do
    print " #{dp[u][1].max}"
end


n, r, avg = gets.split.map(&:to_i)
total = avg * n

arr = []
n.times do
   ai, bi = gets.split.map(&:to_i)
   total -= ai
   arr << [bi, ai]
end

res = 0
if total > 0
    arr = arr.sort
    arr.each do |item|
        tmp = [total, r - item[1]].min
        res += tmp * item[0]
        total -= tmp
    end
end

puts res

n, r, avg = gets.split.map(&:to_i)
total = avg * n

arr = []
n.times do
   ai, bi = gets.split.map(&:to_i)
   total -= ai
   if r > ai
       arr << [bi, r - ai]
   end
end

res = 0
if total > 0
    arr = arr.sort
    arr.each do |item|
        tmp = [total, item[1]].min
        res += tmp * item[0]
        total -= tmp
    end
end

puts res

n, r, avg = gets.split.collect {|x| x.to_i}
total = avg * n

arr = []
n.times do
   ai, bi = gets.split.collect {|x| x.to_i}
   total -= ai
   arr.push [bi, ai]
end

res = 0
if total > 0
    arr = arr.sort
    arr.each do |item|
        tmp = [total, r - item[1]].min
        res += tmp * item[0]
        total -= tmp
    end
end

puts res

n, r, avg = gets.split(" ").collect {|x| x.to_i}
total = avg * n

arr = []
n.times do
   ai, bi = gets.split(" ").collect {|x| x.to_i}
   total -= ai
   arr.push [bi, ai]
end

res = 0
if total > 0
    arr = arr.sort
    arr.each do |item|
        tmp = [total, r - item[1]].min
        res += tmp * item[0]
        total -= tmp
    end
end

puts res

n, r, avg = gets.split.map(&:to_i)
total = avg * n

arr = []
n.times do
   ai, bi = gets.split.map(&:to_i)
   total -= ai
   arr.push [bi, ai]
end

res = 0
if total > 0
    arr = arr.sort
    arr.each do |item|
        tmp = [total, r - item[1]].min
        res += tmp * item[0]
        total -= tmp
    end
end

puts res

n, r, avg = gets.split(' ').map(&:to_i)
total = avg * n

arr = []
n.times do
   ai, bi = gets.split(' ').map(&:to_i)
   total -= ai
   arr.push [bi, ai]
end

res = 0
if total > 0
    arr = arr.sort
    arr.each do |item|
        tmp = [total, r - item[1]].min
        res += tmp * item[0]
        total -= tmp
    end
end

puts res

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

n, r, avg = gets.chomp.split.map(&:to_i)
req = n * avg

s = []
n.times do
  tmp = gets.split.map(&:to_i)
  req -= tmp[0]
  unless r <= tmp[0]
    tmp[0] = r - tmp[0]
    s << tmp
  end
end

if req <= 0
  puts 0
  exit 0
end

s.sort_by! { |a| a[1] }

i = 0
essays = 0
while 0 < req and i < n
  a = s[i][0]
  essay = s[i][1]
  if req <= a
    essays += req * essay
    break
  end
  essays += a * essay
  i += 1
  req -= a
end

puts essays

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

n, r, avg = gets.chomp.split.map(&:to_i)
req = n * avg

s = []
n.times do
  tmp = gets.split.map(&:to_i)
  req -= tmp[0]
  unless r <= tmp[0]
    #tmp[0] = r - tmp[0]
    s << tmp
  end
end

if req <= 0
  puts 0
  exit 0
end

s.sort_by! { |a| a[1] }

i = 0
essays = 0
while 0 < req and i < n
  grade = s[i][0]
  essay = s[i][1]
  a = (r - grade)
  if req <= a
    essays += req * essay
    break
  end
  essays += a * essay
  i += 1
  req -= a
end

puts essays

class Point
  def initialize(a,b)
    @a = a
    @b = b
  end
  def a
    @a
  end
  def b
    @b
  end
end

p= Array.new
n , r , avg = gets.split.map(&:to_i)
sum, ans, i= 0, 0, 0
n.times do
  x, y= gets.split.map(&:to_i)
  p<<Point.new(x,y)
  sum += x
end

p.sort_by!{|x| x.b}

  until sum>=avg*n
    sum += r-p[i].a
    ans += (r-p[i].a)*p[i].b
    i += 1
  end
  r=ans-(sum-avg*n)*p[i-1].b
  puts r>0?r:0

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

n, r, avg = read_ints
avg *= n
a = []
cur_avg = 0
n.times do
  a << read_ints
  cur_avg += a[-1][0]
end
ans = 0
a.sort_by!{|x| x[1]}
a.each do |x|
  break if cur_avg >= avg
  mn =[r - x[0], avg - cur_avg].min
    ans += mn * x[1]
    cur_avg += mn
end
puts ans

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

n, r, avg = read_ints
avg *= n
a = []
cur_avg = 0
n.times do
  a << read_ints
  cur_avg += a[-1][0]
end
if cur_avg >= avg
  puts 0
  exit 0
end
ans = 0
a.sort_by!{|x| x[1]}
a.each do |x|
  if r - x[0] >= avg - cur_avg
    ans += x[1] * (avg - cur_avg)
    break
  end
    ans += (r - x[0]) * x[1]
    cur_avg += (r - x[0])
end

puts ans

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

n, r, avg = read_ints
avg *= n
a = []
cur_avg = 0
n.times do
  a << read_ints
  cur_avg += a[-1][0]
end
if cur_avg >= avg
  puts 0
  exit 0
end
ans = 0
a.sort_by{|x| x[1]}.each do |x|
  if r - x[0] >= avg - cur_avg
    ans += x[1] * (avg - cur_avg)
    break
  end
    ans += (r - x[0]) * x[1]
    cur_avg += (r - x[0])
end

puts ans

#$stdin = File.open("input.txt")

n, r, avg = gets.split.map &:to_i
exams = gets(nil).split("\n").map { |line| line.split.map &:to_i }

need = avg * n
have = exams.transpose.first.inject(:+)

exams.sort! { |a, b| a.last <=> b.last }

essays = 0
while have < need
	a, b = exams.first

	up = [need - have, r - a].min

	have   += up
	essays += up * b

	exams.shift
end

puts essays

def read_ints; STDIN.gets.split.map(&:to_i); end
n, r, avg = read_ints
a = []
i = 0
curSum = 0
n.times do
  a << read_ints
  a[i].reverse!
  curSum+= a[i][1]
  i+=1
end
a.sort!
avgSum = avg*n
resEssays = 0
i=0
while(curSum<avgSum)
  j = [avgSum - curSum, r - a[i][1]]
  curEssays = j.min
  resEssays+= curEssays * a[i][0];
  curSum += curEssays
  i+=1
end
puts resEssays

class Stu
  attr_accessor:a
  attr_accessor:b
end

pa=Array.new
pb=Array.new

str=gets.to_s
pa=str.split
n=pa[0].to_i
r=pa[1].to_i
avg=pa[2].to_i
s=n*avg

for i in 0..n-1
  str=gets.to_s
  pa=str.split
  pb[i]=Stu.new
  s-=pa[0].to_i
  pb[i].a=r-pa[0].to_i
  pb[i].b=pa[1].to_i
end

if s<0
  s=0
end
pb=pb.sort_by{|x| x.b}
#p pb

k=0
for i in 0..n-1
  if pb[i].a>=s
    k+=pb[i].b*s
    break
  else
    s-=pb[i].a
    k+=pb[i].b*pb[i].a
  end
end

puts k

class Stu
  attr_accessor:a,:b
end

pa=Array.new
pb=Array.new

str=gets.to_s
pa=str.split
n=pa[0].to_i
r=pa[1].to_i
avg=pa[2].to_i
s=n*avg

for i in 0..n-1
  str=gets.to_s
  pa=str.split
  pb[i]=Stu.new
  s-=pa[0].to_i
  pb[i].a=r-pa[0].to_i
  pb[i].b=pa[1].to_i
end

if s<0
  s=0
end

pb.sort! {|x,y| x.b<=>y.b }
#p pb

k=0
for i in 0..n-1
  if pb[i].a>=s
    k+=pb[i].b*s
    break
  else
    s-=pb[i].a
    k+=pb[i].b*pb[i].a
  end
end

puts k

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

n, r, avg = gets.chomp.split.map(&:to_i)
req = n * avg

s = []
n.times do
  tmp = gets.split.map(&:to_i)
  req -= tmp[0]
  unless r <= tmp[0]
    tmp[0] = r - tmp[0]
    s << tmp
  end
end

s.sort_by! { |a| a[1] }

essays = 0
s.each do |a|
  is_break = false
  a[0].times do |i|
    req -= 1
    essays += a[1]
    is_break = true and break if req <= 0
  end
  break if is_break
end

puts essays

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

n, r, avg = gets.chomp.split.map(&:to_i)
req = n * avg

s = []
n.times do
  tmp = gets.split.map(&:to_i)
  req -= tmp[0]
  unless r <= tmp[0]
    tmp[0] = r - tmp[0]
    s << tmp
  end
end

if req <= 0
  puts 0
  exit 0
end

s.sort_by! { |a| a[1] }

essays = 0
s.each do |a|
  if 0 <= req - a[0]
    req -= a[0]
    essays += a[0] * a[1]
    break if req <= 0
    next
  end
  req -= 1
  essays += a[1]
  break if req <= 0
end

puts essays

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

n, r, avg = read_ints
avg *= n
a = []
cur_avg = 0
n.times do
  a << read_ints
  cur_avg += a[-1][0]
end
if cur_avg > avg
  puts 0
  exit 0
end
ans = 0
a.each do |x|
  if r - x[0] >= avg - cur_avg
    ans += x[1] * (avg - cur_avg)
    break
  end
    ans += (r - x[0]) * x[1]
    cur_avg += (r - x[0])
end

puts ans

n, r, avg = gets.split.map &:to_i
exams = gets(nil).split("\n").map { |line| line.split.map &:to_i }

need = avg * n
have = exams.transpose.first.inject(:+)

exams.sort! { |a, b| a.last <=> b.last }

essays = 0
while have < need
    a, b = exams.first

    up = [need - have, r - a].min

    have   += up
    essays += up * b

    exams.unshift
end

puts essays


#!/usr/bin/env ruby
phone_length, block_length = gets.split(' ').map(&:to_i)

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

possibilities_in_each_block = a.zip(b).map do |a_i, b_i|
  vsego_kratnix = (10**(block_length) - 1).div(a_i)

  #require 'byebug'
  #debugger
  kratnix_i_startujut_s_b_itogo = ((b_i+1)*(10**(block_length-1)) - 1).div(a_i) - (b_i*(10**(block_length-1)) - 1).div(a_i)

  nulevoi_element = 1

  nulevoi_element + vsego_kratnix - kratnix_i_startujut_s_b_itogo
end

big_modulo = 10**9 + 7

def fast_product_stupid(int_arr_product, big_modulo)
  total = 1
  int_arr_product.each do |num|
    total = (num*total).modulo(big_modulo)
  end
  total
end

puts fast_product_stupid(possibilities_in_each_block, big_modulo)

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

x = 10 ** k
xx = 10 ** k.pred
md = (10 ** 9) + 7

f = "%0" + k.to_s + "d"
ans = 1

a.size.times do |i|
    c = 0
    v = 0
    ai = a[i].to_i
    bi = b[i].to_i
    if a[i].size == k
        while v < x
            if (f % [v])[0] != b[i]
                c += 1
            end
            v += ai
        end
    else
        c = x.pred.div(ai).succ
        if bi == 0
            c -= xx.pred.div(ai).succ
        else
            c -= bi.succ.*(xx).pred.div(ai).succ - bi.*(xx).pred.div(ai).succ
        end
    end
    ans *= c
    ans %= md
end

puts ans.%(md)

n, k = gets.chomp.split.map(&:to_i)
as = gets.chomp.split.map(&:to_i)
bs = gets.chomp.split.map(&:to_i)
mod = (1E9 + 7).to_i

ans = 1
for i in 0...(n / k)
  ans = ans * ((10**k - 1) / as[i] + 1 - (bs[i] * 10**(k - 1) + 10**(k - 1) - 1) / as[i] + (bs[i] * 10**(k - 1) - 1) / as[i]) % mod
end

puts ans

#!/usr/bin/ruby

n, k = gets.strip().split(" ")
n = n.to_i
k = k.to_i
a = gets.strip().split(" ")
a = a.map(&:to_i)
b = gets.strip().split(" ")
b = b.map(&:to_i)
m = n / k;
ans = 1;
for i in 0..m - 1
    if b[i] == 0
        tmp = 10 ** (k - 1)
        mul = (tmp * 10 - 1) / a[i] - tmp / a[i] + 1
        if tmp % a[i] > 0
            mul -= 1
        end
    else
        tmp = 10 ** (k - 1)
        mul = (tmp * 10 - 1) / a[i] - tmp * (b[i] + 1) / a[i] + 1 + (tmp * b[i] - 1) / a[i] + 1
        if tmp * (b[i] + 1) % a[i] > 0
            mul -= 1
        end
    end
    ans = ans * mul
    ans %= 10 ** 9 + 7
end
print ans, "\n"

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

block = n / k

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

answer = 1

for i in 0..(block - 1)
  ans = (10**k - 1) / a[i] + 1

  degree =  10**(k - 1)
  ans -= ((b[i] + 1) * degree - 1) / a[i]
  ans += (b[i] * degree - 1) / a[i]

  answer *= ans
  answer %= 1000000007
end

puts answer

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

block = n / k

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

answer = 1

for i in 0..(block - 1)
  degree =  10**(k - 1)

  ans = (10**k - 1) / a[i] + 1

  ans -= ((b[i] + 1) * degree - 1) / a[i]
  ans += (b[i] * degree - 1) / a[i]

  answer *= ans
end

puts answer

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

x = 10 ** k
xx = 10 ** k.pred

f = "%0" + k.to_s + "d"
ans = 1

n.div(k).times do |i|
    c = 0
    v = 0
    ai = a[i].to_i
    bi = b[i].to_i
    if a[i].size == k
        while v < x
            if (f % v)[0] != b[i]
                c += 1
            end
            v += ai
        end
    else
        c = x.pred.div(ai).succ
        if bi == 0
            c -= xx.pred.div(ai).succ
        else
            c -= bi.*(xx).div(ai).succ - bi.pred.*(xx).div(ai).succ
        end
    end
    ans *= c
end

puts ans.%((10 ** 9) + 7)

#!/usr/bin/env ruby
phone_length, block_length = gets.split(' ').map(&:to_i)

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

possibilities_in_each_block = a.zip(b).map do |a_i, b_i|
  valid_parts_in_block = ('9'*block_length).to_i.div(a_i)
  next 1 if valid_parts_in_block == 0

  valid_parts_in_block.downto(1).take_while do |kratnoe|
    num = (kratnoe*a_i).to_s

    longest_kratnoe = num.length == block_length
    if (longest_kratnoe && num[0] == b_i)
      valid_parts_in_block -= 1
    end

    longest_kratnoe
  end

  next valid_parts_in_block+1 # zero
end

big_modulo = 10**9 + 7

def fast_product_stupid(int_arr_product, big_modulo)
  total = 1
  int_arr_product.each do |num|
    total = (num*total).modulo(big_modulo)
  end
  total
end

puts fast_product_stupid(possibilities_in_each_block, big_modulo)

n, k = gets.chomp.split.map(&:to_i)
as = gets.chomp.split.map(&:to_i)
bs = gets.chomp.split.map(&:to_i)
mod = (1E9 + 7).to_i
ans = 1
for i in 0...(n / k)
  ans = ans * (99 / as[i] - ((bs[i] * 10 + 9) / as[i]) + (bs[i] * 10 - 1) / as[i] + 1) % mod
end

puts ans

#!/usr/bin/ruby

n, k = gets.strip().split(" ")
n = n.to_i
k = k.to_i
a = gets.strip().split(" ")
a = a.map(&:to_i)
b = gets.strip().split(" ")
b = b.map(&:to_i)
m = n / k;
ans = 1;
for i in 0..m - 1
    if b[i] == 0
        tmp = 10 ** (k - 1)
        mul = (tmp * 10 - 1) / a[i] - tmp / a[i] + 1
        if tmp % a[i] > 0
            mul -= 1
        end
    else
        tmp = 10 ** (k - 1)
        mul = (tmp * 10 - 1) / a[i] - tmp * (b[i] + 1) / a[i] + 1 + (tmp * b[i] - 1) / a[i] + 1
        if tmp * (b[i] + 1) % a[i]
            mul -= 1
        end
    end
    ans = ans * mul
    ans %= 10 ** 9 + 7
end
print ans, "\n"


gets.to_i.times do
  a,v,l,r = gets.split.map(&:to_i)
  puts a/v-r/v+(l-1)/v
end

gets.to_i.times do
	n, v, l, r = gets.split.map(&:to_i)
	puts n / v - (r / v - l.pred / v)
end

def go
  l, v, s, e = read_ints

  count = 0
  # interval before train
  from = v
  to = s - 1
  count += ((to-from)/v) + 1 if from <= to

  # interval after train
  from = e + 1
  to = (l.to_f / v).floor * v
  count += ((to-from)/v) + 1 if from <= to

  puts count
end

def solution
  n = read_int
  n.times do
    go
  end
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']

def solve
  t = gets.strip.to_i
  t.times do
    d, v, l, r = gets.strip.split.map(&:to_i)
    puts (d / v) - ( (r / v) - (l / v) + (l % v == 0 ? 1 : 0))
  end
end

solve

# https://codeforces.com/problemset/problem/1066/A
def solution(d, v, l, r)
  return (d / v) - (r / v) + ((l - 1) / v)
end

n = gets.chomp.to_i
n.times do
  d, v, l, r = gets.chomp.split(" ").map(&:to_i)
  puts solution(d, v, l, r)
end

def lights(len, v)
	(len + v)/v - 1
end
n = gets.chomp
n.to_i.times do |i|
	line = gets.chomp
	len, v, l, r = line.split.map(&:to_i)
	res = lights(len, v)
	#puts res
	res = res - ( lights(r,v) - lights(l - 1,v) )
	puts res
end

n = gets.strip.to_i

out = []
n.times do
    arr = gets.strip.split(' ').map(&:to_i)
    start = arr[2] - 1

    start2 = arr[3] + 1
    end2 = arr[0]

    final = (start / arr[1]).floor + (end2 / arr[1]).floor - (start2 / arr[1]).floor
    if (start2 % arr[1] == 0 && end2 % arr[1] == 0)
        final += 1
    elsif (start2 % arr[1] == 0 && end2 % arr[1] != 0)
        final += 1
    end
    out << final
end

out.each do |o|
    puts o
end

n = gets.strip.to_i

out = []
n.times do
    arr = gets.strip.split(' ').map(&:to_i)
    start = arr[2] - 1

    start2 = arr[3] + 1
    end2 = arr[0]

    final = (start / arr[1]).floor + (end2 / arr[1]).floor - (start2 / arr[1]).floor
    if (start2 % arr[1] == 0 && end2 % arr[1] == 0)
        final += 1
    end
    out << final
end

out.each do |o|
    puts o
end

n = gets.strip.to_i

out = []
n.times do
    arr = gets.strip.split(' ').map(&:to_i)
    start = arr[2] - 1

    start2 = arr[3] + 1
    end2 = arr[0]

    final = (start / arr[1]).floor + (end2 / arr[1]).floor - (start2 / arr[1]).floor
    if (start2 % arr[1] == 0 || end2 % arr[1] == 0)
        final += 1
    end
    out << final
end

out.each do |o|
    puts o
end


Pointer = Struct.new(:value)

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  ass = n.times.map { gets.split.map(&:to_i) }
  go = ->(i, j, di, dj, map) {
    pointer = Pointer.new(0)
    while i < n && j < m && i >= 0 && j >= 0
      map[[i, j]] = pointer
      pointer.value += ass[i][j]
      i += di
      j += dj
    end
  }
  ltr, rtl = {}, {}
  0.upto(n - 1) { |i| go[i, 0, 1, 1, ltr] }
  0.upto(m - 1) { |j| go[0, j, 1, 1, ltr] }
  0.upto(n - 1) { |i| go[i, m - 1, 1, -1, rtl] }
  0.upto(m - 1) { |j| go[0, j, 1, -1, rtl] }
  answer = 0.upto(n - 1).map do |i|
    0.upto(m - 1).map do |j|
      ltr[[i, j]].value + rtl[[i, j]].value - ass[i][j]
    end.max
  end.max
  p answer
end

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  ass = n.times.map { gets.split.map(&:to_i) }
  answer = 0.upto(n - 1).map do |i|
    0.upto(m - 1).map do |j|
      sum = ass[i][j]
      [[-1, -1], [-1, 1], [1, -1], [1, 1]].map do |di, dj|
        ii, jj = i + di, j + dj
        while ii >= 0 && ii <= n - 1 && jj >= 0 && jj <= m - 1
          sum += ass[ii][jj]
          ii += di
          jj += dj
        end
      end
      sum
    end.max
  end.max
  p answer
end

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  diag = {}
  rdiag = {}

  as = Array.new(n) { gets.split.map(&:to_i) }

  n.times { |i|
    m.times { |j|
      di = j - i
      diag[di] ||= 0
      diag[di]  += as[i][j]

      rdi = m - 1 - j - i
      rdiag[rdi] ||= 0
      rdiag[rdi]  += as[i][j]
    }
  }

  ans = nil
  n.times { |i|
    m.times { |j|
      cur = diag[j - i] + rdiag[m - 1 - j - i] - as[i][j]
      ans = cur if !ans || cur > ans
    }
  }

  p ans
end

def solve
  h, w = gli
  b = []
  h.times { b << gli }
  ans = 0
  (0...h).each do |i|
    (0...w).each do |j|
      tmp = b[i][j]
      (1..i).each do |q|
        break if i - q < 0 || j - q < 0

        tmp += b[i - q][j - q]
      end
      (1..i).each do |q|
        break if i - q < 0 || j + q >= w

        tmp += b[i - q][j + q]
      end
      (i + 1...h).each do |q|
        break if j - (q - i) < 0

        tmp += b[q][j - (q - i)]
      end
      (i + 1...h).each do |q|
        break if j + (q - i) >= w

        tmp += b[q][j + (q - i)]
      end

      ans = [ans, tmp].max
    end
  end
  puts ans
end

MOD = 10**9 + 7
def gs = gets.chomp
def gi = gets.to_i
def gli = gets.split.map(&:to_i)
def cyesno(x) = x ? :Yes : :No
def uyesno(x) = x ? :YES : :NO

gets.to_i.times { solve }

main = -> {
  t = int
  t.times do
      n, m = ints
      a = rep(n) { ints }
      sum_x = Hash.new(0)
      sum_y = Hash.new(0)
      n.times do |i|
          m.times do |j|
              sum_x[i + j] += a[i][j]
              sum_y[i - j] += a[i][j]
          end
      end
      ans = 0
      n.times do |i|
          m.times do |j|
              ans = [ans, sum_x[i + j] + sum_y[i - j] - a[i][j]].max
          end
      end
      puts ans
  end
}

DEBUG = true
MOD = 10**9+7
$yesno = %w(No Yes)
YesNo = %w(No Yes)
YESNO = %w(NO YES)
INF = 10**18

require "set"
require "prime"

def int; gets.to_s.to_i end
def ints; gets.to_s.split.map { |s| s.to_i } end
def int1s; gets.to_s.split.map { |s| s.to_i - 1 } end
def float; gets.to_s.to_f end
def floats; gets.to_s.split.map { |s| s.to_f } end
def array_of(&convert); gets.to_s.split.map(&convert) end
def string; gets.to_s.chomp end
def strings; gets.to_s.split end
def rep(n, &b); Array.new(n, &b) end
def yes; puts $yesno[1] end
def no; puts $yesno[0] end
def yesno t; puts $yesno[t] end
def YesNo t; puts YesNo[t] end
def YESNO t; puts YESNO[t] end
def zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end
def max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end
def min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end
def minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end
def gcd(*xs); xs.inject(0, :gcd) end
def matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end
def debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end
def debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join("") } if DEBUG; grid end
def if_debug; yield if DEBUG end

module Boolean
  def coerce(other); [other, to_i] end
  def +@; to_i end
  def to_int; to_i end
  def *(other); to_i * other end
end

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

class Integer
  def div_ceil(y); (self + y - 1) / y end
  def mod_inv(mod = MOD); pow(mod-2, mod) end
  def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end
  def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end
  def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end
  def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster
  def bitbrute(&block); (1<<self).times(&block) end
  def nCr(r); x = 1; (1..r).each { |i| x *= self + 1 - i; x /= i }; x; end
  def each_divisor; return Enumerator.new { |y| each_divisor { |k| y << k } } unless block_given?; k = 1; while k * k < self; if self % k == 0; yield k; yield self / k end; k += 1; end; yield k if k * k == self end
  def divisors; each_divisor.to_a end
end

class Range
  def end_open; exclude_end? ? self.end : self.end + 1 end
  def end_close; exclude_end? ? self.end - 1 : self.end end
  def upper_bound; ac, wa = self.begin, self.end_open; while wa - ac > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end
  def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end
end

class Array
  def power(&block); (0 ... 1 << size).each(&block) end
  def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end
  def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end
  def cumdiff(range); self[range.end_open] - self[range.begin]; end
  def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end
end

module Enumerable
  def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end
  def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end
end

main[]


n=gets.to_i

ans=[]
n.times{|i|
	ans<<i+1
}

n.times{
	x=gets.split.map{|e| e.to_i}
	n.times{|i|
		ans[i]=0 if x[i]==2 || x[i]==3
	}
}
c=ans.count{|e| e!=0}
puts c
if c>0 then
puts ans.select{|e| e>0}*" "
end

n = gets.to_i
cars = 1.upto(n).select do |_|
  collisions = gets.split(' ').map(&:to_i)
  !collisions.include?(1) && !collisions.include?(3)
end

puts cars.length
puts cars.join(' ')

n = gets.to_i
ans = Array.new
n.times do |i|
    tmp = gets.split
    ans.push(i+1) if not tmp.include?("1") and not tmp.include?("3")
end
puts ans.size
puts ans.join(" ") unless ans.empty?

n = gets.chomp.to_i
ans = [*1..n]
ar = Array.new(n) { Array.new(n) }
for i in 0...n
  ar[i] = gets.split(" ").map{|i| i.to_i}
end
for i in 0...n
	for j in 0...n
		if ar[i][j] == 3
			ans.delete(i + 1)
			ans.delete(j + 1)
		elsif ar[i][j] == 1
			ans.delete(i + 1)
		elsif ar[i][j] == 2
			ans.delete(j + 1)
		end
	end
end
puts ans.size
puts ans.join(" ")

n, c, s = Integer(gets), 0, ""

(1..n).each do |i|
	x = gets.split(" ")
	unless x.include? "1" or x.include? "3"
		c += 1
		s += "#{i} "
	end
end

puts c
if c > 0
	puts s
end

n = gets.to_i
ans = []
(1..n).each do |i|
    row = gets.split.map &:to_i
    ans.push i if not row.include? 1 and not row.include? 3
end
puts ans.size
ans.each do |a|
    print a.to_s + ' '
end
puts

# http://codeforces.com/contest/545/problem/A
n = gets.chomp.to_i
matrix = [].tap do |arr|
  n.times { arr << gets.chomp.split.map(&:to_i) }
end

good_cars = []
matrix.transpose.each.with_index do |row, idx|
  good_cars << idx + 1 unless (row.include?(2) || row.include?(3))
end

puts good_cars.length
puts good_cars.join(" ") unless good_cars.empty?

arr = []
gets.to_i.times do
  arr << gets.split.map { |x| x.to_i }.to_a
end
indices = []
count = 0
arr.each do |i|
  if !i.include?(1) && !i.include?(3)
    count += 1
    indices << arr.index(i) + 1
  end
end
puts count
indices.each { |x| print x, " " }

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

# a.rb

def main()
    n = gets.to_i
    r = []
    n.times do |i|
        a = gets.chomp.split(" ").map{|s| next s.to_i }
        r << (i + 1) if !(a.index(1) || a.index(3))
    end
    puts r.size
    puts r * " " if r.size > 0
end

if __FILE__ == $0 then
    main()
end

n = gets.to_i
goodCars = []
amountOfGoodCar = 0
n.times do |i|
    crashes = gets.split(' ')
                  .map{|x| x.to_i}
                  .select{|x| x > 0 && x % 2 == 1}
    next if crashes.size > 0
    amountOfGoodCar += 1
    goodCars << i + 1
end

puts amountOfGoodCar
goodCars.each {|car| print "#{car} "}


ab = gets.split(' ')
initial1 = ab[0]
initial2 = ab[1]
n = gets.to_i()
puts "#{initial1} #{initial2}"
for i in 1..n do
  ab = gets.split(' ')
  if initial1 == ab[0]
    initial1 = ab[1]
  else
    initial2 = ab[1]
  end
  puts "#{initial1} #{initial2}"
end

#!/usr/bin/ruby
a,b=gets.split
puts [a,b]*' '
gets
$<.each{|e|
	x,y=e.split
	if a==x
		a=y
	elsif b==x
		b=y
	end
	puts [a,b]*' '
}

#在ruby里我都不需要关心字符串的长度了！
name=gets.chomp.split(" ")
a=name[0]
b=name[1]
n=gets.chomp.to_i
puts "#{a} #{b}"
n.times do
    name=gets.chomp.split(" ")
    x=name[0]
    y=name[1]
    if x==a
        puts "#{b} #{y}"
        a=y
    else
        puts "#{a} #{y}"
        b=y
    end
end

a = gets.split
puts a * ' '
gets.to_i.times{
	x, y = gets.split
	a[a.index(x)] = y
	puts a * ' '
}

p = gets.split
n = gets.to_i
ans = [p]
n.times do
  a, b = gets.split
  p = p - [a] + [b]
  ans << p
end
puts ans.map{|x| x.join(' ')}

x = gets.split
n = gets.to_i
ans = [[*x]]
n.times do
  a1, a2 = gets.split
  if x.delete(a1)
    x.push(a2)
  elsif x.delete(a2)
    x.push(a1)
  end
  ans << x.flatten
end
puts ans.map{|x| x.join(' ')}

l = gets.split
puts l * ' '
gets.to_i.times {
  a, b = gets.split
  l[(l[0] == a) ? 0 : 1] = b
  puts l * ' '
}

a, b = gets.split
print(a, ' ', b)
gets.to_i.times do
  c, d = gets.split
  if a == c
    a = d
  else
    b = d
  end
  print("\n", a, ' ', b)
end

#!/usr/bin/ruby
a,b=gets.split
gets
$<.each{|e|
	x,y=e.split
	if a==x
		a=y
	elsif b==x
		b=y
	end
}
puts [a,b]*' '


N,X,Y = gets.split.map &:to_i
A = gets.split.map &:to_i
if X > Y
  p N; exit
end

p (A.count{ |a| a <= X }+1)/2

n, x, y = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
if x > y
	puts n
else
	puts a.count { _1 <= x }.succ / 2
end

# Input
n, x, y = gets.split(" ").map { |e| e.to_i }
a = gets.split(" ").map { |e| e.to_i }

# total_moves = 10**100, and maximum_hp = 100 * 10**5 = 10**7 => If we are stronger then everything will be destroyed *evil laugh*

# Processing
d = x - y
one_hit = a.count { |n| n <= x }
case
when d > 0
  result = n
when d <= 0
  result = one_hit / 2 + one_hit % 2
end

# Output
puts result

n, x, y = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
if x > y
  puts n
  exit
end
if x == y
  puts (arr.select{|a| a <= x}.size / 2.0).ceil
  exit
end
if x < y
  puts (arr.select{|a| a <= x}.size / 2.0).ceil
  exit
end

# Input
n, k = gets.split(" ").map { |e| e.to_i }
a = gets.split(" ").map { |e| e }

# Processing
count = Hash.new(0)
current_color = Hash.new

a.each do |n|
  count[n] += 1
end

count.each do |n, amount|
  if amount > k
    puts "NO"
    #puts "#{n} amount > k"
    exit
  end
  current_color[n] = (current_color.length % k) + 1
end

c = Array.new(n) do |i|
  #puts current_color
  color = current_color[a[i]]
  current_color[a[i]] = current_color[a[i]] % k + 1
  color
end

# Output
puts "YES"
puts c.join(" ")

# Input
n, x, y = gets.split(" ").map { |e| e.to_i }
a = gets.split(" ").map { |e| e.to_i }

# total_moves = 10**100, and maximum_hp = 100 * 10**5 = 10**7 => If we are stronger then everything will be destroyed *evil laugh*

# Processing
d = x - y
one_hit = a.count { |n| n < x }
case
when d > 0
  result = n
when d <= 0
  result = one_hit / 2 + one_hit % 2
end

# Output
puts result


int,frac = gets.split('.')
if int[-1] == '9'
    puts "GOTO Vasilisa."
elsif frac[0].to_i < 5
    puts int
else
    puts int.to_i+1
end

ss = gets.chomp.split(/\./)
i = ss[0].to_i

s = ss[0]
ch = s[s.length-1]
if ch == "9" then
  puts "GOTO Vasilisa."
else
  rnd = ss[1][0].to_i
  if rnd < 5 then
    puts i
  else
    puts i+1
  end
end

n=gets
s=n.split('\n')[0].split('.')
s[0]=s[0].to_i

if s[0]%10 == 9 then
        puts "GOTO Vasilisa."
else
        if s[1].slice(0)>='5' then
                s[0]+=1
        end
        puts s[0]
end

num = gets
the_dot = num.index('.')
if num[the_dot-1] == '9'
	puts "GOTO Vasilisa."
else
	if num[the_dot+1].to_i >= 5
		puts ((num[0...the_dot].to_i)+1).to_s
	else
		puts num[0...the_dot]
	end
end

s = gets.chomp
t = s.index(".")
if s[t-1] == '9'
  puts "GOTO Vasilisa."
  exit
end
puts s[t+1] <= "4" ? s[0...t] : s[0...t].succ

a,b = gets.chomp.split('.')
if a.to_i % 10 == 9 then puts "GOTO Vasilisa."
else if b[0].to_i >= 5 then p a.to_i + 1
else p a.to_i end end

a,b=gets.split('.')

ch1, ch2 = a[-1,1], b[0,1]

if ch1!='9' && ch2<'5'
	puts a
elsif ch1!='9' && ch2>='5'
	puts a.to_i+1
else
	puts "GOTO Vasilisa."
end

def help_far_away_kingdom
  i, f = gets.split('.')

  if i[i.length-1] == '9'
    puts 'GOTO Vasilisa.'
  else
    if f.nil? or f[0].to_i < 5
      puts i.to_i
    else
      puts i.to_i+1
    end
  end
end
help_far_away_kingdom

int, frac = gets.split('.')
if int[-1] == '9'
    puts "GOTO Vasilisa."
elsif frac[0].to_i < 5
    puts int
else
    puts int.to_i+1
end

while !gets.nil?

integer, float = $_.split('.')

if integer[-1] == '9'
  puts "GOTO Vasilisa."
elsif float[0].to_i <= 4
  puts integer
else
  integer[-1] = integer[-1].succ
  puts integer
end

end

inp = gets.split('.')

int_part = inp[0]

if int_part.rindex('9') == int_part.size-1
  puts "GOTO Vasilisa."
elsif inp[1][0..0].to_i >= 5
  puts inp[0].to_i + 1
else
  puts inp[0]
end

i, f = gets.chomp.split(".")
if i[-1] == "9"
    puts "GOTO Vasilisa."
elsif f[0] < "5"
    puts i
else
    puts i.to_i+1
end

s = gets
case s
when /9\./
  puts 'GOTO Vasilisa.'
when /(\d+)\.[0-4]/
  puts $1.to_i
else
  puts s.to_i + 1
end

#!/usr/bin/ruby
s = $stdin.gets.strip.split(".")
int = s[0].to_i
flt = s[1][0].to_i
Blah = "GOTO Vasilisa."
if int % 10 == 9 then
  puts "#{Blah}"
else
  int += 1 if flt >= 5
  puts "#{int}"
end

x = gets.strip

if x =~ /9\./
	print :"GOTO Vasilisa."
else
	x =~ /(\d+)\.(\d)/
	print $2.to_i < 5 ? $1 : $1.to_i + 1
end

a,b = gets.chomp.split('.')
if a.to_i % 9 == 0 then puts "GOTO Vasilisa."
else if b[0].to_i >= 5 then p a.to_i + 1
else p a.to_i end end

def help_far_away_kingdom
  n = gets.strip
  r, ind = '', 0

  while ind < n.length
    if n[ind+1] == '.'
      if n[ind] == '9'
        puts 'GOTO Vasilisa.'
      else
        fraction = ("0.#{n[ind+2..-1]}").to_f
        if fraction < 0.5
          puts n[0..ind].to_i
        else
          puts n[0..ind].to_i + 1
        end
      end
      break
    end
    ind += 1
  end
  puts n if ind == n.length
end

help_far_away_kingdom

def help_far_away_kingdom
  n = gets.strip
  r, ind = '', 0

  while ind < n.length
    if n[ind+1] == '.'
      if n[ind] == '9'
        puts 'GOTO Vasilisa.'
      else
        fraction = ("0.#{n[ind+2..-1]}").to_f
        if fraction < 0.5
          puts n[0..ind].to_i
        else
          puts n[0..ind].to_i + 1
        end
      end
      break
    end
    ind += 1
  end
end

help_far_away_kingdom

while !gets.nil?

integer, float = $_.split('.')
if integer[-1] == '9'
  puts "GOTO Vasilisa."
elsif float[0].to_i < 4
  integer[-1] = integer[-1].succ
  puts integer
else
  puts integer
end

end

x = gets.strip
f = x.split('.')[1]
print f.count("9") == f.length ? :"GOTO Vasilisa." : x.to_f.round

x = gets.strip
l, r = x.split('.').map &:to_i

if l % 10 == 9
	print :"GOTO Vasilisa."
else
	l += 1 if r.to_s[0].to_i >= 5
	print l
end

x = gets.strip
print x[-1] == "9" ? :"GOTO Vasilisa." : x.to_f.round


def solve(cur_ram, arr1, arr2)
  merge_arr = []
  for index in 0..(arr1.length-1)
    merge_arr << [arr1[index], arr2[index]]
  end

  merge_arr.sort_by! {|x, y| x}

  # puts "merge_arr = #{merge_arr}"

  for index in 0..(merge_arr.length-1)
    break if merge_arr[index][0] > cur_ram
    cur_ram += merge_arr[index][1]
  end
  cur_ram
end

testcases = gets.chomp.to_i

for testcase in 1..testcases
  n, k = gets.chomp.split(" ").map(&:to_i)
  arr1 = gets.chomp.split(" ").map(&:to_i)
  arr2 = gets.chomp.split(" ").map(&:to_i)
  # puts "n, k, arr1, arr2 = #{[n, k, arr1, arr2]}"
  res = solve(k, arr1, arr2)
  puts res
end

t = gets.chomp.to_i

for i in (1..t)
    n, k = gets.chomp.split.map {|e| e.to_i}
    a = gets.chomp.split.map {|e| e.to_i}
    b = gets.chomp.split.map {|e| e.to_i}
    c = (a.zip b).sort
    for j in (0..n-1)
        if k >= c[j][0]
            k += c[j][1]
        end
    end
    puts k
end

gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  abs = gets.split.map(&:to_i).zip(gets.split.map(&:to_i)).sort_by(&:first)
  abs.each do |a, b|
    break if k < a
    k += b
  end
  p k
end


n,*a=$<.read.split.map &:to_i;(n/2).times{|i|a[i],a[n-i-1]=a[n-i-1],a[i]if i.even?};puts a*' '

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

(0...(n / 2)).each do |i|
  next if i.odd?
  t = a[i]
  a[i] = a[n - i - 1]
  a[n - i - 1] = t
end

puts a.join(" ")

TEST_MODE = ARGV[0] == 't'

def evaluate(actual, expected); actual == expected ? "OK" : "**NG**"; end
def tests(); yield if TEST_MODE; end
def to_array(val); val.kind_of?(Array) ? val : [val]; end
def parse_int_array(str); str.split(" ").collect{|s| s.to_i}; end

def t(input, expected_output)
  input = to_array(input)
  expected_output = to_array(expected_output)
  output = []
  solve(input, output)
  puts "#{evaluate(output, expected_output)} : input=#{input.to_s}, expected=#{expected_output.to_s}, actual=#{output.to_s}"
end

def solve(input, output)
  arr = parse_int_array(input[1])
  (0..arr.size/2-1).step(2) {|i|
    temp = arr[i]
    arr[i] = arr[arr.size-i-1]
    arr[arr.size-i-1] = temp
  }
  output << arr.join(" ")
end

tests do
  # test cases
  t(["7","4 3 7 6 9 1 2"], "2 3 9 6 7 1 4")
  t(["8","6 1 4 2 5 6 9 2"], "2 1 6 2 5 4 9 6")
end

puts solve(STDIN.read.split("\n"), []) unless TEST_MODE

n=gets.to_i
s=gets.chomp.split
(s.size/2).times{|i|
	if i%2==0 then
		s[i],s[n-i-1]=s[n-i-1],s[i]
	end
}
puts s*" "

n = gets.to_i
a = gets.split.map(&:to_i)
c = n / 2 + (n.even? ? 0 : 1) - 1
ans = []
c.times do |i|
  if i % 2 == 0
    ans[i] = a[n - 1 - i]
    ans[n - 1 - i] = a[i]
  else
    ans[i] = a[i]
    ans[n - 1 - i] = a[n - 1 - i]
  end
end
a2 = a[c..(n - 1 - c)]
a2.reverse!  if c % 2 == 0
ans[c..(n - 1 - c)] = a2
puts ans.join(' ')

n = gets.to_i
a = gets.split.map(&:to_i)
(n / 2).times do |i|
  a[i], a[-1 - i] = a[-1 - i], a[i] if i % 2 == 0
end
puts a.join(' ')

n=gets.to_i
a=gets.split.map &:to_i
for i in 0 ... n / 2
  if i % 2 == 0
    tmp = a[i]
    a[i] = a[n - 1 - i]
    a[n - 1 - i] = tmp
  end
end
puts a*' '


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

def main
  gi.times do
    n = gi
    d = 1
    s = 0
    c = 0
    while n > s
      s += d
      c += 1
      d += 2
    end
    puts c
  end
end

main

t = gets.chomp.to_i

t.times do
  n = gets.chomp.to_i
  ans = 0
  curr_max = 1
  sum = 0
  while sum < n do
    ans += 1
    n -= curr_max
    curr_max += 2
  end

  ans += 1 if n > 0

  puts "#{ans}\n"
end


T=gets().to_i
line=gets().split(" ").map!{|x|x.to_i}
sum=(T*(T+1))/2
sb=line.inject(:+)
puts sum-sb

n=gets.to_i
mas=gets.split.map &:to_i
puts (n*(n+1)/2) - mas.inject(:+)


regs = [/(\w+) posted on (\w+)'s wall/,
        /(\w+) commented on (\w+)'s post/,
        /(\w+) likes (\w+)'s post/]

myname = gets.chomp
action = gets.to_i
player = {}
action.times do |i|
  tmp = gets
  if tmp =~ regs[0]
 #  p $1,$2
    if myname == $1
      if myname != $2
        player.store($2,0) if player[$2] == nil
        player[$2] += 15
      end
    elsif myname == $2
      if myname != $1
        player.store($1,0) if player[$1] == nil
        player[$1] += 15
      end
    else
      player.store($1,0) if player[$1] == nil
      player.store($2,0) if player[$2] == nil
    end
  elsif tmp =~ regs[1]
#   p $1,$2
    if myname == $1
      if myname != $2
        player.store($2,0) if player[$2] == nil
        player[$2] += 10
      end
    elsif myname == $2
      if myname != $1
        player.store($1,0) if player[$1] == nil
        player[$1] += 10
      end
    else
      player.store($1,0) if player[$1] == nil
      player.store($2,0) if player[$2] == nil
    end
  elsif tmp =~ regs[2]
#   p $1,$2
    if myname == $1
      if myname != $2
        player.store($2,0) if player[$2] == nil
        player[$2] += 5
      end
    elsif myname == $2
      if myname != $1
        player.store($1,0) if player[$1] == nil
        player[$1] += 5
      end
    else
      player.store($1,0) if player[$1] == nil
      player.store($2,0) if player[$2] == nil
    end
  end
end
values = player.values
keys   = player.keys
while true do
    maxary = []
    max = values.max
    while true do
        maxpos = values.index(max)
        maxary << keys[maxpos].dup
        #p max,maxpos,maxary
        values.delete_at(maxpos)
        keys.delete_at(maxpos)
        break if values == []
        break if max > values.max
    end
    maxary.map{|item| item.extend Comparable}
    sorted = maxary
    sorted = maxary.sort if maxary.size > 1

    puts sorted
    break if values == []
end

my_name = STDIN.gets.strip
n = STDIN.gets.strip.to_i
actions = []
n.times do
  actions.push STDIN.gets.strip
end
persons = {}
regexps = [/(.*) posted on (.*)'s wall/,/(.*) commented on (.*)'s post/,/(.*) likes (.*)'s post/]
points = [15,10,5]
actions.each do |action|
  3.times do |i|
    if match = regexps[i].match(action)
      if match[1] == my_name || match[2] == my_name
        if persons[match[1]]
          persons[match[1]] += points[i]
        else
          persons[match[1]] = points[i]
        end
        if persons[match[2]]
          persons[match[2]] += points[i]
        else
          persons[match[2]] = points[i]
        end
      else
        if !persons[match[1]]
          persons[match[1]] = 0
        end
        if !persons[match[2]]
          persons[match[2]] = 0
        end
      end
    end
  end
end
persons.delete(my_name)
sorted = persons.to_a.sort_by{|x| [-x[1],x[0]]}
sorted.each do |person|
  puts person[0]
end

me = gets.chomp

map = Hash.new {|hash, key| hash[key] = 0 }

(gets.to_i).times do
  line = gets.chomp.split
  x = line[0]
  y = nil
  points = 0
  if line[1] == "posted"
    points = 15
    y = line[3]
  elsif line[1] == "commented"
    points = 10
    y = line[3]
  elsif line[1] == "likes"
    points = 5
    y = line[2]
  end
  y = y[0..y.size - 3]
  if x == me
    map[y] += points
  elsif y == me
    map[x] += points
  else
    map[y] += 0
    map[x] += 0
  end
end

(map.sort {|a, b| if a[1] == b[1]
   a[0] <=> b[0] else b[1] <=> a[1] end }).each do |m|
    puts m[0]
end

my_name = gets.chomp
names = Hash.new(0)
gets.to_i.times do
  gets.chomp =~ /^(\w+) (posted on|commented on|likes) (\w+)'s (wall|post)$/
  n1, n2 = $1, $3
  other_name = if n1 == my_name
    n2
  elsif n2 == my_name
    n1
  end
  if other_name
    point = case $2
    when "posted on"
      15
    when "commented on"
      10
    else # "likes"
      5
    end
    names[other_name] += point
  else
    names[n1] += 0
    names[n2] += 0
  end
end

names.sort{|a,b| (b[1] <=> a[1]).nonzero? || a[0] <=> b[0] }.each{|n| puts n[0]}

h = Hash.new(0)
u = gets.chomp
gets.to_i.times do
	$_ = gets
	if /(\w+) posted on (\w+)'s wall/
		x, y, z = $1, $2, 15
	elsif /(\w+) commented on (\w+)'s post/
		x, y, z = $1, $2, 10
	elsif /(\w+) likes (\w+)'s post/
		x, y, z = $1, $2, 5
	else
		warn 555
	end
	if x == u
		h[y] += z
	elsif y == u
		h[x] += z
	else
		h[x] += 0
		h[y] += 0
	end
end
puts h.map{|k, v| [-v, k]}.sort.map{|a, b| b} * "\n"

MYNAME = gets.chomp
N = gets.to_i
persons = Hash.new(0)
N.times do
  str = gets.split(' ')
  add = 0
  x = str[0]
  y = str[3]
  if (str[1] == "posted")
    add = 15
  elsif (str[1] == "commented")
    add = 10
  else
    add = 5
    y = str[2]
  end
  y.gsub!("'s", "")
  if (x != MYNAME && y != MYNAME) # Priority factor according to me!
    add = 0 # but we have to print ALL persons
  end
  persons[x] += add
  persons[y] += add
end

result = persons.to_a.sort{|l, r|
  if (l[1] != r[1])
    r[1] <=> l[1]
  else
    l[0] <=> r[0]
  end
}

#p result

result.each {|e|
  puts e[0] if (e[0] != MYNAME)
}

name = gets.chomp
n = gets.to_i
priority = Hash.new(0)
n.times do
	v = gets.chomp.downcase
	arr = v.split(" ")
	a = arr[0]
	p = 0
	if arr[1].eql?("posted")
		p = 15
		b = arr[3]
	elsif arr[1].eql?("commented")
		p = 10
		b = arr[3]
	else
		p = 5
		b = arr[2]
	end
	b = b.sub("'s","")
	p = 0 unless (a.eql?(name) || b.eql?(name))
	priority[a] += p
	priority[b] += p
end

priority.keys.sort{|k1,k2| priority[k1] != priority[k2] ? -(priority[k1] <=> priority[k2]) : k1 <=> k2}.each do |k|
	puts k unless k.eql?(name)
end

regs = [/(\w+) posted on (\w+)'s wall/,
        /(\w+) commented on (\w+)'s post/,
        /(\w+) likes (\w+)'s post/]

myname = gets.chomp
action = gets.to_i
player = {}
action.times do |i|
  tmp = gets
  if tmp =~ regs[0]
 #  p $1,$2
    if myname == $1
      if myname != $2
        player.store($2,0) if player[$2] == nil
        player[$2] += 15
      end
    elsif myname == $2
      if myname != $1
        player.store($1,0) if player[$1] == nil
        player[$1] += 15
      end
    else
      player.store($1,0) if player[$1] == nil
      player.store($2,0) if player[$2] == nil
    end
  elsif tmp =~ regs[1]
#   p $1,$2
    if myname == $1
      if myname != $2
        player.store($2,0) if player[$2] == nil
        player[$2] += 10
      end
    elsif myname == $2
      if myname != $1
        player.store($1,0) if player[$1] == nil
        player[$1] += 10
      end
    else
      player.store($1,0) if player[$1] == nil
      player.store($2,0) if player[$2] == nil
    end
  elsif tmp =~ regs[2]
#   p $1,$2
    if myname == $1
      if myname != $2
        player.store($2,0) if player[$2] == nil
        player[$2] += 5
      end
    elsif myname == $2
      if myname != $1
        player.store($1,0) if player[$1] == nil
        player[$1] += 5
      end
    else
      player.store($1,0) if player[$1] == nil
      player.store($2,0) if player[$2] == nil
    end
  end
end
values = player.values
keys   = player.keys

while true do
    while true do
        maxary = []
        max = values.max
        maxpos = values.index(values.max)
        maxary << keys[maxpos]
#       p max,maxpos,maxary
        values.delete_at(maxpos)
        keys.delete_at(maxpos)
        break if max > values.max.to_i
    end
    #maxary.sort_by!{|a,b| b <=> a} if maxary.size > 1
#   puts maxary
    break if values == []
end

MYNAME = gets.chomp
N = gets.to_i
persons = Hash.new(0)
N.times do
  str = gets.split(' ')
  add = 0
  x = str[0]
  y = str[3]
  if (str[1] == "posted")
    add = 15
  elsif (str[1] == "commented")
    add = 10
  else
    add = 5
    y = str[2]
  end
  y.gsub!("'s", "")
  persons[x] += add
  persons[y] += add
end

result = persons.to_a.sort{|l, r|
  if (l[1] != r[1])
    r[1] <=> l[1]
  else
    l[0] <=> r[0]
  end
}

#p result

result.each {|e|
  puts e[0] if (e[0] != MYNAME)
}

name = gets.chomp
n = gets.to_i
priority = Hash.new(0)
n.times do
	v = gets.chomp.downcase
	arr = v.split(" ")
	a = arr[0]
	p = 0
	if arr.include?("posted")
		p = 15
		b = arr[3]
	elsif arr.include?("commented")
		p = 10
		b = arr[3]
	else
		p = 5
		b = arr[2]
	end
	b = b.sub("'s","")
	p = 0 unless (a.eql?(name) || b.eql?(name))
	priority[a] += p
	priority[b] += p
end

priority.keys.sort{|k1,k2| priority[k1] != priority[k2] ? -(priority[k1] <=> priority[k2]) : k1 <=> k2}.each do |k|
	puts k unless k.eql?(name)
end

name = gets.chomp
n = gets.to_i
priority = Hash.new(0)
n.times do
	v = gets.chomp.downcase
	arr = v.split(" ")
	a = arr[0]
	p = 0
	if arr.include?("posted")
		p = 15
		b = arr[3]
	elsif arr.include?("commented")
		p = 10
		b = arr[3]
	else
		p = 5
		b = arr[2]
	end
	b = b.sub("'s","")
	p = 0 unless (a.include?(name) || b.include?(name))
	priority[a] += p
	priority[b] += p
end

priority.keys.sort{|k1,k2| priority[k1] != priority[k2] ? -(priority[k1] <=> priority[k2]) : k1 <=> k2}.each do |k|
	puts k unless k.eql?(name)
end

me = gets.chomp

map = Hash.new {|hash, key| hash[key] = 0 }

(gets.to_i).times do
  line = gets.chomp.split
  x = line[0]
  y = nil
  points = 0
  if line[1] == "posted"
    points = 15
    y = line[3]
  elsif line[1] == "commented"
    points = 10
    y = line[3]
  elsif line[1] == "likes"
    points = 5
    y = line[2]
  end
  y = y[0..y.size - 3]
  map[x] += points
  map[y] += points
end

(map.sort {|a, b| if a[1] == b[1]
   a[0] <=> b[0] else b[1] <=> a[1] end }).each do |m|
  if m[0] != me
    puts m[0]
  end
end

my_name = gets.chomp
names = Hash.new(0)
gets.to_i.times do
  gets.chomp =~ /^(\w+) (posted on|commented on|likes|) (\w+)'s (wall|post)$/
  n1, n2 = $1, $3
  other_name = if n1 == my_name
    n2
  elsif n2 == my_name
    n1
  end
  if other_name
    point = case $2
    when "posted on"
      15
    when "commented on"
      10
    else # "likes"
      5
    end
    names[other_name] += 5
  else
    names[n1] += 0
    names[n2] += 0
  end
end

names.sort{|a,b| (b[1] <=> a[1]).nonzero? || a[0] <=> b[0] }.each{|n| puts n[0]}


times = gets.to_i

def resolve(binary_s)
  game_a = binary_s.split("0").map(&:size).sort{|a,b| b <=> a}
  game_a.delete(0)
  alice_points = 0
  game_a.each_with_index {|e,i| alice_points += e if i % 2 == 0}
  alice_points
end

(1..times).each do |i|
  s = gets.chomp
  puts resolve(s)
end

gets.to_i.times do
  puts gets.chomp.split(/0+/).map(&:length).sort{ |a, b| b <=> a }.select.each_with_index{|_, index| index.even?}.sum
end

t = gets.chomp.to_i
t.times do
    s = gets.chomp.split("")
    ookisa = s.size
    ans = 0
    count = 0
    arr = []
    (0..ookisa-1).each_with_index do |item,i|
        if item == "1" && s[i] == "1" && s[i-1] == "1" && i != 0
            count += 1
        elsif s[i] == "1"
            count += 1
        elsif s[i] == "0"
            arr << count
            count = 0
        end
        if ans <= count
            ans = count
            point = i
        end
    end
    arr << count
    arr = arr.sort
    arisa = 0
    bob = 0
    while true
        if arr != []
            arisa += arr.pop
        else
            break
        end
        if arr != []
            bob += arr.pop
        else
            break
        end
    end
    puts arisa
end

t = gets.chomp.to_i
t.times {
  str = gets.chomp
  index = -1
  (0...str.size).each do |ind|
    next if str[ind] == '0'

    index = ind
    break
  end

  list = []
  count = 1

  if index == -1
    puts '0'
    next
  end
  (index+1...str.size).each do |ind|
    if str[ind] == '1'
      count += 1
    else
      list << count if count != 0
      count = 0
    end
  end

  list << count if count != 0
  list.sort_by! {|ele| -1 * ele}

  ans = 0
  i = 0
  while ( i < list.size ) do
    ans += list[i]
    i += 2
  end
  puts ans
}

t = gets.to_i
ans = []
t.times do
  a = gets.chomp.split("").map(&:to_i)
  arr = []
  cnt = 0
  a.each do |v|
    if v == 1
      cnt += 1
    else
      if cnt > 0
        arr << cnt
        cnt = 0
      end
    end
  end
  if cnt > 0
    arr << cnt
  end
  arr = arr.sort.reverse
  len = arr.length
  value = 0
  len.times do |i|
    value += arr[i] if i%2 == 0
  end
  ans << value
end
ans.each do |x|
  puts x
end

gets.to_i.times do
  puts gets.split(/0+/).map(&:length).sort{ |a, b| b <=> a }.select.each_with_index{|_, index| index.even?}.sum
end

t = gets.chomp.to_i
t.times {
  str = gets.chomp
  index = -1
  (0...str.size).each do |ind|
    next if str[ind] == '0'

    index = ind
    break
  end

  list = []
  count = 1

  if index == -1
    puts '0'
    next
  end

  (index+1...str.size).each do |ind|
    if str[ind] == '1'
      count += 1
    else
      list << count if count != 0
      count = 0
    end
  end

  list << count if count != 0
  list.sort_by {|ele| -1 * ele}

  ans = 0
  i = 0
  while ( i < list.size ) do
    ans += list[i]
    i += 2
  end
  puts ans
}


at_exit do
  task_count = get_numbers_from_line[0]
  tasks = []
  task_count.times do
    t,c = get_numbers_from_line[0..1]
    tasks << {t: t, sms_count: c}
  end

  time = 0
  max_queue_length = 0
  task_in_queue_count = 0
  while true
    next_task = tasks.shift
    if next_task != nil
      dt = next_task[:t] - time
      task_in_queue_count = [0, task_in_queue_count - dt].max
      task_in_queue_count += next_task[:sms_count]
      max_queue_length = task_in_queue_count if task_in_queue_count > max_queue_length
      time += dt
    elsif task_in_queue_count != 0
      time += task_in_queue_count
      break
    else
      break
    end
  end

  puts [time, max_queue_length].join(' ')
end

def get_numbers_from_line
  gets.strip.split.map(&:to_i)
end

num_tasks = gets.to_i
tasks = Array.new
for i in 0...num_tasks
  tasks << gets.split.collect{ |n| n.to_i }
end
current_task = tasks.shift
t = current_task[0]
to_send = current_task[1]
max_queries = to_send
if tasks.length < 1
  puts (t+to_send).to_s + " " + max_queries.to_s
  exit
end
current_task = tasks.shift
while true
  t = t+1
  to_send -= 1 if to_send > 0
  if t == current_task[0]
    to_send += current_task[1]
    if to_send > max_queries
      max_queries = to_send
    end
    if tasks.length < 1
      break
    end
    current_task = tasks.shift
  end
end
puts (t+to_send).to_s + " " + max_queries.to_s

class A
  def initialize
    n = $stdin.gets.chomp.to_i
    queue_size = 0
    b_time = 0
    next_time = 0
    max_size = 0

    n.times do
      time, task = $stdin.gets.chomp.split(' ').map(&:to_i)
      d_time = time - b_time

      queue_size = [0, queue_size-d_time].max

      b_time = time

      queue_size += task

      max_size = queue_size if max_size < queue_size
    end

    puts "#{b_time+queue_size} #{max_size}"
  end
end

a = A.new

n = gets.to_i

tc = n.times.map{ gets.split.map(&:to_i) }

t = 1
max = 0
prev = [0, 0]

tc.each do |i|
  t = (t < i[0] ? i[0] : t) + i[1]
  m = prev[1] + prev[0] - i[0]
  m = m > 0 ? m + i[1] : i[1]
  max = m if max < m
  prev = [i[0], m]
end

puts "#{t} #{max}"

n = gets.chomp.to_i

times = []
messages_count = []

max_queue = 1
queue = 0

ftime = true

r_queue = 0

n.times do |i|
  input = gets.chomp.split(" ")

  times << input[0].to_i
  messages_count << input[1].to_i

  if ftime == false
    queue = queue - (input[0].to_i - times[i-1])
  end

  if queue < 0
    queue = 0
  end

  queue += input[1].to_i

  if queue > max_queue
    max_queue = queue
  end

  r_queue = queue

  if ftime
    ftime = false
  end
end

time = times.last + r_queue

puts "#{time} #{max_queue}"

n = gets.chomp.to_i

times = []
messages_count = []

max_queue = 1
queue = 0

r_queue = 0

n.times do
  input = gets.chomp.split(" ")

  times << input[0].to_i
  messages_count << input[1].to_i

  queue += input[1].to_i

  r_queue = queue

  if queue > max_queue
    max_queue = queue
  end

  queue -= input[0].to_i
  if queue < 0
    queue = 0
  end
end

time = times.last + r_queue

puts "#{time} #{max_queue}"

n = gets.chomp.to_i

times = []
messages_count = []

max_queue = 1
queue = 0

n.times do
  input = gets.chomp.split(" ")
  times << input[0].to_i
  messages_count << input[1].to_i
  queue += input[1].to_i

  if queue > max_queue
    max_queue = queue
  end

  queue -= 1
end

time = times.first + messages_count.inject(:+)

puts "#{time} #{max_queue}"

n = gets.chomp.to_i

times = []
messages_count = []

max_queue = 1
queue = 0

r_queue = 0

n.times do |i|
  input = gets.chomp.split(" ")

  times << input[0].to_i
  messages_count << input[1].to_i

  queue += input[1].to_i

  r_queue = queue

  if queue > max_queue
    max_queue = queue
  end

  queue = queue - (input[0].to_i - times[i-1])

  if queue < 0
    queue = 0
  end
end

time = times.last + r_queue

time -= 1 if times.size != 1
max_queue -= 1 if times.size != 1

puts "#{time} #{max_queue}"