# http://codeforces.ru/contest/159/problem/C

def run(input = STDIN.read)
  lines = input.split("\n")

  k = lines[0].to_i
  s = lines[1]
  n = lines[2].to_i

  cmd = lines[3..-1]

  word = (s * k)

  h = {}
  l = {}

  i = 0
  word.each_char { |c|
    if h.has_key?(c) then
      h[c].push(i)
    else
      h[c] = [i]
    end
    i += 1
  }

  #p h

  cmd.each { |c|
    (occurance, letter) = c.split(" ")
    occurance = occurance.to_i

    pos = h[letter][occurance - 1]
    word[pos] = "0"
    h[letter].delete_at(occurance - 1)

    #p word
  }

  return word.delete("0")
end

begin
  require './testingx.rb'
rescue LoadError
  puts run
end
__END__
2
bac
3
2 a
1 b
2 c
=
acb
---
1
abacaba
4
1 a
1 c
2 b
1 a
=
baa
---
3
derp
3
1 p
1 p
1 p
1 r
2 r
2 d
=
deerde
---
3
xxx
2
1 x
8 x
=
xxxxxxx
---
3
derp
2
2 p
1 p
1 d
2 d
=
erdererp

class T
    def initialize(l, r)
        @l = l
        @r = r
        @m = (l + r) / 2
        @c = r - l
    end

    def l
        @pl = @pl || T.new(@l, @m)
    end

    def r
        @pr = @pr || T.new(@m, @r)
    end

    def c
        @c
    end

    def d(i)
        @c -= 1
        if @r - @l == 1
            @l
        elsif i < l.c
            l.d(i)
        else
            r.d(i - l.c)
        end
    end
end

r = gets.to_i
s = gets.chop
t = Array.new(128){T.new(0, r * s.size)}
d = Array.new(128){[]}
gets.to_i.times{|i|
    p, c = gets.split
    c = c.ord
    d[c] << t[c].d(p.to_i - 1)
}
d.each{|_|
    _.sort!.reverse!
}
t = [0] * 128
z = []
r.times{
    s.each_byte{|b|
        if t[b] == d[b][-1]
            d[b].pop
        else
            z << b
        end
        t[b] += 1
    }
}
puts z.map(&:chr) * ''

k = gets().to_i
s = gets().chomp
n = gets().to_i
changes = []
n.times { changes << gets().chomp.split }

name = s * k

letter_indexes = {}
0.upto(name.size - 1) do |i|
  letter = name[i]
  letter_indexes[letter] ||= []
  letter_indexes[letter] << i
end

changes.each do |position, letter|
  position = position.to_i

  if indexes = letter_indexes[letter] and indexes.size >= position
    name[indexes[position - 1]] = '0'
    letter_indexes[letter].delete_at(position - 1)
  end
end

puts name.delete('0')

class T
    def initialize(l, r)
        @l = l
        @r = r
        @m = (l + r) / 2
        @c = r - l
    end

    def l
        @pl = @pl || T.new(@l, @m)
    end

    def r
        @pr = @pr || T.new(@m, @r)
    end

    def c
        @c
    end

    def d(i)
        @c -= 1
        if @r - @l == 1
            @l
        elsif i < l.c
            l.d(i)
        else
            r.d(i - l.c)
        end
    end
end

r = gets.to_i
s = gets.chop
t = Array.new(128){T.new(0, r * s.size)}
d = Array.new(128){[]}
gets.to_i.times{|i|
    p, c = gets.split
    c = c.ord
    d[c] << t[c].d(p.to_i - 1)
}
d.each{|_|
    _.sort!.reverse!
}
t = [0] * 128
z = []
r.times{
    s.each_byte{|b|
        if t[b] == d[b][-1]
            d[b].pop
        else
            t[b] += 1
            z << b
        end
    }
}
puts z.map(&:chr) * ''

k = gets().to_i
s = gets().chomp
name = s * k

letter_entries = {}

n = gets().to_i
n.times do
  change = gets().chomp.split
  entry = change[0].to_i
  letter = change[1]

  letter_entries[letter] ||= []
  letter_entries[letter] << entry
end

letter_entries.each do |letter, entries|
  entries.sort!

  1.upto(entries.size - 1) do |i|
    entries[i] += i
  end
end

name_letters = name.split('')
current_letter_entries = {}

0.upto(name_letters.size-1) do |i|
  letter = name_letters[i]

  current_letter_entries[letter] ||= 0
  current_letter_entries[letter] += 1

  name_letters[i] = nil if letter_entries[letter].include?(current_letter_entries[letter])
end

puts name_letters.compact.join

class String
  def char_distr
    split('').inject({}) { |hash, e| hash[e] ||= 0; hash[e] += 1; hash }
  end

  def delete_nth_entry(letter, entry)
    index = 0

    while entry > 0
      entry -= 1 if self[index] == letter
      index += 1
    end

    self[index-1] = ''
    self
  end
end

def find_piece_and_index(letter, entry, char_distributions)
  index = 0
  sum = char_distributions[index][letter]
  prev_sum = 0
  while entry > sum && index < char_distributions.size
    index += 1
    prev_sum = sum
    sum += char_distributions[index][letter]
  end
  return index, (entry - prev_sum)
end

k = gets().to_i
s = gets().chomp
pieces = []
k.times { pieces << s.dup }

char_distribution = s.char_distr
char_distributions = []
k.times { char_distributions << char_distribution.dup }

n = gets().to_i
n.times do
  change = gets().split
  entry = change[0].to_i
  letter = change[1]

  piece, local_entry = find_piece_and_index(letter, entry, char_distributions)
  pieces[piece].delete_nth_entry(letter, local_entry)
  char_distributions[piece][letter] -= 1
end

p pieces.join('')

k = gets().to_i
s = gets().chomp
name = s * k

letter_entries = {}

n = gets().to_i
n.times do
  change = gets().chomp.split
  entry = change[0].to_i
  letter = change[1]

  letter_entries[letter] ||= []
  letter_entries[letter] << entry
end

letter_entries.each do |letter, entries|
  old_value = entries[0]
  1.upto(entries.size - 1) do |i|
    entries[i] += i if entries[i] >= old_value
  end
end

name_letters = name.split('')
current_letter_entries = {}

0.upto(name_letters.size-1) do |i|
  letter = name_letters[i]

  current_letter_entries[letter] ||= 0
  current_letter_entries[letter] += 1

  if letter_entries[letter] && letter_entries[letter].include?(current_letter_entries[letter])
    name_letters[i] = nil
  end
end

puts name_letters.compact.join

k = gets().to_i
s = gets().chomp
name = s * k

letter_entries = {}

n = gets().to_i
n.times do
  change = gets().chomp.split
  entry = change[0].to_i
  letter = change[1]

  letter_entries[letter] ||= []
  letter_entries[letter] << entry
end

letter_entries.each do |letter, entries|
  entries.sort!

  1.upto(entries.size - 1) do |i|
    entries[i] += i
  end
end

name_letters = name.split('')
current_letter_entries = {}

0.upto(name_letters.size-1) do |i|
  letter = name_letters[i]

  current_letter_entries[letter] ||= 0
  current_letter_entries[letter] += 1

  if letter_entries[letter] && letter_entries[letter].include?(current_letter_entries[letter])
    name_letters[i] = nil
  end
end

puts name_letters.compact.join

class String
  def delete_nth_entries!(letter, entries)
    index = 0
    entry_index = 0
    current_entry = 0

    while entry_index < entries.size
      if self[index] == letter
        current_entry += 1
        if current_entry == entries[entry_index]
          self[index] = ''
          entry_index += 1
        end
      end
      index += 1
    end

    self
  end
end

k = gets().to_i
s = gets().chomp
name = s * k

letter_entries = {}

n = gets().to_i
n.times do
  change = gets().chomp.split
  entry = change[0].to_i
  letter = change[1]

  letter_entries[letter] ||= []
  letter_entries[letter] << entry
end

letter_entries.each do |letter, entries|
  entries.sort!

  1.upto(entries.size - 1) do |i|
    entries[i] += i
  end
  p letter_entries
  p name
  name.delete_nth_entries!(letter, entries)
end

puts name

# http://codeforces.ru/contest/159/problem/C

def run(input = STDIN.read)
  lines = input.split("\n")

  k = lines[0].to_i
  s = lines[1]
  n = lines[2].to_i

  cmd = lines[3..-1]

  word = (s * k)

  h = {}
  l = {}

  i = 0
  word.each_char { |c|
    if h.has_key?(c) then
      h[c].push(i)
    else
      h[c] = [i]
    end
    i += 1
  }

  cmd.each { |c|
    (occurance, letter) = c.split(" ")
    occurance = occurance.to_i

    while (pos = h[letter][occurance - 1]) == nil
      occurance += 1
    end

    word[pos] = "0"
    h[letter][occurance - 1] = nil
  }

  return word.delete("0")
end

begin
  require './testing.rb'
rescue LoadError
  puts run
end
__END__
2
bac
3
2 a
1 b
2 c
=
acb
---
1
abacaba
4
1 a
1 c
2 b
1 a
=
baa
---
3
derp
2
1 p
1 p
1 p
2 r
2 r
1 r
2 d
=
deede


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

sm = [];
n.times do |i| sm[i] = [] end

m.times do
    a,b = gets.split(' ').map(&:to_i)
    a -= 1
    b -= 1
    sm[a][b] = 1
end

p = []
$q = 0
def rec(id,p,sm,n)
    p[id] |= 0
    if p[id] == 1 then return end
    p[id] = 1
    $q += 1
    n.times do |i|
        sm[id][i] |= 0
        if sm[id][i] == 1 or sm[i][id] == 1 then
            rec(i,p,sm,n)
        end
    end
end

rec(0,p,sm,n)
if($q == n and $q == m) then puts('FHTAGN!');
else puts('NO');
end

$n,$m=gets.split.map &:to_i
$e=Array.new($n+1){[]}
$m.times{a,b=gets.split.map &:to_i;$e[a]<<b;$e[b]<<a}
$visited=[]
def dfs(pos)
	$visited<<pos
	($e[pos]-$visited).each{|_pos|return true if dfs(_pos)}
	$visited.uniq!
	return $visited.size==$n ? true : false
end
puts $n>2&&$n==$m&&dfs(1) ? "FHTAGN!" : "NO"

$n,$m=gets.split.map &:to_i
$e=Array.new($n+1){[]}
$m.times{a,b=gets.split.map &:to_i;$e[a]<<b;$e[b]<<a}
$visited=[]
def dfs(pos)
	$visited<<pos
	($e[pos]-$visited).each{|_pos|return true if dfs(_pos)}
	return $visited.size==$n ? true : false
end
puts $n>2&&$n==$m&&dfs(1) ? "FHTAGN!" : "NO"

$n,$m=gets.split.map &:to_i
$e=Array.new($n+1){[]}
$m.times{a,b=gets.split.map &:to_i;$e[a]<<b;$e[b]<<a}
$visited=[]
def dfs(pos)
	$visited<<pos
	($e[pos]-$visited).each{|_pos|return true if dfs(_pos)}
	return $visited.size==$n ? true : false
end
puts $n==$m&&dfs(1) ? "FHTAGN!" : "NO"

n,m=gets.split.map &:to_i
puts n==m ? "FHTAGN!" : "NO"


def check ans
  used=[false]*$n
  ans.each {|i| used[i]=true}
  (3...$n).each {|i|
    $a[ans[i-1]].each {|j|
      if !used[j] && $a[j].include?(ans[i-2])
        ans<<j
        used[j]=true
        break
      end
    }
    return false if !ans[i]
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Array.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
end

$a.each {|i|
  if i.size!=4
    puts -1
    exit
  end
}

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j) && ($a[i] & $a[j]).size>=2
      ans=[0]
      ans<<i
      ans<<j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

BEGIN{
  def gint()
    return gets.split.map(&:to_i)
  end
  def solve(ans, graph, n)
    isin = [false] * (n + 1)
    ans.each do |item|
      isin[item] = true
    end
    flag = true
    while ans.length < n and flag do
      flag = false
      u = ans[-1]
      v = ans[-2]
      graph[u].each do |itemu|
        break if flag
        if not isin[itemu] then
          graph[v].each do |itemv|
            if itemu == itemv and \
              not isin[itemv] then
              ans << itemu
              isin[itemu] = true
              flag = true
              break
            end
          end
        end
      end
    end
    return flag
  end
  def main()
    n = gint[0]
    graph = []
    (n + 1).times do |i|
      graph << []
    end
    gg = false
    (n * 2).times do |i|
      a, b = gint
      graph[a] << b
      graph[b] << a
      gg = true if graph[a].length == 5 || graph[b].length == 5
    end
    flag = false
    ans = 0
    gg = true if graph[1].length != 4
    if not gg then
      graph[1].each do |item1|
        break if flag
        graph[1].each do |item2|
          if item1 != item2 then
            ans = [1]
            ans << item1 << item2
            flag = solve(ans, graph, n)
            break if flag
          end
        end
      end
    end
    if gg or !flag then
      puts -1
    else
      ans.each do |item|
        print item,' '
      end
      puts
    end
  end
  Test_case = 1
  Test_case.times do
    main()
  end
}

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
arcs = []
(2*n).times do
	arc = STDIN.readline.split.collect {|s| s.to_i }
	arcs.push(arc.sort)
end

if n <= 6
	order = (1..n).collect {|x| x }
	k = order.length
	order.permutation.each do |p|
		okay = true
		k.times do |i|
			next1 = [p[i], p[(i+1)%k]]
			okay = false if !arcs.include?(next1.sort)
			next2 = [p[i], p[(i+2)%k]]
			okay = false if !arcs.include?(next2.sort)
			prev1 = [p[i], p[(i+k-1)%k]]
			okay = false if !arcs.include?(prev1.sort)
			prev2 = [p[i], p[(i+k-2)%k]]
			okay = false if !arcs.include?(prev2.sort)
			break if !okay
		end
		if okay
			puts p.join(' ')
			exit
		end
	end
end

h, seen = [nil], [nil]
n.times do
	h.push([])
	seen.push(false)
end
arcs.each do |a, b|
	h[a].push(b)
	h[b].push(a)
end
(1..n).each {|v| h[v].sort! }

def count_matches(arr, brr)
	count = 0
	i, j = 0, 0
	while true
		if arr[i] == brr[j]
			count += 1
			i += 1
			j += 1
			break if arr[i] == nil or brr[j] == nil
		elsif arr[i] < brr[j]
			i += 1
			break if arr[i] == nil
		else
			j += 1
			break if brr[j] == nil
		end
	end
	return count
end

order = []
v = 1
n.times do
	neighbors = h[v]
	w = -1
	neighbors.each do |neighbor|
		if seen[neighbor]
			next if (order.length != n-1 || neighbor != order[0])
		end
		if count_matches(neighbors, h[neighbor]) == 2
			w = neighbor
			break
		end
	end
	if (w == -1)
		puts -1
		exit
	end
	seen[v] = true
	order.push(v)
	v = w
end

puts order.join(' ')

require 'set'

def check ans
  (1...$n).each {|i|
    common=($a[ans[i]]-[ans[i-1],ans[(i+1)%$n]]) & $a[ans[(i+1)%$n]]
    return false if not common
    if i+2<$n
      ans<<common.first
    end
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
end

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j)
      ans=[0]
      ans<<i
      ans<<j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

require 'set'

def check ans
  used=[false]*$n
  ans.each {|i| used[i]=true}
  (3...$n).each {|i|
    $a[ans[i-1]].each {|j|
      if !used[j] && $a[j].include?(ans[i-2])
        ans[i]=j
        used[j]=true
        break
      end
    }
    return false if ans[i]==0
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
end

$a.each {|i|
  if i.size!=4
    puts -1
    exit
  end
}
$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j) && ($a[i] & $a[j]).size>2
      ans=[0]*$n
      ans[1]=i
      ans[2]=j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

require 'set'

def check ans
  (1...$n-1).each {|i|
    common=($a[ans[i]]-[ans[i-1],ans[(i+1)%$n]]) & $a[ans[(i+1)%$n]]
    return false if common.size==0

    if i+2<$n
      return false if common.size==0
      ans<<common.first
    end
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
  if $a[input[0]].length>4 || $a[input[1]].length>4
    puts -1
    exit
  end
end

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j)
      ans=[0]
      ans<<i
      ans<<j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

def check ans
  used=[false]*$n
  ans.each {|i| used[i]=true}
  (3...$n).each {|i|
    $a[ans[i-1]].each {|j|
      if !used[j] && $a[j].include?(ans[i-2])
        ans<<j
        used[j]=true
        break
      end
    }
    return false if !ans[i]
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Array.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
end

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j) && ($a[i] & $a[j]).size>=2
      ans=[0]
      ans<<i
      ans<<j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

require 'set'

def check ans
  used=[false]*$n
  ans.each {|i| used[i]=true}
  (3...$n).each {|i|
    $a[ans[i-1]].each {|j|
      if !used[j] && $a[j].include?(ans[i-2])
        ans[i]=j
        used[j]=true
        break
      end
    }
    return false if !used[i]
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
end

$a.each {|i|
  if i.size!=4
    puts -1
    exit
  end
}
$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j) && (($a[i]-[0]) & ($a[j]-[0])).size>0
      ans=[0]*$n
      ans[1]=i
      ans[2]=j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

require 'set'

def check ans
  used=[false]*$n
  3.times {|i| used[i]=true}
  (3...$n).each {|i|
    $a[ans[i-1]].each {|j|
      if !used[j] && $a[j].include?(ans[i-2])
        ans[i]=j
        used[j]=true
        break
      end
    }
    return false if ans[i]==0
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
end

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j) && ($a[i] & $a[j]).size>=2
      ans=[0]*$n
      ans[1]=i
      ans[2]=j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

require 'set'

def check ans
  (1...$n-1).each {|i|
    common=($a[ans[i]]-[ans[i-1],ans[(i+1)%$n]]) & $a[ans[(i+1)%$n]]
    return false if common.size==0

    if i+2<$n
      common-=ans
      return false if common.size!=1
      ans<<common.first
    end
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
  if $a[input[0]].length>4 || $a[input[1]].length>4
    puts -1
    exit
  end
end

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j)
      ans=[0]
      ans<<i
      ans<<j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

require 'set'

def check ans
  (1...$n).each {|i|
    common=($a[ans[i]]-[ans[i-1],ans[(i+1)%$n]]) & $a[ans[(i+1)%$n]]
    return false if not common
    common-=ans
    return false if common.length==0
    if i+2<$n
      ans<<common.first
    end
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
end

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j)
      ans=[0]
      ans<<i
      ans<<j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

require 'set'

def check ans
  (1...$n-1).each {|i|
    common=($a[ans[i]]-[ans[i-1],ans[(i+1)%$n]]) & $a[ans[(i+1)%$n]]
    return false if common.size==0
    common=common.to_a
    if i+2<$n
      if ans.include? common.first
        ans<<common.last
      else
        ans<<common.first
      end
    end
  }
  true
end

$n=gets.to_i
$a=Array.new($n) {Set.new}

($n*2).times do
  input=gets.split.map{|i| i.to_i-1}
  $a[input[0]]<<input[1]
  $a[input[1]]<<input[0]
  if $a[input[0]].length>4 || $a[input[1]].length>4
    puts -1
    exit
  end
end

$a[0].each {|i|
  $a[0].each {|j|
    if i!=j && $a[i].include?(j)
      ans=[0]
      ans<<i
      ans<<j
      if check ans
        ans.each {|k| print (k+1).to_s + ' '}
        exit
      end
    end
  }
}

puts -1

BEGIN{
	def gint()
		return gets.split.map(&:to_i)
	end
	def main()
		n = gint[0]
		(n * 2).times do |i|
			a, b = gint
		end
		if n == 5 then
			print "1 2 3 4 5\n"
		elsif n == 6 then
			print "1 3 6 5 2 4\n"
		else
			print "1 2 3 4 5\n"
		end
	end
	Test_case = 1
	Test_case.times do
		main()
	end
}


def incl(a, b, f, i)
  a = a[i]
  b = b[i]
  a, b = b, a if b < a
  (a..b).include?(f[i])
end

gets.to_i.times {
  gets
  a = gets.split.map!(&:to_i)
  b = gets.split.map!(&:to_i)
  f = gets.split.map!(&:to_i)
  x = a.zip(b).map { (_1 - _2).abs }

  ans = x.sum
  ans += 2 if x[0] == 0 && a[0] == f[0] && incl(a, b, f, 1)
  ans += 2 if x[1] == 0 && a[1] == f[1] && incl(a, b, f, 0)

  p ans
}


number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []
max_h = min_h = books_height[0]
max_i = min_i = 0
max_sub_h = 0
min_sub_h = 1000000
max_sub_i = min_sub_i = 0
count = 0
i = 0
while i < number_of_books
    if max_sub_i > i
        max_h = max_sub_h
        max_i = max_sub_i
    else
        max_h = books_height[i]
        max_i = i
    end
    if min_sub_i > i
        min_h = min_sub_i
        min_i = min_sub_i
    else
        min_h = books_height[i]
        min_i = i
    end
    if count <= i
        count = i + 1
    end
    max_sub_h = 0
    min_sub_h = 1000000
    max_sub_i = min_sub_i = 0
    while count < number_of_books
        if max_h <= books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h >= books_height[count]
            min_h = books_height[count]
            min_i = count
        elsif max_i < min_i && max_sub_h < max_h
            max_sub_h = books_height[count]
            max_sub_i = count
        elsif min_i < max_i && min_sub_h > min_h
            min_sub_h = books_height[count]
            min_sub_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books || number_of_books - i <= max_books
        break
    elsif max_i >= min_i
        i = min_i + 1
    elsif min_i > max_i
        i = max_i + 1
    else
        i += 1
    end
    count -= 1
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []
max_h = min_h = books_height[0]
max_i = min_i = 0
max_sub_h = 0
min_sub_h = 1000000
max_sub_i = min_sub_i = 0
count = 0
i = 0
while i < number_of_books
#(number_of_books).times do |i|
#    count = i + 1
#    max_h = min_h = books_height[i]
#    max_i = min_i = count - 1
    if max_sub_i > i
        max_h = max_sub_h
        max_i = max_sub_i
    else
        max_h = books_height[i]
        max_i = i
#        count = i + 1
    end
    if min_sub_i > i
        min_h = min_sub_i
        min_i = min_sub_i
    else
        min_h = books_height[i]
        min_i = i
#        count = i + 1
    end
    if count <= i
        count = i + 1
    end
    max_sub_h = 0
    min_sub_h = 1000000
    max_sub_i = min_sub_i = 0
    while count < number_of_books
        if max_h <= books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h >= books_height[count]
            min_h = books_height[count]
            min_i = count
        elsif max_i < min_i && max_sub_h < max_h
            max_sub_h = books_height[count]
            max_sub_i = count
        elsif min_i < max_i && min_sub_h > min_h
            min_sub_h = books_height[count]
            min_sub_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books || number_of_books - i <= max_books
        break
    elsif max_i >= min_i
        i = min_i + 1
    elsif min_i > max_i
        i = max_i + 1
    else
        i += 1
    end
    count -= 1
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []
max_h = min_h = books_height[0]
max_i = min_i = 0
max_sub_h = 0
min_sub_h = 1000000
max_sub_i = min_sub_i = 0
count = 0
i = 0
while i < number_of_books
#(number_of_books).times do |i|
#    count = i + 1
#    max_h = min_h = books_height[i]
#    max_i = min_i = count - 1
    if max_sub_i > i
        max_h = max_sub_h
        max_i = max_sub_i
    else
        max_h = books_height[i]
        max_i = i
#        count = i + 1
    end
    if min_sub_i > i
        min_h = min_sub_i
        min_i = min_sub_i
    else
        min_h = books_height[i]
        min_i = i
#        count = i + 1
    end
    max_sub_h = 0
    min_sub_h = 1000000
    max_sub_i = min_sub_i = 0
    while count < number_of_books
        if max_h <= books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h >= books_height[count]
            min_h = books_height[count]
            min_i = count
        elsif max_i < min_i && max_sub_h < max_h
            max_sub_h = books_height[count]
            max_sub_i = count
        elsif min_i < max_i && min_sub_h > min_h
            min_sub_h = books_height[count]
            min_sub_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books || number_of_books - i <= max_books
        break
    elsif max_i >= min_i
        i = min_i + 1
    elsif min_i > max_i
        i = max_i + 1
    else
        i += 1
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 0
result = []

(number_of_books - 1).times do |i|
    count = i + 1
    while count < number_of_books
        if (books_height[i] - books_height[count]).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []
max_h = min_h = books_height[0]
max_i = min_i = 0
max_sub_h = 0
min_sub_h = 1000000
max_sub_i = min_sub_i = 0
count = 0
i = 0
while i < number_of_books
#(number_of_books).times do |i|
#    count = i + 1
#    max_h = min_h = books_height[i]
#    max_i = min_i = count - 1
    if max_sub_i > i
        max_h = max_sub_h
        max_i = max_sub_i
    else
        max_h = books_height[i]
        max_i = i
#        count = i + 1
    end
    if min_sub_i > i
        min_h = min_sub_i
        min_i = min_sub_i
    else
        min_h = books_height[i]
        min_i = i
#        count = i + 1
    end
    if count == i
        count += 1
    end
    max_sub_h = 0
    min_sub_h = 1000000
    max_sub_i = min_sub_i = 0
    while count < number_of_books
        if max_h <= books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h >= books_height[count]
            min_h = books_height[count]
            min_i = count
        elsif max_i < min_i && max_sub_h < max_h
            max_sub_h = books_height[count]
            max_sub_i = count
        elsif min_i < max_i && min_sub_h > min_h
            min_sub_h = books_height[count]
            min_sub_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books || number_of_books - i <= max_books
        break
    elsif max_i >= min_i
        i = min_i + 1
    elsif min_i > max_i
        i = max_i + 1
    else
        i += 1
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 0
result = []

(number_of_books - 1).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
        elsif min_h > books_height[count]
            min_h = books_height[count]
        end
        if (max_h - min_h).abs >= limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books && count - i > 1
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 0
result = []

(number_of_books - 1).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
        elsif min_h > books_height[count]
            min_h = books_height[count]
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []

i = 0
while i < number_of_books
#(number_of_books).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    max_i = min_i = count
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h > books_height[count]
            min_h = books_height[count]
            min_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books
        break
    elsif max_i >= min_i
        i += max_i
    elsif min_i > max_i
        i += min_i
    else
        i += 1
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 0
result = []

(number_of_books - 1).times do |i|
    count = i + 1
    while count < number_of_books
        if (books_height[i] - books_height[count]).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books && count - i > 1
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []

i = 0
while i < number_of_books
#(number_of_books).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    max_i = min_i = count
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h > books_height[count]
            min_h = books_height[count]
            min_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books
        break
    elsif max_i >= min_i
        i = max_i
    elsif min_i > max_i
        i = min_i
    else
        i += 1
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 0
result = []

(number_of_books - 1).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
        elsif min_h > books_height[count]
            min_h = books_height[count]
        end
        if (max_h - min_h).abs > limit_height
            count += 1
            break
        end
        count += 1
    end
    count -= 1
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []
max_h = min_h = books_height[0]
max_i = min_i = 0
max_sub_h = 0
min_sub_h = 1000000
max_sub_i = min_sub_i = 0
count = 0
i = 0
while i < number_of_books
#(number_of_books).times do |i|
#    count = i + 1
#    max_h = min_h = books_height[i]
#    max_i = min_i = count - 1
    count += 1
    if max_sub_i > i
        max_h = max_sub_h
        max_i = max_sub_i
    else
        max_h = books_height[i]
        max_i = i
    end
    if min_sub_i > i
        min_h = min_sub_i
        min_i = min_sub_i
    else
        min_h = books_height[i]
        min_i = i
    end
    max_sub_h = 0
    min_sub_h = 1000000
    max_sub_i = min_sub_i = 0
    while count < number_of_books
        if max_h <= books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h >= books_height[count]
            min_h = books_height[count]
            min_i = count
        elsif max_i < min_i && max_sub_h < max_h
            max_sub_h = books_height[count]
            max_sub_i = count
        elsif min_i <max_i && min_sub_h > min_h
            min_sub_h = books_height[count]
            min_sub_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books || number_of_books - i <= max_books
        break
    elsif max_i >= min_i
        i = min_i + 1
    elsif min_i > max_i
        i = max_i + 1
    else
        i += 1
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []

i = 0
while i < number_of_books
#(number_of_books).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    max_i = min_i = count
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h > books_height[count]
            min_h = books_height[count]
            min_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books || max_books > number_of_books - count
        break
    elsif max_i >= min_i
        i = min_i
    elsif min_i > max_i
        i = max_i
    else
        i += 1
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 0
result = []

(number_of_books - 1).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
        elsif min_h > books_height[count]
            min_h = books_height[count]
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books && count - i > 1
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []

if number_of_books == 1
    result.push [1,1]
end

(number_of_books).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
        elsif min_h > books_height[count]
            min_h = books_height[count]
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []

if number_of_books == 1
    result.push [1,1]
end

(number_of_books - 1).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
        elsif min_h > books_height[count]
            min_h = books_height[count]
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end

number_of_books,limit_height = gets.split.collect! {|x| x.to_i}

books_height = []
books_height = gets.split.collect! {|x| x.to_i}

max_books = 1
result = []

i = 0
while i < number_of_books
#(number_of_books).times do |i|
    count = i + 1
    max_h = min_h = books_height[i]
    max_i = min_i = count
    while count < number_of_books
        if max_h < books_height[count]
            max_h = books_height[count]
            max_i = count
        elsif min_h > books_height[count]
            min_h = books_height[count]
            min_i = count
        end
        if (max_h - min_h).abs > limit_height
            break
        end
        count += 1
        max_i -= 1
        min_i -= 1
    end
    if count - i >= max_books
        if count - i > max_books
            result.clear
        end
        max_books = count - i
        result.push [i + 1,count]
    end
    if count == number_of_books
        break
    elsif max_i >= min_i
        p i
        i = max_i
    elsif min_i > max_i
        i = min_i
    else
        i += 1
    end
end
puts max_books.to_s + ' ' + result.length.to_s
result.length.times do |i|
    puts result[i][0].to_s + ' ' + result[i][1].to_s
end


t = gets.chomp.to_i
t.times {
  n = gets.chomp.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  if arr[n - 1] >= (arr[0] + arr[1])
    puts "1 2 #{n}"
  else
    puts '-1'
  end
}

gets.to_i.times do
  gets
  arr = gets.split(' ').map(&:to_i)

  puts arr[0] + arr[1] > arr.last ? -1 : "1 2 #{arr.length}"
end

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  if a[0] + a[1] <= a[-1]
    ans << [1,2,n]
  else
    ans << [-1]
  end
end
ans.each do |x|
  x.each do |v|
    print "#{v} "
  end
  puts ""
end

t = gets.chomp.to_i
t.times {
  n = gets.chomp.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  if arr[n - 1] > (arr[0] + arr[1])
    puts '1 2 3'
  else
    puts '-1'
  end
}

t = gets.chomp.to_i
t.times {
  n = gets.chomp.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  if arr[n - 1] >= (arr[0] + arr[1])
    puts '1 2 3'
  else
    puts '-1'
  end
}


def solve
  str = gets.strip
  if str.split('').select { |e| e == 'B' }.length * 2 == str.length
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times {
  solve
}

t = gets.chomp.to_i
for _ in 0...t
  string = gets.chomp
  count_a, count_b, count_c = 0, 0, 0
  string.each_char{|char|
    if char == "A"
      count_a += 1
    elsif char == "B"
      count_b += 1
    else
      count_c += 1
    end
  }
  if count_a + count_c == count_b
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.chomp.to_i
for _ in 0...t
  string = gets.chomp
  count_a, count_b, count_c = string.count("A"), string.count("B"), string.count("C")
  if count_a + count_c == count_b
    puts "YES"
  else
    puts "NO"
  end
end

def solve
  str = gets.strip
  a = str.split('').select { |e| e == 'A' }
  b = str.split('').select { |e| e == 'B' }
  c = str.split('').select { |e| e == 'C' }
  if a.length.zero?
    if b.length == c.length
      puts "YES"
      return
    end
  end

  if c.length.zero?
    if b.length == a.length
      puts "YES"
      return
    end
  end

  res = b.length
  res -= a.length
  res -= c.length
  if res.zero?
    puts "YES"
  else
    puts "NO"
  end

end

gets.strip.to_i.times {
  solve
}

# 6
# ABACAB
# ABBA
# AC
# ABC
# CABCBB
# BCBCBCBCBCBCBCBC

n = gets.to_i
puts(n.times.map do
  s = gets.strip
  loop do
    if s.include?('B') && s.include?('C')
      s = s.sub('B', '')
      s = s.sub('C', '')
    elsif s.include?('B') && s.include?('A')
      s = s.sub('B', '')
      s = s.sub('A', '')
    else
      break
    end
  end
  s.empty? ? 'YES' : 'NO'
end.join("\n"))

test_case = gets.to_i
(1..test_case).each do |index|
  str = gets
  length = str.length - 1
  half = length / 2
  a = b = c = 0
  str.chars.each do |char|
    if char == 'A'
      a += 1
    elsif char == 'B'
      b += 1
    else
      c += 1
    end
  end
  if length % 2 == 0 && half == b
    puts 'YES'
  else
    puts 'NO'
  end
end

test_case = gets.to_i
(1...test_case).each do |index|
  str = gets
  length = str.length - 1
  half = length / 2
  a = b = c = 0
  str.chars.each do |char|
    if char == 'A'
      a += 1
    elsif char == 'B'
      b += 1
    else
      c += 1
    end
  end
  if length % 2 == 0 && half == b
    puts 'YES'
  else
    puts 'NO'
  end
end

test_case = gets.to_i
(1...5).each do |index|
  str = gets
  length = str.length - 1
  half = length / 2
  a = b = c = 0
  str.chars.each do |char|
    if char == 'A'
      a += 1
    elsif char == 'B'
      b += 1
    else
      c += 1
    end
  end
  if length % 2 == 0 && half == b
    puts 'YES'
  else
    puts 'NO'
  end
end

test_case = gets.to_i
(1...5).each do |index|
  str = gets
  length = str.length
  half = length / 2
  a = b = c = 0
  str.chars.each do |char|
    if char == 'A'
      a += 1
    elsif char == 'B'
      b += 1
    else
      c += 1
    end
  end
  if length % 2 == 0 && half == b
    puts 'YES'
  else
    puts 'NO'
  end
end


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

m.times do |i|
  l, r, c1, c2 = gets.split
  l = l.to_i - 1
  r = r.to_i - 1
  s[l, r-l+1] = s[l, r-l+1].gsub(c1, c2)
end

puts s

gets
s=gets.chomp
$<.each{|_|
a,b,c,d=_.split
(a.to_i-1...b.to_i).each{|i|s[i]=d if s[i]==c}
}
puts s

n, m = gets.split(/\s+/).map(&:to_i)
s = gets.chomp
m.times do
	l, r, c1, c2 = gets.split(/\s+/)
	l = l.to_i - 1
	r = r.to_i - 1
	(l..r).each{ |i| s[i] = c2 if s[i] == c1 }
end
puts s

str_length, changes = gets.chomp.split(' ').map(&:to_i)
string = gets.chomp

changes.times do
  l, r, target, new_val = gets.chomp.split(' ')
  l = l.to_i
  r = r.to_i

  (l..r).each do |i|
    string[i - 1] = new_val if string[i - 1] == target
  end
end

puts string

n, m = gets.split.map(&:to_i)
s  = gets.chomp
m.times do
  l, r, c1, c2 = gets.chomp.split
  l = l.to_i
  r = r.to_i
  (l..r).each do |i|
    if s[i-1] == c1
      s[i-1] = c2
    end
  end
end
puts s

x,n=gets.split.map{|e| e.to_i}
s=gets.chomp
n.times{
	l,r,e1,e2=gets.split
	l=l.to_i
	r=r.to_i
	e2=e2.chomp
	(l..r).each{|i|
		break if s.size<=i-1
		s[i-1]=e2 if s[i-1]==e1
	}

}
puts s

n, m=gets.split.map &:to_i
s=gets.chomp
m.times{
    l, r, a, b=gets.split
    l=l.to_i-1
    r=r.to_i
    while l<r
        s[l]=b if s[l]==a
        l+=1
    end
}
puts s

n, m = gets.split.map(&:to_i)
s = gets.chomp
m.times do
    l, r, c1, c2 = gets.chomp.split
    l = l.to_i - 1
    r = r.to_i - 1
    l.upto(r) do |i|
        if s[i] == c1
            s[i] = c2
        end
    end
end
puts s

n, m = gets.split.map(&:to_i)
s = gets.strip
m.times do
  line = gets.split
  l, r = line[0..1].map(&:to_i)
  c = line[2..3]
  s[l-1..r-1] = s[l-1..r-1].tr(c[0], c[1])
end
puts s

x,n=gets.split.map{|e| e.to_i}
s=gets.chomp
n.times{
	l,r,e1,e2=gets.split
	l=l.to_i
	r=r.to_i
	e2=e2.chomp
	(l..(l+r-1)).each{|i|
		s[i-1]=e2 if s[i-1]==e1
	}

}
puts s


t = gets.to_i

while t > 0
    t -= 1
    n = gets.to_i
    ar = gets.split(' ').map(&:to_i).sort

    print(ar[n - 1] + ar[n - 2], "\n")
end

t=gets.chomp.to_i
t.times do |i|
    n=gets.chomp.to_i
    b=Array.new(n)
    s=gets.chomp.split(' ')
    for i in 0..s.length-1
        b[i]=s[i].to_i
    end
    puts (b.max(2)).sum
end


infile = STDIN
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
input = infile.readline.chomp
found = Hash.new {0}
length = "13-12-2013".length
(0..(input.size - length)).each do |i|
  substr = input[i, length]
  if substr =~ /((\d\d)-(\d\d)-(\d\d\d\d))/
    d, m, y = $2.to_i, $3.to_i, $4.to_i
    if (2013..2015) === y && (1..12) === m && d > 0 && d <= days_in_month[m - 1]
      found[$1] += 1
    end
  end
end
puts found.max_by { |k, v| v }.first

s=gets

RUI=[1,3,5,7,8,10,12]

def digit? c
  c>= ?0 && c<= ?9
end

def day? month, day
  return false if day==0
  if month==2
    return day<=28
  end

  if RUI.include?month
    return day<=31
  end

  return day<=30
end

def valid? date
  return false if not /(?<day>\d{2})-(?<month>\d{2})-(?<year>\d{4})/ =~ date

  return false if year.to_i<2013 || year.to_i>2015
  return day? month.to_i,day.to_i
end

hm={}
hm.default=0
ans=""

(0...s.length-10).each do |i|
  key=s[i,10]
  hm[key]+=1 if valid? key
  ans=key if ans=="" || hm[key]>hm[ans]
end

puts ans

s=gets

RUI=[1,3,5,7,8,10,12]

def digit? c
  c>= ?0 && c<= ?9
end

def day? month, day
  return false if day==0
  if month==2
    return day<=28
  end

  if RUI.include?month
    return day<=31
  end

  return day<=30
end

def valid? date
  return false if !digit?(date[0]) || !digit?(date[1])
  day=date[0,2].to_i
  date=date[2..-1]

  return false if digit? date[0]
  date=date[1..-1]

  return false if !digit?(date[0]) || !digit?(date[1])
  month=date[0,2].to_i
  date=date[2..-1]

  return false if digit? date[0]
  date=date[1..-1]

  return false if !digit?(date[0]) || !digit?(date[1]) || !digit?(date[2]) || !digit?(date[3])
  year=date[0,4].to_i

  return false if year<2013 || year>2015
  return day? month,day
end

hm={}
hm.default=0
ans=""

(0...s.length-10).each do |i|
  key=s[i,10]
  hm[key]+=1 if valid? key
  ans=key if ans=="" || hm[key]>hm[ans]
end

puts ans

s=gets

RUI=[1,3,5,7,8,10,12]

def day? month, day
  return false if day==0
  if month==2
    return day<=28
  end

  if RUI.include?month
    return day<=31
  end

  return day<=30
end

def valid? date
  return false if not /(?<day>\d{2})-(?<month>\d{2})-(?<year>\d{4})/ =~ date

  return false if year.to_i<2013 || year.to_i>2015
  return day? month.to_i,day.to_i
end

hm={}
hm.default=0
ans=nil

(0...s.length-10).each do |i|
  key=s[i,10]
  hm[key]+=1 if valid? key
  ans=key if hm[key]>hm[ans]
end

puts ans

s=gets
w = {

}
w.default=0
ans=-1;
j = -1;
m=[0,31,28,31,30,31,30,31,31,30,31,30,31]

def good?(s,m)
	a=s[0..1].to_i
	b=s[3..4].to_i
	c=s[6..9].to_i
	if c>=2013 and c<=2015 and b>=1 and b<=12 and a>=1 and a<=m[b] then
		return true
	end
	return false
end

s.length.times do |i|
	if s.length-10 <= i then
		break
	end
	t = s[i,10]
	if t =~ /\d{2}-\d{2}-\d{4}/ and good?(t,m) then
		w[t]+=1
		if(ans<w[t]) then
			j=t
			ans=w[t]
		end
	end
end
puts j

# -*- encoding: utf-8 -*-
require 'date'

s = gets.chomp
hash = Hash.new {0}

(9 .. s.length).each do |idx|
  s2 = s[idx - 9 .. idx]
  match = s2.match(/(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9])-(201[3-5])/)
  next if not match

  year, month, date = match[3].to_i, match[2].to_i, match[1].to_i

  if Date.valid_date? year, month, date
    d = Date.new year, month, date
    hash[d] += 1
  end
end
ans = hash.max_by { |k, v| v }.first
puts ans.strftime "%d-%m-%Y"

#!/usr/bin/ruby1.9

prophecy = STDIN.readline.strip

monthdays =  [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
			#Jan  Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

count = {}

(9..prophecy.length).each do |pos|
	s = prophecy[pos-9..pos]
	match = s.match(/(\d\d)-(\d\d)-(\d\d\d\d)/)
	next if not match
	day, month, year = match.to_a[1..-1].collect {|s| s.to_i }
	next if year < 2013 or  year > 2015
	next if month < 1 or month > 12
	next if day < 1 or day > monthdays[month-1]
	if count.member? s
		count[s] += 1
	else
		count[s] = 1
	end
end
arr = count.to_a.sort {|x, y| x[1]-y[1] }
puts arr[-1][0]

require 'date'

def run
  hash = Hash.new(0)

  line = ARGF.readline.chomp
  line.length.times do |i|
    begin
      str = line[i,10]
      if str =~ /[0-9]{2}-[0-9]{2}-[0-9]{4}/
        parse = Date.strptime(str, "%d-%m-%Y")
        hash[str] += 1 if parse.year.between?(2013, 2015)
      end
    rescue
    end
  end

  ret = hash.to_a.max_by { |item| item[1] }
  puts ret[0] if ret
end

run if $0 == __FILE__

# 777-444---21-12-2013-12-2013-12-2013---444-777
require 'Date'

def days_in_month(year, month)
	Date.new(year, month, -1).day
end
count = Hash.new(0)

l = gets.chomp.split("-")
# puts "#{l}"
l.each_cons(3) do |(d,m,y)|
	h = d.to_i
	if h > 0
		m = m.to_i
		if m >= 1 && m <= 12

			y = y.length > 4 ? y[0..3].to_i : y.to_i
			d = d.length > 2 ? d[-2..-1].to_i : d.to_i

			if y >= 2013 && y <= 2015 && (d <= days_in_month(y,m))
				count["#{d.to_s.rjust(2,"0")}-#{m.to_s.rjust(2,"0")}-#{y}"] += 1
			end
		end
	end
end
max = count.values.max
# puts "#{count}"

puts count.detect{|k,v| v == max}[0]

s=gets

RUI=[1,3,5,7,8,10,12]

def day? month, day
  return false if day==0
  if month==2
    return day<=28
  end

  if RUI.include?month
    return day<=31
  end

  return day<=30
end

def valid? date
  return false if not /(?<day>\d{2})-(?<month>\d{2})-(?<year>\d{4})/ =~ date

  return false if year.to_i<2013 || year.to_i>2015
  return day? month.to_i,day.to_i
end

hm={}
hm.default=0
ans=""

(0...s.length-10).each do |i|
  key=s[i,10]
  hm[key]+=1 if valid? key
  ans=key if ans=="" || hm[key]>hm[ans]
end

puts ans

s=gets

RUI=[1,3,5,7,8,10,12]

def digit? c
  c>= ?0 && c<= ?9
end

def day? month, day
  return false if day==0
  if month==2
    return day<=28
  end

  if RUI.include?month
    return day<=31
  end

  return day<=30
end

def valid? date
  return false if !digit?(date[0]) || !digit?(date[1])
  day=date[0,2].to_i
  date=date[2..-1]

  return false if digit? date[0]
  date=date[1..-1]

  return false if !digit?(date[0]) || !digit?(date[1])
  month=date[0,2].to_i
  date=date[2..-1]

  return false if digit? date[0]
  date=date[1..-1]

  return false if !digit?(date[0]) || !digit?(date[1]) || !digit?(date[2]) || !digit?(date[3])
  year=date[0,4].to_i

  return false if year<2013 || year>2015
  return day? month,day
end

hm={}
(0...s.length-10).each do |i|
  key=s[i,10]
  if valid? key
    hm[key]? hm[key]+=1:hm[key]=1
  end
end

ans=""

hm.each do |key,value|
  if ans=="" || value>hm[ans]
    ans=key
  end
end

puts ans

# -*- encoding: utf-8 -*-
require 'date'

s = gets.chomp
ans = nil

(9 .. s.length).each do |idx|
  s2 = s[idx - 9 .. idx]
  match = s2.match(/(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9])-(201[3-5])/)
  next if not match

  year, month, date = match[3].to_i, match[2].to_i, match[1].to_i

  if Date.valid_date? year, month, date
    d = Date.new year, month, date
    if ans.nil? or d > ans
      ans = d
    end
  end
end

puts ans.strftime "%d-%m-%Y"

# -*- encoding: utf-8 -*-
require 'date'

s = gets.chomp
date = nil

(9 .. s.length).each do |idx|
  s2 = s[idx - 9 .. idx]
  match = s2.match(/(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9])-(201[3-5])/)
  next if not match

  year, month, date = match[3].to_i, match[2].to_i, match[1].to_i

  if Date.valid_date? year, month, date
    d = Date.new year, month, date
    if date.nil? or d > date
      date = d
    end
  end
end

puts date.strftime "%d-%m-%Y"

# 777-444---21-12-2013-12-2013-12-2013---444-777
require 'Date'

def days_in_month(year, month)
	Date.new(year, month, -1).day
end
count = Hash.new(0)

l = gets.chomp.split("-")
# puts "#{l}"
l.each_cons(3) do |(d,m,y)|
	h = d.to_i
	if h > 0
		m = m.to_i
		if m >= 1 && m <= 12
			y = y.to_i
			if y >= 2013 && y <= 2015
				if(h <= days_in_month(y,m))
					count["#{d}/#{m}/#{y}"] += 1
				elsif(d[-2..-1].to_i <= days_in_month(y,m))
					count["#{d[-2..-1]}-#{m}-#{y}"] += 1
				end

			end
		end
	end
end
max = count.values.max
# puts "#{count}"

puts count.detect{|k,v| v == max}[0]

# 777-444---21-12-2013-12-2013-12-2013---444-777
require 'Date'

def days_in_month(year, month)
	Date.new(year, month, -1).day
end
count = Hash.new(0)

l = gets.chomp.split("-")
# puts "#{l}"
l.each_cons(3) do |(d,m,y)|
	h = d.to_i
	if h > 0
		m = m.to_i
		if m >= 1 && m <= 12
			y = y.to_i
			if y >= 2013 && y <= 2015
				if(h <= days_in_month(y,m))
					count["#{d}/#{m}/#{y}"] += 1
				elsif(d[-2..-1].to_i <= days_in_month(y,m))
					count["#{d[-2..-1]}-#{m}-#{y}"] += 1
				end

			end
		end
	end
end
max = count.values.max
puts "#{count}"

puts count.detect{|k,v| v == max}[0]

# 777-444---21-12-2013-12-2013-12-2013---444-777
require 'Date'

def days_in_month(year, month)
	Date.new(year, month, -1).day
end
count = Hash.new(0)

l = gets.chomp.split("-")
# puts "#{l}"
l.each_cons(3) do |(d,m,y)|
	h = d.to_i
	if h > 0
		m = m.to_i
		if m >= 1 && m <= 12
			y = y.to_i
			if y >= 2013 && y <= 2015
				# if ( || h[-2..-1].to_i <= days_in_month(y,m))
				if(h <= days_in_month(y,m))
					count["#{d}/#{m}/#{y}"] += 1
				end
				if(d[-2..-1].to_i <= days_in_month(y,m))
					count["#{d[-2..-1]}/#{m}/#{y}"] += 1
				end

			end
		end
	end
end
max = count.values.max
# puts "#{count}"

puts count.detect{|k,v| v == max}[0]

s=gets
w,ans,j={},-1,0
w.default=0
def good?(day,month,year)
	begin
		t = Time.new(year,month,day)
		if t<=Time.new(2015,12,31) and t>=Time.new(2013,1,1) then
			return true
		end
	rescue
		return false
	end
	return false
end
s.scan(/(?<day>\d{2})-(?<month>\d{2})-(?<year>\d{4})/).each do |item|
	if(good?(item[0].to_i,item[1].to_i,item[2].to_i)) then
		w[$&]+=1
		if ans<w[$&] then
			ans=w[$&]
			j=$&
		end
	end
end
puts j

s=gets
w,ans,j={},-1,0
w.default=0
def good?(day,month,year)
	begin
		t = Time.new(year,month,day)
		if t<=Time.new(2015,12,31) and t>=Time.new(2013,1,1) then
			return true
		end
	rescue
		return false
	end
	return false
end
s.scan(/(?<day>\d{2})-(?<month>\d{2})-(?<year>\d{4})/).each do |item|
	if(good?(item[0].to_i,item[1].to_i,item[2].to_i)) then
		t = "" << item[0]<<'-'<< item[1]<< '-' << item[2]
		w[t]+=1
		if ans<w[t] then
			ans=w[t]
			j=t
		end
	end
end
puts j


testcases = Integer(gets.chomp)
resultados = []

for i in 0..testcases-1
    n = Integer(gets.chomp)
    arreglo = gets.chomp
    a = arreglo.split(" ")
    advertencia = 0
    aumentoCrecimiento = false
    crecimiento = 1
    for j in 0..n-1
        if a[j] == "1"
            advertencia = 0
            if aumentoCrecimiento
                crecimiento += 5
            else
                crecimiento += 1
                aumentoCrecimiento = true
            end
        else
            aumentoCrecimiento = false
            advertencia += 1
        end
        if advertencia == 2
            crecimiento = -1
            break
        end
    end
    resultados.push(crecimiento)
end

puts resultados

for _ in 0...(gets.chomp.to_i)
  n = gets.chomp.to_i
  arr = gets.chomp.split().map{|x| x.to_i}
  height, flag = 1, true
  for i in 1...(n + 1)
    if (arr[i] == 0) and (arr[i-1] == 0)
      puts -1
      flag = false
      break
    elsif (arr[i] == 1) and (arr[i-1] == 1)
      height += 5
    elsif arr[i-1] == 1
      height += 1
    end
  end
  if flag
    puts height
  end
end


n = gets.to_i
a = gets.split.map(&:to_i)
ans = 0
for i in 0...n
	sum = 0
	for j in i...n
		sum = sum^a[j]
		if sum > ans then ans = sum end
	end
end
print ans

n = gets.to_i

mas = gets.split.map{|i| i.to_i}

xor = []

i = 0
while i < n do
    j = i + 1
    xor << mas[i]
    while j < n do
        xor << (xor[-1]^mas[j])
        j += 1
    end
    i += 1
end

xor = xor.sort

puts xor[-1]

n=gets
a=gets.split.map(&:to_i)
max=a.max
for i in 0...a.size do
	for j in i...a.size do
		tm=a[i]
		a[i+1..j].each{|e| tm=tm ^ e} if i!=j
		max=tm if tm>max
	end
end
puts max

n = gets.to_i
a = gets.split.map(&:to_i)
max = 0
for i in 0...n
  xor = 0
  for j in i...n
    xor = xor^a[j]
    if xor > max then max = xor end
  end
end
print max

#!/usr/bin/env ruby
n = gets.to_i
t = gets.split.map(&:to_i)
result = 0
n.times do |d|
  (0..(n - d - 1)).each do |i|
    x = 0
    (i..(i + d)).each do |j|
      x ^= t[j]
    end
    result = [result, x].max
  end
end
puts result

n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)
ret = []
for i in 0...n do
  t = 0
  for j in i...n do
    t ^= a[j]
    ret << t
  end
end
puts ret.max

n=gets.to_i
a=gets.split.map(&:to_i)
res=a.max
for i in 0..n-1 do
    t=a[i]
    for j in i+1..n-1 do
        t^=a[j]
        res=t if res<t
    end
end
puts res

n=gets.to_i
a=gets.split.map(&:to_i)
ans=0
for i in 0...n do
	mx=0
	a[i...n].each{|v|
		mx^=v
		ans = mx if mx > ans
	}
end
puts ans

n = gets.chomp.to_i
line = gets.chomp.split(" ").collect! {|x| x.to_i}
max = 0
(0..(n-1)).each {|k|
 (k..(n-1)).each { |t|
   p = 0
   (k..t).each {|q|
    p ^= line[q]
  }
  if p> max
	max = p
  end
 }

}

puts max

n = gets.to_i
x = gets.split.map(&:to_i).unshift(0)
n.times { |i| x[i+1] ^= x[i] }
mx = 0
1.upto(n) do |i|
	i.upto(n) do |j|
		mx = [mx, x[j] ^ x[i-1]].max
	end
end
puts mx

t = gets.to_i

a = gets.split.collect{|x| x.to_i}

r = 0

i = 0
while i < a.size
	j = i
	while j < a.size
		t = a[i]

		k = i+1

		while k <= j

			t ^= a[k]
			k += 1
		end

		r = [t,r].max
		j += 1
	end
	i+=1
end

puts r

#!/usr/bin/env ruby
n = gets.to_i
t = gets.split.map(&:to_i)
result = 0
n.times do |d|
  (0..(n - d - 1)).each do |i|
    x = 0
    (i..(i + d)).each do |j|
      x |= t[j]
    end
    result = [result, x].max
  end
end
puts result

n=gets.to_i
a=gets.split.map(&:to_i)
res=0
for i in 0..n-1 do
    t=a[i]
    for j in i+1..n-1 do
        t^=a[j]
    end
    res=t if res<t
end
puts res

n=gets.to_i
a=gets.split.map(&:to_i)
ans=0
for i in 0...n do
	mx=0
	a[i...n].each{|v|mx^=v}
	ans = mx if mx > ans
end
puts ans

n = gets.chomp.to_i
line = gets.chomp.split(" ").collect! {|x| x.to_i}
max = 0
(0..(n-1)).each {|k|
 p = 0
 (k..(n-1)).each { |t|
  p ^= line[t]
 }
 if p> max
	max = p
 end
}

puts max

n = gets.to_i
x = gets.split.map(&:to_i)
n.times { |i| i > 0 ? x[i] ^= x[i-1] : 1 }
mx = x[0]
1.upto(n-1) do |i|
	i.upto(n-1) do |j|
		mx = [mx, x[j] ^ x[i-1]].max
	end
end
puts mx

n = gets.to_i
x = gets.split.map(&:to_i)
n.times { |i| i ? x[i] ^= x[i-1] : _ }
mx = x[0]
1.upto(n-1) do |i|
	i.upto(n-1) do |j|
		mx = [mx, x[j] ^ x[i-1]].max
	end
end
puts mx