gets.to_i.times do
    x,y = gets.split.map(&:to_i)
    puts (x-y > 1) ? "Yes" : "No"
end

t = gets.to_i
t.times do
  x, y = gets.split.map(&:to_i)
  d = x - y
  puts d == 1 ? 'NO' : 'YES'
end

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
primes = [2,3]
t.times {
    x, y = gets.split.map(&:to_i)
    # x > y, guaranteed
    d = x - y
    if d < 2
        puts "NO"
    else
        puts "YES"
    end
}

t = gets.to_i
for i in 1..t
    a,b = gets.split.map(&:to_i);
    flag = 0
    if (a-b>=2)
        print "YES"
        puts "\n"
        flag = 1
    end
    if flag != 1
        print "NO"
        puts "\n"
    end
end

t = gets.to_i
t.times do
  x, y = gets.split.map &:to_i
  puts x - y == 1 ? 'NO' : 'YES'
end

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
primes = [2,3]
t.times {
    x, y = gets.split.map(&:to_i)
    # x > y, guaranteed
    d = x - y
    if d < 2
        puts "NO"
    else
        if d%2 == 0
            puts "YES"
        else
            # prime sub check
            is_prime_sub = false
            for i in primes
                break if i > d
                if d%i == 0
                    is_prime_sub = true
                    break
                end
            end

            # generate primes and check prime sub
            sqd = Math.sqrt(d).ceil
            if primes.last < sqd
                for i in (primes.last)..sqd
                    is_prime = true
                    for j in primes
                        if i%j == 0
                            is_prime = false
                            break
                        end
                    end

                    primes << i if is_prime

                    if d%i == 0
                        is_prime_sub = true
                        break
                    end
                end
            end

            if is_prime_sub
                puts "YES"
            else
                puts "NO"
            end
        end
    end
}

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
primes = [2,3]
t.times {
    x, y = gets.split.map(&:to_i)
    # x > y, guaranteed
    d = x - y
    if d < 2
        puts "NO"
    else
        if d%2 == 0
            puts "YES"
        else
            # prime sub check
            is_prime_sub = false
            sqd = Math.sqrt(d).floor

            for j in primes
                break if j > sqd
                if sqd%j == 0
                    is_prime_sub = true
                    break
                end
            end

            if is_prime_sub == false
                for i in (primes.last)..sqd
                    next if i%2 == 0
                    is_prime = true
                    for j in primes
                        if i%j == 0
                            is_prime = false
                            break
                        end
                    end

                    if is_prime
                        primes << i

                        if sqd%i == 0
                            is_prime_sub = true
                            break
                        end
                    end
                end
            end

            # 안되면 끝까지 구해야지 별 수 있나
            # 가즈아! (근데 이걸로는 submission 못해봄.)
            # 1
            # 999984 1

            # p primes
            if is_prime_sub
                puts "YES"
            else
                puts "NO"
            end
        end
    end
}


n = gets.to_i

n.times{
  s = gets
  n = s.length-1
  if n <= 10
    puts s
   else
    puts s[0] + (n-2).to_s + s[n-1]
  end
}

array = STDIN
array.each { |x| if x.size > 11 then puts "#{x[0]}#{x.size - 3}#{x[-2]}" elsif x =~ /[0-9]/ then nil else puts x end }

n = gets.to_i
n.times.map { gets.chomp }.each do |word|
    if (s = word.size - 2) > 8
        puts "#{word[0]}#{s}#{word[-1]}"
    else
        puts word
    end
end

n = gets.to_i
n.times do
  str = gets.chomp
  if str.length>10
    puts str[0]+(str.length-2).to_s+str[-1]
  else
    puts str
  end

end

n = gets.to_i
while n > 0 do
    word = gets.chomp
    if (s = word.size - 2) > 8
        puts [word[0], s, word[-1]].join
    else
        puts word
    end
    n -= 1
end

n = gets.to_i
words = n.times.map { gets.chomp }
words.map do |word|
    if (s = word.size - 2) > 8
        "#{word[0]}#{s}#{word[-1]}"
    else
        word
    end
end.each { |w| puts w }

n = gets.to_i
while n > 0 do
    word = gets.chomp
    if word.size > 10
        puts [word[0], word.size-2, word[-1]].join
    else
        puts word
    end
    n -= 1
end

n = gets.to_i

n.times{
  s = gets
  n = s.length-1
  if n <= 10
    puts s
   else
    puts s[0] + (s[1..n-2].length.to_s) + s[n-1]
  end
}

n = gets.chomp.to_i
words = []
(1..n).each do |_i|
  words << gets.chomp
end

words.each do |w|
  if w.size < 11
    puts w
  else
    puts "#{w[0]}#{w.size - 2}#{w[-1]}"
  end
end

n = gets.to_i

$i = 0

while $i < n
    s = gets
    if s.length < 12
        puts s
    elsif
        puts "#{s[0]}#{s.length - 3}#{s[s.length - 2]}"
    end

    $i += 1
end

t = gets.to_i

lines = []
t.times do
  current = gets.chomp
  if current.length > 10
    lines.push(current[0] + (current.length - 2).to_s + current[current.length-1])
  else
    lines.push(current)
  end
end

lines.each do |line|
  puts line
end

n = gets.to_i

n.times do
  input = gets.chomp
  if input.size > 10
    puts "#{input.chars.first}#{input.length - 2}#{input.chars.last}"
  else
    puts input
  end
end

ans=Array.new
gets.to_i.times{s=gets; s.size>11? (ans<<"#{s[0]}#{s.size-3}#{s[-2]}"):(ans<<s)};
puts ans

gets.chomp.to_i.times do |_|
  word = gets.chomp.each_char
  printf(word.size > 10 ? "#{word.first}#{word.size-2}#{word.to_a.last}\n" : "#{word.to_a.join}\n")
end

t=gets.to_i
t.times{

  s=gets.chomp
  n=s.size
  if n<=10
    puts s
  else
    puts "#{s[0]}#{n-2}#{s[-1]}"
  end

}

number = gets.chomp.to_i
number.times do
  word = gets.chomp

  if word.size > 10
    puts word[0] + (word.size - 2).to_s + word[-1]
  else
    puts word
  end
end

gets.to_i.times do
s=gets.chomp
	if s.length > 10
	puts s[0] << (s.length - 2).to_s << s[s.length - 1]
	else
	puts s
	end
end

n = gets.to_i
a =[]
for i in 0..n-1 do
  a = a + [gets]
  if a[i].size > 11
    a[i] = (a[i][0] + (a[i].size - 3).to_s + a[i][a[i].size - 2])
  end
end
puts a

n = gets.to_i
n.times do |i|
  l = gets.strip
  s = l.size
  puts s>10 ? l[0]+(s-2).to_s+l[s-1] : l
end

#!/usr/bin/ruby
n = STDIN.gets.chop.to_i

n.times do
  word = STDIN.gets
  word.chop!

  if word.length > 10
    print word[0, 1]
    print word.length-2
    print word[-1, 1]
  else
    print word
  end

  print "\n"
end

n=gets.to_i

n.times {
i=gets
a=i.length
if (a <= 11)
 puts i
else
 out= i[0] + (a-3).to_s + i[a-2]
 puts out
end
}

n = gets.to_i

a = Array.new(n)
for i in 0..(n - 1)
  a[i] = gets.to_s
end

a.each {|b|
if b.length >= 12
  f = b.split("")[0]
  l = b.split("")[(b.length-2)]
  c = b.split("")[1..(b.length-3)].join("")
  puts "#{f}#{c.length}#{l}"
else
  puts "#{b}"
end
}

lines = STDIN.read.split("\n")
if 2 <= lines.size
  lines[1..(lines.size - 1)].each do |word|
    puts word.length <= 10 ? word : "#{word[0]}#{word.length - 2}#{word[word.length - 1]}"
  end
end

limit =gets.chomp()
limit = limit.to_i
while limit>0
	word = gets.chomp()
	if word.length>10
		print word[0],word.length-2,word[-1]
		print "\n"
	else
		puts word
	end
	limit-=1
end

gets.to_i.times do
	word = gets.chomp
	if word.length <= 10
		puts word
	else
		print word[0], word.length - 2, word[-1], "\n"
	end
end

T = gets.chomp.to_i
for i in (0...T)
  a = gets.chomp
  if(a.length > 10)
    puts a[0] + (a.length - 2).to_s + a[a.length - 1]
  else
    puts a
  end
end

number = gets.chomp.to_i

number.times do
word = gets.chomp

if word.length > 10
str = ''
number = word.length - 2
str = word[0] + number.to_s + word[word.length - 1]
puts str
else
puts word
end
end

def abb ()
    n = gets.to_i
  words = Array.new(n)
    for i in 0...n
      word = gets.to_s
      words[i] = word.chomp!
    end
  words.each{ |word|
        if word.length <= 10
            puts word
        else
            puts "#{word[0]}#{word.length-2}#{word[-1]}"
        end
  }
end

abb

n = gets.to_i
n.times do
a = gets.chomp
d = Array.new
b = a[0]
c = a[-1, 1]
x = a[1..-2]
s = x.size.to_i
 if s > 8
 d << b << s << c
 d.map { |sammy| sammy}
 puts d.join
 else
 puts a
 end
 end

num = gets.chomp
num = num.to_i

num.times do
    input = gets.chomp #get the next lines of input
    if (input.size > 10) #if the size of the string is 10 or more..
         firstLetter = input.split("").first
         lastLetter = input.split("").last
         input = input.split("")
         input.pop
         input.shift
         input = input.join
         new_word = "#{firstLetter}#{input.length}#{lastLetter}\n"
         print new_word
    else
        word = "#{input}\n"
        print word
    end
end

x = gets.chomp.to_i

for i in 0...x do
   words = gets.chomp.to_s
   if words.length > 10
    puts words[0].to_s + (words.length-2).to_s + words[-1]
   else
    puts words
   end
end

input = STDIN.read

n, lines = input.split("\n", 2)

lines = lines.split("\n")
# n = n.to_i
lines.each do |line|
  if line.length < 11
    puts line
  else
    puts "#{line[0]}#{line.length - 2}#{line[line.length - 1]}"
  end
end

# frozen_string_literal: true

n = gets.to_i

1.upto(n).each do |_i|
  str = gets.strip
  if str.length <= 10
    puts str
    next
  end
  len = str.length - 2
  puts "#{str[0]}#{len}#{str[str.length - 1]}"
end

(gets.to_i).times do
  ss=gets
  if(ss.length>11)
    ss[1,ss.length-3] = (ss.length-3).to_s
  end
  puts (ss)
end

n = gets.chomp.to_i
i = 0
while i < n  do
   s = gets.chomp
   t = s.size
   if t > 10
    print s[0]
    print t - 2
    puts s[t-1]
   else
    puts s
   end
   i += 1
 end

class LongWord
  def initialize(word)
    @word = word
  end

  def short_version
    return @word if @word.size <= 10

    first_char = @word[0]
    last_char = @word[-1]
    rest_word_count = @word[1..-2].size

    "#{first_char}#{rest_word_count}#{last_char}"
  end
end

# Main
count_of_input = gets.strip.to_i
word_collection = []
count_of_input.times do
  word = gets.strip
  word_collection << word
end

word_collection.each do |word|
  long_word = LongWord.new(word)
  puts long_word.short_version
end

n=gets.chomp.to_i
n.times do
	a=gets.chomp.to_s
	num=a.length - 2
	if a.length>10
		word=a[0]+num.to_s+a[num+1]
	else
		word=a
	end
	puts word
end

n=gets.chomp.strip.to_i
n.times do
	a=gets.chomp.to_s
	num=a.length - 2
	if a.length>10
		word=a[0]+num.to_s+a[num+1]
	else
		word=a
	end
	puts word
end

n = gets.chomp.to_i
for i in(0...n)
    s = gets.chomp
    if s.length() < 11
        puts s
    else
        print s[0],s.length()-2,s[s.length()-1]
        puts
    end
end

gets.chomp.to_i.times do
    word = gets.chomp
    if word.length > 10
        print word[0], ((word.length)-2).to_s, word[-1], "\n"
    else
        print word, "\n"
    end
end

gets.to_i.times do
  s = gets.chomp
  puts s.size>10?"#{s[0]}#{s.size-2}#{s[-1]}":s
end

n = gets.strip.to_i
n.times do |i|
  word = gets.strip
  result = ""
  if word.length > 10 then
    result = "#{word[0]}#{word[1..-2].length}#{word[-1]}"
  else
    result = word
  end
  puts result
end

n=gets.to_i
a= []
for i in 0..n-1
	a[i] = gets
end
a.each do |val|
	val = val.gsub("\n","")
	len = val.length
	puts  len <= 10 ? val : "#{val[0]}#{len-2}#{val[len-1]}"
end

#!/usr/bin/ruby

n = gets.chomp.to_i

n.times do
  s = gets.chomp.to_s

  if s.length > 10
    first_letter = s[0]
    last_letter = s[s.length-1]
    abbr = (s.length - 2).to_s
    puts (first_letter + abbr + last_letter)
  else
    puts s
  end
end

n = gets.chomp.to_i
0.upto n-1 do |i|
  w = gets.chomp
  if w.length > 10 then
	ww = "#{w[0]}#{w.length-2}#{w[w.length-1]}"
	puts ww
  else
    puts w
  end
end

n = gets.to_i

for i in 0...n
  x = gets;
  if x.size > 11
  print x[0]
  print x.size-3;
  print x[x.size-2].chr;
    puts
  elsif
    puts x;
  end
end

n = gets.chomp.to_i.times do |i| a = gets.chomp.to_s; if a.length>10 then print(a[0]); print(a.length-2); puts(a[a.length-1]); else puts(a) end end

#!/usr/bin/env ruby
class Test
	kol = gets.chomp
	ik = kol.to_i
	for num in 1..ik
		str = gets.chomp
	if str.length > 10
	len = str.length - 2
	len = len.to_s
	temp = str[0..0] + len + str[str.length-1..str.length-1]
		puts temp
	else
		puts str
	end
	end
end

n=gets.chomp.to_i.times{a=gets.chomp.to_s;o=a.length;if o>10 then printf("%c%d%c\n",a[0],o-2,a[o-1]);else puts(a)end}

a = gets.to_i

for arr in (1..a)
  arr = gets.to_s
  if arr.length > 11
    startLetter = arr[0]
    lastLetter = arr[arr.length - 2]
    puts arr = startLetter + (arr.length.to_i - 3).to_s + lastLetter.to_s
  else
    puts arr.to_s
  end
end

n = gets.to_i

words = []

n.times do
	curr = gets

	if curr.length > 11
		last = curr[curr.length - 2, 1]
		curr = curr[0] + (curr.length - 3).to_s
		curr = curr + last
	end

	words << curr
end

puts words

n = gets.to_i
n.times do
  s = gets.chomp
  if s.size <= 10
    puts s
  else
    puts "#{s[0]}#{s.size - 2}#{s[-1]}"
  end
end

len = 10
n = gets.to_i
while n > 0 do
    word = gets.chomp
    if word.size > len
        puts [word.chars.first, word.size-2, word.chars.last].join
    else
        puts word
    end
    n -= 1
end

def too_long_word (word)
    x = 0
    n = gets
    num = n.to_i

    while x < num
      word = gets
      char = word.chomp.split('')
      sum_char = char.size
      char_min = ((sum_char) - 2).to_s
        if sum_char <= 10
          puts word
        else
          puts char.first + char_min + char.last
        end
    x+=1
    end
end

too_long_word ()

n = readline.to_i

i = 0
a = Array.new()

while i < n do
  begin
    a[i] = readline
    i += 1
  end
end
a.each do
  |i|
  if (i.size > 11)
    puts i[0] + (i.size - 3).to_s + i[i.size - 2]
  else
    puts i
  end
end

gets.chomp.to_i.times {
	s=gets.chomp
	if(s.length<=10)
		puts s
	elsif
		puts "#{s[0]}#{s.length-2.to_i}#{s[-1]}"
	end
}

gets.chomp.to_i.times do
  str = gets.chomp
  puts str.length > 10 ? "#{str[0]}#{str.length - 2}#{str[-1]}" : str
end

x = gets.to_i

def check(j)
	if j.length <= 10
		puts j
	else
		puts j[0] + (j.length - 2).to_s + j[-1]
	end
end

for i in 1..x do
	y = gets.chomp
	check(y)
end

m=gets.to_i
m.times do
  s = gets.chomp
  if s.size <= 10
    puts s
  else
    puts "#{s[0]}#{s.size - 2}#{s[-1]}"
  end
end

a = gets.to_i
for i in 1..a
    b = gets
    c = b.length
    c = c - 1
    if c <= 10
        puts b
    else
        d = c - 2
        print b[0]
        print d
        print b[c-1]
        puts
    end
end

1.upto(gets.to_i) do
  a = gets.chomp
  a = "#{a[0, 1]}#{a.length - 2}#{a[-1, 1]}" if a.length > 10

  puts a
end

a = gets.split(" ")
n = a[0].to_i
for i in 1..n
  s = gets.chomp
  if(s.size > 10)
    s1 = s[0]+(s.size-2).to_s+s[s.size - 1]
    puts(s1)
  else puts(s)
  end
end

x=gets.to_i
x.times do
n=gets.chomp
z=n.length
if z > 10
print n[0]
print z-2
print n[z-1]
print "\n"
else
print n
print "\n"
end
end

a=Array.new;
n=gets;
n.to_i.times{|i|a[i]=gets.chop}
a=a.map{|i|if i.size>10 then i=i[0..0]+(i.size-2).to_s+i[-1..-1] else i=i end}
a.each{|i|puts i}

n = gets.chomp.to_i

n.times do
  word = gets.chomp
  if word.size > 10
    n = word.size
    puts "#{word[0]}#{n - 2}#{word[n-1]}"
  else
    puts word
  end
end

a = Array.new
n = gets.to_i
n.times do
 a << gets.to_s.chop
end
a.each do |i|
 if i.size <= 10
  p i
 else
  p "#{i[0]}#{i.size-2}#{i[(i.size-1)]}"
  end
end

n = gets.chomp.to_i
(0...n).each do |i|
	word = gets.chomp
	if word.length > 4
		print word[0], (word.length - 2), word[(word.length - 1)], "\n"
	else puts word
	end
end

(gets.to_i).times do
  str=gets
   if str.length > 10 then
     str[1,str.length - 2] = (str.length - 2).to_s
   end
   puts str
 end

a = gets.to_s
d = []
b = a[0]
c = a[-1, 1]
x = a[1..-2]
s = x.size.to_i
y = s.to_s
 d << b << y << c
 if s > 10
 puts d.join
 else
 puts a
 end

n = gets.to_i

$i = 0

while $i < n
    s = gets
    if s.size < 11
        puts s
    elsif
        puts "#{s[0]}#{s.size - 3}#{s[s.size - 2]}"
    end

    $i += 1
end

n = gets.chomp.to_i
for i in(0...n)
    s = gets.chomp
    if s.length() < 10
        puts s
    else
        print s[0],s.length()-2,s[s.length()-1]
        puts
    end
end

a = gets.to_s
b = a[0]
c = a[-1]
s = a.size
x = s - 2
z = x.to_s
 if s > 10
 puts b + z + c
 else
 puts a
 end

a = gets.to_i

for arr in (1..a)
  arr = gets.to_s
  if arr.length > 10
    startLetter = arr[0]
    lastLetter = arr[arr.length - 2]
    puts arr = startLetter + (arr.length.to_i - 3).to_s + lastLetter.to_s
  else
    puts arr.to_s
  end
end

def abb ()
    n = gets.to_i
    for i in 0...n
    word = gets.to_s
    word.chomp!
        if word.length < 10
            puts word
        else
            puts "#{word[0]}#{word.length-2}#{word[-1]}"
end
end
end

abb

#!/usr/bin/env ruby
class Test
	kol = gets.chomp
	ik = kol.to_i
	for num in 1..ik
		str = gets.chomp
	if str.length > 10
	len = str.length.to_s
	temp = str[0..0] + len + str[str.length-1..str.length-1]
		puts temp
	else
		puts str
	end
	end
end

n = gets.chomp.to_i

n.times do
    s = gets.chomp
    if s.length >= 10
        p "#{s[0]}#{s.length-2}#{s[s.length-1]}"
    else
        p s
    end
end

n = gets.chomp.to_i
for i in(0...n)
    s = gets.chomp
    if s.length() < 11
        puts s
    else
        print s[0],s.length()-2,s[s.length()-1]
    end
end

n = gets.to_i
n.times do |i|
	temp = gets
	if (temp.size < 11)
		puts temp
	else
		k = temp.size - 2
		curr = temp[0] + k.to_s + temp[k]
		puts curr
	end
end

a = gets.to_s
b = a[0]
c = a[-1]
d = a[1...-2]
 if a.length > 10
 puts b + d + c
 else
 puts a
 end

array = STDIN
array.each { |x| if x.size > 10 then puts "#{x[0]}#{x.size - 2}#{x[-1]}" elsif x =~ /[0-9]/ then nil else puts x end }

counter = gets.chomp.to_i

counter.times do
  word = gets.chomp.to_s

  if word.length > 10
    word = word[0,1] + ( word.length - 2 ).to_s + word[word.length-1, 1]
  end

  p word
end

n = gets.chomp.to_i
for i in(0...n)
    s = gets.chomp
    if s.length() < 10
        puts s
    else
        puts s[0],s.length(),s[s.length()-1]
    end
end

x = gets.to_i

def check(j)
	if j.length <= 10
		puts j
	else
		puts j[0] + j.length.to_s + j[-1]
	end
end

for i in 1..x do
	y = gets.chomp
	check(y)
end

n = gets.to_i

$i = 0

while $i < n
    s = gets
    puts s.length
    if s.length < 12
        puts s
    elsif
        puts "#{s[0]}#{s.length - 3}#{s[s.length - 2]}"
    end

    $i += 1
end

#!/usr/bin/env ruby
str = ARGF.read

if str.scan(/\D/).empty?
    exit
elsif str.size <= 10
    puts str
else
    puts str[0] + (str.length - 2).to_s + str[str.length - 1]
end

n = gets.to_i
n.times do
a = gets.chomp
d = Array.new
b = a[0]
c = a[-1, 1]
x = a[1..-2]
s = x.size.to_i
 if s >= 10
 d << b << s << c
 d.map { |sammy| sammy}
 puts d.join
 else
 puts a
 end
 end

a = gets.to_i
for i in 1..a
    b = gets
    c = b.length
    c = c - 1
    if c <=4
        puts b
    else
        d = c - 2
        print b[0]
        print d
        print b[c-1]
    end
end

n = gets.split.map &:to_i
n.each do |i|
    word = gets.split.map
    if word.count > 10
        p w[0] + w.count-2 + w[-1]
    else
        p word
    end
end

string = gets.chomp
puts (string[0] + (string.length-2).to_s + string[-1])

n = gets.to_i
n.times do
a = gets.chomp
d = Array.new
b = a[0]
c = a[-1, 1]
x = a[1..-2]
s = x.size.to_i
 if s > 10
 d << b << s << c
 d.map { |sammy| sammy}
 puts d.join
 else
 puts a
 end
 end

i=gets
a=i.length
if (a <= 11)
 puts i
else
 out= i[0] + (a-2).to_s + i[a-2]
 puts out
end

c, arr = gets.to_i, []
c.times { arr << gets }

arr.map! do |w|
  w.length > 10 ? "#{w[0]}#{w.length-3}#{w[-2]}" : w
end

puts arr

n = gets.to_i

a = Array.new(n)
for i in 0..(n - 1)
  a[i] = gets.to_s
end

a.each {|b|

if b.length >= 10
  f = b.split("")[0]
  l = b.split("")[(b.length-2)]
  c = b.split("")[1..(b.length-3)].join("")
  puts "#{f}#{c.length}#{l}"
else
  puts "#{b}"
end
}

a = gets.to_s
b = a[0]
c = a[-1]
s = a.size
z = s.to_s
 if s > 10
 puts b z c
 else
 puts a
 end

a = String.new
a = gets
b = a[0]
c = a[-1]
d = a[1..-2]
 s = a.size.to_i
 if s >= 10
 puts b + d + c
 else
 puts a
 end

n = gets.split.map &:to_i
n.each do |i|
    w = gets.strip
    if w.size > 10
        puts w[0] + w.size-2 + w[-1]
    else
        puts w
    end
end

n = gets.to_i
n.times do
  s = gets.chomp
  if s.size < 10
    puts s
  else
    puts "#{s[0]}#{s.size - 2}#{s[-1]}"
  end
end

n=gets.to_i
n.times do
str=gets.chomp.to_s
puts "#{str[0]}#{str.size-2}#{str[str.size-1]}"
end

x=gets.to_i
x.times do
n=gets.chomp
z=n.length
if z > 10
print n[0]
print z-2
print n[z-1]
else
print n
end
end

x=gets.to_i
x.times do
n=gets.chomp
z=n.length
if z > 10
print n[0]
print z-2
print n[z-1]
else
print n
print "\n"
end
end

n = Integer(gets)
a = Array.new
for i in 0...n
	a[i] = gets
end
		def word_to_long(b)
			l = b.length
			if l > 10
				return b[0] + "#{l - 3}"+ b[l-2]
			else
				return b
			end
		end

	for i in 0...n
		puts word_to_long(a[i])
	end

t = gets.chomp.to_i

for i in 1..t
  s = gets.chomp
  if s.length < 5
    puts s
    next
  end
  print s[0];print s.length-2; puts s[-1]
end

line_count = gets.to_i

for i in 1..line_count
    line = gets
    if line.length > 10
        p line[0] + (line.length-2).to_s + line[-1,1]
    else
        p line
    end
end

n = gets
n.to_i.times do
    word = gets.chomp
    l = word.size
    puts l
    if l > 10
        puts "#{word[0]}#{l-2}#{word[-1]}"
    else
        puts word
    end
end

a = gets.to_s
b = a[0]
c = a[-1]
s = a.size
x = s - 2
z = x.to_s
 if x >= 10
 puts b + z + c
 else
 puts a
 end

a = gets.to_s
b = a[0]
c = a[-1]
s = a.size
x = s - 2
z = x.to_s
 if s >= 10
 puts b + z + c
 else
 puts a
 end

line_count = gets.to_i

for i in 1..line_count
    line = gets.chomp
    if line.length > 10
        p line[0] + (line.length-2).to_s + line[-1,1]
    else
        p line
    end
end

n = gets.chomp.to_i
(0...n).each do |i|
	word = gets.chomp
	if word.length >= 4
		print word[0], (word.length - 2), word[(word.length - 1)], "\n"
	else puts i
	end
end

class LongWord
  def initialize(word)
    @word = word
  end

  def short_version
    return @word if @word.size <= 10

    first_char = @word[0]
    last_char = @word[-1]
    rest_word_count = @word[1..-2].size

    "#{first_char}#{rest_word_count}#{last_char}"
  end
end

# Main
word = gets.strip
short_version = LongWord.new(word).short_version
puts short_version

n = gets.to_i
while n > 0 do
    word = gets.chomp
    if (s = word.size - 2) >= 10
        puts [word[0], s, word[-1]].join
    else
        puts word
    end
    n -= 1
end

a = Array.new
n = gets.to_i
n.times do
 a << gets.to_s.chop
end
a.each do |i|
 if i.size < 10
  p i
 else
  p "#{i[0]}#{i.size-2}#{i[(i.size-1)]}"
  end
end

#!/usr/bin/env ruby
str = gets

if str.scan(/\D/).empty?
 return
end

if str.size <= 10
    puts str
    return
end

puts str[0] + (str.length - 2).to_s + str[str.length - 1]

puts "How many words would ou like to abreviate?"
words = gets.to_i
puts "Write down below #{words} words"

abreviadas = []
for number in 1..words
	palavra = gets.strip
	tamanho = (palavra.size) -2
	if palavra.size > 10
		palavras = palavra[0] + tamanho.to_s + palavra[(palavra.size)-1]
	else
		palavras = palavra
	end
		abreviadas<<palavras
end

puts "Your words are:"

puts abreviadas

STDIN.read.split("\n").drop(1).each do |w|
  puts res =  w.size < 10 ? w : w[0] + "#{(w.size - 2)}" + w[-1]
end

a = gets.to_s
b = a[0]
c = a[-1]
s = a[1...-2].to_i
 if s >= 10
 puts b s c
 else
 puts a
 end

a = gets.split(" ")
n = a[0].to_i
for i in 1..n
  s = gets.chomp
  if(s.size > 10)
    s1 = s[0]+(s.size-2).to_s+s[s.size - 1]
    p(s1)
  else p(s)
  end
end

lines = []
while line =(ARGV.shift or (!STDIN.tty? and STDIN.gets) )
  lines.push(line)
end

puts lines

num_line = gets.to_i()
num_line.times {
  str = gets()
  puts str.length()>10 ? str[0]+(str.length()-3).to_s+str[str.length()-2] : str
}

n=gets.to_i
a= []
for i in 0..n-1
	a[i] = gets
end
a.each do |val|
	len = val.length
	puts  len <= 10 ? val : "#{val[0]}#{len-3}#{val[len-2]}"
end

n=gets.to_i
a= []
for i in 0..n-1
	a[i] = gets
end
a.each do |val|
	len = val.length
	puts  len < 10 ? val : "#{val[0]}#{len-3}#{val[len-2]}"
end

a = gets.to_s
b = a[0]
c = a[-1]
s = a.size
x = s - 2
z = x.to_s
 if s > 10
 puts b + z + c
 else
 puts a
 end

x = gets.chomp.to_i

for i in 0...x do
   words = gets.chomp.to_s
   if words.length >= 10
    puts words[0].to_s + (words.length-2).to_s + words[-1]
   else
    puts words
   end
end

(gets.to_i).times do
  ss=gets
  ss[1,ss.length-3] = ss.length>10? (ss.length-3).to_s : ss[1,ss.length-3]
  puts (ss)
end

gets.chomp.to_i.times do
    a = gets.chomp
    if a.length<10
        puts a
    else
        print a[0],a.length-2,a[a.length-1],"\n"
    end
end

len = 10
n = gets.to_i
while n > 0 do
    word = gets.chomp
    if word.size > len
        p [word.chars.first, word.size-2, word.chars.last].join
    else
        p word
    end
    n -= 1
end

n = gets
n = n.to_i

for i in (1..n)
	string = gets
	if(string.size()<11)
		puts string
	else
		stringFinal = string[0]
		numero = string.size - 3
		stringFinal << numero.to_s
		stringFinal << string[string.size - 2]
		puts stringFinal
	end
end

n = STDIN.gets
while word = STDIN.gets
  word.strip!
  if word.length > 10
    puts "#{word[0,1]}#{word.length - 2}#{word[word.length-1,1]}"
  end
end

n = gets.to_i

$i = 0

while $i < n
    s = gets
    if s.size < 11
        puts s
    elsif
        puts "#{s[0]}#{s.size - 3}#{s[s.size - 2]}"
    end

    $i += 1
end

number_of_words = gets.to_i

words = []

number_of_words.times do |n|
    word = gets
    words.push(word)
end

words.each do |word|
    if word.length <= 10
        puts word
    else
        number_of_letters_between = word.length - 3
        fist_letter = word[0]
        last_letter = word[-2]
        puts "#{fist_letter}#{number_of_letters_between}#{last_letter}"
    end
end

n = gets.to_i
i = 0
y = []
while i < n
  x = gets
  if x.length > 10
    x[1...x.length-2] = (x.length-2).to_s
  end
  y << x
  i +=1
end

puts y

gets.chomp.to_i.times do
    word = gets.chomp
    if word.length >= 10
        print word[0], ((word.length)-2).to_s, word[-1], "\n"
    else
        print word, "\n"
    end
end

n = gets.to_i

words = []

n.times do
	curr = gets

	if curr.length > 10
		last = curr[curr.length - 2, 1]
		curr = curr[0] + (curr.length - 3).to_s
		curr = curr + last
	end

	words << curr
end

puts words

len = gets.to_i

while word = gets do
    word = word.chomp
    if word.size > len
        p [word.chars.first, word.size-2, word.chars.last].join
    else
        p word
    end
end

a = gets
b = []
c = a[0]
b << c
d = a[1..-2].length
b << d
e = a[-1]
b << e
f = b.join

if d > 10
puts f

else
puts a

end

(gets.to_i).times do
  str = gets
   if str.length > 10 then
     str= str[0..0] + (str.length - 2).to_s + str[-1..-1]
   end
   p str
end

number_of_words = gets.to_i

words = []

number_of_words.times do |n|
    word = gets
    words.push(word)
end

words.each do |word|
    if word.length < 10
        puts word
    else
        number_of_letters_between = word.length - 3
        fist_letter = word[0]
        last_letter = word[-2]
        puts "#{fist_letter}#{number_of_letters_between}#{last_letter}"
    end
end

n = gets.chomp.to_i
for i in(0...n)
    s = gets.chomp
    if s.length() < 10
        puts s
    else
        print s[0],s.length()-2,s[s.length()-1]
        puts
    end
end

n=gets.chomp.to_i.times{a=gets.chomp.to_s;o=a.length;if o>10 then printf("%c%d%c",a[0],o-2,a[o-1]);else puts(a)end}

n=gets.to_i
a= []
for i in 0..n-1
	a[i] = gets
end
a.each do |val|
	len = val.length
	puts  len <= 10 ? val : "#{val[0]}#{len-3}#{val[len-2]}"
end

array = STDIN
array.each { |x| if x.size > 10 then puts "#{x[0]}#{x.size - 2}#{x[-1]}" elsif x =~ /[0-9]/ then nil else puts x end }

n = gets.to_i
i = 0
y = []
while i < n
  x = gets
  if x.length > 10
    x[1...x.length-2] = (x.length-3).to_s
  end
  y << x
  i +=1
end

puts y

array = [4, "word", "localization", "internationalization", "pneumonoultramicroscopicsilicovolcanoconiosis"]
array.each { |x| if x.size > 10 then puts "#{x[0]}#{x.size - 2}#{x[-1]}" elsif x == x.to_s then puts x end }

n = gets.to_i
a =[]
for i in 0..n-1 do
  a = a + [gets]
  if a[i].size >= 10
    a[i] = (a[i][0] + (a[i].size - 3).to_s + a[i][a[i].size - 2])
  end
end
puts a

x = gets.to_i

def check(j)
	if j.length <= 10
		puts j
	else
		puts j[0] + j.length.to_s + j[-1]
	end
end

for i in 1..x do
	y = gets.chomp
	check(y)
end

x=gets.to_i
x.times do
n=gets.chomp
z=n.length
if z > 10
print n[0]
print z-2
print n[z-1]
else
print n
print "\n"
end
end

b = /[0-9]/
x = gets.chomp
if x.size > 10
  puts "#{x[0]}#{x.size - 2}#{x[-1]}"
elsif x =~ /[0-9]/
 nil
else
  puts x
end

a = gets.to_s
b = a[0]
c = a[-1]
s = a.size
 if s >= 12
 puts b
 puts s
 puts c
 else
 puts a
 end

n = gets.to_i
n.times do |i|
	temp = gets
	if (temp.size < 11)
		puts temp
	else
		k = temp.size - 2
		curr = temp[0] + k.to_s + temp[k]
		puts curr
	end
end

a = gets.to_s
b = a[0]
c = a[-1]
s = a.size
x = s - 2
z = x.to_s
 if x >= 10
 puts b + z + c
 else
 puts a
 end

n = gets.chomp.to_i

n.times do
    s = gets.chomp
    if s.length >= 10
        p "#{s[0]}#{s.length-2}#{s[s.length-1]}"
    else
        p s
    end
end

def abb ()
    n = gets.to_i
  words = Array.new(n)
    for i in 0...n
      word = gets.to_s
      words[i] = word.chomp!
    end
  words.each{ |word|
        if word.length < 10
            puts word
        else
            puts "#{word[0]}#{word.length-2}#{word[-1]}"
        end
  }
end

abb

gets.to_i.times do
  s=gets
  puts s.size>10 ? "#{s[0]}#{s.size-1}#{s[-1]}": s
end


gets.strip.to_i.times {
	n = gets.strip.to_i
	sq = Math.sqrt(n).to_i
	an = []
	sq.times { |i|
		an << i + 1
		an << n / (i + 1)
	}
	an << 0
	an = an.uniq.sort
	puts an.size
	puts an.join(' ')
}

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

t = inp[0]
t.times do
  n = inp[0]
  ans = []
  (0...((n**0.5).ceil)).each do |d|
    ans.push(d)
  end
  mae = ans[-1]
  (1..((n**0.5).ceil)).reverse_each do |d|
    if n/d > mae
      mae = n/d
      ans.push(mae)
    end
  end
  p ans.size
  puts ans.join(" ")
end

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

t = inp[0]
t.times do
  n = inp[0]
  mae = 0
  mae = 1
  ans = [0,1]
  (1..((n**0.5).ceil)).reverse_each do |d|
    if n/d > mae
      mae = n/d
      ans.push(mae)
    end
  end
  puts ans.size
  puts ans.join(" ")
end

gets.strip.to_i.times {
	n = gets.strip.to_i
	sq = Math.sqrt(n).to_i
	an = []
	sq.times { |i|
		next if i == 0
		an << i
		an << n / i
	}
	an << 0
	puts an.size
	print an.uniq.sort.join(' ')
}

gets.strip.to_i.times {
	n = gets.strip.to_i
	sq = Math.sqrt(n).to_i
	an = []
	sq.times { |i|
		next if i == 0
		an << i
		an << n / i
	}
	an << 0
	puts an.uniq.sort.join(' ')
}

gets.strip.to_i.times {
	n = gets.strip.to_i
	sq = Math.sqrt(n).to_i
	an = []
	sq.times { |i|
		an << i + 1
		an << n / (i + 1)
	}
	an << 0
	puts an.size
	puts an.uniq.sort.join(' ')
}


t = gets.chomp.to_i
t.times do
  a, b = gets.chomp.split(' ').map(&:to_i)
  s = gets.chomp.split('')
  del = []
  cur = 0
  s.each do |el|
    if el == '1'
      del << cur if cur > 0
      cur = 0
    else
      cur += 1
    end
  end
  del.shift if s[0] == '0'

  sum = a
  del.each do |el|
    sum += [a, b * el].min
  end
  if s.count('1') >= 1
    puts sum
  else
    puts 0
  end
end

t = gets.chomp.to_i
t.times do
    a, b = gets.chomp.split(" ").map(&:to_i)
    x = gets.chomp.split("")
    ookisa = x.size
    dp = Array.new(ookisa+1,Float::INFINITY)
    dp[0] = 0
    cost = 0
    i = 1
    renzoku = 0
    zero = 0
    first = 0
    until x == []
        me = x.shift
        #p me
        #p renzoku
        if me == "1"
           #p renzoku
            if renzoku >= 1
                dp[i] = dp[i-1]
            else
                #p dp
                #p zero
                if first >= 1
                    dp[i] = [zero*b+dp[i-1],dp[i-1]+a].min
                else
                    dp[i] = a
                    first += 1
                end
            end
            # p renzoku
            # p zero
            zero = 0
            renzoku += 1
        else
            zero += 1
            renzoku = 0
            dp[i] = dp[i-1]
        end
        i += 1
        #p zero
        #p dp
    end
    puts dp.pop
end

t = gets.chomp.to_i
t.times do
  a, b = gets.chomp.split(' ').map(&:to_i)
  s = gets.chomp.split('')
  del = []
  cur = 0
  s.each do |el|
    if el == '1'
      del << cur if cur > 0
      cur = 0
    else
      cur += 1
    end
  end
  del.shift if s[0] == '0'

  sum = a
  del.each do |el|
    sum += [a, b * el].min
  end
  if s.count('1') > 1
    puts sum
  else
    puts 0
  end
end

t = gets.chomp.to_i
t.times do
  a, b = gets.chomp.split(' ').map(&:to_i)
  s = gets.chomp.split('')
  del = []
  cur = 0
  s.each do |el|
    if el == '1'
      del << cur if cur > 0
      cur = 0
    else
      cur += 1
    end
  end
  puts "s = " + s.to_s
  del.shift if s[0] == '0'

  sum = a
  del.each do |el|
    sum += [a, b * el].min
  end
  if s.count('1') >= 1
    puts sum
  else
    puts 0
  end
end


gets.to_i.times{
  a=gets.split.map(&:to_i)-[-1]
  ans=a.inject(0,:|)
  print ans.to_s+' '
}

gets.to_i.times{
  a=gets.split.map(&:to_i)-[-1]
  p a.inject(0,:|)
}

gets.to_i.times{
  a=gets.split.map(&:to_i)-[-1]
  ans=a.inject(:|)
  print ans.to_s+' '
}


gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  vis = Array.new(n, false)

  l = 1
  cnt = 1
  max = 1

  num = a[0]
  a_l = 0
  a_r = 0

  n_l = nil
  n_r = 0

  while l < n
    next l += 1 if vis[l-1] && vis[l]
    if a[l-1] == a[l]
      n_l ||= l-1
      n_r = l
      cnt += 1
    else
      if cnt > 1
        r = l
        cnt1 = cnt
        while r < n
          if a[r] == a[n_l]
            vis[r] = true
            cnt1 += 1
          else
            cnt1 -= 1
          end
          if cnt1 > cnt
            cnt = cnt1
            n_r = r
          end
          break if cnt1 <= 0
          r += 1
        end
      end
      if cnt > max
        a_l = n_l
        a_r = n_r
        max = cnt
        num = a[a_l]
      end
      n_l = nil
      n_r = 0
      cnt = 1
    end
    l += 1
  end
  if cnt > max
    a_l = n_l
    a_r = n_r
    max = cnt
    num = a[n_l]
  end

  puts [num, a_l+1, a_r+1].join(" ")
end

main = -> {
    t = int
    t.times do
        n = int
        a = ints

        indices = {}
        a.each_with_index do |x, i|
            (indices[x] ||= []) << i
        end

        ans = 1
        ans_value = [a[0], 1, 1]
        indices.each do |x, row|
            sum = 1
            first = row[0]
            prev = row[0]
            row[1..].each do |i|
                sum = sum - (i - prev) + 2
                prev = i
                # p [x, first, i, sum]
                if sum < 1
                    sum = 1
                    first = i
                end
                if ans < sum
                    ans = sum
                    ans_value = [x, first + 1, i + 1]
                end
            end
        end

        puts ans_value * " "
    end
}

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

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  last = {}
  dp = [0] * xs.size
  xs.each.with_index do |x, i|
    if y = last[x]
      dp[i] = [dp[y] - (i - y) + 2, 1].max
    else
      dp[i] = 1
    end
    last[x] = i
  end
  max = dp.max
  l = r = dp.index(max)
  a = xs[l]
  count = 1
  until count == max
    l -= 1
    count += xs[l] == a ? 1 : -1
  end
  puts "#{a} #{l + 1} #{r + 1}"
end

t = gets.to_i
t.times do
  n = gets.to_i
  x = gets.split.map(&:to_i)
  indices = {}
  x.each_with_index do |a, i|
    (indices[a] ||= []) << i
  end

  ans = 1
  ans_value = [x[0], 1, 1]
  indices.each do |a, row|
    b = [1]
    row.each_cons(2) do |i, j|
      b << (i - j + 1) << 1
    end

    l = 0
    sum = 0
    b.each_with_index do |x, r|
      if sum < 0
        l = r
        sum = x
      else
        sum += x
      end
      if sum > ans
        ans = sum
        ans_value = [a, row[l / 2] + 1, row[r / 2] + 1]
      end
    end
  end

  puts ans_value.join(" ")
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  index = {}

  a.each_with_index do |e, i|
    index[e] ||= []
    index[e] << i
  end

  cnt = 1

  ans = a[0]
  l = 1
  r = 1
  index.each do |k, v|
    sum = 1
    first = v[0]
    prev = v[0]

    v[1..].each do |i|
      sum = sum-(i-prev)+2
      prev = i
      if sum < 1
        sum = 1
        first = i
      end

      if cnt < sum
        cnt = sum
        ans = k
        l = first+1
        r = i+1
      end
    end
  end

  puts [ans, l, r].join(" ")
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  vis = Array.new(n, false)

  l = 1
  cnt = 1
  max = 1

  num = a[0]
  a_l = 0
  a_r = 0

  n_l = nil
  n_r = 0

  while l < n
    next l += 1 if vis[l-1] && vis[l]
    if a[l-1] == a[l]
      n_l ||= l-1
      n_r = l
      cnt += 1
    else
      if cnt > 1
        r = l
        cnt1 = cnt
        while r < n
          if a[r] == a[n_l]
            vis[r] = true
            cnt1 += 1
          else
            cnt1 -= 1
          end
          if cnt1 > cnt
            cnt = cnt1
            n_r = r
          end
          break if cnt1 <= 0
          r += 1
        end
      end
      if cnt > max
        a_l = n_l
        a_r = n_r
        max = cnt
      end
      n_l = nil
      n_r = 0
      cnt = 1
    end
    l += 1
  end
  if cnt > max
    a_l = n_l
    a_r = n_r
    max = cnt
    num = a[n_l]
  end

  puts [num, a_l+1, a_r+1].join(" ")
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  vis = Array.new(n, false)

  l = 1
  cnt = 1
  max = 1

  num = a[0]
  a_l = 0
  a_r = 0

  n_l = nil
  n_r = 0

  while l < n
    next l += 1 if vis[l]
    vis[l] = true

    if a[l-1] == a[l]
      n_l ||= l-1
      n_r = l
      cnt += 1
    else
      if cnt > 1
        r = l
        cnt1 = cnt
        while r < n
          if a[r] == a[l-1]
            vis[r] = true
            cnt1 += 1
          else
            cnt1 -= 1
          end
          if cnt1 > cnt
            cnt = cnt1
            n_r = r
          end
          break if cnt1 <= 0
          r += 1
        end
      end
      if cnt > max
        a_l = n_l
        a_r = n_r
        max = cnt
        num = a[l-1]
      end
      n_l = nil
      n_r = 0
      cnt = 1
    end
    l += 1
  end
  if cnt > max
    a_l = n_l
    a_r = n_r
    max = cnt
    num = a[n_l]
  end

  puts [num, a_l+1, a_r+1].join(" ")
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  vis = Array.new(n, false)

  l = 1
  cnt = 1
  max = 1

  num = a[0]
  a_l = 0
  a_r = 0

  n_l = nil
  n_r = 0

  while l < n
    next l += 1 if vis[l]
    vis[l] = true

    if a[l-1] == a[l]
      n_l ||= l-1
      n_r = l
      cnt += 1
    else
      if cnt > 1
        r = l
        cnt1 = cnt
        while r < n
          if a[r] == a[l-1]
            vis[r] = true
            cnt1 += 1
          else
            cnt1 -= 1
          end
          if cnt1 > cnt
            cnt = cnt1
            n_r = r
          end
          break if cnt1 < 0
          r += 1
        end
      end
      if cnt > max
        a_l = n_l
        a_r = n_r
        max = cnt
        num = a[l-1]
      end
      n_l = nil
      n_r = 0
      cnt = 1
    end
    l += 1
  end
  if cnt > max
    a_l = n_l
    a_r = n_r
    max = cnt
    num = a[n_l]
  end

  puts [num, a_l+1, a_r+1].join(" ")
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  vis = Array.new(n, false)

  l = 1
  cnt = 0
  max = 0
  num = -1

  n_l = nil
  n_r = 0

  a_l = -1
  a_r = -1

  while l < n
    next l += 1 if vis[l]
    vis[l] = true

    if a[l-1] == a[l]
      n_l ||= l-1
      n_r = l
      cnt += 1
    else
      if cnt > 0
        r = l
        cnt1 = cnt
        while r < n
          if a[r] == a[l-1]
            vis[r] = true
            cnt1 += 1
          else
            cnt1 -= 1
          end
          if cnt1 > cnt
            cnt = cnt1
            n_r = r
          end
          break if cnt1 < 0
          r += 1
        end
      end
      if cnt > max
        a_l = n_l
        a_r = n_r
        max = cnt
        num = a[l-1]
      end
      n_l = nil
      n_r = 0
      cnt = 0
    end
    l += 1
  end
  if cnt > max
    a_l = n_l
    a_r = n_r
    max = cnt
    num = a[n_l]
  end

  if n == 1 || num == -1
    num = a[0]
    a_l = 0
    a_r = 0
  end
  puts [num, a_l+1, a_r+1].join(" ")
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  vis = Array.new(n, false)

  l = 1
  cnt = 0
  max = 0
  num = -1

  n_l = nil
  n_r = 0

  a_l = -1
  a_r = -1

  while l < n
    next l += 1 if vis[l]
    vis[l] = true

    if a[l-1] == a[l]
      n_l ||= l-1
      n_r = l
      cnt += 1
    else
      if cnt > 0
        r = l
        cnt1 = cnt
        while r < n
          if a[r] == a[l-1]
            vis[r] = true
            cnt1 += 1
          else
            cnt1 -= 1
          end
          if cnt1 > cnt
            cnt = cnt1
            n_r = r
          end
          break if cnt1 < 0
          r += 1
        end
      end
      if cnt > max
        a_l = n_l
        a_r = n_r
        max = cnt
        num = a[l-1]
      end
      n_l = nil
      n_r = 0
      cnt = 0
    end
    l += 1
  end
  if cnt > max
    a_l = n_l
    a_r = n_r
    max = cnt
    num = a[l-1]
  end
  if n == 1 || num == -1
    num = a[0]
    a_l = 0
    a_r = 0
  end
  puts [num, a_l+1, a_r+1].join(" ")
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  vis = Array.new(n, false)

  l = 1
  cnt = 0
  max = 0
  num = 0

  n_l = nil
  n_r = 0

  a_l = -1
  a_r = -1

  while l < n
    next l += 1 if vis[l]
    vis[l] = true

    if a[l-1] == a[l]
      n_l ||= l-1
      cnt += 1
    else
      if cnt > 0
        r = l
        n_r = r-1
        cnt1 = cnt
        while r < n
          if a[r] == a[l-1]
            vis[r] = true
            cnt1 += 1
          else
            cnt1 -= 1
          end
          if cnt1 > cnt
            cnt = cnt1
            n_r = r
          end
          break if cnt1 < 0
          r += 1
        end
      end
      if cnt > max
        a_l = n_l
        a_r = n_r
        max = cnt
        num = a[l-1]
      end
      n_l = nil
      n_r = 0
      cnt = 0
    end
    l += 1
  end
  if n == 1 || a_l == -1
    num = a[0]
    a_l = 0
    a_r = 0
  end
  puts [num, a_l+1, a_r+1].join(" ")
end

t = gets.to_i
t.times do
  n = gets.to_i
  x = gets.split.map(&:to_i)
  indices = {}
  x.each_with_index do |a, i|
    (indices[a] ||= []) << i
  end

  ans = 1
  ans_value = [x[0], 1, 1]
  indices.each do |a, row|
    b = [1]
    row.each_cons(2) do |i, j|
      b << (i - j + 1) << 1
    end

    l = 0
    sum = 1
    b.each_with_index do |x, r|
      if sum < 0
        l = r
        sum = x
      else
        sum += x
      end
      if sum > ans
        ans = sum
        ans_value = [a, row[l / 2] + 1, row[r / 2] + 1]
      end
    end
  end

  p ans
  puts ans_value.join(" ")
end

t = gets.to_i
t.times do
  n = gets.to_i
  x = gets.split.map(&:to_i)
  indices = {}
  x.each_with_index do |a, i|
    (indices[a] ||= []) << i
  end

  ans = 1
  ans_value = [x[0], 1, 1]
  indices.each do |a, row|
    b = [1]
    row.each_cons(2) do |i, j|
      b << (i - j + 1) << 1
    end

    l = 0
    sum = 1
    b.each_with_index do |x, r|
      if sum < 0
        l = r
        sum = x
      else
        sum += x
      end
      if sum > ans
        ans = sum
        ans_value = [a, row[l / 2] + 1, row[r / 2] + 1]
      end
    end
  end

  puts ans_value.join(" ")
end

t = gets.to_i
t.times do
  n = gets.to_i
  x = gets.split.map(&:to_i)
  indices = {}
  x.each_with_index do |a, i|
    (indices[a] ||= []) << i
  end

  ans = 0
  ans_value = nil
  indices.each do |a, row|
    b = [1]
    row.each_cons(2) do |i, j|
      b << (i - j + 1) << 1
    end

    l = 0
    sum = 1
    b.each_with_index do |x, r|
      if sum < 0
        l = r
        sum = x
      else
        sum += x
      end
      if sum > ans
        ans = sum
        ans_value = [a, row[l / 2] + 1, row[r / 2] + 1]
      end
    end
  end

  puts ans_value.join(" ")
end

t = gets.to_i
t.times do
  n = gets.to_i
  x = gets.split.map(&:to_i)
  indices = {}
  x.each_with_index do |a, i|
    (indices[a] ||= []) << i
  end

  ans = 0
  ans_value = nil
  indices.each do |a, row|
    b = [1]
    row.each_cons(2) do |i, j|
      b << (j - i + 1) << 1
    end

    l = 0
    sum = 1
    b.each_with_index do |x, r|
      if sum < 0
        l = r
	sum = x
      else
        sum += x
      end
      if sum > ans
        ans = sum
	ans_value = [a, row[l / 2] + 1, row[r / 2] + 1]
      end
    end
  end

  puts ans_value.join(" ")
end


# coding: utf-8

# if __FILE__ == $0
# end
C = Struct.new(:x, :y)
t = gets.to_i
t.times {
    n = gets.to_i
    ar = []
    n.times {
        ar << C.new(*gets.split.map(&:to_i))
    }
    ar.sort! { |a, b|
        if a.x != b.x
            a.x - b.x
        else
            a.y - b.y
        end
    }
    possible = true
    commands = []
    cx, cy = 0, 0
    ar.each { |c|
        # p ar
        if cx <= c.x && cy <= c.y
            # lexicographical move
            # ABCDEFG.....OPQ'R'ST'U'
            # R first, U last
            commands << 'R'*(c.x-cx)
            commands << 'U'*(c.y-cy)
            cx = c.x
            cy = c.y
        else
            possible = false
            break
        end
    }

    if possible
        puts "YES"
        puts commands.join('')
    else
        puts "NO"
    end
}

#!usr/bin/env ruby

# https://codeforces.com/contest/1294/problem/B

require 'set'

n = gets.to_i

n.times do
 size = gets.to_i
 pkgs = {}
 size.times do
  cords = gets.split(' ').map(&:to_i)

  pkgs[cords[0]] ||= []
  pkgs[cords[0]] << cords[1]
 end

 x = y = 0
 answer = ""
 failed = false

 while !pkgs.empty? do
  if pkgs[x].nil?
   x += 1
   answer += "R"
  else
   mini = pkgs[x].min
   maxi = pkgs[x].max

   if mini < y
    failed = true
    break
   else
    answer += "U" * (maxi - y)
    y = maxi
    pkgs.delete x
   end
  end
 end

 if failed
  puts "NO"
 else
  puts "YES"
  puts answer
 end
end

#!usr/bin/env ruby

# https://codeforces.com/contest/1294/problem/B

n = gets.to_i

n.times do
 size = gets.to_i
 pkgs = {}
 size.times do
  cords = gets.split(' ').map(&:to_i)

  pkgs[cords[0]] ||= []
  pkgs[cords[0]] << cords[1]
 end

 x = y = 0
 answer = ""
 failed = false

 while !pkgs.empty? do
  if pkgs[x].nil?
   x += 1
   answer += "R"
  else
   mini = maxi = y

   pkgs[x].each do |val|
    mini = val if val < mini
    maxi = val if val > maxi
   end

   if mini < y
    failed = true
    break
   else
    answer += "U" * (maxi - y)
    y = maxi
    pkgs.delete x
   end
  end
 end

 if failed
  puts "NO"
 else
  puts "YES"
  puts answer
 end
end

#!usr/bin/env ruby

# https://codeforces.com/contest/1294/problem/B

n = gets.to_i

n.times do
 size = gets.to_i
 pkgs = []
 size.times do
  pkgs << gets.split(' ').map(&:to_i)
 end

 pkgs.sort!

 x = y = 0
 failed = false
 answer = ""

 pkgs.each do |p|
  dx = p[0] - x
  dy = p[1] - y

  if dx < 0 || dy < 0
   failed = true
   break
  else
   answer += "R" * dx + "U" * dy
   x = p[0]
   y = p[1]
  end
 end

 if failed
  puts "NO"
 else
  puts "YES"
  puts answer
 end
end

T = gets.to_i

T.times do
  n = gets.to_i

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

  packages.sort_by! { |x, y| [x, y] }

  ans = ''
  cx = 0
  cy = 0
  success = true

  packages.each do |x, y|
    dx = x - cx
    dy = y - cy

    if dx < 0 || dy < 0
      success = false
      break
    end

    ans += 'R' * dx + 'U' * dy
    cx = x
    cy = y
  end

  if success
    puts 'YES'
    puts ans
  else
    puts 'NO'
  end
end

# coding: utf-8

# if __FILE__ == $0
# end
C = Struct.new(:x, :y)
t = gets.to_i
t.times {
    n = gets.to_i
    ar = []
    n.times {
        ar << gets.chomp
    }
    ar.sort!
    possible = true
    commands = []
    cx, cy = 0, 0
    ar.each { |c|
        x, y = c.split.map(&:to_i)
        if cx <= x && cy <= y
            commands << 'R'*(x-cx)
            commands << 'U'*(y-cy)
            cx = x
            cy = y
        else
            possible = false
            break
        end
    }

    if possible
        puts "YES"
        puts commands.join('')
    else
        puts "NO"
    end
}


def solve
  n, m, x, y = gets.strip.split(' ').map(&:to_i)
  an = 0
  n.times {
    str = gets.strip.split('')
    count = 0
    str.each_with_index { |e, i|
      if e == '.' && i < str.size - 1
        count += 1
      else
        count += 1 if e == '.'
        two = (count / 2).floor
        if two * y < two * 2 * x && !two.zero?
          an += (two * y)
        else
          an += (two * 2 * x)
        end
        an += x if count.odd?
        count = 0
      end
    }
  }
  puts an
end

gets.strip.to_i.times { solve }

  q = gets.chomp.to_i

  q.times {

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

    table = []
    n.times {
      row = gets.chomp
      table << row
    }

    d = []
    n.times { |i|
      d << []
      m.times {
        d[i] << 0
      }
    }

    n.times { |i|
      m.times { |j|

        if j == 0

          if table[i][j] == '.'
            d[i][j] = x
          end

          next
        end

        d[i][j] = d[i][j - 1]

        if table[i][j] == '*'
          next
        end

        d[i][j] = d[i][j - 1] + x

        if table[i][j - 1] == '.'
          curVal = 0
          if j == 1
            curVal = y
          else
            curVal = d[i][j - 2] + y
          end

          d[i][j] = [d[i][j], curVal].min
        end
      }
    }

    sum = 0
    d.each { |row|
      sum += row.last
    }

    puts sum
  }

def solve
  n, m, x, y = gets.strip.split(' ').map(&:to_i)
  an = 0
  n.times {
    str = gets.strip.split('')
    count = 0
    str.each_with_index { |e, i|
      if e == '.' && i < str.size - 1
        count += 1
      else
        count += 1 if e == '.'
        two = (count / 2).floor
        if two * y < two * 2 * x && !two.zero?
          an += (two * y)
        else
          an += (two * 2 * x)
          an += x if count.odd?
        end
        count = 0
      end
    }
  }
    puts an
end

gets.strip.to_i.times { solve }

def solve
  n, m, x, y = gets.strip.split(' ').map(&:to_i)
  an = 0
  n.times {
    str = gets.strip.split('')
    count = 0
    str.each_with_index { |e, i|
      if e == '.' && i < str.size - 1
        count += 1
      else
        count += 1 if e == '.'
        two = (count / 2).to_i
        if two * y < count * x && !two.zero?
          an += (two * y)
        else
          an += (count * x)
        end
        count = 0
      end
    }
  }
    puts an
end

gets.strip.to_i.times { solve }

def solve
  n, m, x, y = gets.strip.split(' ').map(&:to_i)
  an = 0
  n.times {
    str = gets.strip.split('')
    count = 0
    str.each_with_index { |e, i|
      if e == '.' && i < str.size - 1
        count += 1
      else
        count += 1 if e == '.'
        two = (count / 2).to_i
        if two * y < two * 2 * x && !two.zero?
          an += (two * y)
        else
          an += (two * 2 * x)
          an += x if count.odd?
        end
        count = 0
      end
    }
  }
    puts an
end

gets.strip.to_i.times { solve }

def solve
  n, m, x, y = gets.strip.split(' ').map(&:to_i)
  an = 0
  n.times {
    str = gets.strip.split('')
    count = 0
    p str
    str.each_with_index { |e, i|
      if e == '.' && i < str.size - 1
        count += 1
      else
        count += 1 if e == '.'
        two = (count / 2).to_i
        if two * y < count * x && !two.zero?
          an += (two * y)
        else
          an += (count * x)
        end
        count = 0
      end
    }
  }
    puts an
end

gets.strip.to_i.times { solve }


n, m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
p (0...n).map{|i|b.map{|x| (0...n).select{|j|i!=j}.map{|j|a[j]*x}.max}.max}.min

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
tat=1
res=-1e18
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e18.to_i
a[tat]=a[n-1]
for i in 0..n-2
  for j in b
    res=[res,a[i]*j].max
  end
end
p res

n, m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
p (0...n).map{ |i|
b.map{|x|
(0...n).select{ |j| i!=j }.map{|j|a[j]*x}.max}.max}.min

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
if a[0]==-1000000000
  p b[0]
end
tat=1
res=-1e18
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e18
a[tat]=0
for i in a
  for j in b
    res=[res,i*j].max
  end
end
p res

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
tat=1
res=-1e18
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e18
a[tat]=0
for i in a
  for j in b
    res=[res,i*j].max
  end
end
p res

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
tat=1
res=-1e9
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e9
a[tat]=0
for i in a
  for j in b
    res=[res,i*j].max
  end
end
p res

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
if a[0]==-1000000000
  p b[0]
end
tat=1
res=-1e18
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e18
a[tat]=a[n-1]
for i in 0..n-2
  for j in b
    res=[res,a[i]*j].max
  end
end
p res

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
tat=1
res=-1e18
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e18
a[tat]=a[n-1]
for i in 0..n-2
  for j in b
    res=[res,a[i]*j].max
  end
end
p res

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
tat=1
res=-1e18
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e18
a[tat]=0
for i in a
  for j in b
    if i!=tat
    res=[res,i*j].max
    end
  end
end
p res

n,m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
if a[0]==-1000000000
  p b[0]
end
tat=1
res=-1e18
for i in 0..n-1
  for j in 0..m-1
    if a[i]*b[j]>res
      res=a[i]*b[j]
      tat=i
    end
  end
end
res=-1e18.to_i
a[tat]=a[n-1]
for i in 0..n-2
  for j in b
    res=[res,a[i]*j].max
  end
end
p res

n, m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
a.delete a.max
puts a.max * b.max

n, m=gets.split.map &:to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
a.sort!
puts a[n-2] * b.max


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

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

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

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

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

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

#!/usr/bin/ruby

B = {}
S = {}

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

algo

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

ag_buy = {}
ag_sell = {}

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

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

puts ar

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

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

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

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

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

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

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

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

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

out = ''

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

print out

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

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

s = Hash.new
b = Hash.new

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

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

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

out = ''

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

print out

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

s = Hash.new
b = Hash.new

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

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

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

out = ''

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

print out

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

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

s = Hash.new
b = Hash.new

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

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

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

out = ''

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

puts out

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#!/usr/bin/ruby

B = {}
S = {}

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

algo

#!/usr/bin/ruby

B = {}
S = {}

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

algo


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

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

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

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

end

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

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

# a = []

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

piglet_honey = 0

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

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

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

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

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

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

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

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

require 'scanf'

input = open("input.txt")

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

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

ret = 0

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

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


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

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

gets.strip.to_i.times { solve }

def solution
  n = read_int

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

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

  end

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

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

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

gets.strip.to_i.times { solve }


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

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

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

  tot_sec
end

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

puts calculate(n,l,r)

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

n = gets.to_i

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

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

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

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

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

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

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

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

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

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

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

puts ret

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

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

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

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

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

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

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

n = gets.to_i
data = []

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

  left_open+=left
  right_open+=right
}

left_closed = n - left_open
right_closed = n - right_open

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

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

n = gets.to_i
data = []

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

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

left_closed = n - left_open
right_closed = n - right_open

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

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

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

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

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

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

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

puts movement_count

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

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

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

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

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

n = gets.to_i

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

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

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

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

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

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

puts [left, right].max

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

puts ret.to_i

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

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

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

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

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

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

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

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

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


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

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

  end

end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

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

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

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

main

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


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

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

  end
end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

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

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

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

main


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

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


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

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

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

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

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

    end

    longest = time if time > longest
  end
  puts longest
end

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

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

gets.strip.to_i.times do
  puts solve
end

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

inp = gets.chomp.to_i
otv = []

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

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

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

crans = gets.chomp.split(" ")

startup = []
startdown = []

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

loop do

crans.size.times{|e|

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

}

#puts map.to_s

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

end

}

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

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

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

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

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

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

gets.strip.to_i.times do
  puts solve
end

inp = gets.chomp.to_i
otv = []

inp.times{ |w|
count = 0

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

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

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

otv.push(1)

else

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

end

}

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

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

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


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

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

t = gets.to_i

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

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

#  p arr

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

#  p arr

  newarr = []

  z = 1

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

  hasone = false
  hastwo = false

  maxtillnow = -1.0/0.0

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

#  next if done

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

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

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

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

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

t = gets.to_i

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

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

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

  newarr = []

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

  next if done

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

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

t = gets.to_i

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

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

#  p arr

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

#  p arr

  newarr = []

  z = 1

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

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

#  next if done

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

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

#  p hasone
#  p hastwo

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

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

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

t = gets.to_i

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

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

#  p arr

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

#  p arr

  newarr = []

  z = 1

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

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

#  next if done

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

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

#  p hasone
#  p hastwo

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

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

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

t = gets.to_i

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

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

#  p arr

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

#  p arr

  newarr = []

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

  next if done

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

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

t = gets.to_i

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

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

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

  newarr = []

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

  next if done

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


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

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

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

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

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

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

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

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

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

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