def solution
  n = gets.chomp.to_i
  data = []
  n.times do
    input = gets.chomp
    if input.size >= 2 && input[-2..-1] == 'po'
      data.push 'FILIPINO'
    elsif input.size >= 4 && input[-4..-1] == 'desu' || input[-4..-1] == 'masu'
      data.push 'JAPANESE'
    elsif input.size >= 5 && input[-5..-1] == 'mnida'
      data.push 'KOREAN'
    end
  end
  data.each { |e| puts e }
end

solution

test=gets.strip.to_i

while test != 0
		n_str=gets.strip
		str = n_str.reverse();
		if str[0] == 'o' && str[1] == 'p'
			puts "FILIPINO"
		elsif str[0] == 'a' && str[1] == 'd' && str[2] == 'i' && str[3] == 'n' && str[4] == 'm'
			puts "KOREAN"
		else
			puts "JAPANESE"
		end
		test -= 1
end

T=gets.chomp.to_i
T.times do |x|
    string = gets.chomp
    puts "FILIPINO" if string.end_with? "po"
    puts "JAPANESE" if string.end_with? "masu"
    puts "JAPANESE" if string.end_with? "desu"
    puts "KOREAN" if string.end_with? "mnida"
end

t = gets.to_i
t.times do
	s = gets.chomp.reverse!
	if s.slice(0,2) == 'op'
		puts "FILIPINO"
	elsif ["used", "usam"].include?s.slice(0,4)
		puts "JAPANESE"
	else
		puts "KOREAN"
	end
end

N=STDIN.gets.strip.to_i
(0..N-1).each do |i|
  s=STDIN.gets.strip
  if s =~ /po$/
    puts 'FILIPINO'
  elsif s =~ /desu$/ || s =~ /masu$/
    puts 'JAPANESE'
  elsif s =~ /mnida$/
    puts 'KOREAN'
  end
end

number = gets.chomp.to_i
cases = []
number.times { cases << gets.chomp }
for line in cases
	if line[-5..-1] == 'mnida'
		puts("KOREAN")
	elsif line[-4..-1] == 'desu' || line[-4..-1] == 'masu'
		puts("JAPANESE")
	elsif line[-2..-1] == 'po'
		puts("FILIPINO")
	else
		puts("Just for safety, ya know :3")
	end
end

gets.to_i.times do
    s = gets.chomp
    if s[-1] == "o"
        puts "FILIPINO"
    elsif s[-1] == "a"
        puts "KOREAN"
    elsif s[-1] == "u"
        puts "JAPANESE"
    end
end

nb = gets.to_i
nb.times do
  case gets.chomp[-1]
  when 'a'
    puts 'KOREAN'
  when 'u'
    puts 'JAPANESE'
  when 'o'
    puts 'FILIPINO'
  end
end

words = []
gets.chomp().to_i.times do |i|
  words << gets.chomp().to_s #input
end
words.length().times do |i|
  if(words[i][-1].eql? "o")
    puts "FILIPINO"
  elsif(words[i][-1].eql? "u")
    puts "JAPANESE"
  else
    puts "KOREAN"
  end
end

gets.to_i.times do
  s = gets.chop
  puts 'FILIPINO' if s[-2..-1] == 'po'
  puts 'JAPANESE' if s[-4..-1] == 'masu' or s[-4..-1] == 'desu'
  puts 'KOREAN' if s[-5..-1] == 'mnida'
end

gets.to_i.times do
  s = gets.chomp
  puts 'FILIPINO' if s[-2..-1] == 'po'
  puts 'JAPANESE' if s[-4..-1] == 'masu' or s[-4..-1] == 'desu'
  puts 'KOREAN' if s[-5..-1] == 'mnida'
end

if ARGV[0]=="LOCAL_DEV"
  define_method("debug") do |var|
    p var
  end
else
  define_method("debug") do |var|
  end
end

N=STDIN.gets.strip.to_i
(0..N-1).each do |i|
  s=STDIN.gets.strip
  if s =~ /^.+?po$/
    puts 'FILIPINO'
  elsif s =~ /^.+?desu$/ || s =~ /^.+?masu$/
    puts 'JAPANESE'
  elsif s =~ /^.+?mnida$/
    puts 'KOREAN'
  end
end


def solve n
  first = (1...(n/2)).to_a + [n]
  first = first.flatten
  second = ((n/2)...n).to_a
  a = first.map {|i| 2**i}.reduce(:+)
  b = second.map {|i| 2**i}.reduce(:+)
  (a-b).abs
end

t = STDIN.gets.chomp.to_i

t.times do |i|
  n = STDIN.gets.chomp.to_i
  res = solve n
  puts res
end

# require 'pry-byebug'

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN
t = file.gets.chomp.to_i

t.times do
  n = file.gets.chomp.to_i

  a = 2**n
  min = 2**(n/2)-2

  b = 2**(n-1)
  min2 = 2**(n-1)-2-min

  puts (a+min)-(b+min2)
end

pw2 = Array.new(32,0);
pw2[0]=1
for i in 1..31
    pw2[i]=pw2[i-1]*2
end

t = gets.chomp.to_i
while t>0 do
    n = gets.chomp.to_i
    sum = 0
    for i in 1..(n/2-1)
        sum+=pw2[i]
    end
    sum+=pw2[n]
    puts sum - (pw2[n+1]-sum-2)
    t-=1

end

n = gets.to_i

n.times {
  v = gets.to_i
  puts 2 ** (v / 2 + 1) - 2
}

def min_weigth(n)
  f1 = (2 ** (n/2)) - 2
  (f1 + (2 ** n)) - ((2 ** n) - 2 - f1)
end

t = gets.chomp.to_i

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

# Phoenix has 𝑛 coins with weights 21,22,…,2𝑛. He knows that 𝑛 is even.

# He wants to split the coins into two piles such that each pile has exactly 𝑛2
# coins and the difference of weights between the two piles is minimized.
# Formally, let 𝑎 denote the sum of weights in the first pile, and 𝑏 denote the
# sum of weights in the second pile. Help Phoenix minimize |𝑎−𝑏|, the absolute
# value of 𝑎−𝑏.

# Input
# The input consists of multiple test cases. The first line contains an integer
# 𝑡 (1≤𝑡≤100) — the number of test cases.

# The first line of each test case contains an
# integer 𝑛 (2≤𝑛≤30; 𝑛 is even) — the number of coins that Phoenix has.

# Output
# For each test case, output one integer — the minimum possible difference of
# weights between the two piles.

# Example
# inputCopy
# 2
# 2
# 4
# outputCopy
# 2
# 6
# Note
# In the first test case, Phoenix has two coins with weights 2 and 4. No matter
# how he divides the coins, the difference will be 4−2=2.

# In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16.
# It is optimal for Phoenix to place coins
# with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another
# pile. The difference is (2+16)−(4+8)=6.

def phoenix(queries)
  queries.each do |query|
    query = query.split(' ')

    return 0 unless query.length == 1

    stack_a = 0
    stack_a_count = 0
    stack_b = 0
    stack_b_count = 0
    input_n = query.first.to_i
    half = input_n / 2

    (input_n).downto(1) do |operand|
      product = 2 ** operand

      if stack_a_count == half
        stack_b += product
        stack_b_count += 1
        next
      end

      if stack_b_count == half
        stack_a += product
        stack_a_count += 1
        next
      end

      if stack_a > stack_b
        stack_b += product
        stack_b_count += 1
      else
        stack_a += product
        stack_a_count += 1
      end
    end

    puts (stack_a - stack_b).abs
  end

  return nil
end

# Codeforces required setup
number_of_queries = gets.rstrip.to_i

queries = []

number_of_queries.times do |query|
  queries << gets.rstrip
end

result = phoenix queries
puts result

pw2 = Array.new(32,0);
pw2[0]=1
for i in 1..31
    pw2[i]=pw2[i-1]*2
end

t = gets.chomp.to_i
while t>0 do
    n = gets.chomp.to_i
    sum = 0
    for i in i..(n/2-1)
        sum+=pw[i]
    end
    sum+=pw2[n]

    puts sum - (pw2[n+1]-sum-2)
    t-=1

end

def min_weigth(n)
  f1 = (2 ** (n/2)) - 2
  (f1 + (2 ** n)) - ((2 ** n) - 2 - f1)
end

t = gets.chomp.to_i

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

# Phoenix has 𝑛 coins with weights 21,22,…,2𝑛. He knows that 𝑛 is even.

# He wants to split the coins into two piles such that each pile has exactly 𝑛2 coins and the difference of weights
# between the two piles is minimized. Formally, let 𝑎 denote the sum of weights in the first pile, and 𝑏 denote the sum of
# weights in the second pile. Help Phoenix minimize |𝑎−𝑏|, the absolute value of 𝑎−𝑏.

# Input
# The input consists of multiple test cases. The first line contains an integer 𝑡 (1≤𝑡≤100) — the number of test cases.

# The first line of each test case contains an integer 𝑛 (2≤𝑛≤30; 𝑛 is even) — the number of coins that Phoenix has.

# Output
# For each test case, output one integer — the minimum possible difference of weights between the two piles.

# Example
# inputCopy
# 2
# 2
# 4
# outputCopy
# 2
# 6
# Note
# In the first test case, Phoenix has two coins with weights 2 and 4. No matter how he divides the coins, the difference will be 4−2=2.

# In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16. It is optimal for Phoenix to place coins
# with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)−(4+8)=6.

def phoenix(queries)
  queries.each do |query|
    query = query.split(' ')

    return 0 unless query.length == 1

    stack_a = 0
    stack_a_count = 0
    stack_b = 0
    stack_b_count = 0
    n = query.first.to_i

    (n).downto(1) do |operand|
      product = 2 ** operand

      if stack_b > stack_a
        if stack_a_count > stack_b_count
          stack_b_count += 1
          stack_b += product
          next
        end

        stack_a_count += 1
        stack_a += product
      else
        if stack_b_count > stack_a_count
          stack_a_count += 1
          stack_a += product
          break
        end

        stack_b_count += 1
        stack_b += product
      end
    end

    puts (stack_a - stack_b).abs
  end

  return nil
end

# Codeforces required setup
number_of_queries = gets.rstrip.to_i

queries = []

number_of_queries.times do |query|
  queries << gets.rstrip
end

result = phoenix queries
puts result


n,x=gets.split.map &:to_i
mas=gets.split.map(&:to_i).sort
k=0
mas.each { |m| k+=m*x; x-=1 if x!=1 }
puts k

#!/usr/bin/ruby
n,x=gets.split.map(&:to_i)
r=0
gets.split.map(&:to_i).sort.each_with_index{|e,i|
	r+=[x-i,1].max*e
}
p r

n,m=gets.split.map{|e| e.to_i}
ans=0
gets.split.map{|e| e.to_i}.sort.each{|e|
	ans+=e*m
	m-=1
	m=1 if m<1
}
puts ans

num = gets.chomp.split
n = Integer(num[0])
x = Integer(num[1])
nums = gets.chomp.split
c = []
for i in 0...n
    c[i] = Integer(nums[i])
end

c.sort!
time = 0
for i in 0...n
    time += c[i] * x
    if x > 1
        x -= 1
    end
end
puts time

def read_ints; STDIN.gets.split.map(&:to_i); end
n,x = read_ints
a = read_ints
a.sort!
i=0
res = 0
n.times do
  res+=a[i]*x
  j = [x-1, 1]
  x = j.max
  i+=1
end
puts res

n, x = gets.split.map(&:to_i)
c = gets.split.map(&:to_i).sort
result = 0
c.each do |ci|
  result += ci * x
  x -= 1 if x > 1
end
puts result

n,x = gets.split.map(&:to_i)
c = gets.split.map(&:to_i).sort

puts c.inject([x,0]) { |now, subjects|
  [[1, now[0]-1].max, now[1] + now[0] * subjects]
}[1]

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

n, x = gets.chomp.split.map(&:to_i)
c = gets.chomp.split.map(&:to_i).sort

min = 0
for i in 0..(n - 1)
  min += x * c[i]
  x -= 1 if x > 1
end

p min

class DumbGuy
  n,x = gets.chomp.split(/\s+/).map{|o| o.to_i}
  array = gets.chomp.split(/\s+/).map{|o| o.to_i}
  array.sort!
  res = 0
  for i in 0..n-1
    res += array[i] * x
    x = x - 1 < 1 ? 1 : x - 1
  end
  puts res
end


I =->{gets.split.map &:to_i}
n,x=I[]; a=I[]; b=I[]
a.sort!; b.sort!; r = n - 1; a.each{|ai|
	r -= 1 if ai + b[r] >= x
}
puts [1,n-r-1]*' '

n,x=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
a.sort!; b.sort!; r = n - 1; a.each{|ai|
	r -= 1 if ai + b[r] >= x
}
puts [1,n-r-1]*' '

n, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
a.sort! { |i, j| j <=> i }
b.sort!
worst = 0
j = 0;
for i in 0..a.size do
  while j < b.size && a[i] + b[j] < x do
    j = j + 1
  end
  worst += 1 if j < b.size
  j = j + 1
end

puts "1 #{worst}"

n,x=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
a.sort!;b.sort!;
z=0;n-=1;a.each{|_|
	if _+b[n]>=x then
		z+=1;n-=1;
	end
}
puts [1,z]*' '


def solve(first_sec, second_sec, k)
  first_ptr = 0
  second_ptr = 0
  solution = []
  while first_ptr < first_sec.size && second_ptr < second_sec.size do
    if first_sec[first_ptr] < second_sec[second_ptr]
      return -1 if first_sec[first_ptr] > k
      solution << first_sec[first_ptr]
      first_ptr += 1
    else
      return -1 if second_sec[second_ptr] > k
      solution << second_sec[second_ptr]
      second_ptr += 1
    end
    k += 1 if solution[-1] == 0
  end

  while first_ptr < first_sec.size do
    return -1 if first_sec[first_ptr] > k
    solution << first_sec[first_ptr]
    first_ptr += 1
    k += 1 if solution[-1] == 0
  end

  while second_ptr < second_sec.size do
    return -1 if second_sec[second_ptr] > k
    solution << second_sec[second_ptr]
    second_ptr += 1
    k += 1 if solution[-1] == 0
  end

  solution.join(' ')
end

t = gets.chomp.to_i
t.times do
  gets
  k, _ = gets.chomp.split(' ').map(&:to_i)
  first_sec = gets.chomp.split(' ').map(&:to_i)
  second_sec = gets.chomp.split(' ').map(&:to_i)

  puts "#{solve(first_sec, second_sec, k)}\n"
end

gets.to_i.times {
  gets
  k, n, m = gets.split.map!(&:to_i)
  as = gets.split.map!(&:to_i)
  bs = gets.split.map!(&:to_i)
  xs = []
  i = 0; j = 0
  while i < n || j < m
    if i < n && (as[i] <= k)
      xs << as[i]
      k += 1 if as[i] == 0
      i += 1
    elsif j < m && (bs[j] <= k)
      xs << bs[j]
      k += 1 if bs[j] == 0
      j += 1
    else
      break
    end
  end

  if i == n && j == m
    puts xs.join(' ')
  else
    p -1
  end
}


tmp=gets.split
n=tmp[0].to_i
m=tmp[1].to_i
c=gets.split
a=gets.split
i=0
j=0
ans=0
while i<n and j<m
	#puts "#{i} #{n}/ #{j} #{m}"
	a[j]=a[j].to_i
	c[i]=c[i].to_i
	#puts a[j]>c[i]
	if a[j]>=c[i]
		ans=ans+1
		j=j+1
	end
	i=i+1
end
puts ans

_,a,b=$<.map{|e|e.split.map &:to_i}
r=0
a.each{|e|b[r].to_i>=e&&r+=1}
p r

N, M = gets.split.map(&:to_i)
cs = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
ans = 0
cs.each do |c|
    if c <= as[0]
        ans += 1
        as.shift
        break if as.length == 0
    end
end
puts ans

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

ans = 0
j = 0

0.upto(n-1).each do |i|
  if cost[i] <= bills[j]
    ans += 1
    j += 1
  end
  break if j >= m
end

puts ans

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

ans = 0
a = as.shift
(0..n-1).each do |i|
  if a >= cs[i]
    ans += 1
    a = as.shift
  end
  break unless a
end

puts ans

n, m = gets.split.map &:to_i
c = gets.split.map &:to_i
a = gets.split.map &:to_i
ngames = 0
(0...n).each do |i|
  if a[0] && a[0] >= c[i]
    ngames += 1
    a.shift
  end
end
puts ngames

n,m=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
ans=0
c.each{|v|
  break if a.empty?
  if v<=a.first
    ans+=1
    a.shift
  end
}

puts ans


n,m = gets.chomp.split.collect{|i| i.to_i}
seats = gets.chomp.split.collect{|i| i.to_i}.sort

minz = 0
mint = 0
nplanes = seats.size
0.upto(nplanes-1) do |i|
    s = seats[i]
    while mint!=n and s!=0
        minz += s
        s -= 1
        mint += 1
    end
    break if mint == n
end
maxz = 0
maxt = 0
seats = seats.reverse
i = 0
while maxt != n
    maxz += seats[0]
    seats[0] -= 1
    maxt += 1
    seats[0] = nil if seats[0] == 0
    seats = seats.compact.sort.reverse
end

puts "#{maxz} #{minz}"

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

min_a, k, j, score_min = a.sort, n, 0, 0
while k > 0
  score_min += (min_a[j] <= k ? min_a[j]*(min_a[j]+1)/2 : min_a[j]*(min_a[j]+1)/2-(min_a[j]-k)*(min_a[j]-k+1)/2)
  k -= min_a[j]
  j+=1
end

max_a, k, score_max = a.sort.reverse, n, 0, 0
el = max_a[0]
c = max_a.count el
while k > 0
  score_max += (c <= k ? el*c : el*k)
  el -= 1
  k -= c
  if max_a[c] == el
    while c < m and max_a[c] == el
      c+=1
    end
  end
end

puts "#{score_max} #{score_min}"

n, m = gets.chomp.split.map{|e|e.to_i}
a = Array.new(m){}
a = gets.chomp.split.map{|e|e.to_i}
b = Marshal.load(Marshal.dump(a))
max = min = 0

n.times do |i|
  a.sort!{|c, d| d <=> c }.reject!{|c| c == 0}
  max += a[0]
  a[0] -= 1
end

n.times do |i|
  b.sort!.reject!{|c| c == 0}
  min += b[0]
  b[0] -= 1
end

puts "#{max} #{min}"

#!/usr/bin/ruby
n,m = gets.chomp.split(' ').map{|x| x.to_i}
a = gets.chomp.split(' ').map{|x| x.to_i}
atm = a.clone
minc = 0
maxc = 0
for i in 0..n-1
  max = -1
  k = 0
  for j in 0..m-1
    if atm[j] > max && atm[j] > 0
      max = atm[j]
      k = j
    end
  end
  atm[k]-=1
  maxc += max
end
for i in 0..n-1
  min = 2147483647
  k = 0
  for j in 0..m-1
    if a[j] < min && a[j] > 0
      min = a[j]
      k = j
    end
  end
  a[k]-=1;
  minc += min
end
puts "#{maxc} #{minc}"

n, m = gets.split.map(&:to_i)
ar_down = gets.split.map(&:to_i).sort { |e| -e }
ar_up = ar_down.sort

min = max = 0
n_up = n
n_down = n

ar_up.each do |e|
  if n_up > e
    n_up -= e
    min += e*(1 + e)/2
  else
    min += (n_up)*(e-n_up+e+1)/2
    break
  end
end

n_down.times do
  m = ar_down.max
  max += m
  ar_down[ar_down.find_index(m)] -=1
end

puts "#{max} #{min}"

def calc(p, a)
  cost = 0
  while p > 0
    index = yield(a)
    cost += a[index]
    a[index] -= 1
    p -= 1
  end
  cost
end

n, _ = gets.chomp.split.map { |x| x.to_i }
a = gets.chomp.split.map { |x| x.to_i }
puts [calc(n, a.clone) { |x| max, index = -1, 0; x.each_with_index { |e, i| if e > max; max = e; index = i end }; index },
      calc(n, a) { |x| min, index = 1001, 0; x.each_with_index { |e, i| if e > 0 && e < min; min = e; index = i end }; index }] * ' '

n,k = gets.chomp.split(/ /).map!{|x| x.to_i}
ra = gets.chomp.split(/ /).map!{|x| x.to_i}
n1 = n
n2 = n
s1 = 0
s2 = 0
ra2 = ra.sort
ra1 = ra.sort.reverse
idx = 0
while n1 > 0
    rew = false
    rew = true if idx >= k - 1 || ra1[idx] > ra1[idx+1]
    s1 += ra1[idx]
    ra1[idx] -= 1
    n1 -= 1
    if rew
        idx = 0
    else
        idx += 1
    end
end

idx = 0
while n2 > 0
    s2 += ra2[idx]
    n2 -= 1
    ra2[idx] -= 1
    if ra2[idx] <= 0
        idx += 1
    end
end
puts s1.to_s + " " + s2.to_s

n,m=gets.split.map &:to_i;
a=gets.split.map &:to_i;

b = a; x=y=0;

n.times{
	a=a.sort;x+=a[0];a[0]-=1;
}

n.times{
	b=b.sort;y+=b[-1];b[-1]-=1;b.pop if b[-1]==0
}

puts [y, x]*' '

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

min_a, k, j, score_min = a.sort, n, 0, 0
while k > 0
  score_min += (min_a[j] <= k ? min_a[j]*(min_a[j]+1)/2 : k*(k+1)/2)
  k -= min_a[j]
  j+=1
end

max_a, k, score_max = a.sort.reverse, n, 0, 0
el = max_a[0]
c = max_a.count el
while k > 0
  score_max += (c <= k ? el*c : el*k)
  el -= 1
  k -= c
  if max_a[c] == el
    while c < m and a[c] == el
      c+=1
    end
  end
end

puts "#{score_max} #{score_min}"

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

min_a, k, j, score_min = a.sort, n, 0, 0
while k > 0
  score_min += (min_a[j] <= k ? min_a[j]*(min_a[j]+1)/2 : min_a[j]*(min_a[j]+1)/2-(min_a[j]-k)*(min_a[j]-k+1)/2)
  k -= min_a[j]
  j+=1
end

max_a, k, score_max = a.sort.reverse, n, 0, 0
el = max_a[0]
c = max_a.count el
while k > 0
  score_max += (c <= k ? el*c : el*k)
  el -= 1
  k -= c
  if max_a[c] == el
    while c < m and a[c] == el
      c+=1
    end
  end
end

puts "#{score_max} #{score_min}"

n,m = gets.chomp.split.collect{|i| i.to_i}
seats = gets.chomp.split.collect{|i| i.to_i}.sort

minz = 0
mint = 0
nplanes = seats.size
0.upto(nplanes-1) do |i|
    s = seats[i]
    while mint!=n and s!=0
        minz += s
        s -= 1
        mint += 1
    end
    break if mint == n
end
maxz = 0
maxt = 0
seats = seats.reverse
i = 0
while maxt != n
    if seats[i] > 0 then
        maxz += seats[i]
        seats[i] -= 1
        maxt += 1
    end
    i += 1
    i %= nplanes
end

puts "#{maxz} #{minz}"

n,m = gets.chomp.split.collect{|i| i.to_i}
seats = gets.chomp.split.collect{|i| i.to_i}.sort

minz = 0
mint = 0
nplanes = seats.size
0.upto(nplanes-1) do |i|
    s = seats[i]
    while mint!=n and s!=0
        minz += s
        s -= 1
        mint += 1
    end
    break if mint == n
end
maxz = 0
maxt = 0
seats = seats.reverse
i = 0
while maxt != n
    while seats[i] >= seats[(i+1)%nplanes]  and maxt!=n
        maxz += seats[i]
        seats[i] -= 1
        maxt += 1
    end
    break if maxt == n
    i += 1
    i %= nplanes
end

puts "#{maxz} #{minz}"


def get_right_count(array, value)
  n, sum = array.size, array[-1]
  result = Array.new(n, 0)
  result[-1] = 1 if sum == value
  (array.size - 2).downto(0) do |index|
    sum += array[index]
    result[index] += result[index + 1]
    result[index] += 1 if sum == value
  end
  result
end

n = gets.to_i
values = gets.split.map { |v| v.to_i }
sum, result = values.inject { |s, v| s + v }, 0

if sum % 3 == 0
  sum /= 3
  left_sum, count = 0, get_right_count(values, sum) + [0, 0]
  values.each_index do |index|
    left_sum += values[index]
    result += count[index + 2] if left_sum == sum
  end
end

puts result

def solve(a)
    sum = a.inject { |s, x| s + x }
    return 0 unless sum % 3 == 0
    one_third = sum / 3
    a.pop
    local = 0
    thirds_count = 0
    ans = 0
    a.each do |x|
        local += x
        ans += thirds_count if local == one_third * 2
        thirds_count += 1 if local == one_third
    end
    return ans
end

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

def get_right_count(array, value)
  n, sum = array.size, 0
  result = Array.new(n, 0)
  result[-1] = 1 if array[-1] == value
  (array.size - 2).downto(0) do |index|
    sum += array[index]
    result[index] += result[index + 1]
    result[index] += 1 if sum == value
  end
  result
end

n = gets.to_i
values = gets.split.map { |v| v.to_i }
sum, result = values.inject { |s, v| s + v }, 0

if sum % 3 == 0
  sum /= 3
  left_sum, count = 0, get_right_count(values, sum) + [0, 0]
  values.each_index do |index|
    left_sum += values[index]
    result += count[index + 2] if left_sum == sum
  end
end

puts result


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

neg=a.count {|i| i<0}
[k,neg].min.times {|i| a[i]=-a[i]}

min=a.min_by {|i| i.abs}
sum=a.inject(:+)

k-=neg
if k>0 && k%2==1
  sum-=2*min
end

p sum

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

neg=a.count {|i| i<0}
[k,neg].min.times {|i| a[i]=-a[i]}

min=a.min_by {|i| i.abs}
sum=a.inject(:+)

k-=neg
sum-=2*min if k>0 && k%2==1

p sum

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

neg=a.count {|i| i<0}
[k,neg].min.times {|i| a[i]=-a[i]}

sum=a.inject(:+)

k-=neg
sum-=2*a.min if k>0 && k%2==1

p sum

n,k = gets.split.map &:to_i
a = gets.split.map &:to_i
counter = 0
a.each {|el| counter+=1 if el < 0}
null_position = a.index(0)
pre_null = nil

if counter >= k
  k.times do |index|
    a[index] = -a[index]
  end
elsif null_position
  a.each_with_index do |el,index|
    if el<0
      a[index] *= -1
    else
      break
    end
  end
else
 a.each_with_index do |el,index|
    if el<0
      a[index] = -el
    else
      pre_null = index-1
      break
    end
  end
  if (k-counter) % 2 != 0
    a[a.index(a.min)] *= -1
  end

end
puts a.inject{|sum, el| sum+el}

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

n, k = read_next_line
a = read_next_line
i, p = 0, [n, k].min
while i < p && a[i] < 0
  a[i] = -a[i]
  i += 1
end
if (k - i).odd?
  i -= 1 if i == n || (i > 0 && a[i - 1] < a[i])
  a[i] = -a[i]
end
puts a.inject(:+)

BEGIN{
	def get_line_int()
		return gets.split.map(&:to_i)
	end
	def main()
		n, k = get_line_int
		a = get_line_int
		sum = 0
		a.each_with_index do |x,i|
			break if k == 0
			if x < 0 then
				a[i] *= -1
				k -= 1
			else
				break
			end
		end
		a.each do |x|
			sum += x
		end
		if k != 0 then
			sum -= a.min * 2 if k % 2 == 1
		end
		puts sum
	end
	Test_case = 1
	Test_case.times do
		main()
	end
}

a = gets.split(" ").collect{|x| x.to_i}
n = a[0]
k = a[1]

a = gets.split(" ").collect{|x| x.to_i}
result = 0

a.each_index { |i|
	if a[i] < 0 and k > 0
		a[i] *= -1
		k -= 1
	end
}

result = a.inject{|result, x| result += x}

if k % 2 == 1
	m = a.min { |a, b| a.abs <=> b.abs }
	result -= 2*m
end

puts result

n, k = gets.split(' ').map{|i| i.to_i}
a = gets.split(' ').map{|i| i.to_i}
i = 0
while k > 0
	if i < a.length and a[i] < 0
		a[i] *= -1
		k -= 1
		i += 1
	else
		break
	end
end

sum = a.reduce{|a, b| a+b}
sum -= (k%2) * a.min * 2
puts sum

n, k = gets.chomp.split.map(&:to_i)
puts gets.chomp.split.map(&:to_i).map { |x| (k > 0 && x < 0) ? (k -= 1; -x) : x }.sort.tap { |x| k = k % 2 }.map { |x|
	k > 0 ? (k = 0; -x ) : x }.reduce(:+)

# -*- encoding: utf-8 -*-

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

i = 0
k.times do
  break if i >= n or sequence[i] >= 0

  sequence[i] *= -1
  i += 1
end

if (k - i) > 0
  sequence.sort!
  if (k - i) % 2 == 1
    sequence[0] *= -1
  end
end

puts sequence.inject{|sum, i| sum+= i}

n,k=gets.split(' ').map(&:to_i)
a=gets.split(' ').map(&:to_i)
neg=a.count {|i| i<0}
[k,neg].min.times {|i| a[i]=-a[i]}
min=a.min_by {|i| i.abs}
sum=a.inject(:+)
k-=neg
if k>0 && k%2==1
  sum-=2*min
end
puts sum

def run
  lines = ARGF.readlines

  n, k = lines.shift.chomp.split(" ").map {|x| x.to_i }
  vals = lines.shift.chomp.split(" ").map {|x| x.to_i }

  vals.length.times do |i|
    if vals[i] < 0
      vals[i] *= -1
      k -= 1
    else
      if k % 2 == 1
        if vals[i - 1] < vals[i]
          vals[i - 1] *= -1
        else
          vals[i] *= -1
        end
      end
      k = 0
    end

    break if k == 0
  end

  vals[vals.length - 1] *= -1 if k % 2 == 1

  puts vals.reduce(0) { |sum, n| sum + n }
end

run if $0 == __FILE__

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

s = 0
min = 10001
line.sort!
line.each {|x|
 if x<0
	if k>0
		k -= 1
		s += x.abs
	else
		s += x
	end
	min = x.abs
 else
	if  min > x
		min = x
	end
	s += x
 end
}

if k%2 == 1
	s -= (2*min)
end

puts s

n, k = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}.sort
ans = 0
cnt = 0
a.each{|i| cnt+=1 if i<=0}
if cnt>=k
    n.times do |i|
        if i < k
            ans += -a[i]
        else
            ans += a[i]
        end
    end
else
    k -= cnt
    a = a.collect{|i| i.abs}
    if k.odd?
        ans = a.inject{|sum, i| sum+=i}-2*a.min
    else
        ans = a.inject{|sum, i| sum+=i}
    end
end
puts ans

# -*- encoding: utf-8 -*-

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

idx = 0

k.times do
  if idx < n
    sequence[idx] *= -1
    idx += 1
  else
    sequence.sort!
    idx = 0
  end
end

puts sequence.inject{|sum, i| sum+= i}

def run
  lines = ARGF.readlines

  n, k = lines.shift.chomp.split(" ").map {|x| x.to_i }
  vals = lines.shift.chomp.split(" ").map {|x| x.to_i }

  vals.length.times do |i|
    if vals[i] < 0
      vals[i] *= -1
      k -= 1
    else
      if vals[i - 1] < vals[i]
        vals[i - 1] *= -1
      else
        vals[i] *= -1
      end
      k = 0
    end

    break if k == 0
  end

  puts vals.reduce(0) { |sum, n| sum + n }
end

run if $0 == __FILE__

def run
  lines = ARGF.readlines

  n, k = lines.shift.chomp.split(" ").map {|x| x.to_i }
  vals = lines.shift.chomp.split(" ").map {|x| x.to_i }

  vals.length.times do |i|
    if vals[i] < 0
      vals[i] *= -1
      k -= 1
    else
      if vals[i - 1].abs < vals[i].abs
        vals[i - 1] *= -1
      else
        vals[i] *= -1
      end
      k = 0
    end

    break if k == 0
  end

  puts vals.reduce(0) { |sum, n| sum + n }
end

run if $0 == __FILE__

def run
  lines = ARGF.readlines

  n, k = lines.shift.chomp.split(" ").map {|x| x.to_i }
  vals = lines.shift.chomp.split(" ").map {|x| x.to_i }

  vals.length.times do |i|
    if vals[i] < 0
      vals[i] *= -1
      k -= 1
    else
      if k % 2 == 1
        if vals[i - 1] < vals[i]
          vals[i - 1] *= -1
        else
          vals[i] *= -1
        end
      end
      k = 0
    end

    break if k == 0
  end

  puts vals.reduce(0) { |sum, n| sum + n }
end

run if $0 == __FILE__

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

s = 0
min = 0
line.each {|x|
 if x<0
	if k>0
		k -= 1
		s += x.abs
	else
		s += x
	end
	min = x.abs
 else
	if  min > x
		min = x
	end
	s += x
 end
}

if k%2 == 1
	s -= (2*min)
end

puts s

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

s = 0
min = 0
line.sort!
line.each {|x|
 if x<0
	if k>0
		k -= 1
		s += x.abs
	else
		s += x
	end
	min = x.abs
 else
	if  min > x
		min = x
	end
	s += x
 end
}

if k%2 == 1
	s -= (2*min)
end

puts s

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

s = 0
min = 0
line.each {|x|
 if x<0
	if k>0
		k -= 1
		s += x.abs
	else
		s += x
	end
	min = x.abs
 else
	if  min > x
		min = x
	end
	s += x
 end
}

if k%2 == 1
	s -= min
end

puts s

n, k = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}.sort
ans = 0
n.times do |i|
    if i < k
        ans += -a[i]
    else
        ans += a[i]
    end
end
puts ans

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
k%=2*n

[k,n].min.times {|i| a[i]=-a[i]}
k-=n
(-1..-k).each {|i| a[i]=-a[i]} if k>0

puts a.inject(:+)

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

k.times do |i|
  if i<n
    case
    when a[i] < 0 then a[i]=-a[i]
    when a[i]==0
      break
    else
      a[0]=-a[0]
    end
  else
    a[0]=-a[0]
  end
end

puts a.inject(:+)

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

k.times do |i|
  if i>=n
    a[0]=-a[0]
  elsif a[i]<0
    a[i]=-a[i]
  elsif a[i]==0
    break
  else
    a[0]=-a[0]
  end
end

puts a.inject(:+)

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
k.times {|i| a[i%n]=-a[i%n]}
puts a.inject(:+)

a = gets.split(" ").collect{|x| x.to_i}
n = a[0]
k = a[1]

a = gets.split(" ").collect{|x| x.to_i}
result = 0
minus = 0

a.sort!

a.each{|x|
	if x < 0
		minus += 1
	end
}

if minus >= k
	a.reverse_each { |e|
		if e < 0 and k > 0
			e *= -1
			k -= 1
		end
		result += e
	}
else
	a.inject{|result, x| result += x}
	k -= n
	if k % 2 == 1
		result -= a[0]
	end
end

puts result


def func x
    return true if x>=@n
    h={}
    i=x
    s=0
    while i>=0
        k=@d[i]
        if k>=0 and !h[k]
            h[k]=1
            s+=@a[k]
        elsif s>0
            s-=1
        end
        i-=1
    end
    h.size==@m and s<=0
end

@n, @m=gets.split.map &:to_i
@d=gets.split.map{|e| e.to_i-1}
@a=gets.split.map &:to_i

x=1<<32
res=2*x-1
while x>0
    res-=x if func res-x
    x>>=1
end

puts (res<@n ? res+1 : -1)

def func x
    return true if x>=@n
    i, s, h=x, 0, {}
    while i>=0
        k=@d[i]
        k<0 or h[k] ? s=s>0 ? s-1 : 0 : (h[k]=1; s+=@a[k])
        i-=1
    end
    h.size==@m and s==0
end

@n, @m=gets.split.map &:to_i
@d=gets.split.map{|e| e.to_i-1}
@a=gets.split.map &:to_i

x=1<<32
res=2*x-1
while x>0
    res-=x if func res-x
    x>>=1
end

puts (res<@n ? res+1 : -1)

def func x
    return true if x>=@n
    h={}
    i=x
    p=x
    while i>=0
        k=@d[i]
        if k>=0 and !h[k]
            h[k]=1
            return false if @a[k]>p
            p-=@a[k]
        else
            p-=1
        end
        i-=1
    end
    h.size==@m
end

@n, @m=gets.split.map &:to_i
@d=gets.split.map{|e| e.to_i-1}
@a=gets.split.map &:to_i

x=1<<32
res=2*x-1
while x>0
    res-=x if func res-x
    x>>=1
end

puts (res<@n ? res+1 : -1)

def func x
    return true if x>=@n
    h={}
    i=x
    s=0
    while i>=0
        k=@d[i]
        if k>=0 and !h[k]
            h[k]=1
            s+=@a[k]
        else
            s-=1
        end
        i-=1
    end
    s<=0
end

@n, @m=gets.split.map &:to_i
@d=gets.split.map{|e| e.to_i-1}
@a=gets.split.map &:to_i

x=1<<32
res=2*x-1
while x>0
    res-=x if func res-x
    x>>=1
end

puts (res<@n ? res+1 : -1)

def func x
    return true if x>=@n
    h={}
    i=x
    s=0
    while i>=0
        k=@d[i]
        if k>=0 and !h[k]
            h[k]=1
            s+=@a[k]
        else
            s-=1
        end
        i-=1
    end
    h.size==@m and s<=0
end

@n, @m=gets.split.map &:to_i
@d=gets.split.map{|e| e.to_i-1}
@a=gets.split.map &:to_i

x=1<<32
res=2*x-1
while x>0
    res-=x if func res-x
    x>>=1
end

puts (res<@n ? res+1 : -1)


class Numeric
  def sign
    self<=>0
  end
end

class Array
  def loop_slide!(x=1)
    return false if self.length <= 1
    x = (x.abs % self.length) * (-x.sign)
    x += self.length if x < 0
    new_a = self.drop(x)+self.take(x)
    self.length.times{|i| self[i] = new_a[i]}
  end

  def loop_slide_simple!(x=1)  # x == 1 always
    new_a = self.drop(1)+self.take(1)
    self.length.times{|i| self[i] = new_a[i]}
  end

  def second
    self[1]
  end

  def third
    self[2]
  end
end

class XElement
  def initialize(n,m)
    @rows, @cols = n, m
    @mtx = Array.new
    n.times{@mtx.push Array.new(m, 0)}
  end

  def print_mtx
    @rows.times { |row|
      @cols.times { |col|
        print @mtx[row][col]
        print ' '
      }
      print "\n"
    }
    #print "\n"
  end

  def shift_row!(row_num)
    @mtx[row_num-1].loop_slide!
  end

  def shift_col!(col_num)
    column = Array.new
    @rows.times{|i| column.push(@mtx[i][col_num-1])}
    column.loop_slide!(1)
    @rows.times{|i| @mtx[i][col_num-1] = column[i]}
  end

  def set_num!(row, col, value)
    row, col = row-1, col-1
    @mtx[row][col] = value
  end

=begin
  def command!(args)
    COMMAND[args.first.to_s].call(args.drop(1))
  end
=end
end

COMMAND = {
    "1" => lambda{|x| $elem.shift_row!(x.first)},
    "2" => lambda{|x| $elem.shift_col!(x.first)},
    "3" => lambda{|x| $elem.set_num!(x.first, x.second, x.third)}
}

n,m,q, = gets.split.map{|x| x.to_i}
$elem = XElement.new(n,m)
stack = Array.new
q.times {|x| stack.push gets}
#$elem.print_mtx
q.times {|_|
  com = stack.pop.split.map{|x| x.to_i}
  COMMAND[com.first.to_s].call(com.drop(1))
}
$elem.print_mtx

class Mat
	attr_reader :rows, :columns

	def initialize(rows, columns)
		@data = []
		@rows = rows
		@columns = columns
		for i in 1..rows
			row = []
			for j in 1..columns
				row << nil
			end
			@data << row
		end
	end

	def get(r, c)
		@data[r][c]
	end
	def set(r, c, value)
		@data[r][c] = value
	end

	def rotate_row(row, amt)
		amt += columns if amt < 0
		rev_horiz(row, 0, amt - 1)
		rev_horiz(row, amt, columns - 1)
		rev_horiz(row, 0, columns - 1)
	end

	def rotate_column(column, amt)
		amt += rows if amt < 0
		rev_vert(column, 0, amt - 1)
		rev_vert(column, amt, rows - 1)
		rev_vert(column, 0, rows - 1)
	end

	def print
		@data.each do |row|
			puts row.join(' ')
		end
	end

	private

	def rev_horiz(row, start, finish)
		while (start < finish)
			tmp = @data[row][start]
			@data[row][start] = @data[row][finish]
			@data[row][finish] = tmp
			start += 1
			finish -= 1
		end
	end

	def rev_vert(col, start, finish)
		while (start < finish)
			tmp = @data[start][col]
			@data[start][col] = @data[finish][col]
			@data[finish][col] = tmp
			start += 1
			finish -= 1
		end
	end
end

line = gets.chomp.split(' ')
n = line[0].to_i
m = line[1].to_i
q = line[2].to_i

RowOp = Struct.new(:row)
ColOp = Struct.new(:column)
SetOp = Struct.new(:row, :column, :value)

ops = []

for i in 1..q
	line = gets.chomp.split(' ')
	type = line[0].to_i
	case type
		when 1
			ops << RowOp.new(line[1].to_i - 1)
		when 2
			ops << ColOp.new(line[1].to_i - 1)
		else
			ops << SetOp.new(line[1].to_i - 1, line[2].to_i - 1, line[3].to_i)
	end
end

matrix = Mat.new(n, m)

ops.reverse!

for i in 0..ops.length-1
	case ops[i]
		when SetOp
			matrix.set(ops[i].row, ops[i].column, ops[i].value)
		when RowOp
			matrix.rotate_row(ops[i].row, -1)
		when ColOp
			matrix.rotate_column(ops[i].column, -1)
	end
end

for i in 0..matrix.rows-1
	for j in 0..matrix.columns-1
		matrix.set(i, j, 0) if matrix.get(i, j).nil?
	end
end

matrix.print

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.chomp end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end
############################################################

n, m, q = gis

a = arr2d n, m

v = []
n.times do |i|
    tv = []
    m.times do |j|
        tv << [i, j]
    end
    v << tv
end

q.times do
    t, *rcx = gis
    case t
    when 1
        r, * = rcx
        v[r - 1].rotate!
    when 2
        c, * = rcx
        ta = v[0][c - 1]
        (n - 1).times do |i|
            v[i][c - 1] = v[i + 1][c - 1]
        end
        v[n - 1][c - 1] = ta
    when 3
        r, c, x = rcx
        i, j = v[r - 1][c - 1]
        a[i][j] = x
    end
end

a.map{|z| puts z.map(&:to_s) * " " }

n, m, q = gets.chomp.split(" ").map(&:to_i)
a = Array.new(n).map{Array.new(m, 0)}
qs = []
while line = gets
  qs << line.split(" ").map(&:to_i)
end
qs.reverse.each do |ope|
  t = ope.shift
  if t == 1 then
    r = ope.shift - 1
    cyc = a[r].dup
    temp = cyc.pop
    cyc.unshift(temp)
    a[r] = cyc
  elsif t == 2 then
    c = ope.shift - 1
    cyc = a.map{|b| b[c]}
    temp = cyc.pop
    cyc.unshift(temp)
    n.times do |i|
      a[i][c] = cyc[i]
    end
  else
    r, c, x = ope
    r -= 1
    c -= 1
    a[r][c] = x
  end
end
a.each do |r|
  puts r.join(" ")
end

class Mat
	attr_reader :rows, :columns

	def initialize(rows, columns)
		@data = []
		@rows = rows
		@columns = columns
		for i in 1..rows
			row = []
			for j in 1..columns
				row << nil
			end
			@data << row
		end
	end

	def get(r, c)
		@data[r][c]
	end
	def set(r, c, value)
		@data[r][c] = value
	end

	def rotate_row(row, amt)
		amt += rows if amt < 0
		rev_horiz(row, 0, amt - 1)
		rev_horiz(row, amt, rows - 1)
		rev_horiz(row, 0, rows - 1)
	end

	def rotate_column(column, amt)
		amt += columns if amt < 0
		rev_vert(column, 0, amt - 1)
		rev_vert(column, amt, columns - 1)
		rev_vert(column, 0, columns - 1)
	end

	def print
		@data.each do |row|
			puts row.join(' ')
		end
	end

	private

	def rev_horiz(row, start, finish)
		while (start < finish)
			tmp = @data[row][start]
			@data[row][start] = @data[row][finish]
			@data[row][finish] = tmp
			start += 1
			finish -= 1
		end
	end

	def rev_vert(col, start, finish)
		while (start < finish)
			tmp = @data[start][col]
			@data[start][col] = @data[finish][col]
			@data[finish][col] = tmp
			start += 1
			finish -= 1
		end
	end
end

line = gets.chomp.split(' ')
n = line[0].to_i
m = line[1].to_i
q = line[2].to_i

RowOp = Struct.new(:row)
ColOp = Struct.new(:column)
SetOp = Struct.new(:row, :column, :value)

ops = []

for i in 1..q
	line = gets.chomp.split(' ')
	type = line[0].to_i
	case type
		when 1
			ops << RowOp.new(line[1].to_i - 1)
		when 2
			ops << ColOp.new(line[1].to_i - 1)
		else
			ops << SetOp.new(line[1].to_i - 1, line[2].to_i - 1, line[3].to_i)
	end
end

matrix = Mat.new(n, m)

first_set = nil
for i in 0..ops.length-1
	next if !first_set && !ops[i].is_a?(SetOp)
	first_set ||= i

	case ops[i]
		when SetOp
			matrix.set(ops[i].row, ops[i].column, ops[i].value)
		when RowOp
			matrix.rotate_row(ops[i].row, 1)
		when ColOp
			matrix.rotate_column(ops[i].column, 1)
	end
end

rev_ops = ops[0..first_set-1].reverse
rev_ops.each do |op|
	case op
		when RowOp
			matrix.rotate_row(ops[i].row, -1)
		when ColOp
			matrix.rotate_column(ops[i].column, -1)
	end
end

for i in 0..matrix.rows-1
	for j in 0..matrix.columns-1
		matrix.set(i, j, 0) if matrix.get(i, j).nil?
	end
end

matrix.print

class Numeric
  def sign
    self<=>0
  end
end

class Array
  def loop_slide!(x=1)
    x = (x.abs % self.length) * (-x.sign)
    x += self.length if x < 0
    new_a = self.drop(x)+self.take(x)
    self.length.times{|i| self[i] = new_a[i]}
  end

  def second
    self[1]
  end

  def third
    self[2]
  end
end

class XElement
  def initialize(n,m)
    @rows, @cols = n, m
    @mtx = Array.new
    n.times{@mtx.push Array.new(m, 0)}
  end

  def print_mtx
    @rows.times { |row|
      @cols.times { |col|
        print @mtx[row][col]
        print ' '
      }
      print "\n"
    }
    #print "\n"
  end

  def shift_row!(row_num)
    @mtx[row_num-1].loop_slide!
  end

  def shift_col!(col_num)
    column = Array.new
    @cols.times{|i| column.push(@mtx[i][col_num-1])}
    column.loop_slide!(-1)
    @cols.times{|i| @mtx[i][col_num-1] = column[i]}
  end

  def set_num!(row, col, value)
    row, col = row-1, col-1
    @mtx[row][col] = value
  end

=begin
  def command!(args)
    COMMAND[args.first.to_s].call(args.drop(1))
  end
=end
end

COMMAND = {
    "1" => lambda{|x| $elem.shift_row!(x.first)},
    "2" => lambda{|x| $elem.shift_col!(x.first)},
    "3" => lambda{|x| $elem.set_num!(x.first, x.second, x.third)}
}

n,m,q, = gets.split.map{|x| x.to_i}
$elem = XElement.new(n,m)
stack = Array.new
q.times {|x| stack.push gets}
#$elem.print_mtx
q.times {|_|
  com = stack.pop.split.map{|x| x.to_i}
  COMMAND[com.first.to_s].call(com.drop(1))

}
$elem.print_mtx


require 'set'

def solve(a, s)
  front = Hash.new do |h, diff|
    h[diff] = diff.abs - 2
  end
  a = a.to_set

  pts = s.map do |diff|
    existing = front[diff]

    left = front[diff + 1]
    return -1 if left < existing
    right = front[diff - 1]
    return -1 if right < existing

    nw = existing + 2
    # y + x = nw
    # y - x = diff
    y = (nw + diff) / 2
    x = (nw - diff) / 2
    pt = [x,y]
    return -1 unless a.include?(pt)

    front[diff] = nw
    pt
  end
end

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

res = solve(a, diffs)
if res == -1
  puts 'NO'
else
  puts 'YES'
  puts res.map{|cs| cs.join(' ')}
end

points = Array.new { Array.new(3) }
a = Array.new { Array.new(2) }
n = STDIN.gets.to_i

def check_values(a, points, n)
  points.sort! do |x, y|
    if x[2] != y[2]
      x[2] <=> y[2]
    else
      x[0] <=> y[0]
    end
  end
  a.sort! do |x, y|
    if x[0] != y[0]
      x[0] <=> y[0]
    else
      x[1] <=> y[1]
    end
  end

  a.each_with_index do |val, i|
    if (val[0] != points[i][2])
      return false
    end
  end

  return true
end

def print_res(a, points, n)
  if !check_values(a, points, n)
    puts "NO"
    return
  else
    res = Array.new
    points.each_with_index do |point, i|
      res[a[i][1]] = point
    end

    #min_x = 0
    #min_y = 0
    #max_x = 0
    #max_y = 0
    #print "#{res} \n"
    max_l = 0
    max_r = 0
    #res.each_with_index do |r, i|
      #if (r[0] < min_x && r[1] < max_y) || (r[0] < max_x && r[1] < min_y)
      #  print "#{r[0]} #{r[1]} #{min_x} #{max_x} #{max_y} "
      #  puts "NO"
      #  return
      #else
      #  if r[0] > max_x
      #    max_x = r[0]
      #    min_y = r[1]
      #  end
      #  if r[1] > max_y
      #    max_y = r[1]
      #    min_x = r[0]
      #  end
     #   print "#{max_y} #{r[1]}     "
  #    for j in 0..i-1 do
  #      if r[0] < res[j][0] && r[1] < res[j][1]
  #        puts "NO"
  #        return
  #      end
  #    end
   #   cur_l = r[1] - r[0]
   #   cur_r = r[0] - r[1]
   #   if (max_l > cur_l && max_r > cur_r)
   #     puts "NO"
   #     return
   #   end
   #   max_l = cur_l if max_l >

    #end
    for i in 1..n - 1 do
      if res[i][0] <= res[i-1][0] && res[i][1] <= res[i-1][1]
        puts "NO"
        return
      end
    end

    puts "YES"
    res.each do |r|
      puts "#{r[0]} #{r[1]}"
    end
  end
end

for i in 0..n-1 do
  points.push(STDIN.gets.split(" ").to_a.map{|q| q.to_i})
  points[i][2] = points[i][1] - points[i][0]
end

STDIN.gets.split(" ").to_a.each_with_index do |num, i|
  a.push((num + " #{i}").split(" ").to_a.map{|q| q.to_i})
end

print_res(a, points, n)

n = gets.chomp.to_i

diff = []
map_x = {}
map_y = {}

n.times do
   x,y = gets.chomp.split.map(&:to_i)
   d = y - x

   diff << d

   if(!map_x[d])

     map_x[d] = []
     map_y[d] = []
   end

   map_x[d] << x
   map_y[d] << y

end

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

px = []
py = []

map_x.keys.each do |k|
  map_x[k].sort!.reverse!
end

map_y.keys.each do |k|
  map_y[k].sort!.reverse!
end

enable = true

if(diff.sort != ws.sort)

  enable = false
else
  ws.each do |d|
    if(!map_x[d] || map_x[d].empty?)
      enable = false;
      break;
    end
    px << map_x[d].pop
    py << map_y[d].pop

  end
end

if enable
  x = -1
  y = -1

  0.upto(px.size - 1) do |index|
    if(px[index] <= x && py[index] <= y)
      enable = false
      break;
    end
    x = px[index]
    y = py[index]
  end
end

if(enable)
  puts "YES"
  0.upto(px.size - 1) do |index|
    printf("%d %d\n", px[index], py[index]);
  end

else
  puts "NO"
end

n = gets.chomp.to_i

diff = []
map_x = {}
map_y = {}

n.times do
   x,y = gets.chomp.split.map(&:to_i)
   d = y - x

   diff << d

   if(!map_x[d])

     map_x[d] = []
     map_y[d] = []
   end

   map_x[d] << x
   map_y[d] << y

end

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

px = []
py = []

map_x.keys.each do |k|
  map_x[k].sort!.reverse!
end

map_y.keys.each do |k|
  map_y[k].sort!.reverse!
end

enable = true

if(diff.sort != ws.sort)

  enable = false
else
  ws.each do |d|
    if(!map_x[d] || map_x[d].empty?)
      enable = false;
      break;
    end
    px << map_x[d].pop
    py << map_y[d].pop

  end
end

if enable
  x = -1
  y = -1

  0.upto(px.size - 1) do |index|
    if(px[index] < x && py[index] < y)
      puts "h3e";
      enable = false
      break;
    end
    x = px[index]
    y = py[index]
  end
end

if(enable)
  puts "YES"
  0.upto(px.size - 1) do |index|
    printf("%d %d\n", px[index], py[index]);
  end

else
  puts "NO"
end

n = gets.chomp.to_i

diff = []
map_x = {}
map_y = {}

n.times do
   x,y = gets.chomp.split.map(&:to_i)
   d = y - x

   diff << d

   if(!map_x[d])

     map_x[d] = []
     map_y[d] = []
   end

   map_x[d] << x
   map_y[d] << y

end

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

px = []
py = []

map_x.keys.each do |k|
  map_x[k].sort!.reverse!
end

map_y.keys.each do |k|
  map_y[k].sort!.reverse!
end

enable = true

if(diff.sort != ws.sort)

  enable = false
else
  ws.each do |d|
    if(!map_x[d] || map_x[d].empty?)
      enable = false;
      break;
    end
    px << map_x[d].pop
    py << map_y[d].pop

  end
end

if enable
  x = -1
  y = -1

  0.upto(px.size - 1) do |index|
    if(px[index] < x && py[index] < y)
      enable = false
      break;
    end
    x = px[index]
    y = py[index]
  end
end

if(enable)
  puts "YES"
  0.upto(px.size - 1) do |index|
    printf("%d %d\n", px[index], py[index]);
  end

else
  puts "NO"
end

points = Array.new { Array.new(3) }
a = Array.new { Array.new(2) }
n = STDIN.gets.to_i

def check_values(a, points, n)
  points.sort! do |x, y|
    if x[2] != y[2]
      x[2] <=> y[2]
    else
      x[1] <=> y[1]
    end
  end
  a.sort! { |x, y| x[0] <=> y[0] }

  a.each_with_index do |val, i|
    if (val[0] != points[i][2])
      return false
    end
  end

  return true
end

def print_res(a, points, n)
  if !check_values(a, points, n)
    puts "NO"
    return
  else
    a.sort! { |x, y| x[0] <=> y[0] }
    res = Array.new
    points.each_with_index do |point, i|
      res[a[i][1]] = point
    end

    max_x = 0
    max_y = 0
    res.each do |r|
      if r[0] < max_x && r[1] < max_y
        puts "NO"
        return
      else
        max_x = r[0] if r[0] > max_x
        max_y = r[1] if r[1] > max_y
      end
    end

    puts "YES"
    res.each do |r|
      puts "#{r[0]} #{r[1]}"
    end
  end
end

for i in 0..n-1 do
  points.push(STDIN.gets.split(" ").to_a.map{|q| q.to_i})
  points[i][2] = points[i][1] - points[i][0]
end

STDIN.gets.split(" ").to_a.each_with_index do |num, i|
  a.push((num + " #{i}").split(" ").to_a.map{|q| q.to_i})
end

print_res(a, points, n)

points = Array.new { Array.new(3) }
a = Array.new { Array.new(2) }
n = STDIN.gets.to_i

def check_values(a, points, n)
  points.sort! do |x, y|
    if x[2] != y[2]
      x[2] <=> y[2]
    else
      x[1] <=> y[1]
    end
  end
  a.sort! { |x, y| x[0] <=> y[0] }

  a.each_with_index do |val, i|
    if (val[0] != points[i][2])
      return false
    end
  end

  return true
end

def print_res(a, points, n)
  if !check_values(a, points, n)
    puts "NO"
    return
  else
    a.sort! { |x, y| x[0] <=> y[0] }
    res = Array.new
    points.each_with_index do |point, i|
      res[a[i][1]] = point
    end

    max_x = 0
    max_y = 0
    res.each do |r|
      if r[0] < max_x && r[1] < max_y
        puts "NO"
        return
      else
        max_x = r[0] if r[0] > max_x
        max_y = r[1] if r[1] > max_y
      end
    end

    puts "Yes"
    res.each do |r|
      puts "#{r[0]} #{r[1]}"
    end
  end
end

for i in 0..n-1 do
  points.push(STDIN.gets.split(" ").to_a.map{|q| q.to_i})
  points[i][2] = points[i][1] - points[i][0]
end

STDIN.gets.split(" ").to_a.each_with_index do |num, i|
  a.push((num + " #{i}").split(" ").to_a.map{|q| q.to_i})
end

print_res(a, points, n)

points = Array.new { Array.new(3) }
a = Array.new { Array.new(2) }
n = STDIN.gets.to_i

def check_values(a, points, n)
  points.sort! do |x, y|
    if x[2] != y[2]
      x[2] <=> y[2]
    else
      x[0] <=> y[0]
    end
  end
  a.sort! do |x, y|
    if x[0] != y[0]
      x[0] <=> y[0]
    else
      x[1] <=> y[1]
    end
  end

  a.each_with_index do |val, i|
    if (val[0] != points[i][2])
      return false
    end
  end

  return true
end

def print_res(a, points, n)
  if !check_values(a, points, n)
    puts "NO"
    return
  else
    res = Array.new
    points.each_with_index do |point, i|
      res[a[i][1]] = point
    end

    max_x = 0
    max_y = 0
   # print "#{res} \n"
   # res.each do |r|
    #  if r[0] < max_x && r[1] < max_y
    #    print "#{r[0]} #{r[1]} #{max_x} #{max_y} "
    #    puts "NO"
    #    return
    #  else
    #    max_x = r[0] if r[0] > max_x
    #    max_y = r[1] if r[1] > max_y
    #    print "#{max_y} #{r[1]}     "
    #  end
    #end

    puts "YES"
    res.each do |r|
      puts "#{r[0]} #{r[1]}"
    end
  end
end

for i in 0..n-1 do
  points.push(STDIN.gets.split(" ").to_a.map{|q| q.to_i})
  points[i][2] = points[i][1] - points[i][0]
end

STDIN.gets.split(" ").to_a.each_with_index do |num, i|
  a.push((num + " #{i}").split(" ").to_a.map{|q| q.to_i})
end

print_res(a, points, n)

points = Array.new { Array.new(3) }
a = Array.new { Array.new(2) }
n = STDIN.gets.to_i

def check_values(a, points, n)
  points.sort! do |x, y|
    if x[2] != y[2]
      x[2] <=> y[2]
    else
      x[0] <=> y[0]
    end
  end
  a.sort! do |x, y|
    if x[0] != y[0]
      x[0] <=> y[0]
    else
      x[1] <=> y[1]
    end
  end

  a.each_with_index do |val, i|
    if (val[0] != points[i][2])
      return false
    end
  end

  return true
end

def print_res(a, points, n)
  if !check_values(a, points, n)
    puts "NO"
    return
  else
    res = Array.new
    points.each_with_index do |point, i|
      res[a[i][1]] = point
    end

    min_x = 0
    min_y = 0
    max_x = 0
    max_y = 0
    #print "#{res} \n"
    res.each do |r|
      if (r[0] < min_x && r[1] < max_y) && (r[0] < max_x && r[1] < max_y)
        #print "#{r[0]} #{r[1]} #{max_x} #{max_y} "
        puts "NO"
        return
      else
        if r[0] > max_x
          max_x = r[0]
          min_y = r[1]
        end
        if r[1] > max_y
          max_y = r[1]
          min_x = r[0]
        end
     #   print "#{max_y} #{r[1]}     "
      end
    end

    puts "YES"
    res.each do |r|
      puts "#{r[0]} #{r[1]}"
    end
  end
end

for i in 0..n-1 do
  points.push(STDIN.gets.split(" ").to_a.map{|q| q.to_i})
  points[i][2] = points[i][1] - points[i][0]
end

STDIN.gets.split(" ").to_a.each_with_index do |num, i|
  a.push((num + " #{i}").split(" ").to_a.map{|q| q.to_i})
end

print_res(a, points, n)

points = Array.new { Array.new(3) }
a = Array.new { Array.new(2) }
n = STDIN.gets.to_i

def check_values(a, points, n)
  points.sort! do |x, y|
    if x[2] != y[2]
      x[2] <=> y[2]
    else
      x[1] <=> y[1]
    end
  end
  #print points
  #puts
  a.sort! { |x, y| x[0] <=> y[0] }

  a.each_with_index do |val, i|
    if (val[0] != points[i][2])
      return false
    end
  end

  return true
end

def print_res(a, points, n)
  if !check_values(a, points, n)
    puts "NO"
    return
  else
    res = Array.new
    points.each_with_index do |point, i|
      res[a[i][1]] = point
    end

    max_x = 0
    max_y = 0
    res.each do |r|
      if r[0] < max_x && r[1] < max_y
        puts "NO"
        return
      else
        max_x = r[0] if r[0] > max_x
        max_y = r[1] if r[1] > max_y
      end
    end

    puts "YES"
    res.each do |r|
      puts "#{r[0]} #{r[1]}"
    end
  end
end

for i in 0..n-1 do
  points.push(STDIN.gets.split(" ").to_a.map{|q| q.to_i})
  points[i][2] = points[i][1] - points[i][0]
end

STDIN.gets.split(" ").to_a.each_with_index do |num, i|
  a.push((num + " #{i}").split(" ").to_a.map{|q| q.to_i})
end

print_res(a, points, n)


s = gets.chomp
char_count = Hash.new(0)
s.each_char do |c|
	char_count[c] += 1
end
odd_count = char_count.values.count{|a| a.odd? }
if odd_count == 1 || odd_count == 0
	first = true
elsif s.length % 2 == 0
	first = false
else
	first = true
end
puts first ? "First" : "Second"

s, ans = gets.to_s.chomp, 0
s_ = s.split(//).uniq
s_.each{|chr| ans += 1 if s.count(chr).odd?}
(ans.odd? or ans.zero?) ? (puts "First") : (puts "Second")

# -*- encoding: utf-8 -*-
chars = gets.chomp.split("")

hash = Hash.new 0
chars.each do |char|
  hash[char] += 1
end

odds = 0
hash.each do |key, value|
  odds += 1 if value % 2 == 1
end

puts (odds == 0 or odds % 2 == 1) ? "First" : "Second"

hash=Array.new(26,0)
gets.chomp.split('').each{ |i| hash[i.ord-97]+=1 }
ct=0
hash.each{ |i| ct+=1 if i&1==1 }
puts (ct==0 || ct&1!=0) ? "First" : "Second"

def is_palindrome?(word)
	odd_count = 0
	word.uniq.map do |x|
		odd_count = odd_count + 1 if word.count(x).odd?
	end
	if odd_count > 1
		false
	else
		true
	end
end

def remove_letter(stringArray)
	stringArray.uniq.map do |x|
		if stringArray.count(x) == 1
			stringArray.delete(x)
			puts stringArray
			return stringArray
		else
			return stringArray
		end
	end
end

def do_stuff(stringArray)
	if is_palindrome(stringArray)
		"win"
	else
		next_turn
	end
end

def test(word)
	odd_count = 0
	word.uniq.map do |x|
		odd_count = odd_count + 1 if word.count(x).odd?
	end
	if odd_count.odd? || odd_count <= 1
		puts "First"
	else
		puts "Second"
	end
end

string = "aabb"
stringArray = gets.chomp.split(//)

test(stringArray)

# coding: utf-8

s = gets.chomp.split(//)
co = Array.new(26) { 0 }
l = s.length
for c in s
    co[c.ord - 'a'.ord] += 1
end
turns = 0
odds = co.count { |c| c.odd? }
if odds > 1
    turns += odds - 1
end

if turns.even?
    puts "First"
else
    puts "Second"
end

# 각 턴에서 임의의 한 글자를 지울 수 있다
# 각 턴이 돌아오기 전 플레이어는 글자를 재배치하여
# 팰린드롬을 구성할 수 있다. 이러면 승리한다.
# 두 플레이어가 최적의 플레이를 수행할 때 누가 이길것인가?

# 팰린드롬을 구성하기 위해 최소로 지워야하는 문자수 = 턴 수
# 이 수에 따라 승패가 갈린다.
# 전체 문자는 reorder 될 수 있으므로 문자의 배열 순서는
# 중요하지 않다. 회문을 구성할 수 있는지 여부만 중요한듯

s = gets.strip

if s == s.reverse
	print :First
else
	ar = s.split('')

	letters = {}
	ar.each do |let|
		if letters[let]
			letters[let] += 1
		else
			letters[let] = 1
		end
	end

	sum = 0
	letters.each_value  do |num|
		sum += 1 if !num.even?
	end

	# p sum
	# p (sum-1).even?

	if sum == 0 || (sum-1).even?
		print :First
	else
		print :Second
	end
end

s = gets.chomp
hash = {}
s.each_char { |ch|
	if !hash.has_key? ch
		hash[ch] = 1
	else
		hash[ch] +=1
	end
}

n = 0

hash.values.each { |v|
  if v%2==1
	n += 1
  end
}

k = n%2
if n==0
 k=1
end
puts (k==1) ? "First" : "Second"

class String
  def is_possible_palindrome?
    g = self.split('').inject(Hash.new(0)) { |h, e| h[e] += 1; h }
    odd = false
    g.values.each { |v|
      if v.odd?
        if odd
          return false
        else
          odd = true
        end
      end
    }
    true
  end
end

w = gets.chomp
puts(w.is_possible_palindrome? || w.size.odd? ? 'First' : 'Second')

# Codeforces Round #169
# Problem B -- Little Girl and Game
count = [0] * 26
gets.chomp.each_char do |c|
    count[c.ord - 'a'.ord] += 1
end
odds = count.count { |x| x % 2 == 1 }
puts (odds <= 1 or odds % 2 == 1) ? "First" : "Second"

s = gets.strip
a = Array.new(30, 0)
s.size.times do |i|
	a[ s[i].ord - 'a'.ord] += 1
end
ans = 0
26.times do |i|
	a[i] -= (a[i]/2)*2
	ans += a[i]
end
puts (ans>0 && ans%2==0) ? "Second" : "First"

s = gets.to_s.chomp
s_ = s.split(//).uniq
ans = 0
s_.each do |chr|
		ans += 1 if s.count(chr).odd?
end
if ans.odd? or ans.zero? then
	puts "First"
	else
		puts "Second"
end

n,k=gets.chomp.split.map(&:to_i)
m=[]
n.times do
	a,b=gets.chomp.split.map(&:to_i)
	if b>k
		m.push(a-b+k)
	else
		m.push(a)
	end
end
puts m.max

def is_palindrome?(word)
	odd_count = 0
	word.uniq.map do |x|
		odd_count = odd_count + 1 if word.count(x).odd?
	end
	if odd_count > 1
		false
	else
		true
	end
end

def remove_letter(stringArray)
	stringArray.uniq.map do |x|
		if stringArray.count(x) == 1
			stringArray.delete(x)
			puts stringArray
			return stringArray
		else
			return stringArray
		end
	end
end

def do_stuff(stringArray)
	if is_palindrome(stringArray)
		"win"
	else
		next_turn
	end
end

def test(word)
	odd_count = 0
	word.uniq.map do |x|
		odd_count = odd_count + 1 if word.count(x).odd?
	end
	if odd_count.odd?
		puts "First"
	else
		puts "Second"
	end
end

string = "abddddbca"
stringArray = gets.chomp.split(//)

test(stringArray)

s = gets.strip

if s == s.reverse
	print :First
else
	ar = s.split('')

	letters = {}
	ar.each do |let|
		if letters[let]
			letters[let] += 1
		else
			letters[let] = 1
		end
	end

	sum = 0
	letters.each_value  do |num|
		sum += 1 if !num.even?
	end

	# p sum

	if sum = 0 || !(sum-1).even?
		print :First
	else
		print :Second
	end
end

s = gets.strip

if s == s.reverse
	print :First
else
	ar = s.split('')

	letters = {}
	ar.each do |let|
		if letters[let]
			letters[let] += 1
		else
			letters[let] = 1
		end
	end

	sum = 0
	letters.each_value  do |num|
		sum += 1 if !num.even?
	end

	if sum = 0 || (sum-1).even?
		print :First
	else
		print :Second
	end
end

s = gets.strip

if s == s.reverse
	print :First
else
	ar = s.split('')

	letters = {}
	ar.each do |let|
		if letters[let]
			letters[let] += 1
		else
			letters[let] = 1
		end
	end

	sum = 0
	letters.each_value  do |num|
		sum += 1 if !num.even?
	end

	if (sum-1).even?
		print :First
	else
		print :Second
	end
end

s = gets.chomp
char_count = Hash.new(0)
s.each_char do |c|
	char_count[c] += 1
end
odd_count = char_count.values.count{|a| a.odd? }
if odd_count == 1
	first = true
elsif s.length % 2 == 0
	first = false
else
	first = true
end
puts first ? "First" : "Second"

class String
  def is_palindrome?
    self[0...size / 2] == self[-size / 2..-1].reverse
  end
end

w = gets.chomp
puts(w.is_palindrome? || w.size.odd? ? 'First' : 'Second')


t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  arr = arr.sort.reverse
  ch = 0
  a = b = 0
  arr.each do |el|
    if ch == 0 && el % 2 == 0
      a += el
    end
    if ch == 1 && el % 2 == 1
      b += el
    end
    ch = 1 - ch
  end
  case
  when a > b
    puts "Alice"
  when b > a
    puts "Bob"
  else
    puts "Tie"
  end
end

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

def solve
    n = gets.chomp.to_i
    ar = arline.sort.reverse
    odd = ar.select{|x| x%2 == 1}.sum
    alice = ar.zip(0..n).select{|x, y| y%2 == 0}.map{|x| x[0]}.sum

    case odd <=> alice
    when 0
        puts "Tie"
    when 1
        puts "Bob"
    when -1
        puts "Alice"
    end
end

tc = gets.chomp.to_i
(1..tc).each do
    solve
end

def solve()
  odds, evens = [], []
  n = gets.to_i
  nums = gets.chomp.split.map!(&:to_i)
  nums.each do |i|
    i.odd? ? odds.append(i) : evens.append(i)
  end
  odds.sort!
  evens.sort!
  alice_point = 0
  bob_point = 0
  alices_turn = true
  until (odds.empty?  && evens.empty?) do
    # puts "Size:#{odds.length}, #{evens.length}"
    if alices_turn
      if evens.empty?
        odds.pop
      elsif (not odds.empty?) and odds.last > evens.last
        odds.pop
      else
        alice_point += evens.pop
      end
    else
      if odds.empty?
        evens.pop
      elsif (not evens.empty?) and evens.last > odds.last
        evens.pop
      else
        bob_point += odds.pop
      end
    end
    alices_turn = !alices_turn
  end
  res = alice_point <=> bob_point
  if res == 1
    puts "Alice"
  elsif res == 0
    puts "Tie"
  else
    puts "Bob"
  end
end

tcase = gets.to_i
tcase.times do solve() end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i).sort.reverse
	x, y = 0, 0
	a.each_with_index do |val, i|
		x += val if i.even? && val.even?
		y += val if i.odd? && val.odd?
	end
	puts x < y ? 'Bob' : x > y ? 'Alice' : 'Tie'
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map &:to_i
  a.sort!.reverse!
  res = [0, 0]
  a.each_with_index do |it, i|
    res[1] += it if it % 2 == 1 && i % 2 == 1
    res[0] += it if it % 2 == 0 && i % 2 == 0
  end
  puts res[0] > res[1] ? "Alice" : (res[0] < res[1] ? "Bob" : "Tie")
end

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i).sort.reverse
  f=0
  s=0
  n.times do |i|
    if i.even?
      if a[i].even?
        f+=a[i]
      end
    else
      if a[i].odd?
        s+=a[i]
      end
    end
  end
  if f>s
    puts "Alice"
  elsif f<s
    puts "Bob"
  else
    puts "Tie"
  end
end


gets.to_i.times{
    x, y = gets.split.map &:to_i
    a, b = [0, 0]
    if x == y
        puts x*2
    else
        move = 0
        (
            move += 2
            x -= 1
            y -= 1
        ) while x > 0 && y > 0
        if x == 0 && y == 0
            puts move
        else
            move += 1
            if x > 0
                x -= 1
            else
                y -= 1
            end
            if x == 0 && y == 0
                puts move
            else
                (
                    x -= 2
                    move += 4
                ) while x > 1
                (
                    y -= 2
                    move += 4
                ) while y > 1
                if x == 1 || y == 1
                   move += 2
                end
                puts move
            end
        end
    end
}

t = gets.chomp.to_i
t.times do
  x, y = gets.chomp.split(' ').map(&:to_i).map{|el| el.abs}
  if (x - y).abs < 2
    puts x + y
  else
    x, y = y, x if x < y
    puts x + y + (x - y) - 1
  end
end

t = gets.chomp.to_i
t.times do
    x, y = gets.chomp.split(" ").map(&:to_i)
    big = [x,y].max
    small = [x,y].min
    sa = big - small
    if x == 0 && y == 0
        puts "0"
    elsif sa <= 1
        puts big + small
    else
        puts small * 2 + (big - small - 1)*2 + 1
    end
end

n = gets.to_i
n.times do
  x, y = gets.chomp!.split.map(&:to_i)
  x, y = y, x unless x >= y
  if x > y
    p y * 2 + ((x - y) * 2 - 1)
  else
    p y * 2
  end
end

def get_int
  gets.chomp.to_i
end

get_int.times do |i|
  x, y = gets.split.map(&:to_i)
  puts 2 * [x, y].max - 1 + ((x == y) ? 1 : 0)
end


def is_posible?(s, k)
    n = s.size
    return true if n == k
    chars = [0]*255
    s[0...k].split("").each{ |c| chars[c.ord] += 1 }
    i = 1

    while i+k <= n do
        chars[s[i+k-1].ord] += 1 if chars[s[i+k-1].ord] and chars[s[i+k-1].ord] > 0
        chars[s[i-1].ord] -= 1 if chars[s[i-1].ord]
        i += 1
    end

    chars.each do |v|
       return true if v > 0
    end
    false
end

def solution(s)
  beg = 1
  last = s.size - 1
  min_k = s.size

  while (beg <= last) do
     mid = (last + beg)/2

     if is_posible?(s, mid)
         min_k = mid
         last = mid - 1
     else
         beg = mid + 1
     end
  end
  min_k
end

puts solution(gets.strip())

module Memoizer
  def memoize(methodname)
    raise ArgumentError, "statefull method [#{methodname}]should not be memoized" if methodname.match(/\!\z/)
    unbound_method = instance_method(methodname)
    raise ArgumentError, "method [#{methodname}] with argument cannot be memoized" if unbound_method.arity > 0
    memoized_object_name = "@_memoized_#{methodname}"
    define_method(methodname) do
      if(instance_variable_get(memoized_object_name).nil?)
        instance_variable_set(memoized_object_name, unbound_method.bind(self).call)
      end
      instance_variable_get(memoized_object_name)
    end
  end
end

class Array
  def enumerate(&block)
    self.zip(0..length).map(&block)
  end
end
class Problem
  def initialize(xs)
    @xs = xs
    @last_index = Array.new(30){ -1 }
    @longests = Array.new(30){ -1 }
  end

  def solve!
    @xs.split("").each.with_index do |x, i|
      p = x.ord - 'a'.ord
      distance = i - @last_index[p]
      @longests[p] = [@longests[p], distance].max
      @last_index[p] = i
    end
    l = @xs.length
    (0..26).each do |x|
      distance = l - @last_index[x]
      @longests[x] = [@longests[x], distance].max
    end
  end

  attr_reader(:longests)
  attr_reader(:last_index)
end

prob = Problem.new(gets.strip)
prob.solve!
print prob.longests.select{|x| x >= 0 }.min

def is_posible?(s, k)
    n = s.size
    return true if n == k
    chars = {}
    s[0...k].split("").uniq.each{ |c| chars[c] = 1 }
    i = 1

    while i+k < n do
        chars[s[i+k-1]] += 1 if chars[s[i+k-1]] and chars[s[i+k-1]] > 0
        chars[s[i-1]] -= 1 if chars[s[i-1]] and chars[s[i-1]] > 0
        i += 1
    end

    chars.each do |k,v|
       return true if v > 0
    end
    false
end

def solution(s)
  beg = 1
  last = s.size - 1
  min_k = s.size

  while (beg <= last) do
     mid = (last + beg)/2

     if is_posible?(s, mid)
         min_k = mid
         last = mid - 1
     else
         beg = mid + 1
     end
  end
  min_k
end

puts solution(gets.strip())

def is_posible?(s, k)
    n = s.size
    return true if n == k
    chars = [0]*255
    s[0...k].split("").each{ |c| chars[c.ord] += 1 }
    i = 1

    while i+k <= n do
        puts "#{chars[97..122]}"
        chars[s[i+k-1].ord] += 1 if chars[s[i+k-1].ord] and chars[s[i+k-1].ord] > 0
        chars[s[i-1].ord] -= 1 if chars[s[i-1].ord]
        i += 1
    end
    puts "#{chars[97..122]}"

    chars.each do |v|
       return true if v > 0
    end
    false
end

def solution(s)
  beg = 1
  last = s.size - 1
  min_k = s.size

  while (beg <= last) do
     mid = (last + beg)/2

     if is_posible?(s, mid)
         min_k = mid
         last = mid - 1
     else
         beg = mid + 1
     end
  end
  min_k
end

puts solution(gets.strip())

def is_posible?(s, k)
    n = s.size
    return true if n == k
    chars = [0]*255
    s[0...k].split("").each{ |c| chars[c.ord] += 1 }
    i = 1

    while i+k < n do
        chars[s[i+k-1].ord] += 1 if chars[s[i+k-1].ord] and chars[s[i+k-1].ord] > 0
        chars[s[i-1].ord] -= 1 if chars[s[i-1].ord]
        i += 1
    end

    chars.each do |v|
       return true if v > 0
    end
    false
end

def solution(s)
  beg = 1
  last = s.size - 1
  min_k = s.size

  while (beg <= last) do
     mid = (last + beg)/2

     if is_posible?(s, mid)
         min_k = mid
         last = mid - 1
     else
         beg = mid + 1
     end
  end
  min_k
end

puts solution(gets.strip())

def is_posible?(s, k)
    n = s.size
    chars = s[0...k].split("").uniq

    i = 0
    while i+k < n do
        chars.each do |c|
            chars.delete(c) if !s[i...(i+k)].include?(c)
        end
        return false if chars.size == 0
        i += 1
    end
    true
end

def solution(s)
  beg = 1
  last = s.size - 1
  min_k = s.size

  while (beg <= last) do
     mid = (last + beg)/2

     if is_posible?(s, mid)
         min_k = mid
         last = mid - 1
     else
        puts mid
         beg = mid + 1
     end
  end
  min_k
end

s = gets.strip()


gets
a=gets.split.map{|e|e.to_i}.sort
r=0
(0..a.size/2-1).each{|i|r+=(a[i]+a[-i-1])**2}
p r

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

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

k = n/2
s = 0

(0..k-1).each do |i|
	s += (arr[i]+arr[n-1-i])**2
end

puts s

# Input
n = gets.to_i
a = gets.split.map { |i| i.to_i }

# Most effecient way: splitting into pairs from the middle

# Processing
a.sort!
sum = 0
(n / 2).times { |i| sum += (a[i] + a[n - 1 - i])**2 }

# Output
puts sum

DBG = true
n = gets.to_i
a = gets.split.map{|z| z.to_i}
  #a,b = gets.split
  #a = a.to_i
  #b = b.to_i

a.sort!
sum = 0
for i in 0...(n/2)
  sum += (a[i]+a[n-1-i])**2
end
puts "#{sum}"

n = gets.to_i
a = gets.split(" ").sort_by{|e| e.to_i}
r = 0
for i in 0..n/2-1
	r+=(a[i].to_i+a[n-1-i].to_i)**2
end
p r

gets
a=gets.split.map{|e|e.to_i}.sort
r=0
a.size.times{|i|r+=(a[i]+a[-i-1])**2}
p r/2

n = gets.to_i
a = gets.split(" ").map{|z| z.to_i}.sort
r = 0
for i in 0..n-1
	r+=(a[i]+a[n-1-i])**2
end
p r/2

n = gets.to_i
a = gets.split(" ")
for i in 0..n-1 do
	a[i] = a[i].to_i
end
a = a.sort
ans = 0
for i in 0..n/2-1 do
	ans+=(a[i]+a[n-1-i])*(a[i]+a[n-1-i])
end
p ans

gets
a = gets.split.map{|e| e.to_i}.sort
r=0
(0..a.size/2-1).each{|i|	r+=(a[i]+a[-i-1])**2}
p r


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

n,k = scan
aa = scan

res = 0.0
l = 0
sum = 0
aa.each_index{|r|
    if r-l >= k
        res += sum
        sum -= aa[l]
        l+=1
    end
    sum += aa[r]
}
res += sum
res /= n-k+1

p res


t=gets.chomp.to_i

while t!=0
n=gets.chomp.to_i
k=4.0

val=n/k
val=val.ceil()

l=n-val
l.times {print "#{9}"}
val.times{print "#{8}"}
puts
t-=1

end

require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  zero = n / 4
  if n % 4 == 0
    puts '9' * (n - zero)           + '8' * zero
  else
    puts '9' * (n - zero - 1) + '8' + '8' * zero
  end
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    # 1.upto (n) do |i|
    #     if i == n
    #         print "8"
    #         print "\n"
    #     else
    #         print "9"
    #     end
    # end
    atai = n / 4
    if n % 4 != 0
        hituyou = atai + 1
    else
        hituyou = atai
    end
    arr = "9" * (n - hituyou)
    print arr
    ar =  "8" * hituyou
    print ar
    print "\n"

end

gets.to_i.times do
    n = gets.to_i
    c = (n + 3) / 4
    (n - c).times do
        print 9
    end
    c.times do
        print 8
    end
    puts
end

def pow(str,n)
  if n == 0
    ""
  elsif n % 2 == 0
    pow((str+str), n/2)
  else
    str + pow(str, n-1)
  end
end
t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  num8 = 1 + (n-1)/4
  num9 = n - num8
  str = ""
  str = pow("9",num9) + pow("8",num8)
  ans << str
end
ans.each do |s|
  puts s
end

require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  puts '9' * (n - 1) + '8'
end

require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  zero = n / 4
  if n % 4 == 0
    puts '9' * (n - zero)           + '0' * zero
  else
    puts '9' * (n - zero - 1) + '8' + '0' * zero
  end
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    # 1.upto (n) do |i|
    #     if i == n
    #         print "8"
    #         print "\n"
    #     else
    #         print "9"
    #     end
    # end
    arr = "9" * (n-1)
    print arr
    print "8"
    print "\n"

end

gets.to_i.times do
    n = gets.to_i
    c = (n + 3) / 4
    (n - c).times do
        print 9
    end
    c.times do
        print 8
    end
end


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

answers = []

count.times do
  n = gets.chomp.to_i

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

  i = 0
  while a[i] == b[i] && i < n do
    i += 1
  end

  if i == n
    answers << "YES"
  elsif (k = a[i] - b[i]) > 0
    answers << "NO"
  else
    while i < n && a[i] - b[i] == k do
      i += 1
    end

    if i == n
      answers << "YES"
    elsif a[i] == b[i]
      i += 1
      while i < n && a[i] == b[i] do
        i += 1
      end

      if i == n
        answers << "YES"
      else
        answers << "NO"
      end
    else
      answers << "NO"
    end
  end
end

answers.each { |answer| puts answer }

g = gets.chomp.to_i
g.times{
n = gets.to_i
a = gets.chomp.split(" ")
b = gets.chomp.split(" ")
more = false
more2 = false
dif = []
n.times{|w|
dif.push(b[w].to_i-a[w].to_i)
if a[w].to_i > b[w].to_i
puts "NO"
more = true
break
end
}
if (!more)
if dif.uniq.size > 2 or (!dif.uniq.include?(0) and dif.uniq.size == 2)
  puts "NO"
else
 index = []
 dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }
 index.size.times{|w|
 if w!=0
 if index[w].to_i - index[w-1] != 1
 puts "NO"
 more2 = true
 break
 end
 end
 }
 if (!more2) then puts "YES" end
 end
end

}

def solve()
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = gets.split.map(&:to_i)
  done = false
  i = 0
  while i < n
    x = a[i] - b[i]
    if x != 0
      if done || x > 0
        puts "NO"
        return
      end
      done = true
      diff = x
      while i < n
        y = a[i] - b[i]
        if y == 0
          break
        elsif y != diff
          puts "NO"
          return
        end
        i += 1
      end
    end
    i += 1
  end
  puts "YES"
end

t = gets.to_i
t.times do
  solve()
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map &:to_i
  b = gets.split.map &:to_i
  i = 0
  i += 1 while i < n && b[i] - a[i] == 0
  diff = b[i] - a[i] if i < n
  yes = true
  while i < n
    d = b[i] - a[i]
    if d < 0
      yes = false
      break
    else
      if d != diff
        if d != 0
          yes = false
        end
        break
      end
    end
    i += 1
  end
  i += 1 while yes && i < n && b[i] - a[i] == 0
  yes = false if i < n
  puts yes ? 'YES' : 'NO'
end

g = gets.chomp.to_i
g.times{
n = gets.to_i
a = gets.chomp.split(" ")
b = gets.chomp.split(" ")
more = false
more2 = false
dif = []
n.times{|w|
dif.push(b[w].to_i-a[w].to_i)
if a[w].to_i > b[w].to_i
puts "NO"
more = true
break
end
}
if (!more)
if dif.uniq.size > 1 and !dif.uniq.include?(0)
  puts "NO"
  break
else
 index = []
 dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }
 index.size.times{|w|
 if w!=0
 if index[w].to_i - index[w-1] != 1
 puts "NO"
 more2 = true
 break
 end
 end
 }

 if (!more2) then puts "YES" end
 end
end

}

g = gets.chomp.to_i
g.times{
n = gets.to_i
a = gets.chomp.split(" ")
b = gets.chomp.split(" ")
more = false
more2 = false
dif = []
n.times{|w|
dif.push(b[w].to_i-a[w].to_i)
if a[w].to_i > b[w].to_i
puts "NO"
more = true
#break
end
}
if (!more)
if dif.uniq.size > 2 or (!dif.uniq.include?(0) and dif.uniq.size == 2)
  puts "NO"
else
 index = []
 dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }
 index.size.times{|w|
 if w!=0
 if index[w].to_i - index[w-1] != 1
 puts "NO"
 more2 = true
 #break
 end
 end
 }
 if (!more2) then puts "YES" end
 end
end

}

g = gets.chomp.to_i
g.times{
n = gets.to_i
a = gets.chomp.split(" ")
b = gets.chomp.split(" ")
more = false
more2 = false
dif = []
n.times{|w|
dif.push(b[w].to_i-a[w].to_i)
if a[w].to_i > b[w].to_i
puts "NO"
more = true
break
end
}
if (!more)
if dif.uniq.size > 2
puts "NO"
else
f = 0
dif.size.times{|w|
f+=dif[w].to_i
if dif[w].to_s == "0" and f.to_i > 0 and w != dif.size-1
puts "NO"
more2 = true
break
end
}
if (!more2)
puts "YES"
end

end

end

}

g = gets.chomp.to_i
g.times{
n = gets.to_i
a = gets.chomp.split(" ")
b = gets.chomp.split(" ")
more = false
more2 = false
dif = []
n.times{|w|
dif.push(b[w].to_i-a[w].to_i)
if a[w].to_i > b[w].to_i
puts "NO"
more = true
break
end
}
if (!more)
if dif.uniq.size > 2
 puts "NO"
else
 index = []
 dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }
 index.size.times{|w|
 if w!=0
 if index[w].to_i - index[w-1] != 1
 puts "NO"
 more2 = true
 break
 end
 end
 }

 if (!more2) then puts "YES" end
 end
end

}

g = gets.chomp.to_i
g.times{
n = gets.to_i
a = gets.chomp.split(" ")
b = gets.chomp.split(" ")
more = false
dif = []
n.times{|w|
if a[w].to_i < b[w].to_i then dif.push(b[w].to_i-a[w].to_i) end
if a[w].to_i > b[w].to_i
puts "NO"
more = true
break
end
}
if (!more)
if dif.uniq.size > 1 then puts "NO" else puts "YES" end
end

}

g = gets.chomp.to_i
g.times{
n = gets.to_i
a = gets.chomp.split(" ")
b = gets.chomp.split(" ")
more = false
more2 = false
dif = []
n.times{|w|
dif.push(b[w].to_i-a[w].to_i)
if a[w].to_i > b[w].to_i
puts "NO"
more = true
#break
end
}
if (!more)
if dif.uniq.size > 1 and !dif.uniq.include?(0)
  puts "NO"
else
 index = []
 dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }
 index.size.times{|w|
 if w!=0
 if index[w].to_i - index[w-1] != 1
 puts "NO"
 more2 = true
 #break
 end
 end
 }

 if (!more2) then puts "YES" end
 end
end

}


n = gets.to_i

chess = []

n.times do
  chess << gets.strip.split(' ').map(&:to_i)
end

m = gets.to_i

prog = []

m.times do
  prog << gets.strip.split(' ').map(&:to_i)
end

chess.sort! { |x,y| x[1] <=> y[1] }
prog.sort! { |x,y| y[0] <=> x[0] }

max_diff = 0

# chess.each do |c|
#   prog.each do |p|
#     break if c[0] - p[1] <= 0 && p[0] - c[1] <= 0
#     break if c[0] - p[1] <= max_diff && p[0] - c[1] <= max_diff
#     max_diff = [c[0] - p[1], max_diff].max
#     max_diff = [p[0] - c[1], max_diff].max
#   end
# end

# puts max_diff

lowest_in_chess = chess[0][1]
highest_in_prog = prog[0][0]
max_diff = [highest_in_prog - lowest_in_chess, max_diff].max

chess.sort! { |x,y| y[0] <=> x[0] }
prog.sort! { |x,y| x[1] <=> y[1] }
highest_in_chess = chess[0][0]
lowest_in_prog = prog[0][1]

max_diff = [highest_in_chess - lowest_in_prog, max_diff].max

puts max_diff

def f()
  lo, hi = 10**9, 0
  gets.to_i.times do
    l, r = gets.split.map { |s| s.to_i }
    if lo > r
      lo = r
    end
    if hi < l
      hi = l
    end
  end
  return lo, hi
end

a, b = f
c, d = f
p [0, b - c, d - a].max

def f()
  lo, hi = 10**9, 0
  gets.to_i.times do
    l, r = gets.split.map(&:to_i)
    if lo > r
      lo = r
    end
    if hi < l
      hi = l
    end
  end
  return lo, hi
end

a, b = f
c, d = f
p [0, b - c, d - a].max

n=gets.chomp.to_i
chess_start=[]
chess_end=[]
n.times do
    time=gets.chomp.split
    #puts "#{time[0]} #{time[1]}"
    chess_start<<time[0].to_i
    chess_end<<time[1].to_i
end
chess_start.sort!
chess_end.sort!
#puts chess_start.join(" ")
#puts chess_end.join(" ")
program_start=[]
program_end=[]
m=gets.chomp.to_i
m.times do
    time=gets.chomp.split
    #puts "#{time[0]} #{time[1]}"
    program_start<<time[0].to_i
    program_end<<time[1].to_i
end
program_start.sort!
program_end.sort!
#puts program_start.join(" ")
#puts program_end.join(" ")

rest=program_start[-1] - chess_end[0]
#puts "program maxest start time=#{program_start[-1]} chess minest end time=#{chess_end[0]}"
#puts "chess maxest start time=#{chess_start[-1]} program minest end time=#{program_end[0]}"
if chess_start[-1]-program_end[0] > rest
    rest = chess_start[-1]-program_end[0]
end
if rest < 0
    puts 0
else
    puts rest
end

n = gets.to_i
nt = n.times.map{gets.split.map(&:to_i)}
m = gets.to_i
mt = m.times.map{gets.split.map(&:to_i)}
mx = 0
mx = [mt.max_by{|l, r| l}[0] - nt.min_by{|l, r| r}[1], mx].max
mx = [nt.max_by{|l, r| l}[0] - mt.min_by{|l, r| r}[1], mx].max
p mx

n = gets.to_i

chess = []

n.times do
  chess << gets.strip.split(' ').map(&:to_i)
end

m = gets.to_i

prog = []

m.times do
  prog << gets.strip.split(' ').map(&:to_i)
end

chess.sort! { |x,y| x[0] <=> y[0] }
prog.sort! { |x,y| y[0] <=> x[0] }

max_diff = 0

chess.each do |c|
  prog.each do |p|
    break if p[0] <= c[1]
    max_diff = [c[1] - p[0], max_diff].max
    max_diff = [p[0] - c[1], max_diff].max
  end
end

puts max_diff


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

a_even = a.select{|e| e.even? }.size
a_odd = n - a_even

b_even = b.select{|e| e.even? }.size
b_odd = m - b_even

max = [a_even, b_odd].min + [a_odd, b_even].min
p max

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

a_odds = A.count(&:odd?)
a_evens = A.count(&:even?)
b_odds = B.count(&:odd?)
b_evens = B.count(&:even?)

puts [a_odds, b_evens].min + [a_evens, b_odds].min

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

asodd = as.count { |a| a.odd? }
aseven = n - asodd

bsodd = bs.count { |b| b.odd? }
bseven = m - bsodd

puts [asodd, bseven].min + [aseven, bsodd].min

n,m = gets.split.map(&:to_i)
a = gets.split.map{|i|i.to_i%2}
b = gets.split.map{|i|i.to_i%2}
puts [a.count(0),b.count(1)].min+[a.count(1),b.count(0)].min

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

cntC0 = 0
cntC1 = 0
cntK0 = 0
cntK1 = 0

chests.each do |c|
  if c % 2 == 0
    cntC0 += 1
  elsif c % 2 == 1
    cntC1 += 1
  end
end

keys.each do |k|
  if k % 2 == 0
    cntK0 += 1
  elsif k % 2 == 1
    cntK1 += 1
  end
end

puts [cntC0, cntK1].min + [cntC1, cntK0].min

n, m = gets.split.map &:to_i
a = gets.split.map &:to_i
b = gets.split.map &:to_i
f = a.select { |x| x%2 == 0 }.size
s = b.select { |x| x%2 == 0 }.size
puts [f, b.size - s].min + [a.size - f, s].min


n,m = gets.split.map(&:to_i)
puts n.times.map{ gets.split.map(&:to_f).inject(:/) }.min * m

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

bargains = []
n.times do
    bargains << gets.split().map { |s| s.to_i }
end

best_bargain = 2<<2048
bargains.each do |bargain|
    best_bargain = [best_bargain, bargain[0]/Float(bargain[1])].min
end

puts (best_bargain*m).round(8)

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

n.times{
    a, b = gets.split.map(&:to_i)
    ans = [ ans, (a * m) / b.to_f ].min
}

p ans

n, m = gets.split.map{|x| x.to_i}
t, f = 0.0, 101.0
n.times do
    a, b = gets.split.map{|x| x.to_i + 0.0}
    f = [f, a/b].min
    t = f*m
end
puts t

n,m = gets.chomp.split

n = n.to_i
m = m.to_f

# price = Array.new(n)
tprice = Array.new(n)

for i in 0...n
    a,b = gets.chomp.split
    tprice[i] = a.to_f / b.to_f
end

puts (tprice.min)*m

n,m=gets.split.map &:to_i
ans=1000000000.0
n.times do
    a,b=gets.split.map &:to_f
    cost=m*(a/b)
    ans=[ans,cost].min
end
p ans

n,m=gets.split.map &:to_i
ans=1000000000.0
n.times do
    a,b=gets.split.map &:to_f
    cost=m*(a/b)
    ans=ans<cost ? ans : cost
end
p ans

n , m = gets.split.map(&:to_i)
ar = []
n.times do
  s = gets.chomp.split.map(&:to_i)
  ar << m / (s[1] / (s[0] + 0.0))
end
puts ar.min

n, m = gets.split.map(&:to_i)
x = 10**9
n.times do
  a, b = gets.split.map(&:to_f)
  x = [x, a/b].min
end
puts x * m

n, m=gets.split.map &:to_i
puts "%.9f" % (((1..n).map{
    a, b=gets.split.map &:to_i
    a.to_r/b.to_r
}.min)*m)

inp = gets.chomp.split(" ")
score = []

wantbuy = inp[1].to_f

inp[0].to_i.times{

mag = gets.chomp.split(" ")
score.push((mag[0].to_f/mag[1].to_f)*wantbuy)
}
puts score.min.round(8)


t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = (1..n).to_a
  arr << arr.shift
  puts arr.join(' ')
end

t = gets.to_i

1.upto t do
  n = gets.to_i
  list = (1..n).to_a.reverse
  if n.odd?
    list[n/2], list[n/2+1] = list[n/2+1], list[n/2]
  end
  puts list.join(" ")
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    arr = [*1..n]
    i = arr.pop
    arr.unshift(i)
    puts arr.join(" ")
end

gets.to_i.times do
  x=gets.to_i
  if x==1
    puts 1
  else
    puts ([*2..x]+[1]).join(" ")
  end
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = (1..n).to_a
  puts arr.reverse.join(' ')
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = (1..n).to_a
  arr.map{|el| el + 1}
  arr[0] = n
  arr[-1] = 1
  puts arr.join(' ')
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = (1..n).to_a
  arr = arr.map{|el| el + 1}
  arr[0] = n
  arr[-1] = 1
  puts arr.join(' ')
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    arr = [*1..n]
    i = []
    until arr == []
        i << arr.pop
        if arr == []
            break
        end
        i << arr.shift
    end
    puts i.join(" ")
end

gets.to_i.times do
  puts [*1..gets.to_i].join(" ")
end