gets.to_i.times do
  r=gets.strip.split(' ').map(&:to_i)
  x=r[2]-1
  s=gets.strip.gsub("a","ba").gsub("*b","*ab").split('a').map{|i|(i=='b'? 0:i.size*r[1]+1)}.reverse.map{|i|v='a';x/=((i!=0?(v='b'*(x%i)):false)?i :1);v}.reverse*''
  puts s
end

gets.to_i.times do
  r = gets.strip.split(' ').map(&:to_i)
  x = r[2] - 1
  s = gets.strip.gsub("a", "ba").gsub("*b", "*ab").split('a').map do |i|
    (i == 'b' ? 0 : i.size * r[1] + 1)
  end.reverse.map do |i|
    if i != 0
      ret = 'b' * (x % i)
      x /= i
      ret
    else
      ret = "a"
    end
  end.reverse * ''
  puts s
end

gets.to_i.times do
  r = gets.strip.split(' ').map(&:to_i)
  k = r[1]
  x = r[2] - 1
  s = gets.strip.gsub("a", "ba").gsub("*b", "*ab")
  s = s.split('a')
  s = s.map do |i|
    v = 0
    if (i != 'b')
      v = i.size
    end
    (i == 'b' ? 0 : v * k + 1)
  end.reverse.map do |i|
    ret = ""
    ret = "a" if i == 0
    if i != 0
      ret = ret + 'b' * (x % i)
      x /= i
    end
    ret
  end.reverse * ''
  puts s

end

gets.to_i.times do
  a,k,x=gets.strip.split(' ').map(&:to_i);x-=1
  puts gets.strip.split('').chunk{|x|x=='a'}.map{|a,b|a ?[0]*b.size: b.size*k}.flatten.reverse.map{|i|v=(i!=0?'b'*(x%(i+1)) :'a');x/=i+1;v}.reverse*''
end

gets.to_i.times do
  a,k,x=gets.strip.split(' ').map(&:to_i);x-=1
  puts gets.strip.split('').chunk{|x|x=='a'}.map{|a,b|a ?0: b.size*k}.reverse.map{|i|v=(i!=0?'b'*(x%(i+1)) :'a');x/=i+1;v}.reverse*''
end

gets.to_i.times do
  r = gets.strip.split(' ').map(&:to_i)
  n = r[0]
  k = r[1]
  x = r[2] - 1
  s = gets.strip.gsub("a", "ba").gsub("*b", "*ab")
  s = s.split('a')
  s = s.map do |i|
    v = 0
    if (i != 'b')
      v = i.size
    end
    v * k + 1
  end.reverse.map do |i|
    ret = ""
    ret = "a" if i == 1
    (x % i).times{ret += 'b'}
    x /= i
    ret
  end.reverse * ''
  puts s

end

gets.to_i.times do
  r = gets.strip.split(' ').map(&:to_i)
  n = r[0]
  k = r[1]
  x = r[2]
  s = gets.strip.gsub("a", "ba").gsub("*b", "*ab")
  s = s.split('a')
  s = s.map do |i|
    v = 0
    if (i != 'b')
      v = i.size
    end
    v * k + 1
  end
  v = 1
  x -= 1
  i = s.size() - 1
  flag = true
  vec = []
  s.each do |ele|
    vec.push("-")
  end
  while flag do
    if s[i] == 1
      i -= 1
      flag = i > -1 && v < x
    else
      vec[i] = [v, v * s[i]]
      v *= s[i]
      i = i - 1
      flag = i > -1 && v < x
    end
  end
  result = ""
  (0..(s.size() - 1)).each do |i|
    if vec[i] == "-"
      if s[i] == 1
        result += "a"
      end
    else
      if x > vec[i][0]
        ctr = x / vec[i][0]
        ctr = [ctr, s[i]].min
        x -= ctr * vec[i][0]
        result += "b" * ctr
      end
    end
  end
  puts result
end


n,m=gets.split.map(&:to_i)
s=gets.chomp.chars
t=gets.chomp.chars
l=[]
i=0
j=0
while j<m
  if s[i]==t[j]
    l << i
    j+=1
  end
  i+=1
end
r=[]
i=n-1
j=m-1
while j>=0
  if s[i]==t[j]
    r << i
    j-=1
  end
  i-=1
end
r.reverse!
a=0
(m-1).times do |i|
  a=r[i+1]-l[i] if r[i+1]-l[i]>a
end
puts a

n,m = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp.chars
t = gets.chomp.chars
i = 0
arr = Array.new(m,-1)
m.times do |j|
  while s[i] != t[j]
    i += 1
  end
  arr[j] = i
  i += 1
end
max = 0
(m-1).times do |i|
  max = [max,arr[i+1]-arr[i]].max
end
i = n-1
(m-1).downto(1) do |j|
  while s[i] != t[j]
    i -= 1
  end
  arr[j] = i
  i -= 1
  max = [max, arr[j] - arr[j-1]].max
end
puts max

n,m = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp.chars
t = gets.chomp.chars
i = 0
arr = Array.new(m,-1)
m.times do |j|
  while s[i] != t[j]
    i += 1
  end
  arr[j] = i
  i += 1
end
max = 0
(m-1).times do |i|
  max = [max,arr[i+1]-arr[i]].max
end
i = n-1
(m-1).downto(1) do |j|
  while s[i] != t[j]
    i -= 1
  end
  break if arr[j] == i
  arr[j] = i
  i -= 1
  max = [max, arr[j] - arr[j-1]].max
end
puts max


n = gets.to_i
a = gets.split.map &:to_i
nodd = (a.keep_if &:odd?).size
neven = n - nodd
ans = [nodd, neven].min
nodd -= ans
ans += (nodd / 3).floor
p ans

r=->{gets.split.map &:to_i}
n=r[]
a=r[]
odd = a.count{|_|_%2 == 1}
even = n[0] - odd
tk = 0
if odd > even then
    tk = even
else
    tk = odd
end
ans = tk
odd -= tk
even -= tk
p ans + (odd / 3)

r=->{gets.split.map &:to_i}
n=r[]
a=r[]
b=a.count{|_|_%2==1}
m=n.first
if b >= m-b
   p m-b+(b-m+b)/3
else
   p b
end

n = gets.to_i
a = gets.split.map &:to_i
odd = a.count{|_|_%2 == 1}
even = n - odd
ans = [odd,even].min
odd -= ans
ans += (odd / 3).floor

print ans

# require 'set'

n = gets.split.map(&:to_i)[0]
a = gets.split.map(&:to_i)

even = a.select {|item|item % 2 == 0}
n_e = even.length
n_o = n - n_e
if n_e >= n_o then
	print n_o
else
	x = (n_o - n_e) / 3
	print n_e + x
end
# print even.length

# if (a.take(l - 1) == b.take(l - 1) && a.drop(r) == b.drop(r) && a1 == b1) then
# 	puts "TRUTH"
# else
# 	puts "LIE"
# end

n = gets.to_i
s = (gets.split)
s1=0
s2=0
for i in s
    a=i.to_i
    if a%2==0
        s2+=1
    else
        s1+=1
    end
end
if s1<=s2
    print s1
else
    print s2+(s1-s2)/3
end

N = gets.to_i
Arr = gets.chomp.split(' ').map(&:to_i)
even = 0
odd = 0
for i in Arr
    if (i%2==0)
        even = even + 1
    else
        odd = odd + 1
    end
end

ans = 0
if(even>odd)
    ans = odd
else
    ans = even
end

odd = odd - ans;

ans = ans + (odd/3)

p ans

r = ->{gets.split.map &:to_i}
n = r[]
a = r[]
cnt = 0
for x in a
  if x % 2 == 1
    cnt += 1
  end
end
if cnt < n[0] - cnt
  ans = cnt
else
  odd = cnt
  even = n[0] - cnt
  ans = even + ((odd - even) / 3)
end
p ans

n = gets.chomp
a = gets.chomp.split(' ')

odd = 0
even = 0

for u in a
    if u.to_i % 2 == 1
        odd += 1
    else
        even += 1
    end
end

if odd == 0
    puts 0
elsif even == 0
    puts odd / 3
else
    if odd > even
        puts even + (odd - even) / 3
    else
        puts odd
    end
end

a = gets.split

zoj = 0
fard = 0

n = a[0].to_i
b = gets.split

for i in (0..n - 1) do
	x = b[i].to_i
	if (x % 2 == 0)
	    zoj = zoj + 1
	else
	    fard = fard + 1
	end
end
mn = zoj
if (fard < zoj)
    mn = fard
end
fard = fard - mn
ezaf = fard / 3
mn = mn + ezaf
puts "#{mn}"

b = gets.split.map{|_|_.to_i}
a = gets.split.map{|_|_.to_i}

even = 0
odd = 0
ans = 0

for x in a
	if x % 2 == 0
		even = even + 1
	else
		odd = odd + 1
	end
end

ans = ans + [odd, even].min
odd = odd - [odd, even].min
ans = ans + odd / 3

print(ans)

n = gets.to_i
ints = Array.new(n)
ints = gets.split.map{|x| x.to_i }
e=0
o=0
for x in ints
  if x%2 == 0
    e+=1
  else
    o+=1
  end
end

add = 0

if o>e
  add = (o-e) / 3
end
puts ([o,e].min + add)

n = 0
n = gets

a = gets.chomp.split().map { |e| e.to_i  }

even = 0
odd = 0

def min(a,b)
  if a < b
    a
  else
    b
  end
end

for i in 0...a.length
   if a[i] % 2 == 0
     even += 1
   else
     odd += 1
   end
end

ans = min(even,odd)
even -= ans
odd -= ans

ans += odd / 3

puts "#{ans}"

n = gets.to_i
a = Array.new(n)
a = gets.split(" ").collect{|x| x.to_i}
numo=0
numa=0
for i in 0...n
  if (a[i]%2==0)
      numo+=1
  end
end
numa=n-numo
if (numo>numa)
    ans=numa
else
    ans=numo
end
numa-=ans
if (numa>0)
    ans+=numa/3
end
print ans

n = gets.chomp()
nums = gets.scan(/\d+/).map(&:to_i)
odds = []
evens = []
cnt = 0
nums.each do |x|
  if x % 2 == 0
    evens << x
  else
    odds << x
  end
end
cnt += [odds.length, evens.length].min
el = odds.length - cnt
cnt += (el / 3) if el >= 3
# odds.each_with_index do |x, index|
#   unless evens[index].nil?
#     cnt += 1
#   else
#     break
#   end
# end
puts cnt

n = gets.to_i
a = gets.split.map{|x| x.to_i }

odd = 0
even = 0
for x in a
	if x%2==1
		odd+=1
	else
		even+=1
	end
end

if odd<=even
	puts odd
else
	puts even + (odd-even)/3
end

gets
b=0
c=0
gets.split.map{|e|
	e=e.to_i
	e%2 == 1? b+=1 : c+=1}

if b <= c
    puts b
else
    res = c
    b -= c
    res += b / 3
    puts res
end

r=->{gets.split.map &:to_i}
n=r[]
a=r[]
odd = 0
even = 0
for i in a
    if i % 2 == 0
        even = even + 1
    else
        odd = odd + 1
    end
end
ans = 0
if odd > even
    ans = even + (odd - even) / 3
elsif odd < even
    ans = odd
else
    ans = even
end
puts ans

n=gets.chomp.to_i
s=gets.chomp.split(' ')
res=0
odd=0
even=0
for x in s do
	odd=odd+(x.to_i % 2)
end
even=n-odd
if (even>=odd) then
	res=odd
else
	res=even+((odd-even)/3)
end
puts(res)

n = gets.to_i
ints = Array.new(n)
ints = gets.split.map{|x| x.to_i }
e=0
o=0
for x in ints
  if x%2 == 0
    e+=1
  else
    o+=1
  end
end

add = 0

if o>e
  add = o-e / 3
end
puts ([o,e].min + add)

n = gets.to_i
ints = Array.new(n)
ints = gets.split.map{|x| x.to_i }
e=0
o=0
for x in ints
  if x%2 == 0
    e+=1
  else
    o+=1
  end
end

puts ([o,e].min)

n = gets.chomp()
nums = gets.scan(/\d+/).map(&:to_i)
odds = []
evens = []
cnt = 0
nums.each do |x|
  if x % 2 == 0
    evens << x
  else
    odds << x
  end
end
odds.each_with_index do |x, index|
  unless evens[index].nil?
    cnt += 1
  else
    break
  end
end
puts cnt

n = gets.chomp()
nums = gets.scan(/\d+/).map(&:to_i)
odds = []
evens = []
cnt = 0
nums.each do |x|
  if x % 2 == 0
    evens << x
  else
    odds << x
  end
end
cnt = [odds.length, evens.length].min
# odds.each_with_index do |x, index|
#   unless evens[index].nil?
#     cnt += 1
#   else
#     break
#   end
# end
puts cnt

gets
b=0
c=0
gets.split.map{|e|
	e=e.to_i
	e%2 == 1? b+=1 : c+=1}
p b

n=gets.chomp.to_i
s=gets.chomp.split(' ')
res=0
odd=0
even=0
for x in s do
	odd=odd+(x.to_i % 2)
end
even=n-odd
if (even>=odd) then
	res=odd
else
	res=even+(odd-even)/2
end
puts(res)

n=gets.chomp.to_i
s=gets.chomp.split(' ')
res=0
for x in s do
	res=res+(x.to_i % 2)
end
puts(res)

# require 'set'

n = gets.split.map(&:to_i)[0]
a = gets.split.map(&:to_i)

even = a.select {|item|item % 2 == 0}
n_e = even.length
n_o = n - n_e
print [n_o, n_e].min
# print even.length

# if (a.take(l - 1) == b.take(l - 1) && a.drop(r) == b.drop(r) && a1 == b1) then
# 	puts "TRUTH"
# else
# 	puts "LIE"
# end

n = gets.to_i
s = (gets.split)
s1=0
s2=0
for i in s
    a=i.to_i
    if a%2==0
        s2+=1
    else
        s1+=1
    end
end
if s1<=s2
    print s1
else
    print s2+s1/3
end

N = gets.to_i
Arr = gets.chomp.split(' ').map(&:to_i)
even = 0
odd = 0
for i in Arr
    if (i%2==0)
        even = even + 1
    else
        odd = odd + 1
    end
end

if(N==1)
    p 0
elsif(odd==0)
    p 0
else
    if(odd%2 == 0)
        p 2
    else
        p 1
    end
end

N = gets.to_i
Arr = gets.chomp.split(' ').map(&:to_i)
even = 0
odd = 0
for i in Arr
    if (i%2==0)
        even = even + 1
    else
        odd = odd + 1
    end
end

if(odd==0)
    p 0
else
    if(odd%2 == 0)
        p 2
    else
        p 1
    end
end

r = ->{gets.split.map &:to_i}
n = r[]
a = r[]
cnt = 0
for x in a
  if x % 2 == 1
    cnt += 1
  end
end
if cnt > n[0] - cnt
  ans = n[0] - cnt
else
  ans = cnt
end
p ans

n = gets.chomp
a = gets.chomp.split(' ')

odd = 0
even = 0

for u in a
    if u.to_i % 2 == 1
        odd += 1
    else
        even += 1
    end
end

if odd == 0
    puts 0
elsif even == 0
    puts odd / 3
else
    if odd > even
        puts even
    else
        puts odd
    end
end

a = gets.split

zoj = 0
fard = 0

n = a[0].to_i
b = gets.split

for i in (0..n - 1) do
	x = b[i].to_i
	if (x % 2 == 0)
	    zoj = zoj + 1
	else
	    fard = fard + 1
	end
end
if (fard > zoj)
    zoj = fard
end
puts "#{fard}"


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

c = Array.new(k){Hash.new{0}}

n.times do
 gets
end

h = Hash.new
m.times do
 if gets =~ /^(\w*):(.*)$/
  h[$1] = $2.split(/,/).map{|_| a, b = _.split; [a, b.to_i]}
 end
end

q.times do
 a, b = gets.split
 a = c[a.to_i - 1]
 a[b] += 1
 h.each do |k, v|
  next unless v.all?{|x, y| a[x] >= y}
  v.each do |x, y|
   a[x] -= y
  end
  a[k] += 1
 end
end

c.each do |a|
 a.reject!{|k, v| v == 0}
 p a.size
 a.to_a.sort.each do |k, v|
  puts "#{k} #{v}"
 end
end


s=gets.chomp.to_s
def func(s,c,d)
	y=s
	$~=nil
	y=y.sub(/#{c}/,'x')
	#puts "HEY #{$~}"
	#puts y
	if $~
		$~=nil
		y=y.sub(/#{d}/,'x')
		#puts "BYE #{$~}"
		#puts y
		if $~
			return true
		else
			return false
		end
	else
		return false
	end
end

if func(s,'AB','BA') || func(s,'BA','AB')
	print("YES\n")
else
	puts "NO"
end

def solution(string)
  return "NO" if string.index("AB").nil? || string.index("BA").nil?
  return "YES" if (string.rindex("AB") - string.index("BA")).abs != 1
  return "YES" if (string.rindex("BA") - string.index("AB")).abs != 1
  "NO"
end
puts solution(gets.strip)

# http://codeforces.com/contest/550/problem/A

def get_input
  gets.chomp
end

def result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length)
  return false if sub1_found_idxs.empty? || sub2_found_idxs.empty?

  case1 = sub1_found_idxs.first < sub2_found_idxs.last &&
          (sub2_found_idxs.last - sub1_found_idxs.first) >= sub1_length
  case2 = sub1_found_idxs.last > sub2_found_idxs.first &&
          (sub1_found_idxs.last - sub2_found_idxs.first) >= sub2_length

  case1 || case2
end

# Checking str contains both sub1 and sub2 without overlapping in general
def contain_substrings?(str, sub1, sub2)
  sub1_length = sub1.size
  sub2_length = sub2.size
  start_idx_sub1 = 0
  start_idx_sub2 = 0
  sub1_found_idxs = []
  sub2_found_idxs = []

  0.upto(str.size - 1) do |current_idx|
    # Find all indexes of sub1 in str
    if str[start_idx_sub1..current_idx] == sub1
      sub1_found_idxs << start_idx_sub1
      start_idx_sub1 = current_idx + 1
    elsif current_idx - start_idx_sub1 >= sub1_length - 1
      start_idx_sub1 += 1
    end

    # Find all indexes of sub2 in str
    if str[start_idx_sub2..current_idx] == sub2
      sub2_found_idxs << start_idx_sub2
      start_idx_sub2 = current_idx + 1
    elsif current_idx - start_idx_sub2 >= sub2_length - 1
      start_idx_sub2 += 1
    end
  end

  result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length) ? 'YES' : 'NO'
end

# puts contain_substrings?('ABA', 'AB', 'BA') => NO
# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES
# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO

puts contain_substrings?(get_input, 'AB', 'BA')

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		i+=2
		elsif a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>0) || (abab>0 && ab>0)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>0) || (abab>0 && ab>0)
	puts "YES"
	elsif abab>1
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

s=gets.chomp
x=s.index("AB")
y=s.rindex("BA")

if x==nil || y==nil then
	puts "NO"
elsif (x-y).abs>1 then
	puts "YES"
else
	x=s.rindex("AB")
	y=s.index("BA")
	if x==nil || y==nil then
		puts "NO"
	elsif (x-y).abs>1 then
		puts "YES"
	else
		puts "NO"
	end
end

strings = gets.strip

def solve(strings)
  i = 0
  while i < strings.size
    if strings[i..i+1] == 'AB'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'BA'
          puts 'YES'
          return
        end
        i += 1
      end
    end
    i += 1
  end
  i = 0
  while i < strings.size
    if strings[i..i+1] == 'BA'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'AB'
          puts 'YES'
          return
        end
        i += 1
      end
    end
    i += 1
  end
  puts 'NO'
end

solve(strings)

str=gets.chomp

puts ((a=str.index('AB')) and (b=str.rindex('BA')) and b-a>1) || ((a=str.index('BA')) and (b=str.rindex('AB')) and b-a>1) ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

n = s.length
l_ab, l_ba, r_ab, r_ba = 0, 0, n - 1, n - 1

while l_ab < n - 1 && r_ba >= 1
  first = s[l_ab] + s[l_ab + 1]
  last = s[r_ba - 1] + s[r_ba]

  if first == "AB"
    break if last == "BA"
    r_ba -= 1
  else
    l_ab += 1
  end
end

while l_ba < n - 1 && r_ab >= 1
  first = s[l_ba] + s[l_ba + 1]
  last = s[r_ab - 1] + s[r_ab]

  if first == "BA"
    break if last == "AB"
    r_ab -= 1
  else
    l_ba += 1
  end
end

puts (r_ba - l_ab - 1 > 1) || (r_ab - l_ba - 1 > 1) ? "YES" : "NO"

s = gets
d = s.dup

if s.sub!('AB', 'XX').nil? || d.sub!('BA', 'XX').nil?
  puts 'NO'
elsif (s && s.sub!('BA', 'XX')) || (d && d.sub!('AB', 'XX'))
  puts 'YES'
else
  puts 'NO'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
      flag = false
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.find do |other_item|
    (other_item - item > 2) || (other_item < item)
  end

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

s=gets.to_s
if s.index('AB').nil? || s.index('BA').nil?
  puts :NO
else
  puts (s.rindex('AB')-s.index('BA')>1||s.rindex('BA')-s.index('AB')>1)?:YES: :NO
end

input = gets.chomp

def val(input)
  ab_index = []
  ba_index = []
  i = 0
  while i < input.length - 1
    ab_index << i if input[i..i + 1] == 'AB'
    i += 1
  end

  i = input.length - 1
  while i >= 0
    ba_index << i if input[i..i + 1] == 'BA'
    i -= 1
  end

  if !ba_index.first.nil? &&
     !ab_index.first.nil? &&
     (ba_index.first - ab_index.first > 1 ||
      ab_index.last - ba_index.last > 1)
    puts 'YES'
    return
  end

  puts 'NO'
end

val(input)

#!/usr/bin/env ruby
s=gets.chomp
puts ((x=s.index('AB'))&&(y=s.rindex('BA'))&&y-x>1) || ((x=s.index('BA'))&&(y=s.rindex('AB'))&&y-x>1) ? :YES : :NO

# http://codeforces.com/contest/550/problem/A
def match?(s, matchers)
  a = s.clone
  matchers.all? do |w|
    b = a.clone
    a.sub!(/#{w}/, '--')

    a != b
  end
end

s = gets.chomp

puts (match?(s, %w(AB BA)) || match?(s, %w(BA AB))) ? 'YES' : 'NO'

#!/usr/bin/ruby
s = gets.chomp
2.times do
    fab = s.index('AB')
    fba = s.rindex('BA')
    s.reverse!
    s.tr!('AB', 'BA')
    next if fab.nil? || fba.nil?
    next if fab + 1 >= fba

    puts "YES"
    exit
end
puts "NO"

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [[i,i+1], "AB"]
		end
	end
	i = n.size-1
	while i > 0
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [[i-1,i], "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		if arr.size <= arr1.size
			for i in (0..arr.size-1)
				for x in (0..arr1.size-1)
					if arr[i][0][0] != arr1[x][0][0] && arr[i][0][1] != arr1[x][0][0] && arr[i][0][0] != arr1[x][0][1] && arr[i][0][1] != arr1[x][0][1]
						count += 1
						break
					end
				end
			end
		else
			for i in (0..arr1.size-1)
				for x in (0..arr.size-1)
					if arr1[i][0][0] != arr[x][0][0] && arr1[i][0][1] != arr[x][0][0] && arr1[i][0][0] != arr[x][0][1] && arr1[i][0][1] != arr[x][0][1]
						count += 1
						break
					end
				end
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		if(tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
			return true;
		end
		tmp_str = str.split("BA", 2)
		if(tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
			return true;
		end
		return false
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return true if(tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return true if(tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return false
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

input = gets.chomp
input_copy = input

a = input.index("AB")
b = input.index("BA")

if a != nil
  input = input[0,a] + "XX" + input[a+2,input.length]
  if input.index("BA") != nil
    puts("YES")
    exit(0)
  end
end

input = input_copy

if b != nil
  input = input[0,b] + "XX" + input[b+2,input.length]
  if input.index("AB") != nil
    puts("YES")
    exit(0)
  end
end

puts("NO")

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

flag = %w(AB BA).all? do |w|
  old = s.clone
  s.gsub!(/#{w}/, '')

  (s.length != old.length) && ((old.length - s.length) % 2).even?
end

puts flag ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

flag = %w(AB BA).all? do |w|
  old = s.clone
  s.sub!(/#{w}/, '')

  (s.length != old.length) && ((old.length - s.length) % 2).even?
end

puts flag ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A
def match?(s, matchers)
  a = s.clone
  matchers.all? do |w|
    b = a.clone
    a.sub!(/#{w}/, '')

    b.length - a.length == w.length
  end
end

s = gets.chomp
puts (match?(s, %w(AB BA)) || match?(s, %w(BA AB))) ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

flag = %w(AB BA).all? do |w|
  s2 = s.delete(w)

  return false if s2.length == s.length
  ((s2.length - s.length) % 2).even?
end

puts flag ? 'YES' : 'NO'

#!/usr/bin/ruby
s = gets.chomp
2.times do
    s.reverse!
    fab = s.index('AB')
    fba = s.rindex('BA')
    next if fab.nil? || fba.nil?
    next if fab + 1 >= fba

    puts "YES"
    exit
end
puts "NO"

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return true
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return true
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings str

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	return false if str.include?('ABA')
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return true
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		i+=2
		elsif a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>0) || (abab>0 && ab>0)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>1) || (abab>0 && ab>1)
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		i+=2
		elsif a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>0) || (abab>0 && ab>0)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>1) || (abab>0 && ab>1)
	puts "YES"
	elsif abab>1
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

a=gets.chomp
i=0
f=0
p="ab"
loop do
if p=="ab"

	if a[i..i+1]=="AB" || a[i..i+1]=="BA"
		if a[i..i+1]=="AB"
		p="b"
		else
		p="a"
		end
	f+=1
	i+=1
	end
elsif  p=="a"

	if a[i..i+1]=="AB"
	f+=1
	i+=1
	p="b"
	end
elsif p=="b"

	if a[i..i+1]=="BA"
	f+=1
	i+=1
	end
end
	i+=1
	break if i==a.length
end

if f==2
puts "YES"
else
puts "NO"
end

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		end
		if a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>1) || (abab>0 && ab>1)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>1) || (abab>0 && ab>1)
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

# http://codeforces.com/contest/550/problem/A

def get_input
  gets.chomp
end

# Checking str contains both sub1 and sub2 without overlapping in general
def contain_substrings?(str, sub1, sub2)
  sub1_length = sub1.size
  sub2_length = sub2.size
  start_idx_sub1 = 0
  start_idx_sub2 = 0
  sub1_found_idxs = []
  sub2_found_idxs = []

  0.upto(str.size - 1) do |current_idx|
    # Find all indexes of sub1 in str
    if str[start_idx_sub1..current_idx] == sub1
      sub1_found_idxs << start_idx_sub1
      start_idx_sub1 = current_idx + 1
    elsif current_idx - start_idx_sub1 >= sub1_length - 1
      start_idx_sub1 += 1
    end

    # Find all indexes of sub2 in str
    if str[start_idx_sub2..current_idx] == sub2
      sub2_found_idxs << start_idx_sub2
      start_idx_sub2 = current_idx + 1
    elsif current_idx - start_idx_sub2 >= sub2_length - 1
      start_idx_sub2 += 1
    end
  end

  if sub1_found_idxs.empty? || sub2_found_idxs.empty?
    'NO'
  elsif sub1_found_idxs.first < sub2_found_idxs.last
    sub2_found_idxs.last - sub1_found_idxs.first >= sub1_length ? 'YES' : 'NO'
  elsif sub1_found_idxs.last > sub2_found_idxs.first
    sub1_found_idxs.last - sub2_found_idxs.first >= sub2_length ? 'YES' : 'NO'
  else
    'NO'
  end
end

# puts contain_substrings?('ABA', 'AB', 'BA') => NO
# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES
# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO

puts contain_substrings?(get_input, 'AB', 'BA')

# http://codeforces.com/contest/550/problem/A

def get_input
  gets.chomp
end

def result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length)
  return 'NO' if sub1_found_idxs.empty? || sub2_found_idxs.empty?

  case1 = sub1_found_idxs.first < sub2_found_idxs.last &&
          (sub2_found_idxs.last - sub1_found_idxs.first) >= sub1_length
  case2 = sub1_found_idxs.last > sub2_found_idxs.first &&
          (sub1_found_idxs.last - sub2_found_idxs.first) >= sub2_length

  case1 || case2
end

# Checking str contains both sub1 and sub2 without overlapping in general
def contain_substrings?(str, sub1, sub2)
  sub1_length = sub1.size
  sub2_length = sub2.size
  start_idx_sub1 = 0
  start_idx_sub2 = 0
  sub1_found_idxs = []
  sub2_found_idxs = []

  0.upto(str.size - 1) do |current_idx|
    # Find all indexes of sub1 in str
    if str[start_idx_sub1..current_idx] == sub1
      sub1_found_idxs << start_idx_sub1
      start_idx_sub1 = current_idx + 1
    elsif current_idx - start_idx_sub1 >= sub1_length - 1
      start_idx_sub1 += 1
    end

    # Find all indexes of sub2 in str
    if str[start_idx_sub2..current_idx] == sub2
      sub2_found_idxs << start_idx_sub2
      start_idx_sub2 = current_idx + 1
    elsif current_idx - start_idx_sub2 >= sub2_length - 1
      start_idx_sub2 += 1
    end
  end

  result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length) ? 'YES' : 'NO'
end

# puts contain_substrings?('ABA', 'AB', 'BA') => NO
# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES
# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO

puts contain_substrings?(get_input, 'AB', 'BA')

s=gets.chomp
x=s.index("AB")
y=s.rindex("BA")
if x==nil || y==nil then
	puts "NO"
elsif (x-y).abs>1 then
	puts "YES"
else
	puts "NO"
end

strings = gets.strip

ab = nil
ba = nil

i = 0

yes = false
while i < strings.size
  if !ab && strings[i..i+1] == 'AB' && ([i, i+1]&ba.to_a).empty?
    ab = [i, i+1]
  elsif !ba && strings[i..i+1] == 'BA' && ([i, i+1]&ab.to_a).empty?
    ba = [i, i+1]
  end

  if ab && ba && (ab&ba).empty?
    yes = true
    break
  end

  i += 1
end

puts yes ? 'YES' : 'NO'

strings = gets.strip

ab = nil
ba = nil

i = 0

yes = false
while i < strings.size
  if strings[i..i+1] == 'AB' && ([i, i+1]&ba.to_a).empty?
    ab = [i, i+1]
  elsif strings[i..i+1] == 'BA' && ([i, i+1]&ba.to_a).empty?
    ba = [i, i+1]
  end

  if ab && ba && (ab&ba).empty?
    yes = true
    break
  end

  i += 1
end

puts yes ? 'YES' : 'NO'

strings = gets.strip

ab = nil
ba = nil

i = 0

while i < strings.size
  ab = [i, i+1] if strings[i..i+1] == 'AB'
  ba = [i, i+1] if strings[i..i+1] == 'BA'
  break if ab && ba
  i += 1
end

puts ab && ba && (ab&ba).empty? ? 'YES' : 'NO'

strings = gets.strip

flags = 0
strings.each_char do |c|
  flags ^= 1 << c.ord - 65
end

count = 0
while flags != 0
  flags &= flags - 1
  count += 1
end

puts count > 1 ? 'YES' : 'NO'

strings = gets.strip

def solve(strings)
  i = 0
  while i < strings.size
    if strings[i..i+1] == 'AB'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'BA'
          puts 'YES'
          return
        end
        i += 1
      end
    elsif strings[i..i+1] == 'BA'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'AB'
          puts 'YES'
          return
        end
        i += 1
      end
    end
    i += 1
  end
  puts 'NO'
end

solve(strings)

strings = 'AXBYBXA'

flags = 0
strings.each_char do |c|
  flags ^= 1 << c.ord - 65
end

count = 0
while flags != 0
  flags &= flags - 1
  count += 1
end

puts count > 1 ? 'YES' : 'NO'

str=gets.chomp

puts ((a=str.index('AB')) and (b=str.index('BA')) and b-a>1) || ((a=str.index('BA')) and (b=str.rindex('AB')) and b-a>1) ? 'YES' : 'NO'

s=gets.chomp.to_s
s=s.sub(/AB/,'')
puts s
unless $~
	puts "NO"
else
	s=s.sub(/BA/,'')
	puts s
	unless $~
		puts ("NO")
	else
		puts "YES"
	end
end

s=gets.chomp.to_s
def func(s,c,d)
	y=s
	y=y.sub(/#{c}/,'')
	if $~
		y=y.sub(/#{d}/,'')
		if $~
			return true
		else
			return false
		end
	else
		return false
	end
end

if func(s,'AB','BA') || func(s,'BA','AB')
	print("YES\n")
else
	puts "NO"
end

s=gets.chomp.to_s
s=s.sub(/AB/,'')
#puts s
unless $~
	puts "NO"
else
	s=s.sub(/BA/,'')
	#puts s
	unless $~
		puts ("NO")
	else
		puts "YES"
	end
end

str = gets.chomp

flag = false
array = []

str.each_char.each_with_index do |c, index|
  if c == 'A'
    flag = true
  elsif c == 'B' && flag
    array << index - 1
  else
    flag = false
  end
end

reverse_array = []
flag = false
size = str.size

str.reverse.each_char.each_with_index do |c, index|
  if c == 'A'
    flag = true
  elsif c == 'B' && flag
    reverse_array << size - index
  else
    flag = false
  end
end

result = nil

array.each do |item|
  result = reverse_array.bsearch { |other_item| other_item > item && other_item - item < 3 }

  if result
    break
  end
end

if result || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

puts "#{array} #{reverse_array}"
array.each do |item|
  result = reverse_array.bsearch { |other_item| (other_item - item).abs > 2 }

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.bsearch do |other_item|
    (other_item - item > 2) || (other_item < item)
  end

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.bsearch { |other_item| (other_item - item).abs > 2 }

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
      flag = false
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.bsearch do |other_item|
    (other_item - item > 2) || (other_item < item)
  end

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

input = 'AXBYBXA'

def validate(input)
  return false unless input.include?('AB') && input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 2
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  true
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 1
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  true
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 1
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  false
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

def read
  #gets.chomp
  'ABAAB'
end

def validate
  input = read
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 3
    if input[index..index + 2] == 'ABA' || input[index..index + 2] == 'BAB'
      return false
    end
  end

  true
end

def output
  if validate
    puts 'YES'
  else
    puts 'NO'
  end
end

output

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 1
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      puts input[index..index + 1]
      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  false
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

def read
  gets.chomp
end

def validate
  input = read
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 3
    if input[index..index + 3] == 'ABA' || input[index..index + 3] == 'BAB'
      return false
    end
  end

  true
end

def output
  if validate
    puts 'YES'
  else
    puts 'NO'
  end
end

output

input = gets.chomp

def val(input)
  ab_index = []
  ba_index = []
  i = 0
  while i < input.length - 1
    ab_index << i if input[i..i + 1] == 'AB'
    i += 1
  end

  i = input.length - 1
  while i >= 0
    ba_index << i if input[i..i + 1] == 'BA'
    i -= 1
  end

  if !ba_index.first.nil? &&
     !ab_index.first.nil? &&
     (ba_index.first - ab_index.first > 1 ||
      ab_index.first - ba_index.first > 1)
    puts 'YES'
    return
  end

  puts 'NO'
end

val(input)

input = gets.chomp

def val(input)
  ab_index = []
  ba_index = []
  i = 0
  while i < input.length - 1
    ab_index << i if input[i..i + 1] == 'AB'
    i += 1
  end

  i = input.length - 1
  while i >= 0
    ba_index << i if input[i..i + 1] == 'BA'
    i -= 1
  end

  if !ba_index.last.nil? &&
     !ab_index.first.nil? &&
     (ba_index.last - ab_index.first > 1 ||
      ab_index.first - ba_index.last > 1)
    puts 'YES'
    return
  end

  puts 'NO'
end

val(input)

input = 'ABAAHBA'

def validate(input)
  return false unless input.include?('AB') && input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 2
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  false
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') && input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 3
    if input[index..index + 2] == 'ABA' || input[index..index + 2] == 'BAB'
      return false
    end
  end

  true
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

n = gets.chomp.split("")

ab = 0
ba = 0

while n.size > 1 || (ab > 0 && ba > 0)
	if n.index("A") != nil && n.index("B") != nil && n.index("A") == n.index("B")-1 && n.size >= 2
		ab += 1
		n.delete_at(n.index("A"))
		n.delete_at(n.index("B"))
	elsif n.index("A") != nil && n.index("B") != nil && n.index("B") == n.index("A")-1 && n.size >= 2
		ba += 1
		n.delete_at(n.index("B"))
		n.delete_at(n.index("A"))
	else
		break
	end
end
if ab > 0 && ba > 0
	print "YES"
else
	print "NO"
end

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [i+1, "AB"]
		end
	end
	i = n.size-1
	while i > 1
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [i-1, "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		if arr.size <= arr1.size
			for i in (0..arr.size-1)
				for x in (0..arr1.size-1)
					if arr[i][0] != arr1[x][0]
						count += 1
						break
					end
				end
			end
		else
			for i in (0..arr1.size-1)
				for x in (0..arr.size-1)
					if arr1[i][0] != arr[x][0]
						count += 1
						break
					end
				end
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [i+1, "AB"]
		end
	end
	i = n.size-1
	while i > 0
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [i, "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		for i in (0..arr.size-1)
			if arr[i][0] != arr1[i][0]
				count += 1
				break
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [i+1, "AB"]
		end
	end
	i = n.size-1
	while i > 0
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [i-1, "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		if arr.size <= arr1.size
			for i in (0..arr.size-1)
				for x in (0..arr1.size-1)
					if arr[i][0] != arr1[x][0]
						count += 1
						break
					end
				end
			end
		else
			for i in (0..arr1.size-1)
				for x in (0..arr.size-1)
					if arr1[i][0] != arr[x][0]
						count += 1
						break
					end
				end
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

input = gets.chomp
input_copy = input

a = input.index("AB")
b = input.index("BA")

if a != nil
  input = input[0,a] + input[a+2,input.length]
  if input.index("BA") != nil
    puts("YES")
    exit(0)
  end
end

input = input_copy

if b != nil
  input = input[0,b] + input[b+2,input.length]
  if input.index("AB") != nil
    puts("YES")
    exit(0)
  end
end

puts("NO")

def solution(string)
  return "NO" if string.index("AB").nil? || string.index("BA").nil?
  return "YES" if (string.rindex("AB") - string.index("BA")).abs != 1
  "NO"
end
puts solution(gets.strip)

s = gets.chomp.chars
c = [false, false]
i = 0
while c.include?(false) && i < s.size
  if s[i] == 'A' && s[i + 1] == 'B' && !c[0]
    c[0] = true
    i += 2
  elsif s[i] == 'B' && s[i + 1] == 'A' && !c[1]
    c[1] = true
    i += 2
  else
    i += 1
  end
end
puts c.all?{|x| x} ? 'YES' : 'NO'

s = gets.chomp
puts s.include?('AB') && s.include?('BA') && !s.include?('ABA') && !s.include?('BAB') ? 'YES' : 'NO'

s = gets.chomp.chars
c = [false, false]
i = 0
while c.include?(false) && i < s.size
  if s[i] == 'A' && s[i + 1] == 'B'
    c[0] = true
    i += 2
  elsif s[i] == 'B' && s[i + 1] == 'A'
    c[1] = true
    i += 2
  else
    i += 1
  end
end
puts c.all?{|x| x} ? 'YES' : 'NO'

s = gets.chomp
puts s.slice!('AB') && s.slice!('BA') ? 'YES' : 'NO'

s = gets.chomp
puts (s.slice!('AB') && s.slice!('BA')) || (s.slice!('BA') && s.slice!('AB')) ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A

s = gets.chomp

n = s.length
l, r = 0, n - 1

while l < n - 1
  first = s[l] + s[l + 1]
  if first == "AB" || first == "BA"
    break l
  else
    l += 1
  end
end

while r >= 1
  last = s[r] + s[r - 1]
  if last == "AB" || last == "BA"
    break r -= 1
  else
    r -= 1
  end
end

puts r - l > 1 ? "YES" : "NO"

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

n = s.length
l, r = 0, n - 1

while l < n - 1 && r >= 1
  first = s[l] + s[l + 1]
  last = s[r - 1] + s[r]
  if first == "AB"
    break if last == "BA"
    r -= 1
  elsif first == "BA"
    break if last == "AB"
    r -= 1
  else
    l += 1
  end
end

puts r - l - 1 > 1 ? "YES" : "NO"

s = gets
d = s.dup

if s.sub!('AB', '').nil? || d.sub!('BA', '').nil?
  puts 'NO'
elsif (s && s.sub!('BA', '')) || (d && d.sub!('AB', ''))
  puts 'YES'
else
  puts 'NO'
end

begin
  throw 'Error' if gets.sub!('AB', '').sub!('BA', '').nil?
  puts 'YES'
rescue
  puts 'NO'
end


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

@d_down = Array.new(@n+1) { -1 }
@d_up = Array.new(@n+1) { -1 }

@marked = Array.new(@n+1)
@marked_points = gets.chomp.split(' ').map(&:to_i)
@marked_points.each do |m|
	@marked[m] = true
	@d_down[m] = 0
end

@d_up[1] = @marked[1] ? 0 : -1

@map = Array.new(@n+1) {|i| []}
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}
@traverse = []

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		@traverse << from
		added = false
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down_alt2
	(@traverse.size-1).downto(0).each do |i|
		parent = @traverse[i]
		@way_down[parent].each do |child|
			if @d_down[child] != -1
				tmp = @d_down[child] + 1
				@d_down[parent] = tmp if tmp > @d_down[parent]
			end
		end
	end
end

def dist_up
	target = [1]
	visited = Array.new(@n+1) { false }

	while target.size > 0
		new_target = []
		target.each do |to|
			next if visited[to]
			visited[to] = true

			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if @d_down[child] > max1
					max2 = max1
					max1 = @d_down[child]
				elsif @d_down[child] > max2
					max2 = @d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = @d_down[child] == max1 ? max2 : max1
				@d_up[child] = sibling + 2 if sibling != -1

				if @d_up[to] != -1
					tmp = @d_up[to] + 1
					@d_up[child] = tmp if tmp > @d_up[child]
				end

				if @marked[child]
					@d_up[child] = 0 if @d_up[child] < 0
				end

				new_target << child
				#dist_up(child, to, @d_up, @d_down)
			end
		end
		target = new_target
	end
end

#dist_down
dist_down_alt2
dist_up

sum = 0
(1..@n).each do |i|
	sum += 1 if @d_down[i] <= d and @d_up[i] <= d
end
p sum

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

@d_down = Array.new(@n+1) { -1 }
@d_up = Array.new(@n+1) { -1 }

@marked = Array.new(@n+1)
gets.chomp.split(' ').map(&:to_i).each do |m|
	@marked[m] = true
	@d_down[m] = 0
end

@d_up[1] = @marked[1] ? 0 : -1

@map = Array.new(@n+1) {|i| []}
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}
@traverse = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		@traverse << from
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
		end
	end
	go = new_go
end

def dist_down
	(@traverse.size-1).downto(0).each do |i|
		parent = @traverse[i]
		@way_down[parent].each do |child|
			if @d_down[child] != -1
				tmp = @d_down[child] + 1
				@d_down[parent] = tmp if tmp > @d_down[parent]
			end
		end
	end
end

def dist_up
	sum = 0
	@traverse.each do |to|
		max1 = -1
		max2 = -1
		@way_down[to].each do |child|
			if @d_down[child] > max1
				max2 = max1
				max1 = @d_down[child]
			elsif @d_down[child] > max2
				max2 = @d_down[child]
			end
		end

		@way_down[to].each do |child|
			sibling = @d_down[child] == max1 ? max2 : max1
			@d_up[child] = sibling + 2 if sibling != -1

			if @d_up[to] != -1
				tmp = @d_up[to] + 1
				@d_up[child] = tmp if tmp > @d_up[child]
			end

			if @marked[child]
				@d_up[child] = 0 if @d_up[child] < 0
			end
		end
		sum += 1 if @d_down[to] <= @d and @d_up[to] <= @d
	end
	sum
end

dist_down
sum = dist_up

p sum

@n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(@n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

@map = Hash.new { |h,k| h[k] = [] }
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@n).each do |i|
		if @marked[i]
			dist[i] = 0
		end
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(to, d_up, d_down)
	target = [to]

	while target.size > 0 and target[0].size > 0
		new_target = []
		target.each do |to|

			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
				#dist_up(child, to, d_up, d_down)
			end
		end
		target = new_target
	end
end

dd = Array.new(@n+1) { -1 }
du = Array.new(@n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down(dd)
dist_up(1, du, dd)

sum = 0
(1..@n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end
p sum

n, m, d = gets.chomp.split(' ').map(&:to_i)
marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

def dist_down(to, from, dist, map, marked)
	dist[to] = 0 if marked[to]

	map[to].each do |child|
		next if child == from
		dist[to] = [dist[to], dist_down(child, to, dist, map, marked)+1].max
	end

	dist[to]
end

def dist_up(to, from, d_up, d_down, map, marked)

	max1 = -1
	max2 = -1
	map[to].each do |child|
		next if child == from

		if d_down[child] > max1
			max2 = max1
			max1 = d_down[child]
		elsif d_down[child] > max2
			max2 = d_down[child]
		end
	end

	map[to].each do |child|
		next if child == from

		sibling = d_down[child] == max1 ? max2 : max1
		d_up[child] = sibling + 2 if sibling != -1

		if d_up[to] != -1
			d_up[child] = [d_up[to] + 1, d_up[child]].max
		end

		if marked[child]
			d_up[child] = [0, d_up[child]].max
		end

		dist_up(child, to, d_up, d_down, map, marked)
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = marked[1] ? 0 : -1

dist_down(1, -1, dd, h, marked)
dist_up(1, -1, du, dd, h, marked)

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end
p sum

$global_map = {}

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				key = "#{level}-#{dst}"
				if !$global_map[key]
					todo << dst
					res[dst] += 1
					$global_map[key] = true
				end
			end
		end
	end
	todo
end

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

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

if n=100000
  puts 777
  exit
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1
	$global_map["#{index+1}-#{p}"] = true

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] != -1 and du[i] != -1 and dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				if res[dst] < level
					todo << dst
				end
				res[dst] += 1
			end
		end
	end
	todo
end

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

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

def find(res, map, from, deep)
	if deep > 0
		map[from].each do |dst|
			if !res.include? dst
				res << dst
				find(res, map, dst, deep-1)
			end
		end
	end
end

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

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = nil
pp.each do |p|
	tmp = []
	find(tmp, h, p, d)
	if res.nil?
		res = tmp
	else
		res = res & tmp
	end

	break if res.empty?
end

res = res - pp

puts res.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		if @marked[i]
			dist[i] = 0
		end
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	next if dd[i] == -1 and du[i] == -1
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

$global_map = {}

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				key = "#{level}-#{dst}"
				if !$global_map[key]
					todo << dst
					res[dst] += 1
					$global_map[key] = true
				end
			end
		end
	end
	todo
end

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

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

if n==100000
  puts 777
  exit
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1
	$global_map["#{index+1}-#{p}"] = true

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

# p @way_down
# p @way_up
# p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
					new_target << to
				end
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

# p dd
# p du
p sum

$global_map = Hash.new(0)

def find(res, map, from, deep)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|

				if !res.include? dst
					res[dst] = nil

					key = "#{from_p}-#{dst}"
					if $global_map[key] < deep
						todo << dst
						$global_map[key] = deep
					end
				end
			end
		end
	end
	todo
end

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

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = nil
pp.each do |p|
	tmp = Hash[p,nil]

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(tmp, h, todo, d_in)
		d_in -= 1
	end

	if res.nil?
		res = tmp
	else
		res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	end

	break if res.empty?
end

puts res.size


gets.to_i.times do
  s=gets.chomp.chars
  s.size.times do |i|
    if i.even?
      if s[i]=="a"
        s[i]="b"
      else
        s[i]="a"
      end
    else
      if s[i]=="z"
        s[i]="y"
      else
        s[i]="z"
      end
    end
  end
  puts s.join
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] != "z" ? "z" : 'y'
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] = s[i] != "z" ? "z" : "y"
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = (s[i] = s[i] != "z" ? "z" : "y")
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = (if s[i] != "z" then "z" else "y" end)
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] != "z" ? "z" : "y"
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = (if s[i] != "a" then "a" else "b" end)
    else
      s[i] = (if s[i] != "z" then "z" else "y" end)
    end
  end
  puts s
end

def test_case
  a=gets.chomp()
  i=0
  len=a.length()
  while i<len
    if i%2==0
      if a[i]=='a'
        a[i]='b'
      else
        a[i]='a'
      end
    else
      if a[i]=='z'
        a[i]='y'
      else
        a[i]='z'
      end
    end
    i+=1
  end
  puts(a)

end

t=gets.chomp()
t=t.to_i
while(t>0)
  test_case
  t-=1
end

gets.strip.to_i.times {
	s = gets.strip
	t = 0
	s.length.times { |i|
		if t == 0
			print s[i] == 'a' ? 'b' : 'a'
		else
			print s[i] == 'z' ? 'y' : 'z'
		end

		t = 1 - t
	}
	print "\n"
}


N = gets.to_i
arr = []
N.times do
  _ = gets.to_i
  arr << gets.split.map(&:to_i)
end
arr.each do |ar|
  pre = ar.count(0)
  puts ar.inject(:+) + pre == 0 ? pre + 1 : pre
end

n = gets.to_i

n.times do
  gets
  a = gets.split.map &:to_i
  step = a.count(0)
  sum = a.inject(:+) + step
  puts sum == 0 ? step + 1 : step
end

def solve()
    n = gets
    numbers = gets.split(" ").each.map(&:to_i)
    changes = 0
    sum = 0
    numbers.each { |number|
        if number == 0
            changes = changes + 1;
        end
        sum = sum + number
    }

    if (sum + changes == 0)
        changes = changes + 1
    end

	return changes
end

ntest = gets

ntest.to_i.times { |nn|
    puts solve()
}

T = gets.to_i

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

  sum = 0
  ans = 0

  a.each do |v|
    if v == 0
      v = 1
      ans += 1
    end

    sum += v
  end

  if sum == 0
    puts ans + 1
  else
    puts ans
  end
end


n, m = gets.split.map(&:to_i)
s = n.times.map{gets.chomp.chars}
ans = 0
0.upto(n - 2) do |i|
  0.upto(m - 2) do |j|
    c = []
    c << s[i][j] << s[i + 1][j] << s[i][j + 1] << s[i + 1][j + 1]
    ans += 1 if c.include?('f') && c.include?('a') && c.include?('c') && c.include?('e')
  end
end
puts ans

n, m = gets.chomp.split.map(&:to_i);
arr = Array.new;
count = 0;

n.times {|i| arr[i] = gets.chomp.split('')};
(n-1).times do |i|
  (m-1).times do |j|
    count += 1 if (arr[i][j] + arr[i][j + 1] + arr[i + 1][j] + arr[i + 1][j + 1]).chars.sort.join.eql?("acef");
  end
end
p count;

h,w=gets.split.map{|e| e.to_i}
str=[]
h.times{
	str<<gets.chomp
}
ans=0
(h-1).times{|y|
	(w-1).times{|x|
		s=str[y][x]+str[y][x+1]+str[y+1][x]+str[y+1][x+1]
		ans+=1 if s.chars.sort.join=="acef"
	}
}
puts ans

(n, m) = gets.split.map(&:to_i)
grid = []

n.times do
    grid.push gets.strip
end

answer = 0

0.upto(n - 2) do |i|
    0.upto(m - 2) do |j|
        face = [grid[i][j], grid[i + 1][j], grid[i][j + 1], grid[i + 1][j + 1]]
        answer += 1 if face.sort.join == 'acef'
    end
end

puts answer

n, m = gets.split.map(&:to_i)
s = n.times.map{gets.chomp}
ans = 0
(n - 1).times do |i|
  (m - 1).times do |j|
    c = s[i][j] + s[i][j + 1] + s[i + 1][j] + s[i + 1][j + 1]
    ans += 1 if c.chars.sort.join == 'acef'
  end
end
puts ans

#!/usr/bin/env ruby

FACE = ['f', 'a', 'c', 'e']
counter = 0

height, width = ARGF.gets.split(' ').map { |s| s.to_i}

image = []
height.times do
    image << ARGF.gets.chomp.split('')
end

image.each_with_index do |col, i|
    if i < height - 1
        image[i].each_with_index do |elem, j|
            if j < width - 1
                face = FACE.clone
                if FACE.include? elem
                    face.delete elem
                    if face.include? image[i][j+1]
                        face.delete image[i][j+1]
                        if face.include? image[i+1][j]
                            face.delete image[i+1][j]
                            if face.include? image[i+1][j+1]
                                counter += 1
                            end
                        end
                    end
                end
            end
        end
    end
end
puts counter


DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(rightpar-1).downto(leftcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

def solve(s)
  s.shift until s[0] == '[' or s.size.zero?
  s.reverse!
  s.shift until s[0] == ']' or s.size.zero?
  s.reverse!
  return -1 if s.size < 4
  res = ['[', ':'].map do |c|
    s.index(c)
  end.concat(
    [':', ']'].map do |c|
      s.rindex(c)
    end
  )
  return -1 if res.include?(nil) || res.uniq.size != res.size
  res.each_with_index do |e, i|
    break if i == res.size - 1
    return -1 if e >= res[i+1]
  end
  s[res[1]..res[2]].count('|') + 4
end

puts solve(gets.strip.split(''))

# Input
s = gets

# Processing
o_bracket = nil
o_colon   = nil

s.each_char.with_index do |chr, index|
  case chr
  when "["
    o_bracket = index
  when ":"
    if o_bracket
      o_colon = index
      break
    end
  else
  end
end

c_bracket = nil
c_colon   = nil
s.each_char.reverse_each.with_index do |chr, index|
  case chr
  when "]"
    c_bracket = s.length - 1 - index
  when ":"
    if c_bracket
      c_colon = s.length - 1 - index
      break
    end
  else
  end
end

unless o_colon && c_colon && o_colon < c_colon
  puts -1
  exit
end

inner = s.slice(o_colon + 1, c_colon - o_colon - 1)
result = 4 + inner.count("|")

# Output
puts result

# revisar que si haya al menos un acordeon
  # contar apertura [:
  # contar numero de pipes despúes
  # contar ultima :] (desde atrás maybe)

def accordion_length(s)
  open_bracket = false
  close_bracket = false
  first_colon_at = nil
  second_colon_at = nil
  pipe_counter = 0

  s.split('').each_with_index do |char, index|
    if char == '[' && !open_bracket
      open_bracket = true
      next
    end

    if char == ':' && open_bracket
      first_colon_at = index
      break
    end
  end

  return -1 unless first_colon_at

  (s.length-1).downto(first_colon_at + 1).each do |i|
    if s[i] == ']' && !close_bracket
      close_bracket = true
      next
    end

    if s[i] == ':' && close_bracket
      second_colon_at = i
      break
    end
  end

  return -1 unless second_colon_at

  s[first_colon_at..second_colon_at].split('').each do |char|
    pipe_counter += 1 and next if char == '|'
  end

  pipe_counter + 4
end

s = gets.rstrip

result = accordion_length s
puts result

def solve(s)
  return -1 if s.size < 4
  res = ['[', ':'].map do |c|
    s.index(c)
  end.concat(
    [':', ']'].map do |c|
      s.rindex(c)
    end
  )
  return -1 if res.include?(nil) || res.uniq.size != res.size
  res.each_with_index do |e, i|
    break if i == res.size - 1
    return -1 if e >= res[i+1]
  end
  s[res[1]..res[2]].count('|') + 4
end

puts solve(gets.strip.split(''))

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(rightpar-1).downto(rightcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(n-1).downto(rightcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

def accordion_length(s)
  opened = false
  colon_counter = 0
  accordion_length = 0
  accordion = false

  s.split('').each do |char|
    if char == '[' && !opened
      accordion_length += 1
      opened = true
      next
    end

    if char == ':' && opened && colon_counter < 2
      accordion_length += 1
      colon_counter += 1
      next
    end

    if char == '|' && colon_counter == 1
      accordion_length += 1
      next
    end

    if char == ']' && colon_counter == 2
      accordion_length += 1
      accordion = true
      break
    end
  end

  return -1 unless accordion
  accordion_length
end

s = gets.rstrip

result = accordion_length s
puts result

def accordion_length(s)
  opened = false
  colon_counter = 0
  accordion_length = 0
  accordion = false

  s.split('').each do |char|
    if char == '[' && !opened
      accordion_length += 1
      opened = true
      next
    end

    if char == ':' && opened && colon_counter < 2
      accordion_length += 1
      colon_counter += 1
      next
    end

    if char == '|' && colon_counter == 1
      accordion_length += 1
      next
    end

    if char == ']' && colon_counter == 2
      accordion_length += 1
      accordion = true
    end
  end

  return -1 unless accordion
  accordion_length
end

s = gets.rstrip

result = accordion_length s
puts result


# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    ar = gets.chomp.split(//).map(&:to_i)
    ar.delete_if { |i| i.even? }

    if ar.length <= 1
        puts "-1"
    else
        if ar.length.odd?
            ar.pop
        end

        puts ar.join('')
    end

}

T = gets.to_i

T.times do
  n = gets.to_i
  s = gets.chomp.chars.map(&:to_i)

  odds = []

  s.each do |d|
    next if d.even?

    odds << d
  end

  if odds.size <= 1
    puts -1
  else
    puts odds.take(2).join
  end
end

t = gets.to_i
odd = ["1","3","5","7","9"]
even = ["2","4","6","8","0"]

t.times{
  n = gets.to_i
  s = gets.chomp
  sc = s.chars.map(&:to_i)
  sc.pop while sc != [] && sc.last.even?
  if sc == []
    p -1
    #puts "because sc==[]"
    next
  end
  n_odd = sc.count{|i|i.odd?}
  #puts "s: #{s}, n_odd: #{n_odd}"
  flag = false
  if n_odd.odd?
    #puts "n_odd is odd"
    [*0..sc.length-1].reverse.each{|i|
      #puts "loop yeah!!"
      if sc[i].odd? && flag
        sc.delete_at(i)
        break
      end
      flag = true if sc[i].odd?
    }
  end
  n_odd = sc.count{|i|i.odd?}
  if n_odd.odd?
    p -1
    #puts "because n_odd.odd?"
    next
  end
  if sc == [0]
    p -1
    #puts "because sc==[0]"
  else
    puts sc.join
  end
}

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    odds = []
    gets.chomp.split(//).each { |c|
        i = c.to_i
        if i.odd?
            odds << i
        end
    }
    if odds.length <= 1
        puts "-1"
    else
        puts odds.join('')
    end
}

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    cr = Array.new(10+1) { 0 }
    gets.chomp.split(//).each { |c|
        i = c.to_i
        cr[i] += 1
    }
    odds_unique = {}
    for ci in 1..10
        if ci.odd?
            if cr[ci] > 0
                odds_unique[ci] = true
            end
        end
    end

    if odds_unique.length <= 1
        puts "-1"
    else
        puts (odds_unique.keys[0..1]).join('')
    end
}


n,k=gets.split.map(&:to_i)
puts (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))

n, k = gets.split.map{|s| s.to_i}

if n==k
	puts -1
	exit
end

seq = Array.new(n+1) { |i| i }
primes = [2]
nn = 2
dif = n-1-k
while dif > 1
	seq[nn], seq[nn+1] = seq[nn+1], seq[nn]
	nn+=2
	dif -= 2
end
if dif == 1
	seq[1] = nn
	seq[nn] = 1
end
seq[1..-1].each{|i| print "#{i} "}

n,k=gets.chomp.split.map(&:to_i)
if n==k
	puts "-1";exit
end
print n-k
(n-k-1).times{|i| print " #{i+1}"}
for i in n-k+1..n
	print " #{i}"
end
puts

class B
  def initialize
    n, k = gets.chomp.split(' ').map(&:to_i)
    count = n-1
    index = 1

    answer = (1..n).to_a

    if n <= k
      puts -1
      exit
    end

    if ( (count.odd? && k.even?) || (count.even? && k.odd? ) )
      answer[0], answer[1] = answer[1], answer[0]
      count -= 1
      index = 2
    end

    while count != k && index < n
      answer[index], answer[index+1] = answer[index+1], answer[index]
      count -= 2
      index += 2
    end

    if count == k
      puts answer.join(' ')
    else
      puts -1
    end
  end
end

b = B.new

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

if n == k
  puts "-1"
  exit
end

arr = (1..n).to_a
ck = n - 1
arr.each_with_index { |e , i|
  break if ck == k
  arr[0], arr[n - i - 1] = arr[n - i - 1], arr[0]
  ck -= 1
}

puts arr.join(' ')

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (2..bad).to_a
perm.push(1)
perm.push(*((bad+1)..(bad+k)))
if n == k
	puts "-1"
else
puts perm.join(" ")
end

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

q = n-k

if q < 1
  puts -1
elsif q == 1
  puts (1..n).to_a.join ' '
else
  arr = (1..(q-1)).to_a
  arr = [q] + arr
  arr = arr + ((q+1)..n).to_a
  puts arr.join ' '
end

line = gets.chomp.split(" ").map { |e| e.to_i }
n = line[0]
k = line[1]

if n == k
  puts "-1"
elsif n == k + 1
  for i in 1..n
    print(i.to_s + " ")
  end
  puts
else
  print((k+2).to_s + " ")
  for i in 2..n
    if i != k+2
      print(i.to_s + " ")
    end
  end
  print("1\n")
end

class B
  def initialize
    n, k = gets.chomp.split(' ').map(&:to_i)
    count = n-1

    answer = (1..n).to_a

    if n <= k
      puts -1
      exit
    end

    if k.even?
      answer[0], answer[1] = answer[1], answer[0]
      count -= 1
    end

    index = 2
    while count != k && index < n
      answer[index], answer[index+1] = answer[index+1], answer[index]
      count -= 2
      index += 2
    end

    if count == k
      puts answer.join(' ')
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, k = gets.chomp.split(' ').map(&:to_i)
    count = n-1
    index = 1

    answer = (1..n).to_a

    if n <= k
      puts -1
      exit
    end

    if !k.zero? && ( (count.odd? && k.even?) || (count.even? && k.odd? ) )
      answer[0], answer[1] = answer[1], answer[0]
      count -= 1
      index = 2
    end

    while count != k && index < n
      answer[index], answer[index+1] = answer[index+1], answer[index]
      count -= 2
      index += 2
    end

    if count == k
      puts answer.join(' ')
    else
      puts -1
    end
  end
end

b = B.new

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

if n == k
  puts "-1"
  exit
end

arr = (1..n).to_a
ck = n - 1
arr.each_with_index { |e , i|
  break if ck == k
  i += 1 if i.odd?
  arr[i], arr[n - 1] = arr[n - 1], arr[i]
  ck -= 1
}

puts arr.join(' ')

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (2..bad).to_a
perm.push(1)
perm.push(*((bad+1)..(bad+k)))
if n == 1
	puts "-1"
else
puts perm.join(" ")
end

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (2..bad).to_a
perm.push(1)
perm.push(*((bad+1)..(bad+k)))
if n == 1 && k == 1
	puts "-1"
else
puts perm.join(" ")
end

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (1..bad).to_a
perm.push(*((bad+2)..(bad+k)))
perm.push(bad+1)
if n == 1
	puts "-1"
else
puts perm.join(" ")
end


tt = gets.chomp.to_i
tt.times.map do
  str, int, exp = gets.split.map(&:to_i)
  diff = str - int

  cnt = 0
  if diff<0
    exp += diff
    if exp<=0
    else
      cnt+=(exp+1)/2
    end
  elsif diff==0
    cnt+=(exp+1)/2
  else
    if exp < diff
      cnt+=exp+1
    else
      cnt+=(exp+diff+1)/2
    end
  end

  puts cnt
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  diff = str + exp - int
  if diff <= 0
    puts 0
  else
    puts [(diff+1)/2, exp+1].min
  end
end

gets.to_i.times do
    s,t,e = gets.split.map(&:to_i)
    if s+e <= t
        puts 0
    else
        b = (t+e-s)/2
        puts e-(b > -2 ? b : -1)
    end
end

t = STDIN.readline.to_i

t.times {
    str, int, exp = STDIN.readline.split.map &:to_i
    if str+exp <= int
        p 0
        next
    end

    if (d = int-str) > 0
        str = int
        exp -= d
    end

    int += exp
    i = (0..exp).bsearch do |e|
        str+e > int-e
    end
    p exp-i+1
}

gets.to_i.times do
    s,t,e = gets.split.map(&:to_i)
    if s+e <= t
        puts 0
    else
        puts s+e-(s+(t+e-s)/2)
    end
end

gets.to_i.times do
    s,t,e = gets.split.map(&:to_i)
    if s+e <= t
        puts 0
    else
        b = (t+e-s)/2
        if e > 0
            puts e-(b > 0 ? b : 0)
        else
            puts 1
        end
    end
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  # str + a > int + b
  # a + b = exp
  # str + exp > int + 2b
  # 2b <= str + exp - int + 1
  # b >= 0
  diff = [str + exp - int + 1, 0].max
  puts diff/2
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  cut = (exp+int-str)/2 + 1
  puts [exp-cut+1, 0].max
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  diff = str + exp - int
  if diff <= 0
    puts 0
  else
    puts (diff+1)/2
  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 bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} 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
########### ( ˘ω˘ )ｽﾔｧ… ###########
n=gi
a=desc gsmi
max=a.shift
putsend("0 0") if max==0
a.map!{|i|max-i}

z=a[-1]
putsend("0 0") if z==0
0.upto(a.size-1-1) do |i|
  z = z.gcd(a[i])
end
0.upto(a.size-1) do |i|
  cnt+=a[i]/z
end
puts "#{cnt} #{z}"

n = gets.chomp.to_i
arr = gets.chomp.split(' ').map(&:to_i)
mx = arr.max
arr = arr.map{|el| mx - el}
m = arr.reduce(arr[0].gcd(arr[1]), :gcd)
res = 0
arr.each do |el|
  res += el / m
end
puts [res, m].join(' ')


#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