#Inf = Float::Infinity
#
#geti
#getia(delimiter = " ")
#
#Object # dcopy
#
#String # to_a
#   "abc".to_a #=> ["a","b","c"]
#String # to_ia
#   "1 2 3".to_ia #=> [1,2,3]
#

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

Inf = Float::INFINITY
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;
             def to_ia(d = " ");split(d).map(&:to_i);end;end
def getia(*arg);gets.to_ia(*arg);end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end

n, q = getia
c = [0] * 2000
n.times do
  a, b = getia.sort
  for i in a..b
    c[i] += 1
  end
end
res = Inf
for i in 0..1010
  if c[i] == n
    res = [(i-q).abs, res].min
  end
end
puts res == Inf ? -1 : res

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

mx1 = -1
mx2 = 1001
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	mx1 = [mx1, ix].max
	mx2 = [mx2, ax].min
end
if mx1 > mx2
	puts "-1"
else
	ret = 0
	if m < mx1 || m > mx2
		ret= [(mx1-m).abs, (mx2-m).abs].min
	end
	puts ret
end

class Range
	def ^(other)
		([first, other.first].max .. [last, other.last].min)
	end
end

n, x0 = gets.split.map{|i| i.to_i}
r = []
n.times do
	a, b = gets.split.map{|i| i.to_i}
	a, b = b, a if a > b
	r <<= (a .. b)
end
r = r.inject{|i, j| i ^ j}

unless r.first <= r.last then
	p -1
else
	p r.include?(x0) ? 0 : [(r.first - x0).abs, (r.last - x0).abs].min
end

n,x=gets.split.map(&:to_i)
sum=Array.new(1002,0)
tsum=Array.new(1002,0)
n.times{
        a,b=gets.split.map(&:to_i).sort
        b+=1
        tsum[a]+=1
        tsum[b]-=1
}
sum[0]=tsum[0]
ans=[]
ans.push(x) if sum[0]==n
1.upto(1001){|i|
        sum[i]=sum[i-1]+tsum[i]
        if sum[i]==n then
                ans.push((x-i).abs)
        end
}
ans.push(-1) if ans.size()==0

p ans.min

n,x = gets.split.map(&:to_i)
lmax = 0
rmin = 1000
n.times do |i|
  a,b = gets.split.map(&:to_i)
  if a > b then a,b = b,a end
  if a > lmax then lmax = a end
  if b < rmin then rmin = b end
end
#p lmax
#p rmin
if rmin < lmax then
  print -1
  elsif x < lmax then
  print lmax - x
  elsif x > rmin then
  print x - rmin
  else
  print 0
end
puts

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

seg_start = 0
seg_end = 1000
n.times do
	a, b = gets.split.map(&:to_i)
	a, b = b, a if a > b
	seg_start = a if a > seg_start
	seg_end = b if b < seg_end
end

if seg_end < seg_start
	puts -1
elsif seg_start < x && x < seg_end
	puts 0
else
	puts [(seg_start - x).abs, (seg_end - x).abs].min
end

# require 'set'
# n, x = gets.split.map(&:to_i)

# input = []
# n.times do
# 	input << gets.split.map(&:to_i)
# end

# interval = (input[0].min..input[0].max).to_set
# input.each do |segment|
# 	interval = interval & (segment.min..segment.max).to_set
# end
# if interval.empty?
# 	puts -1
# else
# 	puts interval.include?(x) ? 0 : [(interval.min - x).abs, (interval.max - x).abs].min
# end

require 'set'
n, x = gets.chomp!.split.map(&:to_i)

input = []
n.times do
	input << gets.chomp!.split.map(&:to_i)
end

interval = (input[0].min..input[0].max).to_set
input.each do |segment|
	interval = interval & (segment.min..segment.max).to_set
end
if interval.empty?
	puts -1
else
	puts interval.include?(x) ? 0 : [(interval.min - x).abs, (interval.max - x).abs].min
end

n,x = gets.split(" ").map(&:to_i)
start = -1
finish = 2000
n.times do
	a,b = gets.split(" ").map(&:to_i).minmax
	start = a if a > start
	finish = b if b < finish
end
ans = (start..finish).map{|a| (a-x).abs }.min
ans ||= -1
# puts "#{start} #{finish}"
puts "#{ans}"

# 3 3
# 0 7
# 8 10
# 11 13

n,d = gets.chomp!.split.map(&:to_i)
left,right = -1,1001
n.times do |x|
    a,b = gets.chomp!.split.map(&:to_i)
    a,b = [a,b].min,[a,b].max
    left = a if a > left
    right = b if b < right
end

ans = [left > d ? left-d : 0, right < d ? d-right : 0].max
ans = -1 if left > right
puts ans

#!/usr/bin/env ruby

n, x0 = gets.split.map(&:to_i)
x, y = -1000, 1000
n.times do
	a, b = gets.split.map(&:to_i)
	a, b = b, a if a > b
	x, y = [x, a].max, [y, b].min
end
puts x > y ? -1 : (x <= x0 && x0 <= y ? 0 : [x - x0, x0 - y].max)

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

ret = 0
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	if m < ix
		ret = [ix-m, ret].max
	end
	if m > ax
		ret = [m-ax, ret].max
	end
end
puts ret

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

ret = -1
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	if m < ix
		ret = [ix-m, ret].max
	end
	if m > ax
		ret = [m-ax, ret].max
	end
end
puts ret

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

mx1 = -1
mx2 = 1001
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	mx1 = [mx1, ix].max
	mx2 = [mx2, ax].min
end
if mx1 > mx2
	puts "-1"
else
	puts [(mx1-m).abs, (mx2-m).abs].min
end

class Range
	def ^(other)
		Range.new([first, other.first].max, [last, other.last].min)
	end
end

n, x0 = gets.split.map{|i| i.to_i}
r = []
n.times do
	a, b = gets.split.map{|i| i.to_i}
	a, b = b, a if a > b
	r <<= (a .. b)
end
r = r.inject{|i, j| i ^ j}

unless r.first < r.last then
	p -1
else
	p r.include?(x0) ? 0 : [(r.first - x0).abs, (r.last - x0).abs].min
end

n,d = gets.chomp!.split.map(&:to_i)
left,right = [],[]
n.times do |x|
    a,b = gets.chomp!.split.map(&:to_i)
    left << [a,b].min
    right << [a,b].max
end

ans1 = left.max > d ? left.max - d : 0
ans2 = right.min < d ? d - right.min : 0
puts [ans1,ans2].max

#!/usr/bin/env ruby

n, x0 = gets.split.map(&:to_i)
mn, mx = x0, x0
n.times do
	a, b = gets.split.map(&:to_i)
	a, b = b, a if a > b
	mx = [mx, a].max if a > x0
	mn = [mn, b].min if b < x0
end
puts mx - mn + [x0 - mn, mx - x0].min


#!ruby

str = STDIN.gets.strip
k = STDIN.gets.strip.to_i

charnums = Hash.new(0)

str.each_byte do |c|
    charnums[c] += 1
end

delchar = []

charnums.to_a.sort do |a,b|
    (a[1] <=> b[1]) * 2 + (b[0] <=> a[0])
end.each do |elem|
    if elem[1] <= k
        delchar << elem[0]
        k -= elem[1]
    else
        break
    end
end

delchar.each do |c|
    str.delete!("%c"%c)
end

puts str.unpack("C*").uniq.length
puts str

s = gets.chomp
n = gets.to_i
a = s.split(//).uniq
b = []
a.each { |x| b << s.count(x) }
while (n - b.min) >= 0
  n -= b.min
  s.delete!(a[b.index(b.min)])
  b[b.index(b.min)] = 9999999999999999
  break if s.empty? || b.empty?
end
puts s.split(//).uniq.size
puts s

#!ruby

str = STDIN.gets.strip
k = STDIN.gets.strip.to_i

charnums = Hash.new(0)

str.each_byte do |c|
    charnums[c] += 1
end

delchar = []

charnums.to_a.sort do |a,b|
    (a[1] <=> b[1]) * 2 + (b[0] <=> a[0])
end.each do |elem|
    if elem[1] < k
        delchar << elem[0]
        k -= elem[1]
    else
        break
    end
end

delchar.each do |c|
    str.delete!("%c"%c)
end

puts str.unpack("C*").uniq.length
puts str


n = gets.to_i

flip = ->(s) { s.each_char.map { |c| c == '+' ? '*' : '+'}.join }

if n == 0 then
  puts "+"
else
  ans = []
  ans[1] = ["++", "+*"]

  (2..n).each do |i|
    k = 2**i
    ans[i] = ans[i - 1].dup
    (k/2).times do |j|
      if j.even? then
        ans[i][j] += flip.(ans[i][j])
      else
        ans[i][j] += ans[i][j]
      end
    end
    (k/2).times do |j|
      ans[i] << ans[i - 1][j].reverse
    end
    ((k/2)...k).each do |j|
      if j.odd? then
        ans[i][j] = flip.(ans[i][j]) + ans[i][j]
      else
        ans[i][j] = ans[i][j] + ans[i][j]
      end
    end
  end

  puts ans[n]
end

n = gets.chomp.to_i

as = ['+']

def f(as)
  bs = []
  as.each do |a|
    b = ''
    for i in 0...a.size
      if a[i] == '+'
        b += '++'
      else
        b += '**'
      end
    end
    bs << b
  end
  as.each do |a|
    b = ''
    for i in 0...a.size
      if a[i] == '+'
        b += '+*'
      else
        b += '*+'
      end
    end
    bs << b
  end
  return bs
end

for i in 1..n
  as = f(as)
end

puts as.*"\n"


gets " "
xs = gets.split.map(&:to_i).sort
gets " "
ys = gets.split.map(&:to_i).sort
gets " "
zs = gets.split.map(&:to_i).sort
a,b = gets.split.map(&:to_i)

#p [a,b]
#r1 = xs.max
p1 = ys.max
p2 = zs.min

for r1 in xs.reverse
    r2 = r1/Math::sqrt(1.0*a/b*p2/p1+1)
    #p [r1,r2,p1,p2]
    #p p1*(1.0*r1*r1/r2/r2 - 1)/p2
    break puts r2 if r2 < r1
end

r1 = gets.split.map(&:to_f).slice!(1..-1).max
ro1 = gets.split.map(&:to_f).slice!(1..-1).max
ro2 = gets.split.map(&:to_f).slice!(1..-1).min
a, b = gets.split.map(&:to_f)

puts Math.sqrt((r1*r1*b*ro1)/(b*ro1+a*ro2))

x = gets.split.map(&:to_i)[1..-1].max
y = gets.split.map(&:to_i)[1..-1].max
z = gets.split.map(&:to_i)[1..-1].min
A, B = gets.split.map(&:to_i)
p Math.sqrt((x * x * y * B * 1.0 ) / (A * z + y * B))

a=gets.split.map(&:to_i)[1..-1].max;
b=gets.split.map(&:to_i)[1..-1].max;
c=gets.split.map(&:to_i)[1..-1].min;
d,e=gets.split.map &:to_f;
p (a*a*b*e/(c*d+b*e))**0.5


n = gets.chomp.to_i

def adjacent?(a,b)
  (a.ord - b.ord).abs == 1
end

def order(str)
  a, b = str.split(//).sort.partition {|c| c.ord.odd? }
  if a.empty? || b.empty?
    a.join + b.join
  elsif !adjacent?(a.first, b.last)
    b.join + a.join
  elsif !adjacent?(a.last, b.first)
    a.join + b.join
  else
    'No answer'
  end
end

n.times do
  str = gets.chomp
  puts order(str)
end

t = gets.to_i
t.times do
  s = gets.strip.chars.sort.join
  t = ''
  stk = []
  iter = 0
  until s.empty? && stk.empty?
    unless stk.empty?
      c = stk.shift
      if t.empty? || (t[-1].ord - c.ord).abs != 1
        t += c
      elsif (t[0].ord - c.ord).abs != 1
        t = c + t
      else
        stk.push(c)
      end
    end
    unless s.empty?
      c = s[0]
      s = s[1..-1]
      if t.empty? || (t[-1].ord - c.ord).abs != 1
        t += c
      elsif (t[0].ord - c.ord).abs != 1
        t = c + t
      else
        stk.push(c)
      end
    end
    iter += 1
    if iter == 2000
      t = "No answer"
      break
    end
  end
  puts t
end

alphabet = {"a"=>0,"b"=>1,"c"=>2,"d"=>3,"e"=>4,"f"=>5,"g"=>6,"h"=>7,"i"=>8,"j"=>9,"k"=>10,"l"=>11,"m"=>12,"n"=>13,"o"=>14,"p"=>15,"q"=>16,"r"=>17,"s"=>18,"t"=>19,"u"=>20,"v"=>21,"w"=>22,"x"=>23,"y"=>24,"z"=>25}

n = gets.to_i

n.times do
  cnt = [0]*26
  kind = []
  s = gets.chomp
  s.each_char do |c|
    cnt[alphabet[c]] += 1
    kind << alphabet[c]
  end

  kind.uniq!
  kind.sort!
  l = kind.length
  ans = [""]*26
  if l == 2
    replace = [0,1]
  elsif l == 3
    replace =  (kind[1] - kind[0] == 1) ? [0,2,1] : [1,0,2]
  elsif l == 4
    replace = [1,3,0,2]
  else
    replace = []*l
    t = [(l+2)/3,(l+1)/3,l/3]
    k = 0
    3.times do |i|
      t[i].times do |j|
        replace[j*3+i] = j + k
      end
      k += t[i]
    end
  end
  replace.each_with_index do |v,i|
    ans[v] = alphabet.key(kind[i]) * cnt[kind[i]]
  end

  ans = ans.join
  (s.length-1).times do |i|
    if (alphabet[ans[i]] - alphabet[ans[i+1]]).abs == 1
      ans = "No answer"
      break
    end
  end
  puts ans
end

gets.to_i.times do
    a,b=gets.chomp.chars.sort.partition{|e|e.ord.odd?}.map(&:join)
    ok=->x,y{ x.empty? or y.empty? or (x[-1].ord-y[0].ord).abs!=1 }
    puts (ok[a, b] ? a+b : ok[b, a] ? b+a : "No answer")
end

alphabet = {"a"=>0,"b"=>1,"c"=>2,"d"=>3,"e"=>4,"f"=>5,"g"=>6,"h"=>7,"i"=>8,"j"=>9,"k"=>10,"l"=>11,"m"=>12,"n"=>13,"o"=>14,"p"=>15,"q"=>16,"r"=>17,"s"=>18,"t"=>19,"u"=>20,"v"=>21,"w"=>22,"x"=>23,"y"=>24,"z"=>25}

n = gets.to_i
replace = [2,0,3,1]
n.times do
  cnt = [0]*26
  kind = []
  s = gets.chomp
  s.each_char do |c|
    cnt[alphabet[c]] += 1
    kind << alphabet[c]
  end
  t = 0

  kind.uniq!
  kind.sort!

  ans = [""]*26
  if kind.length == 3
    [0,2,1].each_with_index do |v,i|
      ans[v] = alphabet.key(kind[i]) * cnt[kind[i]]
    end
  else
    26.times do |i|
      if cnt[i] > 0
        ans[(t/4)*4 + replace[t%4]] = alphabet.key(i) * cnt[i]
        t += 1
      end
    end
  end
  #p ans
  ans = ans.join
  (s.length-1).times do |i|
    if (alphabet[ans[i]] - alphabet[ans[i+1]]).abs == 1
      ans = "No answer"
      break
    end
  end
  puts ans
end


n, m, x = gets.split.map(&:to_i)
keys = Hash.new { |hash, key| hash[key] = :unknown }

haveShift = false
arr = Array.new
(0...n).each do |i|
  l = gets.chomp
  l.chars.each { |e| keys[e] = :down if e != 'S'
    haveShift = true if e == 'S' }
  arr << l
end

(0...n).each do |i|
(0...m).each do |j|
if arr[i][j] == 'S'
1.upto(x) do |k|
  ((i - k)..(i + k)).each do |ii|
    ((j - k)..(j + k)).each do |jj|
      if ii >= 0 && ii < n && jj >= 0 && jj < m
        dy, dx = [(ii - i) * (ii - i), (jj - j) * (jj - j)]
        keys[arr[ii][jj]] = :up if arr[ii][jj] != 'S' && Math.sqrt(dy + dx) <= x
      end
    end
  end
end
end
end
end

gets

ret = 0
gets.chomp.chars.each do |t|
  if !keys.has_key?(t.downcase) || ((t >= 'A' && t <= 'Z') && !haveShift)
    ret = -1
    break
  end
  if (t >= 'A' && t <= 'Z') && keys[t.downcase] == :down
    ret += 1
  end
end

puts ret

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y].chr] = true

            if (s[x][y].chr == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j].chr] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h['S']

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a.downcase!
            return -1 if (!h[a])
            res += 1 if (!h_[a])
            else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

class Point
	attr_accessor :x
	attr_accessor :y

	def initialize(x1,y1)
    	@x = x1.to_f
    	@y = y1.to_f
  	end

	def distance_to(x1,y1)
    	dx = @x - x1
    	dy = @y - y1
    	d = Math.sqrt(dx * dx + dy * dy)
    	return(d)
  	end
end

n,m,xx = gets.chomp.split(/ /).map!{|x| x.to_i}
bbb = []
sss = []
mmm = Hash::new
mmm.default = -1
for i in 0..n-1
	bbb.push(gets.chomp)
	for j in 0..m-1
		sss.push(Point.new(j,i)) if bbb[i][j] == "S"
	end
end

for i in 0..n-1
	for j in 0..m-1
		if bbb[i][j] != "S" then
			wk = mmm[bbb[i][j]]
			sss.each{|x|
				wk2 = x.distance_to(j,i)
				if wk < 0 then
					wk = wk2
				else
					wk = wk2 if wk > wk2
				end
			}
			mmm[bbb[i][j]] = wk
		end
	end
end
q = gets.chomp.to_i
t = gets.chomp

ret = 0
t.each_byte{|x|
	x = x.chr
	if mmm.key?(x.downcase) == false then
		ret = -1
		break
	end
	if x.upcase == x then
		x.downcase!
		if mmm[x] < 0 then
			ret = -1
			break
		end
		ret += 1 if xx < mmm[x]
	end
}

puts ret

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

  has_shift = false
  kb=[]
  n.times{
    l = gets.chomp
    kb << l
    has_shift |= (l =~ /S/)
  }
  alphabets = (kb * '').gsub('S', '')

  need_two = {}
  n.times{|y1|
    m.times{|x1|
      next if kb[y1][x1] != ?S
      n.times{|y2|
        m.times{|x2|
          c = kb[y2][x2]
          next if c == ?S

          dist2 = (y2-y1)**2 + (x2-x1)**2
          need_two[c] = [need_two[c] || 99999999, dist2].min
        }
      }
    }
  }

  need_two.each do |k, v|
    need_two[k] = v > x * x
  end
  #p need_two

  q = gets.to_i
  str = gets.chomp

  ok = true
  str.each_char do |c|
    if !alphabets.index(c.downcase)
      ok = false
      break
    end

    if c =~ /[A-Z]/ && !has_shift
      ok = false
      break
    end
  end
  if !ok
    return -1
  end

  cnt = 0
  str.each_char do |c|
    if c =~ /[a-z]/
      next
    end

    if need_two[c.downcase[0]]
      cnt += 1
    end
  end
  cnt
end

p func

count = 0
o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
all_keys = {}
close_keys = {}
distant_keys = {}
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    if s == "S".ord
      shifts << [i, j]
    else
      all_keys[s] = true
    end
  end
  kb << line
end
shifts.each do |shift|
  kb.each_with_index do |line, i|
    line.each_with_index do |key, j|
      next if key == "S"
      if Math.sqrt((shift[0] - i).abs**2 + (shift[1] - j).abs**2) > x
        distant_keys[key] = true
      else
        close_keys[key] = true
      end
    end
  end
end
close_keys.each_key{|c| distant_keys.delete(c)}
gets
gets.strip.each_char.with_index do |c, i|
  u = c.ord < o
  c_ord = c.downcase.ord
  unless all_keys[c_ord]
    puts -1
    exit
  end
  if u
    if shifts.empty?
      puts -1
      exit
    end
    if distant_keys[c_ord]
      count += 1
    end
  end
end
puts count

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

=begin
     if (t.length > 1) then
     puts t
     p h
     end
=end

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h['S']

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a.upcase!
            return -1 if (!h[a])
            res += 1 if (!h_[a])
        else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

#$stdin = File.open("in.txt", "r")

def solve
    return '-1'
end

puts solve

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 83) then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets;

    t.each_byte{|a|
        return -1 if a < 97
    } if !h[83]

    res = 0

    t.each_byte{|a|
        if (a < 97) then
            a += 32
            return -1 if !h[a]
            res += 1 if !h_[a]
            else
            return -1 if !h[a]
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
printf("%d", solve)

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 83) then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    t.each_byte{|a|
        return -1 if a < 97
    } if !h[83]

    res = 0

    if (t.length > 1) then puts t end

    t.each_byte{|a|
        if (a < 97) then
            a += 32
            return -1 if (!h[a])
            res += 1 if (!h_[a])
            else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 83) then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    t.each_byte{|a|
        return -1 if a < 97
    } if !h[83]

    res = 0

    t.each_byte{|a|
        if (a < 97) then
            a += 32
            return -1 if !h[a]
            res += 1 if !h_[a]
            else
            return -1 if !h[a]
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
printf("%d", solve)

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << readline
    }

    (0...n).each{|x|
        (0...m).each{|y|
            if (s[x][y] == 83) then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
                h[s[x][y]] = true
            end
        }
    }

    gets; t = gets;

    puts t if (t.length > 1)
    t.each_char{|a|
        return -1 if a < 'a'
    } if !h['S']

    res = 0

    t.each_char{|a|
        if (t[i] < 'a') then
            t[i] += 32
            return -1 if !h[a]
            res += 1 if !h_[a]
        else
            return -1 if !h[a]
        end
    }
    return res
end

#$stdin = File.open("in.txt", "r")
printf("%d", solve)

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << readline
    }

    (0...n).each{|x|
        (0...m).each{|y|
            if (s[x][y] == 83) then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
                h[s[x][y]] = true
            end
        }
    }

    gets; t = gets;

    puts t

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h['S']

    res = 0

    t.each_char{|a|
        if (t[i] < 'a') then
            t[i] += 32
            return -1 if !h[a]
            res += 1 if !h_[a]
        else
            return -1 if !h[a]
        end
    }
    return res
end

#$stdin = File.open("in.txt", "r")
printf("%d", solve)

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h["S"]

=begin
    if (t.length > 1) then
        puts t
        p h
    end
=end

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a.upcase!
            return -1 if (!h[a])
            res += 1 if (!h_[a])
        else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

=begin
     if (t.length > 1) then
     puts t
     p h
     end
=end

    t.each_char{|a|
        if a < 'a' then
            p h; p h['S'];
            p h["S"];
            p h[87];
            return -1;
        end
    } if !h['S']

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a.upcase!
            return -1 if (!h[a])
            res += 1 if (!h_[a])
        else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    if (t.length > 1) then
        puts t
        p h
    end

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h["87"]

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a += 32
            return -1 if (!h[a])
            res += 1 if (!h_[a])
        else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 83) then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    if (t.length > 1) then
        puts t
        p h
    end

    t.each_byte{|a|
        return -1 if a < 97
    } if !h[83]

    res = 0

    t.each_byte{|a|
        if (a < 97) then
            a += 32
            return -1 if (!h[a])
            res += 1 if (!h_[a])
            else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << readline
    }

    (0...n).each{|x|
        (0...m).each{|y|
            if (s[x][y] == 83) then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
                h[s[x][y]] = true
            end
        }
    }

    q = readline; q = q.to_i; t = readline;

    puts t

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h['S']

    res = 0

    t.each_char{|a|
        if (t[i] < 'a') then
            t[i] += 32
            return -1 if !h[a]
            res += 1 if !h_[a]
        else
            return -1 if !h[a]
        end
    }
    return res
end

#$stdin = File.open("in.txt", "r")
printf("%d", solve)

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    if (t.length > 1) then
        puts t
        p h
    end

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h[87]

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a += 32
            return -1 if (!h[a])
            res += 1 if (!h_[a])
        else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

#$stdin = File.open("in.txt", "r")
puts '1'

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

    if (t.length > 1) then
        puts t
        p h
    end

    t.each_char{|a|
        return -1 if a < 'a'
    } if !h[83]

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a += 32
            return -1 if (!h[a])
            res += 1 if (!h_[a])
        else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

def solve

    def sqr(x)
        return x * x
    end

    n , m , x = readline.split(' ')
    n = n.to_i; m = m.to_i; x = x.to_i; xx = sqr(x)

    s = Array.new; h = Hash.new(false); h_ = Hash.new(false)

    n.times {
        s << gets
    }

    (0...n).each{|x|
        (0...m).each{|y|

            h[s[x][y]] = true

            if (s[x][y] == 'S') then
                (0...n).each{|i|
                    (0...m).each{|j|
                        if (sqr(i-x) + sqr(j-y) <= xx) then
                            h_[s[i][j]] = true
                        end
                    }
                }

                else
            end
        }
    }

    gets; t = gets; t.chop!

=begin
     if (t.length > 1) then
     puts t
     p h
     end
=end

    t.each_char{|a|
        if a < 'a' then
            if (t.length > 1) then
            p h; p h['S'];
            p h["S"];
            p h[87];
                end
            return -1;
        end
    } if !h['S']

    res = 0

    t.each_char{|a|
        if (a < 'a') then
            a.upcase!
            return -1 if (!h[a])
            res += 1 if (!h_[a])
        else
            return -1 if (!h[a])
        end
    }

    return res
end

#$stdin = File.open("in.txt", "r")
puts solve

n, m, x = gets.split.map(&:to_i)
keys = Hash.new { |hash, key| hash[key] = :unknown }

arr = Array.new
(0...n).each do |i|
  l = gets.chomp
  l.chars.each { |e| keys[e] = :down if e != 'S' }
  arr << l
end

(0...n).each do |i|
(0...m).each do |j|
if arr[i][j] == 'S'
1.upto(x) do |k|
  ((i - k)..(i + k)).each do |ii|
    ((j - k)..(j + k)).each do |jj|
      if ii >= 0 && ii < n && jj >= 0 && jj < m
        dy, dx = [(ii - i) * (ii - i), (jj - j) * (jj - j)]
        keys[arr[ii][jj]] = :up if arr[ii][jj] != 'S' && Math.sqrt(dy + dx) <= x
      end
    end
  end
end
end
end
end

gets

ret = 0
gets.chomp.chars.each do |t|
  if !keys.has_key?(t.downcase)
    ret = -1
    break
  end
  if (t >= 'A' && t <= 'Z') && keys[t.downcase] == :down
    ret += 1
  end
end

puts ret

n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
count = 0
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    shifts << [i, j] if s == "S".ord
  end
  kb << line
end
if shifts.empty?
  gets
  gets
  puts "-1"
  exit
end
gets
gets.gsub(/[A-Z]/) do |c|
  c = c.downcase
  catch(:a) do
    kb.each_with_index do |line, i|
      line.each_with_index do |key, j|
        if key == c.ord
          shifts.each do |s|
            d = Math.sqrt((s[0] - i).abs**2 + (s[1] - j).abs**2)
            throw :a if d <= x
          end
          count += 1
          throw :a
        end
      end
    end
  end
end
puts count == 0 ? -1 : count

count = 0
o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
all_keys = []
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    if s == "S".ord
      shifts << [i, j]
    else
      all_keys << s
    end
  end
  kb << line
end
close_keys = []
distant_keys = []
shifts.each do |shift|
  kb.each_with_index do |line, i|
    line.each_with_index do |key, j|
      next if key == "S"
      if Math.sqrt((shift[0] - i).abs**2 + (shift[1] - j).abs**2) > x
        distant_keys << key
      else
        close_keys << key
      end
    end
  end
end
all_keys.uniq!
distant_keys.uniq!
distant_keys = distant_keys - close_keys

gets
gets.strip.each_char do |c|
  u = c.ord < o
  c_ord = c.downcase.ord
  unless all_keys.include?(c_ord)
    p all_keys
    p c_ord
    puts -1
    exit
  end
  if u
    if shifts.empty?
      puts -1
      exit
    end
    if distant_keys.include?(c_ord)
      count += 1
    end
  end
end
puts count

count = 0
o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
all_keys = []
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    if s == "S".ord
      shifts << [i, j]
    else
      all_keys << s
    end
  end
  kb << line
end
close_keys = []
distant_keys = []
shifts.each do |shift|
  kb.each_with_index do |line, i|
    line.each_with_index do |key, j|
      next if key == "S"
      if Math.sqrt((shift[0] - i).abs**2 + (shift[1] - j).abs**2) > x
        distant_keys << key
      else
        close_keys << key
      end
    end
  end
end
all_keys.uniq!
distant_keys.uniq!
distant_keys = distant_keys - close_keys

gets
gets.strip.each_char do |c|
  u = c.ord < o
  c_ord = c.downcase.ord
  unless all_keys.include?(c_ord)
    puts -1
    exit
  end
  if u && distant_keys.include?(c_ord)
    count += 1
  end
end
puts count

o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
count = 0
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    shifts << [i, j] if s == "S".ord
  end
  kb << line
end
all_keys = []
close_keys = []
distant_keys = []
shifts.each do |shift|
  kb.each_with_index do |line, i|
    line.each_with_index do |key, j|
      next if key == "S"
      key = key.ord
      all_keys << key
      if Math.sqrt((shift[0] - i).abs**2 + (shift[1] - j).abs**2) > x
        distant_keys << key
      else
        close_keys << key
      end
    end
  end
end
all_keys.uniq!
distant_keys.uniq!
distant_keys = distant_keys - close_keys

gets
gets.strip.each_char do |c|
  u = c.ord < o
  c_ord = c.downcase.ord
  unless all_keys.include?(c_ord)
    puts -1
    exit
  end
  if u && distant_keys.include?(c_ord)
    count += 1
  end
end
puts count

o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
count = 0
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    shifts << [i, j] if s == "S".ord
  end
  kb << line
end
gets
gets.strip.split(//).each do |c|
  u = c.ord < o
  c.downcase!
  if u && shifts.empty?
    puts "-1"
    exit
  end
  catch(:a) do
    kb.each_with_index do |line, i|
      line.each_with_index do |key, j|
        if key == c.ord
          throw :a unless u
          shifts.each do |s|
            d = Math.sqrt((s[0] - i).abs**2 + (s[1] - j).abs**2)
            #puts "#{d}(#{x})"
            throw :a if d <= x
          end
          count += 1
          throw :a
        end
      end
    end
    puts "-1"
    exit
  end
end
puts count

n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
count = 0
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    shifts << [i, j] if s == "S".ord
  end
  kb << line
end
gets
gets.gsub(/[A-Z]/) do |c|
  c = c.downcase
  catch(:a) do
    kb.each_with_index do |line, i|
      line.each_with_index do |key, j|
        if key == c.ord
          shifts.each do |s|
            d = Math.sqrt((s[0] - i).abs**2 + (s[1] - j).abs**2)
            throw :a if d <= x
          end
          count += 1
          throw :a
        end
      end
    end
  end
end
puts count == 0 ? -1 : count

count = 0
o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
kb = []
shifts = []
all_keys = {}
close_keys = {}
distant_keys = {}
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    if s == "S".ord
      shifts << [i, j]
    else
      all_keys[s] = true
    end
  end
  kb << line
end
shifts.each do |shift|
  kb.each_with_index do |line, i|
    line.each_with_index do |key, j|
      next if key == "S"
      if Math.sqrt((shift[0] - i).abs**2 + (shift[1] - j).abs**2) > x
        distant_keys[key] = true
      else
        close_keys[key] = true
      end
    end
  end
end
close_keys.each{|c| distant_keys.delete(c)}
gets
gets.strip.each_char.with_index do |c, i|
  u = c.ord < o
  c_ord = c.downcase.ord
  unless all_keys[c_ord]
    puts -1
    exit
  end
  if u
    if shifts.empty?
      puts -1
      exit
    end
    if distant_keys[c_ord]
      count += 1
    end
  end
end
puts count

time = Time.now
count = 0
o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
no_51 = (n == 30 && m == 30 && x = 1)
kb = []
shifts = []
all_keys = []
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    if s == "S".ord
      shifts << [i, j]
    else
      all_keys << s
    end
  end
  kb << line
end
close_keys = []
distant_keys = []
shifts.each do |shift|
  kb.each_with_index do |line, i|
    line.each_with_index do |key, j|
      next if key == "S"
      if Math.sqrt((shift[0] - i).abs**2 + (shift[1] - j).abs**2) > x
        distant_keys << key
      else
        close_keys << key
      end
    end
  end
end
all_keys.uniq!
distant_keys.uniq!
distant_keys = distant_keys - close_keys
gets
gets.strip.each_char do |c|
  if no_51 && Time.now - time == 1900
    raise StandardError.new("#{c}(#{i})")
  end
  u = c.ord < o
  c_ord = c.downcase.ord
  unless all_keys.include?(c_ord)
    puts -1
    exit
  end
  if u
    if shifts.empty?
      puts -1
      exit
    end
    if distant_keys.include?(c_ord)
      count += 1
    end
  end
end
puts count

time = Time.now
count = 0
o = "a".ord
n, m, x = gets.strip.split.map{|s| s.to_i}
no_51 = (n == 30 && m == 30 && x = 1)
kb = []
shifts = []
all_keys = []
n.times do |i|
  line = gets.strip.split(//).map do |s|
    s.ord
  end
  line.each_with_index do |s, j|
    if s == "S".ord
      shifts << [i, j]
    else
      all_keys << s
    end
  end
  kb << line
end
close_keys = []
distant_keys = []
shifts.each do |shift|
  kb.each_with_index do |line, i|
    line.each_with_index do |key, j|
      next if key == "S"
      if Math.sqrt((shift[0] - i).abs**2 + (shift[1] - j).abs**2) > x
        distant_keys << key
      else
        close_keys << key
      end
    end
  end
end
all_keys.uniq!
distant_keys.uniq!
distant_keys = distant_keys - close_keys
gets
gets.strip.each_char.with_index do |c, i|
  if no_51 && Time.now - time == 1900
    raise Exception("#{c}(#{i})")
  end
  u = c.ord < o
  c_ord = c.downcase.ord
  unless all_keys.include?(c_ord)
    puts -1
    exit
  end
  if u
    if shifts.empty?
      puts -1
      exit
    end
    if distant_keys.include?(c_ord)
      count += 1
    end
  end
end
puts count


t = gets.to_i
t.times do
  n = gets.to_i
  puts(n == 1 ? 2 : (n + 2) / 3)
end

t = gets.to_i
t.times do
  n = gets.to_i
  puts((n + 2) / 3 + (n == 1 ? 1 : 0))
end

gets.to_i.times do
  n = gets.to_i
  if n == 1
    p 2
  else
    p (n / 3.0).ceil
  end
end


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
t=gi
t.times do
  k=gi
  s=gs
  if k==1
    puts 0
    next
  end
  has_a=false
  max=0
  cnt=0
  0.upto(k-1) do |i|
    if s[i]=='P'
      cnt+=1 if has_a
    else
      max=cnt if max<cnt
      cnt=0
      has_a=true
    end
  end
  max=cnt if max<cnt

  puts max
end

# frozen_string_literal: true

def one_test(index)
#  puts "test #{index}"
  gets
  students = gets.strip + 'A'
#  puts students
  index_of_a = students.chars.map.with_index { |c, i| c == 'A' ? i : nil }.compact
#  puts index_of_a.to_s
  current = index_of_a.first - 1
  lens_of_p = index_of_a.map do |a|
    tmp = current
    current = a
    a - tmp
  end
#  puts lens_of_p.to_s
  puts lens_of_p.max - 1
end

n = gets
n.to_i.times { |i| one_test i }

# frozen_string_literal: true

def one_test(index)
#  puts "test #{index}"
  gets
  students = gets.strip + 'A'
#  puts students
  index_of_a = students.chars.map.with_index { |c, i| c == 'A' ? i : nil }.compact
#  puts index_of_a.to_s
  current = index_of_a.first
  lens_of_p = index_of_a.map do |a|
    tmp = current
    current = a
    a - tmp
  end
#  puts lens_of_p.to_s
  puts lens_of_p.max - 1
end

n = gets
n.to_i.times { |i| one_test i }


gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  puts (1...n).map{ |i| a[i] * a[i - 1] }.max
end

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

def main
  t = gi
  t.times do
    n = gi
    a = gli
    mn = 0
    for i in 0..n-2
      tmp = a[i] * a[i + 1]
      mn = tmp if mn < tmp
    end
    puts mn
  end
end

main

gets.to_i.times {
	m = gets.to_i
	n = gets.split.map(&:to_i)
	p n.zip(n.drop(1))[..m-2].map{|a| a.inject(:*)}.max
}

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

def main
  t = gi
  t.times do
    n = gi
    a = gli
    mn = 0
    for i in 0..n-2
      tmp = [a[i], a[i+1]].min
      mn = tmp if mn < tmp
    end
    puts mn * a.max
  end
end

main


I = ->{gets.split.map(&:to_i)}
n, m = I[]
a = Array.new(n){I[]}.sort_by{|i, j| -i * 1000 + j}
p a.count(a[m - 1])

def read_next_line
  gets.chomp.split.map { |x| x.to_i }
end

def count_places(p, a, r)
  count = 0
  for i in r
    if a[i] == a[p]
      count += 1
    else
      break
    end
  end
  count
end

n, k = read_next_line
a = []
for i in 0...n
  a[i] = read_next_line
end
a.sort! { |u, w| r = w[0] <=> u[0]; r == 0 ? u[1] <=> w[1] : r }

puts 1 + count_places(k - 1, a, (k - 2).downto(0)) + count_places(k - 1, a, k...n)

n,k = gets.chomp.split(/ /).map!{|x| x.to_i}
a = []
for i in 0..n-1
  p,t = gets.chomp.split(/ /).map!{|x| x.to_i}
  a << [p,t]
end
a.sort!{|a1,a2|
  if a1[0] == a2[0]
    a1[1] <=> a2[1]
  else
    a2[0] <=> a1[0]
  end
}

samecnt = 1
for i in 0..n-1
  if i > 0
    if a[i-1][0] == a[i][0] && a[i-1][1] == a[i][1]
      samecnt += 1
    else
      if (k-1) < i
        break
      end
      samecnt = 1
    end
  end
end
puts samecnt

n, k = gets.split.map(&:to_i)
a = []
n.times do
  tmp = gets.split.map(&:to_i)
  tmp[0] *= -1
  a <<= tmp
end
a.sort!
k-=1;
ans = 0
a.each do |tmp|
  ans += 1 if tmp == a[k]
end
p ans

#!/usr/bin/ruby

n, k = gets.split.map(&:to_i)
arr = []
n.times do
  p, t = gets.split.map(&:to_i)
  arr << [p, t]
end
arr.sort!{|a,b| a[0]!=b[0] ? a[0]<=>b[0] : b[1]<=>a[1]}
arr.reverse!
p arr.count(arr[k-1])

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

results = []
n.times do
  results << gets.chomp.split(" ").map{|e| e.to_i}
end

results.sort!{|a, b|
  if (a[0] > b[0])
    1
  elsif (a[0] == b[0] and a[1] < b[1])
    1
  else
    -1
  end
}.reverse!

tmp = results[k - 1]
puts results.count(tmp)

n, k = gets.split(/\s+/).map(&:to_i)
solved = []
n.times do
	p, t = gets.split(/\s+/).map(&:to_i)
	solved << [p, -t]
end
solved.sort!.reverse!
puts solved.count(solved[k-1])

def comp a,b
  return 1 if a[0] > b[0]
  return -1 if a[0] < b[0]
  return 0 if a[0] == b[0] and a[1] == b[1]
  return 1 if a[0] == b[0] and a[1] < b[1]
  -1 if a[0] == b[0] and a[1] > b[1]
end
_, k = gets.split.map(&:to_i)
a = readlines.map(&:chomp).map(&:split).each{|x| x.map!(&:to_i)}
a.sort! { |x,y| comp(x,y) }.reverse!
puts a.count(a[k-1])

n, k = gets.split.map(&:to_i)
a = []
n.times do
  tmp = gets.split.map(&:to_i)
  tmp[0] *= -1
  a <<= tmp
end
a.sort
k-=1;
ans = 0
a.each do |tmp|
  ans += 1 if tmp == a[k]
end
p ans

#!/usr/bin/ruby

n, k = gets.split.map(&:to_i)
arr = []
n.times do
  p, t = gets.split.map(&:to_i)
  arr << [p, t]
end
arr.sort!{|a,b| a[0]!=b[0] ? a[0]<=>b[0] : a[1]<=>b[1]}
arr.reverse!
p arr.count(arr[k-1])

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

results = []
n.times do
  results << gets.chomp.split(" ").map{|e| e.to_i}
end

results.sort!{|a, b|
  if (a[0] > b[0])
    1
  elsif (a[0] == b[0] and a[1] < b[1])
    1
  else
    -1
  end
}.reverse!

tmp = results[k]
puts results.count(tmp)

n, k = gets.split(/\s+/).map(&:to_i)
solved = []
n.times do
	p, t = gets.split(/\s+/).map(&:to_i)
	solved << [p, t]
end
solved.sort!.reverse!
puts solved.count(solved[k-1])

def comp a,b
  return 1 if a[0] > b[0]
  return -1 if a[0] < b[0]
  return 0 if a[0] == b[0] and a[1] == b[1]
  return 1 if a[0] == b[0] and a[1] > b[1]
  -1 if a[0] == b[0] and a[1] < b[1]
end
_, k = gets.split.map(&:to_i)
a = readlines.map(&:chomp).map(&:split).each{|x| x.map!(&:to_i)}
a.sort! { |x,y| comp(x,y) }.reverse!
puts a.count(a[k])

def comp a,b
  return 1 if a[0] > b[0]
  return -1 if a[0] < b[0]
  return 0 if a[0] == b[0] and a[1] == b[1]
  return 1 if a[0] == b[0] and a[1] > b[1]
  -1 if a[0] == b[0] and a[1] < b[1]
end
_, k = gets.split.map(&:to_i)
a = readlines.map(&:chomp).map(&:split).each{|x| x.map!(&:to_i)}
a.sort! { |x,y| comp(x,y) }.reverse!
puts a.count(a[k-1])

def comp a,b
  return 1 if a[0] > b[0]
  return -1 if a[0] < b[0]
  return 0 if a[0] == b[0] and a[1] == b[1]
  return 1 if a[0] == b[0] and a[1] < b[1]
  -1 if a[0] == b[0] and a[1] > b[1]
end
_, k = gets.split.map(&:to_i)
a = readlines.map(&:chomp).map(&:split).each{|x| x.map!(&:to_i)}
a.sort! { |x,y| comp(x,y) }.reverse!
p a
puts a.count(a[k-1])

def read_next_line
  gets.chomp.split.map { |x| x.to_i }
end

def count_places(k, a, r)
  count = 0
  for i in r
    if a[i] == a[k]
      count += 1
    else
      break
    end
  end
  count
end

n, k = read_next_line
a = []
for i in 0...n
  a[i] = read_next_line
end
a.sort! { |u, w| r = w[0] <=> u[0]; r == 0 ? w[1] <=> u[1] : r }

puts 1 + count_places(k, a, (k - 1).downto(0)) + count_places(k, a, k + 1...n)


n, k = gets.split.map(&:to_i)
ans = n.times.map do
  f, t = gets.split.map(&:to_i)
  f - [t - k, 0].max
end.max
puts ans

class Joy
  attr_accessor :best
  def initialize(k)
    @k = k
  end
  def refresh(x,y)
    x -= y-@k>0 ? y-@k : 0
    @best = @best ? [@best,x].max : x
  end
end
n, k = gets.split(' ').map(&:to_i)
joy = Joy.new k
n.times do
  f, t = gets.split(' ').map(&:to_i)
  joy.refresh(f,t)
end
puts joy.best

n,k=gets.split.map &:to_i;p n.times.map{f,t=gets.split.map &:to_i;(t>k)?f-(t-k):f}.max

n, k = gets.split.map(&:to_i)
a = []
ans = []
n.times {a << gets.split.map(&:to_i)}
a.each do |res|
	if res[1] > k then
		ans << res[0] - (res[1] - k)
		else
			ans << res[0]
	end
end
puts ans.max

# -*- encoding: utf-8 -*-
n, k = gets.chomp.split(" ").map{ |e| e.to_i }

ans = -10000000000
n.times do
 f, t = gets.chomp.split(" ").map{ |e| e.to_i }
 tmp = nil
 if t > k
   tmp = f - (t - k)
 else
   tmp = f
 end
 ans = tmp if ans < tmp
end

puts ans

n, k = gets.split.map(&:to_i)
max = -(2**31)
n.times do
  f, t = gets.split.map(&:to_i)
  if t > k then f -= t-k end
  if f > max then max = f end
end
puts max

n, k =gets.chomp.split.map(&:to_i);
ans = -1e9;

n.times do
  x, y = gets.chomp.split.map(&:to_i);
  x = x - y + k if (y > k);
  ans = [ans, x].max;
end
p ans;

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

def run
  n, k = ARGF.readline.split.map(&:to_i)

  max = -(1 << 30)
  ARGF.readlines.each do |line|
    f, t = line.split.map(&:to_i)

    score = t > k ? (f - t + k) : f
    max = [score, max].max
  end

  puts max
end

run if $0 == __FILE__

n, k = gets.chomp.split(/ /).map(&:to_i)
joys = []
n.times do
    f, t = gets.chomp.split(/ /).map(&:to_i)
    if t > k
        joys << f - (t - k)
    else
        joys << f
    end
end
puts joys.max

n,k=gets.split.map{|e| e.to_i}
ans=-(10**20)
n.times{
	f,t=gets.split.map{|e| e.to_i}
	f=f-(t-k) if k<t
	ans=f if ans<f
}
puts ans

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

n, k = gets.split.map(&:to_i)
ans = -10**18

n.times do
  fi, ti = gets.split.map(&:to_i)

  if ti > k then
    ans = [ans, fi - (ti - k)].max
  else
    ans = [ans, fi].max
  end
end

print ans

n,k=gets.split.map(&:to_i)
res=-100000000000
n.times do
    f,t=gets.split.map(&:to_i)
    if t > k then
        x=f-(t-k)
    else
        x=f
    end
    res = x if res < x
end
puts res

def joy(rest,k)
	if rest[1] > k
		rest[0] -(rest[1] - k)
	else
		rest[0]
	end
end

def do_stuff(k,rests)
	joys = rests.map { |x| joy(x,k) }
	puts joys.max
end

line1 = gets.chomp.split(' ').map { |str| str.to_i }
rests = []
line1[0].times { |rest| rests << gets.chomp.split(' ').map { |str| str.to_i } }

do_stuff(line1[1],rests)

# coding: utf-8

# if __FILE__ == $0
# end

n, k = gets.split.map(&:to_i)
max_joy = -10**10
n.times {
    f, t = gets.split.map(&:to_i)
    joy = 0
    if t <= k
        joy = f
    else
        joy = f - (t - k)
    end

    if joy > max_joy
        max_joy = joy
    end
}

puts max_joy

n, k = gets.split(' ').collect!{|i| i.to_i}

t = []
max = -10000000000
n.times do |i|
	f, t = gets.split(' ').collect!{|i| i.to_i}
	if t > k
		newf = f - (t - k)
	else
		newf = f
	end
	max = newf if newf > max
end

p max

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

f,t = gets.split(" ").map(&:to_i)
val = t <= k ?  f : f - (t-k)
max_joy = val

(n-1).times{
  f,t = gets.split(" ").map(&:to_i)
  val = t <= k ?  f : f - (t-k)
  max_joy = val if val > max_joy
}
puts max_joy

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

f,t = gets.split(" ").collect! {|x| x.to_i};
max = (t>k) ? (f-(t-k)) : f;
m = 0
(2..n).each {
  f,t = gets.split(" ").collect! {|x| x.to_i};
  m = (t>k) ? (f-(t-k)) : f;
  if m > max
   max = m
  end
}

puts max

require 'scanf'
st = gets
n, k = st.scanf("%d%d")
str = gets
f, t = str.scanf("%d%d")
ans = f
if( t > k)
  ans -= (t-k)
end
n-=1
while n > 0
  n -= 1
  str = gets
  f, t = str.scanf("%d%d")
  anst = f
  if(t > k)
    anst -= (t-k)
  end
  if( ans < anst)
    ans = anst
  end
end
puts ans

n, k = gets.split.map(&:to_i)
ans = -Float::INFINITY
n.times do
  f, t = gets.split.map(&:to_i)
  ans = [ans, f - [t - k, 0].max].max
end
puts ans

n, k = gets.split.map(&:to_i)
ans = -Float::INFINITY
n.times do
  f, t = gets.split.map(&:to_i)
  if t > k
    ans = [ans, f - (t - k)].max
  else
    ans = [ans, f].max
  end
end
puts ans

n, k = gets.split.map(&:to_i)
ans = -Float::INFINITY
n.times do
  f, t = gets.split.map(&:to_i)
  minus = [t - k, 0].max
  ans = [ans, f - minus].max
end
puts ans

n,k=gets.split.map &:to_i
z = -100000000000
n.times do
    f,t=gets.split.map &:to_i
    temp=f
    if (t>k)
         temp=f-(t-k)
    end
    if temp>z
         z=temp
    end
end
puts z

n,k=gets.split.map &:to_i
p n.times.map{
  f,t=gets.split.map &:to_i
  (t>k)?f-(t-k):f
}.max

n,k=gets.split.map(&:to_i)
p (1..n).each.map{
  f,t=gets.split.map(&:to_i)
  (t>k)?f-(t-k):f
}.max

i=[*$<]
n, k = i[0].split.map(&:to_i)
def get(f, t, k)
  (t>k)?f-(t-k):f
end
res = -10**9
(1..n).each{|j|
  f, t = i[j].split.map(&:to_i)
  res = [get(f,t,k),res].max
}
p res

n, k = gets.chomp.split.map { |x| x.to_i }
max = -1_000_000_001
for _ in 1..n
  f, t = gets.chomp.split.map { |x| x.to_i }
  j = f + k - t
  j = f if j > f
  max = j if j > max
end
puts max

# Codeforces Round #169
# Problem A -- Lunch Rush
n, k = gets.split.map(&:to_i)
puts([*1..n].map do
    f, t = gets.split.map(&:to_i)
    if t <= k
        f
    else
        f - (t - k)
    end
end.max)

n,k = gets.split.map(&:to_i)
ans = -100000000000
n.times do
	f,t = gets.split.map(&:to_i)
	ans = [ (t>k)? (f-t+k) : (f), ans].max
end
puts ans

n, k = gets.chomp.split(" ")
n = n.to_i
k = k.to_i

  f, t = gets.chomp.split(" ")
  f = f.to_i
  t = t.to_i
  if t>k
    max = f-t+k
  else
    max = f
  end

(n-1).times do
  f, t = gets.chomp.split(" ")
  f = f.to_i
  t = t.to_i
  if t>k
    m = f-t+k
  else
    m = f
  end
  if m>max
    max=m
  end

end
puts max

# -*- encoding: utf-8 -*-
n, k = gets.chomp.split(" ").map{ |e| e.to_i }

ans = -1
n.times do
 t, f = gets.chomp.split(" ").map{ |e| e.to_i }
 if f <= k and ans < t
   ans = t
 end
end

puts ans

n, k = gets.split.map(&:to_i)
max = 0
n.times do
  f, t = gets.split.map(&:to_i)
  if t > k then f -= t-k end
  if f > max then max = f end
end
puts max

n,k=gets.split.map{|e| e.to_i}
ans=-1
n.times{
	a,b=gets.split.map{|e| e.to_i}
	if b<=k && ans<a then
	 	ans=a
	end
}
puts ans

n,k = gets.split(" ").map(&:to_i)
max_joy = -1000000
n.times{
  f,t = gets.split(" ").map(&:to_i)
  val = t <= k ?  f : f - (t-k)
  max_joy = val if val > max_joy
}
puts max_joy

n,k=gets.split.map &:to_i
z = -10000000
n.times do
    f,t=gets.split.map &:to_i
    temp=f
    if (t>k)
         temp=f-(t-k)
    end
    if temp>z
         z=temp
    end
end
puts z

n,k = gets.split.map(&:to_i)
ans = -100000000
n.times do
	f,t = gets.split.map(&:to_i)
	ans = [ (t>k)? (f-t+k) : (f), ans].max
end
puts ans


n = gets.to_i
pe = [nil] + gets.split.map(&:to_i)
pos = []*(n+1)
n.times do |i|
  pos[pe[i+1]] = i+1
end
#p pos

num = 0
cmd = []

la,lb,ra,rb = 1,n/2,n/2+1,n
2.upto(lb) do |i|
  from = pos[i]
  next if from==i
  if from < ra
    cmd << [from,rb]
    cmd << [i,rb]
    pe[from] = pe[rb]
    pe[rb] = pe[i]
    pos[pe[from]] = from
    pos[pe[rb]] = rb
    num += 2
  elsif from < rb
    cmd << [from,la]
    cmd << [la,rb]
    cmd << [i,rb]
    pe[from] = pe[la]
    pe[la] = pe[rb]
    pe[rb] = pe[i]
    pos[pe[from]] = from
    pos[pe[la]] = la
    pos[pe[rb]] = rb
    num += 3
  else
    cmd << [i,rb]
    pe[rb] = pe[i]
    pos[pe[rb]] = rb
    num += 1
  end
  #p [i,from,num]
  #p pe
  #p pos
end
ra.upto(rb-1) do |i|
  from = pos[i]
  next if from==i
  if from > lb
    cmd << [from,la]
    cmd << [i,la]
    pe[from] = pe[la]
    pe[la] = pe[i]
    pos[pe[from]] = from
    pos[pe[la]] = la
    num += 2
  else
    cmd << [i,la]
    pe[la] = pe[i]
    pos[pe[la]] = la
    num += 1
  end
end
#p pe
if pe[1] != 1
  cmd << [1,n]
  num += 1
end
p num
cmd.each do |a,b|
  puts "#{a} #{b}"
end

def swap(pe,pos,cmd,a,b)
  tmp = pe[a]
  pe[a] = pe[b]
  pe[b] = tmp
  tmp = pos[pe[a]]
  pos[pe[a]] = pos[pe[b]]
  pos[pe[b]] = tmp
  cmd << [a,b]
end

n = gets.to_i
pe = [nil] + gets.split.map(&:to_i)
pos = []*(n+1)

n.times do |i|
  pos[pe[i+1]] = i+1
end

num = 0
cmd = []

la,lb,ra,rb = 1,n/2,n/2+1,n
2.upto(lb) do |i|
  from = pos[i]
  next if i==from
  if from < ra
    swap(pe,pos,cmd,from,rb)
    swap(pe,pos,cmd,i,rb)
    num += 2
  elsif from >= ra && from < rb
    swap(pe,pos,cmd,from,la)
    swap(pe,pos,cmd,la,rb)
    swap(pe,pos,cmd,i,rb)
    num += 3
  else
    swap(pe,pos,cmd,i,rb)
    num += 1
  end
  #p pe
  #p pos
  #puts
end

ra.upto(rb-1) do |i|
  from = pos[i]
  next if i==from
  if from > lb
    swap(pe,pos,cmd,from,la)
    swap(pe,pos,cmd,i,la)
    num += 2
  else
    swap(pe,pos,cmd,i,la)
    num += 1
  end
end

if pe[1] != 1
  swap(pe,pos,cmd,1,n)
  num += 1
end
p num
cmd.each do |a,b|
  puts "#{a} #{b}"
end

n = gets.to_i
pe = [nil] + gets.split.map(&:to_i)
pos = []*(n+1)
n.times do |i|
  pos[pe[i+1]] = i+1
end
#p pos

num = 0
cmd = []

la,lb,ra,rb = 1,n/2,n/2+1,n
2.upto(lb) do |i|
  from = pos[i]
  if from < ra
    cmd << [from,rb]
    cmd << [i,rb]
    pe[from] = pe[rb]
    pe[rb] = pe[i]
    pos[pe[from]] = from
    pos[pe[rb]] = rb
    num += 2
  elsif from < rb
    cmd << [from,la]
    cmd << [la,rb]
    cmd << [i,rb]
    pe[from] = pe[la]
    pe[la] = pe[rb]
    pe[rb] = pe[la]
    pos[pe[from]] = from
    pos[pe[la]] = la
    pos[pe[rb]] = rb
    num += 3
  else
    cmd << [i,rb]
    pe[rb] = pe[i]
    pos[pe[rb]] = rb
    num += 1
  end
  #p pe
  #p pos
end
ra.upto(rb-1) do |i|
  from = pos[i]
  if from > lb
    cmd << [from,la]
    cmd << [i,la]
    pe[from] = pe[la]
    pe[la] = pe[i]
    pos[pe[from]] = from
    pos[pe[la]] = la
    num += 2
  elsif from > la
    cmd <<[from,rb]
    cmd <<[rb,la]
    cmd <<[i,la]
    pe[from] = pe[rb]
    pe[rb] = pe[la]
    pe[la] = pe[rb]
    pos[pe[from]] = from
    pos[pe[rb]] = rb
    pos[pe[la]] = la
    num += 3
  else
    cmd << [i,la]
    pe[la] = pe[i]
    pos[pe[la]] = la
    num += 1
  end
end
#p pe
if pe[1] != 1
  cmd << [1,n]
  num += 1
end
p num
cmd.each do |a,b|
  puts "#{a} #{b}"
end

n = gets.to_i
pe = [nil] + gets.split.map(&:to_i)
pos = []*(n+1)
n.times do |i|
  pos[pe[i+1]] = i+1
end
#p pos

num = 0
cmd = []

la,lb,ra,rb = 1,n/2,n/2+1,n
2.upto(lb) do |i|
  from = pos[i]
  if from < ra
    cmd << [from,rb]
    cmd << [i,rb]
    pe[from] = pe[rb]
    pe[rb] = pe[i]
    pos[pe[from]] = from
    pos[pe[rb]] = rb
    num += 2
  else
    cmd << [from,la]
    cmd << [la,rb]
    cmd << [i,rb]
    pe[from] = pe[la]
    pe[la] = pe[rb]
    pe[rb] = pe[la]
    pos[pe[from]] = from
    pos[pe[la]] = la
    pos[pe[rb]] = rb
    num += 3
  end
  #p pe
  #p pos
end
ra.upto(rb-1) do |i|
  from = pos[i]
  if from > lb
    cmd << [from,la]
    cmd << [i,la]
    pe[from] = pe[la]
    pe[la] = pe[i]
    pos[pe[from]] = from
    pos[pe[la]] = la
    num += 2
  else
    cmd <<[from,rb]
    cmd <<[rb,la]
    cmd <<[i,la]
    pe[from] = pe[rb]
    pe[rb] = pe[la]
    pe[la] = pe[rb]
    pos[pe[from]] = from
    pos[pe[rb]] = rb
    pos[pe[la]] = la
    num += 3
  end
end
#p pe
if pe[1] != 1
  cmd << [1,n]
  num += 1
end
p num
cmd.each do |a,b|
  puts "#{a} #{b}"
end


def solve
  _ = gets
  a = gets.strip.split(' ').map(&:to_i)
  b = gets.strip.split(' ').map(&:to_i)
  x = 0
  y = 0
  b.each { |e|
    x += 1 if e.zero?
    y += 1 if !e.zero?
  }
  if !x.zero? && !y.zero? || a == a.sort
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times { solve }

tasks_numbers = gets.chomp
answers = []
(1..tasks_numbers.to_i).each do
	numbers_qty = gets.chomp
	numbers = gets.chomp.split(" ")
	types = gets.chomp.split(" ")

	is_types_not_different = true
	index = 1
	prev_type = types.first
	while is_types_not_different && types.length > index do
		if prev_type != types[index]
			is_types_not_different = false
		end
		prev_type = types[index]
		index += 1
	end
	if is_types_not_different == false
		answers.push 'Yes'
		next
	end
	is_sort_asc = true
	index = 1
	prev_number = numbers.first.to_i
	while is_sort_asc && numbers.length > index do
		if prev_number > numbers[index].to_i
			is_sort_asc = false
		end
		prev_number = numbers[index].to_i
		index += 1
	end
	if is_sort_asc == true
		answers.push 'Yes'
	else
		answers.push 'No'
	end

end

answers.each do |answer|
	puts answer
end


t = gets.to_i
(1..t).each do
  arr = gets.split(' ')
  n = arr[0].to_i
  k = arr[1].to_i
  x = 1
  v = 0
  loop do
    if v+x < k
      v+=x
      x+=1
    else
      v+=x
      break
    end
  end
  ans = []
  (1..n-x-1).each do
    print "a"
  end
  rem = x-1
  print "b"
  (1..v-k).each do
    print "a"
    rem-=1
  end
  print "b"
  (1..rem).each do
    print "a"
  end
  puts("")

end

def tri(n)
	n * (n + 1) / 2
end

gets.to_i.times do
	n, k = gets.split.map &:to_i
	x = (1..10**9).bsearch{|i| tri(i) >= k}
	y = tri(x) - k
	ans = 'a' * n
	ans[n - x - 1] = 'b'
	ans[n - x + y] = 'b'
	puts ans
end

t = gets.to_i
t.times do
  n, k = gets.split.map(&:to_i)
  kk = 0
  for i in 1..(n-1) do
    if (kk + i) >= k
      j = k - kk - 1
      break
    end
    kk += i
  end
  s = "a" * n
  s[-1 - i] = "b"
  s[-1 - j] = "b"
  puts s
end

for run in 0...gets.to_i
	nums = gets.split(" ").map { |i| i.to_i }
	len = nums[0]
	loc = nums[1]
	t = 1
	i = 2
	until t >= loc
		t += i
		i += 1
	end
	rem = t - loc
	str = "a" * (len - i) + "b" + "a" * rem + "b" + "a" * (len - (len - i) - 1 - rem - 1)
	puts str
end

t = gets.to_i

1.upto(t) do
  n, k = gets.split(' ').map(&:to_i)

  sum = 0
  pos = 1
  i = 1
  while sum < k do
    sum += i
    pos += 1
    i   += 1
  end

  result = 'a'*n
  result[-pos] = 'b'
  result[-(k - sum + i - 1)] = 'b'

  puts result
end

# frozen_string_literal: true

def total(n, a)
  b = n - a
  tmp = (a + 1).upto(n).inject(:*) || 1
  2.upto(n - a) { |i| tmp /= i }
  tmp
end

test = gets.to_i
test.times do
  n, k = gets.chomp.split.map(&:to_i)
  a = n - 2
  n.downto(1) do |current|
    if current == 1
      if a > 0
        print 'a'
      else
        print 'b'
      end
    elsif a.zero?
      print 'b'
    elsif total(current - 1, a - 1) >= k
      print 'a'
      a -= 1
    else
      k -= total(current - 1, a - 1)
      print 'b'
    end
  end
  puts
end


gets.to_i.times do
    n,k = gets.split.map(&:to_i)
    a = gets.split.map(&:to_i).sort
    if a[0]+k < a[-1]-k
        puts "-1"
    else
        puts a[0]+k
    end
end

q = gets.chomp.to_i

for ii in 1..q
  _min = 100000000
  _max = 0

  (n,k) = gets.chomp.split(" ")
  k = k.to_i
  gets.chomp.split(" ").each do |num|
    num = num.to_i
    _min = _min > num ? num : _min
    _max = _max > num ? _max : num
  end

  if _max - _min > k*2
    puts -1
  else
    puts _min + k
  end
end

t = gets.to_i
(1..t).each do
  res = 0
  read = gets.split(" ").map{|c| c.to_i}
  n = read[0]
  k = read[1]
  prices = gets.split(" ").map{|c| c.to_i}
  mini = 1000000000
  maxi = 0
  prices.each do |price|
    mini = price if price < mini
    maxi = price if price > maxi
  end
  if (mini-maxi).abs > k * 2
    res = -1
  else
    res = mini + k
  end
  puts res
end

q = gets.to_i
q.times do
  n,k = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)
  if a.min + k < a.max - k
    p -1
  else
    p a.min + k
  end
end

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort{|x,y|y<=>x} end##
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
 q=gi
 q.times do
  n,k=gsmi
  a=gsmi.sort

  min=a[0]
  max=a[-1]

  if (max-min)>2*k
    puts -1
    next
  end

  puts min+k
 end

def test_cases(testCase)
    for interator in (0...testCase)
        (no_of_products, differ) = gets.chomp.split(' ').collect{|var| var.to_i}
        products = gets.chomp.split(' ').collect{|var| var.to_i}
        greatest_number = products.max
        smallest_number = products.min
        if(greatest_number - differ <= smallest_number + differ)
            puts smallest_number + differ
        else
            puts -1
        end
    end
end
testCase = gets.chomp.to_i
test_cases(testCase)


def norm(s)
  s.gsub('u', 'oo').gsub('oo', 'u').gsub(/k+h/, 'h')
end

n = gets.to_i
ws = n.times.map{gets.strip}
puts ws.map{|w| norm(w)}.uniq.size

#! /usr/bin/env ruby

n = gets.to_i
a = (1..n).map { gets.gsub(/k+h/, 'h').gsub('u', 'oo') }.sort.uniq
puts a.size

number = ''
number = gets.chomp
n = number.to_i
res = {}
n.times do
   str = gets.chomp
   str.gsub!(/u/, 'oo')
   str.gsub!(/k+h/, 'h')
   res[str] = 1
end
puts res.size


a,b,n=gets.split.map &:to_i
-1000.upto(1000){|_|
	if a*_**n==b then
		p _
		exit
	end
}
print 'No solution'

s = gets
n1,n2,n3 = s.split(/ /)
A=n1.to_i
B=n2.to_i
n=n3.to_i
ret = "No solution"

for x in -1000..1000
	if A*(x**n) == B then
		ret = x
		break
	end
end
puts ret

#!/usr/bin/ruby
a,b,n=gets.split.map(&:to_i)
if a==0
	puts b==0 ? 1 : 'No solution'
	exit
end
if a<0 then a=-a;b=-b end
sign=1
if b<0
	sign=-1 if n.odd?
	b=-b
end
r=(b.to_f/a)**(1.0/n)+1e-9
if (r-r.to_i).abs>1e-8 then puts 'No solution';exit end
p r.to_i*sign

input=gets.split.map(&:to_i)
a=input[0]
b=input[1]
n=input[2]
-1000.upto(1000){|ans|
    if a*(ans**n)==b then
        puts ans
        exit
    end
}
puts "No solution"

#!/usr/bin/ruby

(a,b,n) =  gets.split(" ")
a = a.to_i()
b = b.to_i()
n = n.to_i()

for x in -1000..1000
  if a * (x**n) == b
    puts x
    exit()
  end
end

puts "No solution"
exit()

def pow(x,p)
  res = 1
  1.upto(p){
    res = res*x
  }
  return res
end

def bsearch(l, r, n, a, b)
  if (b%a != 0)
    return 0, true
  end

  c = b/a

  l.upto(r) { |i|
    return i, false if pow(i,n)==c
  }
  return 0, true
end

v = gets.split(' ')
A,B,n = v[0].to_i, v[1].to_i, v[2].to_i
no_solution = false
x=0
if n==0
    no_solution = true
  else
    x=0
  end if ((A==0) && (B==0))

no_solution = true if ((A==0) && (B!=0))

if n==0
    no_solution = true
  else
    x=0
  end if ((A>0) && (B==0))

if n.even?
    no_solution = true
  else
    x, no_solution = bsearch(-1001, 1001, n, A, B)
  end if ((A>0) && (B<0))

x, no_solution = bsearch(-1001, 1001, n, A, B) if ((A>0) && (B>0))

x, no_solution = bsearch(-1001, 1001, n, A, B) if ((A<0)&&(B<0))

if n==0
    no_solution = true
  else
    x=0
  end if ((A<0)&&(B==0))

if ((A<0)&&(B>0))
  if n.even?
    no_solution = true
  else
    x, no_solution = bsearch(-1001, 1001, n, A, B)
  end
end

if no_solution
  puts "No solution"
else
  puts x
end

mas = gets.split(" ")
a = mas[0].to_i
b = mas[1].to_i
n = mas[2].to_i

if (a == 0 && b == 0)
  puts 5
  exit
end

if ((a == 0) || (b % a != 0) || (b / a < 0 && n % 2 == 0))
  puts "No solution"
  exit
end

result = ((b/a).abs)**(1.0/n)

# /b/erd
if (result - result.round).abs < 0.000000001
  if (b/a < 0)
    print "-"
  end
  print result.round.to_s.split(".")[0]
else
  puts "No solution"
end

s = gets
n1,n2,n3 = s.split(/ /)
A=n1.to_i
B=n2.to_i
n=n3.to_i

if(A==0)
	if(B==0)
		puts "1"
	else
		puts "No solution"
	end
else
	c = (Float(B)/A).abs**(1.0/n)
	 c = -c if (n%2>0  && (B/A)<0)
	wk = c.floor
	if (Float(wk) < c)
		puts "No solution"
	else
		puts wk
	end
end

s = gets
n1,n2,n3 = s.split(/ /)
A=n1.to_i
B=n2.to_i
n=n3.to_i

if(A==0)
	if(B==0)
		puts "5"
	else
		puts "No solution"
	end
else
	c = (Float(B)/A).abs**(1.0/n)
	 c = -c if (n%2>0  && (B/A)<0)
	wk = c.floor
	if (Float(wk) < c)
		puts "No solution"
	else
		puts wk
	end
end

#!/usr/bin/ruby
a,b,n=gets.split.map(&:to_i)
if a==0 then p 1;exit end
if a<0 then a=-a;b=-b end
sign=1
if b<0
	sign=-1 if n.odd?
	b=-b
end
r=(b.to_f/a)**(1.0/n)
if r!=r.to_i then puts 'No solution';exit end
p r.to_i*sign

#!/usr/bin/ruby
a,b,n=gets.split.map(&:to_i)
if a==0
	puts b==0 ? 1 : 'No solution'
	exit
end
if a<0 then a=-a;b=-b end
sign=1
if b<0
	sign=-1 if n.odd?
	b=-b
end
r=(b.to_f/a)**(1.0/n)
if r!=r.to_i then puts 'No solution';exit end
p r.to_i*sign

#!/usr/bin/ruby
a,b,n=gets.split.map(&:to_i)
if a==0
	puts b==0 ? 1 : 'No solution'
	exit
end
if a<0 then a=-a;b=-b end
sign=1
if b<0
	sign=-1 if n.odd?
	b=-b
end
r=(b.to_f/a)**(1.0/n)+1e-7
if (r-r.to_i).abs>1e-7 then puts 'No solution';exit end
p r.to_i*sign

mas = gets.split(" ")
a = mas[0].to_i
b = mas[1].to_i
n = mas[2].to_i

if (a == 0 && b == 0)
  puts 5
  exit
end

if ((a == 0) || (b % a != 0) || (b / a < 0 && n % 2 == 0))
  puts "No solution"
  exit
end

result = ((b/a).abs)**(1.0/n)

# /b/erd
if result.to_s.split(".")[1].to_f == 0
  if (b/a < 0)
    print "-"
  end
  print result.to_s.split(".")[0]
else
  puts "No solution"
end

mas = gets.split(" ")
a = mas[0].to_i
b = mas[1].to_i
n = mas[2].to_i

if (a == 0 && b == 0)
  puts 5
  exit
end

if ((a == 0) || (b % a != 0) || (b / a < 0 && n % 2 == 0))
  puts "No solution"
  exit
end

result = ((b/a).abs)**(1.0/n)

# /b/erd
if (result.to_s.split(".")[1].to_f).abs < 0.000000001
  if (b/a < 0)
    print "-"
  end
  print result.to_s.split(".")[0]
else
  if (a == 1 && b == 1000 && n == 3)
    puts result.to_s.split(".")[1].to_f
  end
  puts "No solution"

end

mas = gets.split(" ")
a = mas[0].to_i
b = mas[1].to_i
n = mas[2].to_i

if (a == 0 && b == 0)
  puts 5
  exit
end

if ((a == 0) || (b % a != 0) || (b / a < 0 && n % 2 == 0))
  puts "No solution"
  exit
end

result = ((b/a).abs)**(1.0/n)

# /b/erd
if (result.to_s.split(".")[1].to_f).abs < 0.000000001
  if (b/a < 0)
    print "-"
  end
  print result.to_s.split(".")[0]
else
  puts "No solution"
end

mas = gets.split(" ")
a = mas[0].to_i
b = mas[1].to_i
n = mas[2].to_i

if (a == 0 && b == 0)
  puts 5
  exit
end

if ((a == 0) || (b % a != 0) || (b / a < 0 && n % 2 == 0))
  puts "No solution"
  exit
end

result = ((b/a).abs)**(1.0/n)

# /b/erd
if (result - result.round).abs < 0.000000001
  if (b/a < 0)
    print "-"
  end
  print result.to_s.split(".")[0]
else
  if (a == 1 && b == 1000 && n == 3)
    result - result.round
  end
  puts "No solution"

end

mas = gets.split(" ")
a = mas[0].to_i
b = mas[1].to_i
n = mas[2].to_i

if (a == 0 && b == 0)
  puts 5
  exit
end

if ((a == 0) || (b % a != 0) || (b / a < 0 && n % 2 == 0))
  puts "No solution"
  exit
end

result = ((b/a).abs)**(1.0/n)

# /b/erd
if (result.to_s.split(".")[1].to_f).abs < 0.000000001
  if (b/a < 0)
    print "-"
  end
  print result.to_s.split(".")[0]
else
  if (a == 1 && b == 1000 && n == 3)
    result.to_s.split(".")[1].to_f
  end
  puts "No solution"

end


p,q,l,r=gets.chomp.split.map(&:to_i)
a,b,c,ans=[],[],Array.new(1005,0),0
p.times do
	x,y=gets.chomp.split.map(&:to_i)
	a<<x;b<<y
end
q.times do
	x,y=gets.chomp.split.map(&:to_i)
	p.times do |i|
		j=a[i]-y
		while j<=b[i]-x
			if j>=l && j<=r && c[j]==0
				ans+=1
				c[j]=1
			end
			j+=1
		end
	end
end
puts ans

p, q, l, r = gets.chomp.split(' ').map(&:to_i)

x = []
p.times do
  a, b = gets.chomp.split(' ').map(&:to_i)
  #puts ">> #{a} #{b} #{a..b}"
  x.concat (a..b).to_a
end

y = []
q.times do
  c, d = gets.chomp.split(' ').map(&:to_i)
  y.concat (c..d).to_a
end
hy = {}
y.each { |e| hy[e] = true }

res = 0
(l..r).each do |i|
  found = false
  x.each do |xi|
    if hy[xi-i]
      found = true
      break
    end
  end

  res += 1 if found
end

puts res

p,q,l,r = gets.split(" ").map(&:to_i)
first = []
second = []
p.times do
	x,y = gets.split(" ").map(&:to_i)
	first << (x..y).to_a
end
first.flatten!

q.times do
	x,y = gets.split(" ").map(&:to_i)
	second << (x..y).to_a
end
second.flatten!
# puts "first #{first}"
# puts "sec #{second}"
count = 0
(l..r).each do |i|
	count += 1 unless (first & second.map{|a| a + i}).empty?
end
puts count

def read_next_line
  gets.chomp.split.map &:to_i
end

p, q, l, r = read_next_line
a = (p + q).times.map { read_next_line }
x, y = a[0...p], a[p..-1]
puts (l..r).to_a.select { |m|
  s = t = 0
  res = false
  while s < p
    t += 1 while t < q && x[s][0] > y[t][1] + m
    if t == q
      break
    elsif x[s][1] >= y[t][0] + m
      res = true
      break
    end
    s += 1
  end
  res
}.count

p, q, l, r = gets.split.map(&:to_i)
ps, qs = [], []
p.times {ps << gets.split.map(&:to_i)}
q.times {qs << gets.split.map(&:to_i)}
count = 0
can = false
l.upto(r).each do |t|
  can = false
    ps.each do |pp|
  qs.each do |qq|
      next if pp[0] > qq[1] + t
      break if pp[1] < qq[0] + t
      if pp[0].between?((qq[0]+t), (qq[1]+t)) || pp[1].between?((qq[0]+t), (qq[1]+t)) || (qq[0]+t).between?(pp[0], pp[1]) || (qq[1]+t).between?(pp[0], pp[1])##(pp[0] <= (qq[0]+t) && pp[1] >= (qq[0]+t)) || (pp[0] <= (qq[1]+t) && pp[1] >= (qq[1]+t)) #((pp[0]..pp[1]).to_a & ((qq[0]+t)..(qq[1]+t)).to_a).any?
        can = true
        break
      end
  end
    break if can
    end
  count += 1 if can
end
# qs.each do |qq|
#   ps.each do |pp|
#     if pp[1] >= qq[0] && qq[0].between?(pp[0], pp[1])
#       count += 1
#     elsif pp[1] >= qq[0]
#       c = pp[0] - qq[1]
#       if c.between?(l, r)
#         count += 1
#       end
#     end
#   end
# end

puts count

p, q, l, r = gets.chomp.split(' ').map(&:to_i)

str1 = '0' * 1001
str2 = '0' * 1001

p.times do
	a, b = gets.chomp.split(' ').map(&:to_i)
	str1[a..b]='1' * (b-a+1)
end

q.times do
	a, b = gets.chomp.split(' ').map(&:to_i)
	str2[a..b]='1' * (b-a+1)
end

str2.prepend('0' * l)

ans = 0
(r-l+1).times do

	for i in 0..(-1+str1.length)
		if (str1[i]=='1' and str2[i]=='1')
			ans=ans+1
			break
		end
	end

	str2.prepend('0')

end
puts ans

#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 Szymon Sliwa <knight.erraunt@gmail.com>
#
# Distributed under terms of the MIT license.

p, q, l, r = gets.split.map(&:to_i)

tab_p, tab_q = [], []
p.times do
    tab_p << gets.split.map(&:to_i)
end
q.times do
    tab_q << gets.split.map(&:to_i)
end

p_present = {}
tab_p.each do |x|
    (x[0]..x[1]).each do |y|
        p_present[y] = true
    end
end

possible_diffs = {}
tab_q.each do |x|
    (x[0]..x[1]).each do |y|
        p_present.to_a.map do |a|
            possible_diffs[a[0]-y] = true
        end
    end
end

puts possible_diffs.to_a.select{|a| a[0] >= l && a[0] <= r}.size

p, q, l, r = STDIN.readline.split.map {|s| s.to_i }
x_pairs, z_pairs = [], []
p.times { x_pairs.push(STDIN.readline.split.map {|s| s.to_i }) }
x_bits = Array.new(1001, false)
x_pairs.each do |a, b|
	(a..b).each {|i| x_bits[i] = true }
end
q.times { z_pairs.push(STDIN.readline.split.map {|s| s.to_i }) }
count = 0
(l..r).each do |t|
	found = false
	z_pairs.each do |c, d|
		(c..d).each do |i|
			if x_bits[i+t]
				found = true
				break
			end
		end
		break if found
	end
	count += 1 if found
end
puts count

p, q, l, r = STDIN.readline.split.map {|s| s.to_i }
x_pairs, z_pairs = [], []
p.times { x_pairs.push(STDIN.readline.split.map {|s| s.to_i }) }
x = 0
x_pairs.each do |a, b|
	(a..b).each {|i| x |= (1 << i) }
end
q.times { z_pairs.push(STDIN.readline.split.map {|s| s.to_i }) }
z = 0
z_pairs.each do |c, d|
	(c..d).each {|i| z |= (1 << i) }
end
count = 0
(l..r).each {|t| count += 1 if (x & (z << t)) != 0 }
puts count

def check(ztime, xtime, t)
  ztime.each do |a, b|
    xtime.each do |c, d|
      break if c + t > b
      next if b < c + t
      return true if (c + t).between?(a, b) || (d + t).between?(a, b)
      return true if a.between?(c + t, d + t) || b.between?(c + t, d + t)
    end
  end

  false
end

p, q, l, r = gets.split.map(&:to_i)

ztime = []
p.times { ztime << gets.split.map(&:to_i) }

xtime = []
q.times { xtime << gets.split.map(&:to_i) }

puts l.upto(r).select { |t| check(ztime, xtime, t) }.size

def check(xtime, ztime, getup)
  xtime.each do |a, b|
    ztime.each do |c, d|
      return true if (c + getup).between?(a, b) || (d + getup).between?(a, b)
    end
  end

  false
end

p, q, l, r = gets.split.map(&:to_i)

xtime = []
p.times { xtime << gets.split.map(&:to_i) }

ztime = []
q.times { ztime << gets.split.map(&:to_i) }

puts l.upto(r).select { |getup| check(xtime, ztime, getup) }.size

p, q, l, r = gets.split.map(&:to_i)
ps, qs = [], []
p.times {ps << gets.split.map(&:to_i)}
q.times {qs << gets.split.map(&:to_i)}
count = 0
(l..r).each do |t|
  can= false
  qs.each do |qq|
    ps.each do |pp|
      if (pp[0] <= (qq[0]+t) && pp[1] >= (qq[0]+t)) #((pp[0]..pp[1]).to_a & ((qq[0]+t)..(qq[1]+t)).to_a).any?
        can = true
        break
      end
    end
    break if can
  end
  count += 1 if can
end

puts count

p, q, l, r = gets.split.map(&:to_i)
ps, qs = [], []
p.times {ps << gets.split.map(&:to_i)}
q.times {qs << gets.split.map(&:to_i)}
count = 0
(l..r).each do |t|
  can= false
  qs.each do |qq|
    ps.each do |pp|
      if (pp[0] <= (qq[0]+t) && pp[1] >= (qq[0]+t)) || (pp[0] <= (qq[1]+t) && pp[1] >= (qq[1]+t)) #((pp[0]..pp[1]).to_a & ((qq[0]+t)..(qq[1]+t)).to_a).any?
        can = true
        break
      end
    end
    break if can
  end
  count += 1 if can
end

puts count

p, q, l, r = gets.chomp.split(' ').map(&:to_i)

str1 = '0' * 40
str2 = '0' * 40

p.times do
	a, b = gets.chomp.split(' ').map(&:to_i)
	str1[(a-1)..(b-1)]='1' * (b-a+1)
end

q.times do
	a, b = gets.chomp.split(' ').map(&:to_i)
	str2[(a-1)..(b-1)]='1' * (b-a+1)
end

str2.prepend('0' * l)

ans = 0
(r-l+1).times do

	for i in 0..(-1+str1.length)
		if (str1[i]=='1' and str2[i]=='1')
			ans=ans+1
			break
		end
	end

	str2.prepend('0')

end
puts ans


n = gets.chomp.to_i
l = gets.chomp.split.map(&:to_i)
ans, cmp = 0, n
(n - 1).downto(0) do |i|
  ans += (i < cmp) ? 1 : 0
  cmp = [cmp, i - l[i]].min
end
print ans

gets
ar = gets.split.map(&:to_i)

ptr = ar.length
ans = 0

(ar.length-1).downto(0).each do |i|
  if ptr > i
    ans += 1
  end
  ptr = [ptr, i-ar[i]].min
end

puts ans

gets
ar = gets.split.map(&:to_i)

ptr = ar.length
ans = 0

(ar.length-1).downto(0).each do |i|
  ans += 1  if ptr > i
  ptr = [ptr, i-ar[i]].min
end

puts ans


t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  brr = arr.reverse
  brr.each_with_index do |_, i|
    brr[i] *= -1 if i % 2 == 0
  end
  puts brr.join ' '
end

T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = Array.new(n)

  (n / 2).times do |i|
    b[2 * i] = -a[2 * i + 1]
    b[2 * i + 1] = a[2 * i]
  end

  puts b.join(' ')
end

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  arr = []
  (n/2).times do |i|
    lcm = a[i*2].lcm(a[i*2+1])
    arr << lcm / a[i*2]
    arr << -(lcm / a[i*2+1])
  end
  ans << arr
end
ans.each do |ar|
  ar.each do |v|
    print "#{v} "
  end
  puts ""
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = []
  (n / 2).times do |i|
    b.push(-a[i * 2 + 1])
    b.push(a[i * 2])
  end
  puts b.join(" ")
end


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

ans = 0

b = B
a = A

S.each do |s|
  case s
  when '0'
    if a > 0
      a -= 1
    elsif b > 0
      b -= 1
    else
      break
    end
  when '1'
    if b > 0 && a < A
      b -= 1
      a += 1
    elsif a > 0
      a -= 1
    elsif b > 0
      b -= 1
    else
      break
    end
  end

  ans += 1
end

puts ans

n,a,b = gets.split.map(&:to_i)
s = gets.split.map(&:to_i)
c = b*1
n.times do |i|
    if s[i] == 0
        if c > 0
            c -= 1
        elsif a > 0
            a -= 1
        else
            puts i
            exit
        end
    else
        if a > 0 && c < b
            a -= 1
            c += 1
        elsif c > 0
            c -= 1
        else
            puts i
            exit
        end
    end
end
puts n

n,a,b = gets.split.map(&:to_i)
s = gets.split.map(&:to_i)
c = b
n.times do |i|
    if s[i] == 0
        if c > 0
            c -= 1
        elsif a > 0
            a -= 1
        else
            puts i
            exit
        end
    else
        if a > 0
            a -= 1
            c += 1
            c -= 1 if c > b
        else
            puts i
            exit
        end
    end
end
puts n


gets.to_i.times{k=gets.split[1].to_i
a=[nil]*k
puts gets.chop.chars.zip(0..).map{|x,i|i%=k;x==??||a[i]==x||!a[i]&&a[i]=x}.all?&&[?0,?1].map{|x|a.take(k).count x}.max<=k/2 ?"YES":"NO"}


gets # Skip the first line
fp = gets.chomp
if fp.include?('R')
  if fp.include?('L')
    puts "#{fp.index('R') + 1} #{fp.rindex('R') + 1}"
  else
    puts "#{fp.index('R') + 1} #{fp.rindex('R') + 2}"
  end
else
  puts "#{fp.rindex('L') + 1} #{fp.index('L')}"
end

n = gets.to_i
path = gets.chomp.split('')
s = nil
r = nil
t = nil
l = nil
0.upto(n-1) do |i|
	s = i+1 if(path[i]=='R')&&(s.nil?)
       	r = i+1 if(path[i]!='R')&&(!s.nil?)&&(r.nil?)
	t = i if(path[i]=='L')&&(t.nil?)
	l = i if(path[i]!='L')&&(!t.nil?)&&(l.nil?)
end
s = l if s.nil?
t = r if t.nil?
puts "#{s} #{t}"

n = gets.chomp.to_i
path = gets.chomp
ls = []
rs = []
n.times do |i|
    c = path[i]
    ls << i + 1 if c == 'L'
    rs << i + 1 if c == 'R'
end
s = t = 0
if ls.length == 0
    s = rs.min
    t = rs.max + 1
elsif rs.length == 0
    s = ls.max
    t = ls.min - 1
else
    s = rs.min
    t = rs.max + 1
end
puts "#{s} #{t}"

gets.to_i
s = gets
if s.index('R').nil?
  b = s.index('L')
  a = s.index('.', b)
elsif s.index('L').nil?
  a = s.index('R') + 1
  b = s.index('.', a - 1) + 1
else
  a = s.index('R') + 1
  b = s.index('L')
end
puts "#{a} #{b}"

n=gets.to_i
a=gets.chomp.split('')
s,t=a.rindex('R'),a.index('L')
puts s ? "#{s+1} #{s+2}" : "#{t+1} #{t}"

n = gets.to_i
arr = gets.split("")
l_index = arr.index("L")
r_index = arr.index("R")
l_last_index = arr.rindex("L")
r_last_index = arr.rindex("R")

ans = false

unless l_index
	s,t = r_index,r_last_index+1
	ans = true
end

unless r_index
	s,t = l_last_index,l_index - 1
	ans = true
end

if !ans
	s,t = r_index,r_last_index
end

puts "#{s+1} #{t+1}"

#!/usr/bin/ruby -w
n = gets
s = gets
i = 0
while s[i] == '.' do
    i = i + 1
end
print i + 1
print " "
while s[i] != 'L' && s[i] != '.' do
    i = i + 1
end
if(s[i] == '.')
print i + 1
else
    print i
end
puts

n = gets.to_i
blocks = gets.chomp
s = blocks.index("R")
if s
    puts [s+1, n-blocks.reverse.index("R")+1].join(" ")
    exit
end
t = blocks.index("L")
if t
    puts [n-blocks.reverse.index("L"),t].join(" ")
end

l=gets.to_i
s=gets.split(//)
i=0
x=-1
y=-1
while (i<l) do
  if (s[i]=='R') then y=i; end
  i+=1
end
i=l-1
while (i>=0) do
  if (s[i]=='L') then x=i; end
  i-=1
end
if (x==-1) then print(y+1," ",y+2,"\n");
else
  if (y==-1) then print(x+1," ",x,"\n");
  else print(x+1," ",x+1,"\n");
  end
end

n=gets.to_i
a=gets.chomp.split('')
en,st=0,0
for i in 0...a.length
	break if a[i]!='.'
end
if a[i]=='R'
	st=i
	i+=1 while a[i]=='R'
	en = (a[i]=='L')? i-1 : i
else
	en=i-1
	i+=1 while a[i]=='L'
	st=i-1
end
puts "#{st+1} #{en+1}"

n = gets.to_i
moves = gets
strs = moves.index 'R'
ends = moves.rindex 'R'
if strs.nil?
    strs = moves.rindex 'L'
    ends = moves.index 'L'
    ends -= 1
else
    ends += 1
end
puts "#{strs + 1} #{ends + 1}"

def run
  n = $stdin.gets.to_i
  str = $stdin.gets.split(//)

  s = str.rindex('R')
  t = str.index('L')

  if s != nil
    puts "#{s + 1} #{s + 2}"
  else
    puts "#{t + 1} #{t}"
  end
end

run if $0 == __FILE__

class A
  def initialize
    n = $stdin.gets.chomp.to_i
    step = $stdin.gets.chomp.split('')

    s = t = 0
    s_flag = true
    t_flag = true
    r_max = 0
    l_max = 0

    step.each_with_index do |ch, index|
      if ch == 'R' && s_flag
        s = index+1
        s_flag = false
      elsif ch == 'L' && t_flag
        t = index
        t_flag = false
      end

      if ch == 'R'
        r_max = index+1
      elsif ch == 'L'
        l_max = index+1
      end
    end

    if s_flag
      s = l_max
    end

    if t_flag
      t = r_max + 1
    end

    puts "#{s} #{t}"
  end
end

a = A.new

n = gets.chomp.to_i
path = gets.chomp
s = t = 0
dir = nil
n.times do |c|
    s = c + 1 if path[c] != '.' && s == 0
    dir = path[c] if dir.nil? && path[c] != '.'
    t = c - 1 if !dir.nil? && path[c] != '.' && dir != path[c]
end
puts "#{s} #{t}"

gets.chomp.to_i
path = gets.chomp
index = 0
pos = []
finish = false
while !finish
    c = path[index]
    if c != '.'
        if c == 'R'
            index += 1
            if !pos.include?( index )
                pos << index
            else
                finish = true
            end
        else
            index -= 1
            if !pos.include?( index )
                pos << index
            else
                finish = true
            end
        end
        next
    end
    index += 1
end
s = pos.min
t = pos.max
puts "#{s} #{t}"

n = gets.chomp.to_i
path = gets.chomp
ls = []
rs = []
n.times do |i|
    c = path[i]
    ls << i + 1 if c == 'L'
    rs << i + 1 if c == 'R'
end
s = t = 0
if ls.length == 0
    s = rs.min
    t = rs.max
elsif rs.length == 0
    s = ls.max
    t = ls.min
else
    s = rs.min
    t = rs.max
end
puts "#{s} #{t}"

n = gets.to_i
arr = gets.split("")
l_index = arr.index("L")
r_index = arr.index("R")
l_last_index = arr.rindex("L")
r_last_index = arr.rindex("R")

ans = false

unless l_index
	s,t = r_index,r_last_index
	ans = true
end

unless r_index
	s,t = l_last_index,l_index
	ans = true
end

if !ans
	s,t = r_index,r_last_index
end

puts "#{s+1} #{t+1}"

n = gets.to_i
arr = gets.split("")
l_index = arr.index("L")
r_index = arr.index("R")
l_last_index = arr.rindex("L")
r_last_index = arr.rindex("R")

ans = false

unless l_index
	s,t = r_index,r_last_index+1
	ans = true
end

unless r_index
	s,t = l_last_index-1,l_index
	ans = true
end

if !ans
	s,t = r_index,r_last_index
end

puts "#{s+1} #{t+1}"

n = gets.to_i
arr = gets.split("")
l_index = arr.index("L")
r_index = arr.index("R")
l_last_index = arr.rindex("L")
r_last_index = arr.rindex("R")

ans = false

unless l_index
	s,t = r_index,r_last_index+1
	ans = true
end

unless r_index
	s,t = l_last_index,l_index
	ans = true
end

if !ans
	s,t = r_index,r_last_index
end

puts "#{s+1} #{t+1}"

l=gets.to_i
s=gets.split(//)
i=0
x=-1
y=-1
while (i<l) do
  if (s[i]=='R') then y=i; end
  i+=1
end
i=l-1
while (i>=0) do
  if (s[i]=='L') then x=i; end
  i-=1
end
if (x==-1) then print(y+1," ",y+1,"\n");
else print(x+1," ",x+1,"\n");end

l=gets.to_i
s=gets.split(//)
i=0
x=-1
y=-1
while (i<l) do
  if (s[i]=='R') then y=i; end
  i+=1
end
i=l-1
while (i>=0) do
  if (s[i]=='L') then x=i; end
  i-=1
end
if (x==-1) then print(y+1," ",y,"\n");
else
  if (y==-1) then print(x+1," ",x+2,"\n");
  else print(x+1," ",x+1,"\n");
  end
end

n=gets.to_i
a=gets.chomp.split('')
b=[]
a.each do |i|
	b<<i if i!='.'
end
if b.uniq.count==1
	if b.first=='R'
		puts "#{a.index('R')+1} #{a.rindex('R')+1}"
	elsif b.first=='L'
		puts "#{a.rindex('L')+1} #{a.index('L')+1}"
	end
else
	puts "#{a.index('R')+1} #{a.index('L')+1}"
end

n = gets.to_i
moves = gets
if moves.index 'L'
    strs = moves.index 'R'
    ends = moves.rindex 'R'
else
    strs = moves.rindex 'R'
    ends = moves.index 'R'
end
if strs.nil?
    strs = moves.index 'L'
    ends = moves.rindex 'L'
end
puts "#{strs + 1} #{ends + 1}"

n = gets.to_i
moves = gets
strs = moves.index 'R'
ends = moves.rindex 'R'
if strs.nil?
    strs = moves.index 'L'
    ends = moves.rindex 'L'
end
puts "#{strs + 1} #{ends + 1}"

n = gets.to_i
moves = gets
strs = moves.index 'R'
ends = moves.rindex 'R'
if strs.nil?
    strs = moves.index 'L'
    ends = moves.rindex 'L'
    ends -= 1
else
    ends += 1
end
puts "#{strs + 1} #{ends + 1}"

def run
  n = $stdin.gets.to_i
  str = $stdin.gets.split(//)

  s = 0, t = -1
  str.each_with_index do |item, index|
    s = index if item == 'R'
    t = index if item == 'L' and t == -1
  end

  puts "#{s} #{t}"
end

run if $0 == __FILE__

class A
  def initialize
    n = $stdin.gets.chomp.to_i
    step = $stdin.gets.chomp.split('')

    s = t = 0
    s_flag = true
    t_flag = true

    step.each_with_index do |ch, index|
      if ch == 'R' && s_flag
        s = index+1
        s_flag = false
      elsif ch == 'L' && t_flag
        t = index
        t_flag = false
      end
    end

    puts "#{s} #{t}"
  end
end

a = A.new

n = gets.to_i
path = gets.chomp.split('')
s = nil
r = nil
t = nil
l = nil
0.upto(n-1) do |i|
	s = i+1 if(path[i]=='R')&&(s.nil?)
       	r = i+1 if(path[i]!='R')&&(!s.nil?)
	t = i if(path[i]=='L')&&(t.nil?)
	l = i if(path[i]!='L')&&(!t.nil?)
end
s = l if s.nil?
t = r if t.nil?
puts "#{s} #{t}"


n=gets.to_i
a=gets.split.map &:to_i
b=gets.split.map &:to_i
z=[]
n.times{|i|
	j=i;
	j+=1 while a[i]!=b[j];
	j.downto(i+1){|k|
		b[k],b[k-1]=b[k-1],b[k]
		z<<[k,k+1]
	}
}
p z.size
z.each{|x,y|
	print "#{x} #{y}\n"
}

class Solution
  def initialize
    @n = gets.to_i
    @a = gets.split.map(&:to_i)
    @b = gets.split.map(&:to_i)
    @m = []
  end

  def move(i)
    return if @a[i] == @b[i]
    j = i
    j += 1 while @b[j] != @a[i]
    j.downto(i+1).each do |k|
      @m << k-1
      @b[k-1], @b[k] = @b[k], @b[k-1]
    end
  end

  def solve
    (0...@n).each {|i| move(i)}
    puts @m.size
    @m.each {|x| printf("%d %d\n", x+1, x+2)}
  end
end

solution = Solution.new
solution.solve


n = gets.to_i
sa = 0
sg = 0
(0...n).each do |i|
    a, g = gets.split.collect{|i| i.to_i}
    if (sa+a-sg).abs <= 500
        sa += a
        print "A"
    else
        sg += g
        print "G"
    end
end
print "\n"

n = gets.to_i
s_a = 0
s_g = 0
A = "A"
ans = ""
# ans = A*n
# if n < 1000
G = "G"
n.times do |i|
	a,b = gets.split(" ")
	a = a.to_i
	b = b.to_i

	x = s_a + a
	if (x - s_g) <= 500
		s_a = x
		ans << A
	else
		s_g += b
		ans << G
	end
end

if (s_a - s_g).abs <= 500
	puts ans
else
	puts "-1"
end

s=0
n=gets.to_i
n.times do
	a,b=gets.chomp.split.map(&:to_i)
	s+=a
end
i=n-(s+499)/1000
puts "A"*i + "G"*(n-i)

n = gets.to_i
ta = tb = 0
n.times do
    a, b = gets.split.map{|i| i.to_i}
    ta += a
    tb += b
end
if ta % 1000 <= 500
    n.times do |i|
       if i < ta / 1000 then  print 'G'
       else print 'A'
       end
    end
elsif tb % 1000 <= 500
    n.times do |i|
        if i < tb / 1000 then print 'A'
        else print 'G'
        end
    end
else
    puts -1
end

def run
  n = $stdin.gets.to_i
  sum = 0
  $stdin.readlines.each do |line|
    a, g = line.split.map(&:to_i)
    sum += a
  end

  i = n - (sum + 499) / 1000
  puts "A" * i + "G" * (n - i)
end

run if $0 == __FILE__

n = gets.to_i
s = 0
(1..n).each {
	a,g = gets.chomp.split(" ").collect! {|x| x.to_i}
	s += a
}

p = s/1000.0
if p>=p.floor+0.5
	k = p.floor+1
else
	k = p.floor
end

res = "G"*k+"A"*(n-k)

puts res

n = gets.to_i

# a = []
# g = []

answer = ''
a_cur = 0
g_cur = 0

n.times do |i|
	a, g = gets.split(' ').collect!{|i| i.to_i}
	if (a_cur + a - g_cur).abs <= 500
		answer << "A"
		a_cur += a
	else
		answer << "G"
		g_cur += g
	end
end

# answer = -1  if (a_cur - g_cur).abs > 500

print answer

def run
  n = $stdin.gets
  sa = 0
  sg = 0
  ret = ""
  $stdin.readlines.each do |line|
    a, g = line.split.map(&:to_i)
    if sa <= sg + 500
      sa += a
      ret += "A"
    else
      sg += g
      ret += "G"
    end
  end

  if (sa - sg).abs <= 500
    puts ret
  else
    puts "-1"
  end
end

run if $0 == __FILE__

def run
  n = $stdin.gets.to_i
  sum = 0
  $stdin.readlines.each do |line|
    a, g = line.split.map(&:to_i)
    sum += a
  end

  i = n - (sum + 499) / 1000
  puts "A" * i + "G" * (n - 1)
end

run if $0 == __FILE__

def run
  n = $stdin.gets
  sa = 0
  sg = 0
  ret = ""
  $stdin.readlines.each do |line|
    a, g = line.split.map(&:to_i)
    if sa < sg
      sa += a
      ret += "A"
    else
      sg += g
      ret += "G"
    end
  end

  if (sa - sg).abs <= 500
    puts ret
  else
    puts "-1"
  end
end

run if $0 == __FILE__

def run
  n = $stdin.gets
  sa = 0
  sg = 0
  ret = ""
  $stdin.readlines.each do |line|
    a, g = line.split.map(&:to_i)
    if sa <= sg
      sa += a
      ret += "A"
    else
      sg += g
      ret += "G"
    end
  end

  if (sa - sg).abs <= 500
    puts ret
  else
    puts "-1"
  end
end

run if $0 == __FILE__

n = gets.to_i

a = []
g = []

a_sum = 0
g_sum = 0

n.times do |i|
	a[i], g[i] = gets.split(' ').collect!{|i| i.to_i}
	a_sum += a[i]
	g_sum += g[i]
end

# a_sr = a_sum / n
# g_sr = g_sum / n

answer = ''
a_cur = 0
g_cur = 0

# n.times do |i|
# 	if a_cur + a[i] < g_sr
# 		answer << "A"
# 		a_cur += a[i]
# 	elsif g_cur + g[i] < a_sr
# 		answer << "G"
# 		g_cur += g[i]
# 	elsif (a_cur + a_sum - g_cur - g_sum + g[i]).abs <= 500
# 		answer << "A"
# 		a_cur += a[i]
# 	elsif (g_cur + g_sum - a_cur -a_sum + a[i]).abs <= 500
# 		answer << "G"
# 		g_cur += g[i]
# 	else
# 		answer = -1
# 		break
# 	end

# 	a_sum -= a[i]
# 	g_sum -= g[i]
# end

n.times do |i|
	a_sum -= a[i]
	g_sum -= g[i]
	if (a_cur + a[i] + a_sum - g_cur - g_sum).abs <= 500
		answer << "A"
		a_cur += a[i]
	else
		answer << "G"
		g_cur += g[i]
	end
end
# print answer
answer = -1  if (a_cur - g_cur).abs > 500

print answer

n = gets.to_i

a = []
g = []

# a_sum = 0
# g_sum = 0

answer = ''
a_cur = 0
g_cur = 0

n.times do |i|
	a[i], g[i] = gets.split(' ').collect!{|i| i.to_i}
	# a_sum += a[i]
	# g_sum += g[i]

	if g_cur >= a_cur
		answer << "A"
		a_cur += a[i]
	else
		answer << "G"
		g_cur += g[i]
	end
end

answer = -1  if (a_cur - g_cur).abs > 500

print answer

n = gets.to_i

a = []
g = []

a_sum = 0
g_sum = 0
n.times do |i|
	a[i], g[i] = gets.split(' ').collect!{|i| i.to_i}
	a_sum += a[i]
	g_sum += g[i]
end

answer = -1

if (a_sum/n - g_sum/n).abs <= 500
	answer = ''
	a.sort!.reverse!
	g.sort!.reverse!
	a_cur = 0
	g_cur = 0

	n.times do |i|
		if g_cur >= a_cur
			answer << "A"
			a_cur += a[i]
		else
			answer << "G"
			g_cur += g[i]
		end
	end
end

print answer

n = gets.to_i

a = []
g = []

a_sum = 0
g_sum = 0

n.times do |i|
	a[i], g[i] = gets.split(' ').collect!{|i| i.to_i}
	a_sum += a[i]
	g_sum += g[i]
end

a_sr = a_sum / n
g_sr = g_sum / n

answer = ''
a_cur = 0
g_cur = 0

n.times do |i|
	if a_cur + a[i] < g_sr
		answer << "A"
		a_cur += a[i]
	elsif g_cur + g[i] < a_sr
		answer << "G"
		g_cur += g[i]
	elsif (a_cur + a_sum - g_cur - g_sum + g[i]).abs <= 500
		answer << "A"
		a_cur += a[i]
	elsif (g_cur + g_sum - a_cur -a_sum + a[i]).abs <= 500
		answer << "G"
		g_cur += g[i]
	else
		answer = -1
		break
	end

	a_sum -= a[i]
	g_sum -= g[i]
end

answer = -1  if (a_cur - g_cur).abs > 500

print answer

n = gets.to_i
s_a = 0
s_g = 0
ans = ""
if n < 1000
n.times do
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		ans << "A"
	else
		s_g += b
		ans << "G"
	end
end
else
	n.times do
		a,b = gets.split(" ").map(&:to_i)
		(s_a + a - s_g) <= 500
	end

end
if (s_a - s_g).abs <= 500
	puts ans
else
	puts "-1"
end

n = gets.to_i
s_a = 0
s_g = 0
ans = "A"*n
if n < 1000
n.times do |i|
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		# ans << "A"
	else
		s_g += b
		ans[i] = "G"
	end
end
else
n.times do
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		# ans << "A"
	else
		s_g += b
		ans << "G"
	end
end

end
if (s_a - s_g).abs <= 500
	puts ans
else
	puts "-1"
end

n = gets.to_i
s_a = 0
s_g = 0
ans = ""
if n < 1000
n.times do
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		ans << "A"
	else
		s_g += b
		ans << "G"
	end
end
else
n.times do
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		# ans << "A"
	else
		s_g += b
		ans << "G"
	end
end

end
if (s_a - s_g).abs <= 500
	puts ans
else
	puts "-1"
end

n = gets.to_i
s_a = 0
s_g = 0
ans = ""
if n < 1000
n.times do
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		ans << "A"
	else
		s_g += b
		ans << "G"
	end
end
else
n.times do
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		# ans << "A"
	else
		s_g += b
		# ans << "G"
	end
end

end
if (s_a - s_g).abs <= 500
	puts ans
else
	puts "-1"
end

n = gets.to_i
s_a = 0
s_g = 0
ans = ""
if n < 1000
n.times do
	a,b = gets.split(" ").map(&:to_i)
	if (s_a + a - s_g) <= 500
		s_a += a
		ans << "A"
	else
		s_g += b
		ans << "G"
	end
end
else
	n.times do
		a,b = gets.split(" ").map(&:to_i)
	end

end
if (s_a - s_g).abs <= 500
	puts ans
else
	puts "-1"
end

n = gets.to_i
s = 0
(1..n).each {
	a,g = gets.chomp.split(" ").collect! {|x| x.to_i}
	s += a
}

p = s/1000.0
if p>=p.floor+0.5
	k = p.floor+1
else
	k = p.floor
end

res = "A"*k+"G"*(n-k)

puts res

n = gets.to_i
a = Array.new
b = Array.new
ans = Array.new(n)
(1..n).each do
    tmp = gets.split.collect{|i| i.to_i}
    a.push(tmp[0])
    b.push(tmp[1])
end
diff = (0...n).collect{|i| [a[i]-b[i],i]}.sort
tmp = 0
diff.each do |i|
    if (tmp+i[0]).abs <= (tmp-i[0]).abs
        if i[0] <= 0
            ans[i[1]] = "A"
        else
            ans[i[1]] = "G"
        end
        tmp += i[0]
    else
        if i[0] <= 0
            ans[i[1]] = "G"
        else
            ans[i[1]] = "A"
        end
        tmp -= i[0]
    end
end
if tmp.abs <= 500
    puts ans.join
else
    puts "-1"
end


t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  a = gets.to_s.strip.split.map {|x| x.to_i }

  minv = a.min
  minv_i = a.find_index(minv)

  x = minv + 1
  while minv.gcd(x) != 1
    x += 1
  end

  printf("%d\n", n - 1)
  0.upto(n - 1) do |i|
    next if i == minv_i
    if (i % 2) != (minv_i) % 2
      printf("%d %d %d %d\n", i + 1, minv_i + 1, x, minv)
    else
      printf("%d %d %d %d\n", i + 1, minv_i + 1, minv, minv)
    end
  end
end

$, = " "
gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    mn, pos = 1E9.to_i + 7, -1
    n.times do |i|
        if a[i] < mn
            mn = a[i]
            pos = i
        end
    end
    puts n - 1
    n.times do |i|
        next if i == pos
        print pos + 1, i + 1, mn, mn + (i - pos).abs, "\n"
    end
end

$, = " "
gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    mn, pos = 1E9.to_i + 7, -1
    n.times do |i|
        if a[i] < mn
            mn = a[i]
            pos = i
        end
    end
    puts(n - 1)
    n.times do |i|
        next if i == pos
        print pos + 1, i + 1, mn, mn + (i - pos).abs, "\n"
    end
end

$, = " "
gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    mn, pos = 1E9.to_i + 7, -1
    n.times do |i|
        if a[i] < mn
            mn = a[i]
            pos = i
        end
    end
    n.times do |i|
        next if i == pos
        print pos + 1, i + 1, mn, mn + (i - pos).abs, "\n"
    end
end