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

ans = 0
n, m = gets.split(/\s+/).map(&:to_i)
a = Array.new(n)

n.times do |i|
  a[i] = gets.split(/\s+/).map(&:to_i)
end

0.upto(n-1) do |i|
  p = a[i].reduce(:+)
  q = m - p

  ans += max(2**p - 1 - p, 0)
  ans += max(2**q - 1 - q, 0)
end

0.upto(m-1) do |j|
  p = 0
  0.upto(n-1) {|i| p += a[i][j] }
  q = n - p
  ans += max(2**p - 1 - p, 0)
  ans += max(2**q - 1 - q, 0)
end

puts ans + n * m

n, m=gets.split.map &:to_i
res=0
a=[]
n.times{
    a<<s=gets.split
    k=s.count ?0
    res+=(2**k-1)+(2**(m-k)-1)
}
a.transpose.each{|s|
    k=s.count ?0
    res+=(2**k-1)+(2**(n-k)-1)
}
puts res-n*m

n, m = gets.split.map(&:to_i)
l, res = Array.new(m, 0), 4
n.times do
  row = gets.split.map(&:to_i)
  x = row.count 1
  res += 2**x + 2**(m - x)
  row.each_with_index {|c, i| l[i] += c}
end
l.each {|x| res += 2**x + 2**(n - x)}
p res - (n + 2) * (m + 2)


def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y - y.floor < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  (y.round - y).abs < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1..10000).map { |a| cbrt(x - a.pow(3)) }.any? ? "YES" : "NO"
end
#
#

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y.floor == y
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end
#

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y % 1 == 0
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  (y.round - y).abs < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end

def method(x, sum)
  y = sum ** 2 - x
  return false if y % 3 != 0
  b = -sum
  c = y / 3
  return false if b * b - 4 * c < 0
  d = Integer.sqrt(b * b - 4 * c)
  return false if d ** 2 != (b * b - 4 * c)
  x1, x2 = (-b - d) / 2, (-b + d) / 2
  return x1 > 0 && x2 > 0 && x1 + x2 == -b && x1 * x2 == c
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1...20000).map { |sum| method(x / sum, sum) if x % sum == 0 }.any? ? "YES" : "NO"
end

t=gets.chomp.to_i
t.times do
x = gets.chomp.to_i
a=0.0
b=0.0
c=0.0
to = (x**(1.0/3.0)).to_i

#puts "to " + to.to_s

tr = false

for i in 1..to
    a=(i**3).to_f
    b=(x-a).to_f
    c=(b**(1.0/3.0)).round
    #puts "#{i} + #{a} + #{b} + #{c}"
    if(c**3==b && a+b==x && b!=0)
        puts "YES"
        tr=true
    end
    break if tr
end

if(!tr)
    puts "NO"
end

end

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y.ceil == y
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1..10000).map { |a| cbrt(x - a.pow(3)) }.any? ? "YES" : "NO"
end
#
#

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y.ceil - y < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1..10000).map { |a| cbrt(x - a.pow(3)) }.any? ? "YES" : "NO"
end
#
#


x = 0
mexor = Array.new(300_001) { |i| x ^= i }

t = gets.to_i

t.times do

a, b = gets.split.map!(&:to_i)

if mexor[a-1] == b
  puts a
else
  c = mexor[a-1] ^ b
  if a != c
    puts a + 1
  else
    puts a + 2
  end
end

end

cache = []
acc = 0
0.upto(3 * 100000 + 5).each {|x| cache.push(acc ^= x) }
t = gets.strip.to_i
t.times do
  a, b = gets.strip.split.map {|x| x.to_i }
  x = cache[a - 1]
  if x == b
    puts a
  elsif (x ^ b) != a
    puts a + 1
  else
    puts a + 2
  end
end

x = 0
mexor = Array.new(300_001) { |i| x ^= i }

t = gets.to_i

t.times do

a, b = gets.split.map!(&:to_i)

if mexor[a-1] == b
  puts a
elsif a != b
  puts a + 1
else
  puts a + 2
end

end


gets.to_i.times{
  n=gets.to_i;i=n%2+3;puts 2,'3'*(n%2),1
  (p -~i,i;i+=2)while i<n
}

gets.to_i.times{n=gets.to_i;i=n%2;puts 2,'3'*i,1;i+=3;(p -~i,i;i+=2)while i<n}

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = Array(1..n)
  (0...n).step(2) do |i|
    next if i + 1 == n
    arr[i], arr[i+1] = arr[i+1], arr[i]
  end
  arr[n-1], arr[n-2] = arr[n-2], arr[n-1] if n % 2 == 1

  puts "#{arr.join(' ')}\n"
end

gets.to_i.times{n=gets.to_i;i=n%2;p 2,'3'*i,1;i+=3;(p -~i,i;i+=2)while i<n}


X, Y, x, y = gets.split.map(&:to_i)
s = gets.chomp!

ans = []

m = []
X.times{ |x|
  m[x+1] = []
  Y.times { |y|
    m[x+1][y+1] = true
  }
}

s.each_char do |c|
  if m[x][y]
    ans << 1
    m[x][y] = false
  else
    ans << 0
  end
  case c
  when 'L'
    y -= 1
  when 'R'
    y += 1
  when 'U'
    x -= 1
  when 'D'
    x += 1
  end
  x = [[1, x].max, X].min
  y = [[1, y].max, Y].min
end

ans << X * Y - ans.inject(&:+)

puts ans.join(' ')

#what the f**k the problem statement was talking about.
#Good luk for the Contest 606, Problem: b

X, Y, x, y = gets.strip.split.map(&:to_i)
cmd = gets.strip.split('')

mp = Array.new(X){Array.new(Y,1)}

pos = [x-1, y-1]
res = Array.new
cmd.each do |c|

	res << mp[pos[0]][pos[1]]
	mp[pos[0]][pos[1]] = 0
	next if pos[0] == 0 and c == 'U'
	next if pos[0] == X-1 and c == 'D'
	next if pos[1] == 0 and c == 'L'
	next if pos[1] == Y-1 and c == 'R'
	case c
	when 'U'
		pos[0] -=1
	when 'D'
		pos[0] += 1
	when 'L'
		pos[1] -= 1
	when 'R'
		pos[1] += 1
	end

  end

res << mp.reduce(0){|r,v| r += v.reduce(:+)}

res.each do |e|
	print e,' '
end
print "\n"

x, y, x0, y0 = gets.chomp.split.map(&:to_i)
s = gets.chomp
d = "LRUD"
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
hs = Hash.new(false)

t = x * y
ans = []

for i in 0...s.length
  if hs[[x0, y0]]
    ans << 0
  else
    hs[[x0, y0]] = true
    ans << 1
    t -= 1
  end
  x0 = [[x0 + dx[d.index(s[i])], 1].max, x].min
  y0 = [[y0 + dy[d.index(s[i])], 1].max, y].min
end

ans << t
puts ans.*' '


n = gets.to_i
arr = gets.split(" ").map(&:to_i)
seg = arr[0..1].length
max_seg = seg
if n > 1
	(2..(n-1)).each do |i|
		# puts "#{a == arr[i-2] + arr[i-1] }"
		if arr[i] == arr[i-2] + arr[i-1]
			# puts "here"
			if seg == 0
				seg = 3
			else
				seg += 1
			end
			max_seg = seg if seg > max_seg
		else
			seg = 0
		end
	end
end
puts max_seg
# 16
# 1 1 1 2 3 5 8 1 1 1 2 3 5 8 13 21

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
if a.size < 3
    puts a.size
    exit
end
maxlength = 2
thislength = 2
for i in 2...n
    if a[i] == a[i-1]+a[i-2]
        thislength += 1
        maxlength = thislength if thislength > maxlength
    else
        thislength = 2
    end
end
puts maxlength

n = gets.to_i
a = gets.chomp.split(" ").map{|i| i.to_i}

L = Array.new

(0..n-1).each do |i|
	L[i] = 2
end

L[0] = 1
max = 1

(2..n-1).each do |i|
	if a[i] == a[i-1] + a[i-2]
		L[i] = L[i-1] + 1
	end
	if L[i] > max
		max = L[i]
	end
end

if n == 2
	max = 2
end

puts max

#!/usr/bin/ruby
n=gets.to_i
m=2;c=0
a=gets.split.map(&:to_i)
if a.size==1
	p 1
	exit
end
a.each_cons(3){|e|
	if e[0]+e[1]==e[2]
		c+=1
	else
		m=[m,c+2].max
		c=0
	end
}
p [m,c+2].max

#!/usr/bin/ruby
gets
m=2;c=0
a=gets.split.map(&:to_i)
if a.size==1
	p 1
	exit
end
a.each_cons(3){|e|
	if e[0]+e[1]==e[2]
		c+=1
	else
		m=[m,c+2].max
		c=0
	end
}
p [m,c+2].max

n = STDIN.readline.to_i
arr = STDIN.readline.split.collect {|s| s.to_i }
if n <= 2
	puts n
	exit
end
best = 2
pos = 0
while pos+2 < n
	count = 2
	seek = pos+2
	while seek < n
		break if arr[seek] != arr[seek-1] + arr[seek-2]
		count += 1
		seek += 1
	end
	best = [best, count].max
	pos = seek-1
end
puts best

n=gets.to_i
a=gets.chomp.split.map(&:to_i)
ans,d=0,0
n.times do |i|
	(i<2 || a[i]==a[i-1]+a[i-2]) ? d+=1 : d=2
	ans=d if d>ans
end
puts ans

#!/usr/bin/ruby
n=gets.to_i
m=2;c=0
gets.split.map(&:to_i).each_cons(3){|e|
	if e[0]+e[1]==e[2]
		c+=1
	else
		m=[m,c+2].max
		c=0
	end
}
p [m,c+2].max

n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max
p q

n = gets.to_i
a = gets.chomp.split(" ").map{|i| i.to_i}

L = Array.new

(0..n-1).each do |i|
	L[i] = 2
end

L[0] = 1
max = 2

(2..n-1).each do |i|
	if a[i] == a[i-1] + a[i-2]
		L[i] = L[i-1] + 1
		if L[i] > max
			max = L[i]
		end
	end
end

puts max

n = gets.to_i
a = gets.chomp.split(" ").map{|i| i.to_i}

L = Array.new

(0..n-1).each do |i|
    L[i] = 1
end

max = 1

(1..n-1).each do |i|
    if a[i] >= a[i-1]
        L[i] = 2
        if max < 2
            max = 2
        end
        if i > 1
            if a[i] == a[i-1] + a[i-2]
                L[i] = L[i-1] + 1
                if L[i] > max
                    max = L[i]
                end
            end
        end
    end
end

puts max

n = gets.to_i
a = gets.chomp.split(" ").map{|i| i.to_i}

L = Array.new

(0..n-1).each do |i|
    L[i] = 2
end

L[0] = 1
max = 1

(2..n-1).each do |i|
    if a[i] == a[i-1] + a[i-2]
        L[i] = L[i-1] + 1
        if L[i] > max
            max = L[i]
        end
    end
end

puts max

n = gets.to_i
a = gets.chomp.split(" ").map{|i| i.to_i}

L = Array.new

(0..n-1).each do |i|
    L[i] = 2
end

L[0] = 1
max = 1

(2..n-1).each do |i|
    if a[i] == a[i-1] + a[i-2]
        L[i] = L[i-1] + 1
    end
    if L[i] > max
        max = L[i]
    end
end

puts max


test_cases = gets.chomp.to_i
for i in 1..test_cases do
    n = gets.chomp.to_i
    line = gets.chomp.split(" ")
    odd = Array.new
    even = Array.new
    for j in 0..n - 1 do
        a = line[j].to_i
        if a % 2 == 1
            odd.push(a)
        else
            even.push(a)
        end
    end
    for j in 0..odd.size - 1 do
        print "#{odd[j]} "
    end
    for j in 0..even.size - 1 do
        print "#{even[j]} "
    end
    puts ""
end

t = gets.to_s.to_i

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

  aro = ar.select {|x| x.odd? }
  are = ar.select {|x| x.even? }

  if aro.size > are.size
    printf("%s %s\n", aro.join(" "), are.join(" "))
  else
    printf("%s %s\n", are.join(" "), aro.join(" "))
  end
end


t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  ok = false
  for i in 0...(n-2) do
    for j in (i+2)...n do
      ok = true if a[i] == a[j]
    end
  end
  puts ok ? "YES" : "NO"
end

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
t.times {
    n = gets.to_i
    mc = {}
    gets.chomp.split.each_with_index { |value, index|
        i = value.to_i
        mc[i] ||= []
        mc[i] << index
    }
    # p mc
    found = false
    catch :found do
        for k, v in mc
            if v.length >= 2
                if v.last - v.first > 1 # 모든 간격을 검사할 필요는 없지. at least니까.
                    found = true
                    throw :found
                end
            end
        end
    end

    if found == true
        puts "YES"
    else
        puts "NO"
    end

}

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|x| x.to_i}
    mark = Array.new(5001) {0}
    mark1 = Array.new(5001) {-1}
    ans = false
    (0...n).each do |x|
        cur = arr[x]
        if(mark[cur] == 0)
            mark1[cur] = x
        end
        mark[cur] += 1
        if(mark[cur] > 1)
            if(mark1[cur]+1 < x)
                ans = true
                break
            end
        end
    end
    # print(mark[1])
    if(ans)
        puts ("YES")
    else
        puts ("NO")
    end
end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  l = gets.chomp.split.map(&:to_i)
  seq_palidrom_exists = false
  (0...n).each{|i| seq_palidrom_exists = true if (l.rindex(l[i]) - i) > 1}
  puts seq_palidrom_exists ? "YES" : "NO"
end

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
t.times {
    n = gets.to_i
    mc = {}
    gets.chomp.split.each_with_index { |value, index|
        i = value.to_i
        mc[i] ||= []
        mc[i] << index
    }
    # p mc
    found = false
    catch :found do
        for k, v in mc
            if v.length >= 2
                for i in 0...(v.length - 1)
                    if v[i+1] - v[i] > 1
                        found = true
                        throw :found
                    end
                end
            end
        end
    end

    if found == true
        puts "YES"
    else
        puts "NO"
    end

}

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|x| x.to_i}
    mark = Array.new(5001) {0}
    ans = false
    (0...n).each do |x|
        mark[arr[x]] += 1
        if(mark[arr[x]] > 1)
            if(arr[x] != arr[x-1])
                ans = true
                break
            end
        end
    end
    if(ans)
        puts ("YES")
    else
        puts ("NO")
    end
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  ok = false
  for i in 0...(n-2) do
    ok = true if a[i] == a[i+2]
  end
  for i in 0...(n-3) do
    ok = true if a[i] == a[i+3] && a[i+1] == a[i+2]
  end
  puts ok ? "YES" : "NO"
end


N, M, K = gets.split.map(&:to_i)
P = gets.split.map(&:to_i)

l, r = 1, K
count, answer = 0, 0
m = 0
while m < M
	count = m
	while P[m] && P[m] <= r
		m += 1
	end
	count = m - count
	if count > 0
		answer += 1
		r += count
	elsif P[m]
		pages = ( (P[m] - r) / K.to_r ).ceil
		r += pages * K
		l += pages * K
	end
#	puts count
end
puts answer

n,m,k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 0
i = 0
while i < m
  #p i
  l = (a[i]-i-1) % k + 1
  r = a[i] + k- l
  while i < m && a[i] <= r
     i += 1
  end
  #p [l,r]
  ans += 1
end
p ans

N, M, K = gets.split.map(&:to_i)
P = gets.split.map(&:to_i)

l, r = 1, K
count, answer = 0, 0
m = 0
while m < M
	count = m
	while P[m] && P[m] <= r
		m += 1
	end
	count = m - count
	if count > 0
		answer += 1
		r += count
	elsif P[m]
		pages = ( (P[m] - r) / K.to_r ).ceil
		r += pages * K
#		l += pages * K
	end
#	puts count
end
puts answer

N, M, K = gets.split.map(&:to_i)
P = gets.split.map(&:to_i)

count, answer = 0, 0
m = 0
while m < M
	r = ( (P[m] - m) / K.to_r ).ceil * K
	count = m
	while P[m] && (P[m] - count) <= r
		m += 1
	end
	count = m - count
	if count > 0
		answer += 1
	end
#	puts count
end
puts answer

N, M, K = gets.split.map(&:to_i)
P = gets.split.map(&:to_i)

count, answer = 0, 0
m = 0
while m < M
	r = ( (P[m] - count) / K.to_r ).ceil * K
	while P[m] && (P[m] - count) <= r
		m += 1
	end
	count = m
	answer += 1
#	puts count
end
puts answer


1.upto(gets.to_i) do
  s = gets.chomp

  res = s.reverse.chars.each_with_object([]).with_index do |(char, result), idx|
    result << char + '0' * idx unless char == '0'
  end

  puts res.size
  puts res.join(' ')
end

t = gets.to_i
t.times do
  n = gets.to_i
  res = []
  [10_000, 1_000, 100, 10, 1].each do |d|
    if (i = n / d) != 0
      res << i * d
      n -= res.last
    end
  end
  puts res.size
  puts res.join(' ')
end

t = gets.to_i
t.times do
  n = gets.chomp
  ans = []
  m = n.size
  m.times do |i|
    if n[i] != "0"
      ans << n[i].to_i * 10 ** (m-i-1)
    end
  end
  p ans.size
  puts ans.join(" ")
end

t = gets.to_i
(1).upto(t) do
  x = gets
  x = x.strip.split('')
  arr = []
  i = x.length-1
  x.each do |c|
    if(c!='0')
      arr.push([c,i])
    end
    i-=1
  end
  puts arr.length
  arr.each do |v|
    print v[0]
    (1..v[1]).each do
      print 0
    end
    print ' '
  end
  puts
end

# Approach
# mod = 10
#
# r = n % mod
# n = n - r
t = gets.to_i
for i in 1..t do
  n = gets.to_i
  mod = 10
  rnds = []
  while n*10 >= mod
    r = n % mod
    if r != 0
      rnds << r
      n = n - r
    end
    mod = mod * 10
  end
  puts rnds.size
  puts rnds.join(' ')
end

t = gets.to_i

1.upto t do
  count = 0
  mul = 1
  val = []
  k = gets.chomp
  array = k.each_char.map {|num| num.to_i}
  for i in array.reverse
    if i != 0
      val.push(i * mul)
    else
      mul *= 10
      next
    end
    mul *= 10
    count += 1
  end
  puts count
  puts val.join(" ")
end

gets.to_i.times{
    s=gets.chomp
    n=s.size
    res=[]
    n.downto(1){|i|
        if s[i-1]!=?0
            res<<s[i-1..-1]
            s[i-1]=?0
        end
    }
    puts [res.size, res.reverse*' ']
}

def is_round(n)
  length = n.to_s.size

  if length == 1
    puts '1'
    puts n
    return
  end

  k = 0
  summands = []

  while length > 1
    term = (10 ** (length - 1))
    m = n / term
    summands << (m * term)

    n = n % term
    length = n.to_s.size
    k += 1
  end

  if n > 0
    summands << n
    k += 1
  end

  puts k
  puts summands.join(' ')
end

if ENV['LOCAL_PROJECT']
  input = File.exist?('in.in') ? File.new('in.in','r') : STDIN
else
  input = STDIN
end

t = input.gets.chomp.to_i

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

class Fixnum
  def digits
    d = []
    t = self
    while t > 0
      d << t%10
      t /= 10
    end
    d
  end
end

module Enumerable
  alias filter select
end

gets.to_i.times do
  n = gets.to_i
  puts n.digits.filter{|x| x > 0}.size
  n.digits.each_with_index{|x, i| print x*10**i, ' ' if x > 0}
  puts
end

n = gets.chomp.to_i

nums = []

n.times do
  nums.push(gets.chomp.to_i)
end

nums.each_with_index do |num|
  nums_to_send = []
  num.to_s.reverse!.each_char.with_index do |digit, index|
    nums_to_send.push(digit.to_i * 10 ** index)
  end
  filtered_arr = nums_to_send.filter {|e| e != 0 }
  p filtered_arr.length
  puts filtered_arr.join(' ')
end

# Approach
# mod = 10
#
# r = n % mod
# n = n - r
t = gets.to_i
for i in 1..t do
  n = gets.to_i
  mod = 10
  rnds = []
  while n >= mod
    r = n % mod
    rnds << r
    n = n - r
    mod = mod * 10
  end
  puts rnds.size
  puts rnds.join(' ')
end


n = gets.to_i
s1 = gets.chomp
s2 = gets.chomp
n1 = s1.size
n2 = s2.size
T = n1*n2
D = n%T
N = n/T
sum1 = 0
sum2 = 0
sumd1 = 0
sumd2 = 0
#Символ «R» (от английского rock) означает знак камня, символ «S» (scissors) — ножниц, «P» (paper) — бумаги.
0.upto(T-1) do |i|
  c1 = s1[i%n1]
  c2 = s2[i%n2]
  if c1 == 'R' && c2 == 'S' || c1 == 'S' && c2 == 'P' || c1 == 'P' && c2 == 'R'
    sum1 += 1
    sumd1 += 1 if i < D
  end
  c1,c2=c2,c1
  if c1 == 'R' && c2 == 'S' || c1 == 'S' && c2 == 'P' || c1 == 'P' && c2 == 'R'
    sum2 += 1
    sumd2 += 1 if i < D
  end
end
sum1 = sum1 * N + sumd1
sum2 = sum2 * N + sumd2
printf("#{sum2} #{sum1}")

n=gets.to_i
R,P,S=82,80,83
a,b=2.times.map{gets.chomp.each_byte.to_a}
as=bs=ar=br=0
lcm=a.size.lcm b.size
q,r=n.divmod lcm
lcm.times{|i|
	ar,br=as,bs if i==r
	_a=a[i%a.size]
	_b=b[i%b.size]
	next if _a==_b
	as+=1 if (_a==R&&_b==P)||(_a==P&&_b==S)||(_a==S&&_b==R)
	bs+=1 if (_b==R&&_a==P)||(_b==P&&_a==S)||(_b==S&&_a==R)
}
puts"%d %d"%[as*q+ar,bs*q+br]

def gcd(a, b)
  return a if b == 0
  return gcd(b, a%b)
end

def lcm(a, b)
  return (a/gcd(a, b))*b
end

n = gets.to_i
a = gets.strip
b = gets.strip
m = a.length
k = b.length
x = lcm(m, k)

a *= x/m
b *= x/k

dota = Array.new
dotb = Array.new

dota[0] = 0
dotb[0] = 0

for i in 1..x
  case a[i-1]
    when "R"; c=1
    when "P"; c=2
    when "S"; c=3
  end
  case b[i-1]
    when "R"; c-=1
    when "P"; c-=2
    when "S"; c-=3
  end
  dota[i] = dota[i-1]
  dotb[i] = dotb[i-1]
  case c
    when 1; dotb[i] += 1
    when 2; dota[i] += 1
    when -1; dota[i] += 1
    when -2; dotb[i] += 1
  end
end

print dota[x]*(n/x) + dota[n%x], " ", dotb[x]*(n/x) + dotb[n%x]

def gcd(a, b)
  until b == 0
    temp = a%b
    a = b
    b = temp
  end
  return a
end

def lcm(a, b)
  return (a/gcd(a, b))*b
end

n = gets.to_i
a = gets.strip
b = gets.strip
m = a.length
k = b.length
x = lcm(m, k)

a *= x/m
b *= x/k

dota = Array.new
dotb = Array.new

dota[0] = 0
dotb[0] = 0

for i in 1..x
  case a[i-1]
    when "R"; c=1
    when "P"; c=2
    when "S"; c=3
  end
  case b[i-1]
    when "R"; c-=1
    when "P"; c-=2
    when "S"; c-=3
  end
  dota[i] = dota[i-1]
  dotb[i] = dotb[i-1]
  case c
    when 1; dotb[i] += 1
    when 2; dota[i] += 1
    when -1; dota[i] += 1
    when -2; dotb[i] += 1
  end
end

print dota[x]*(n/x) + dota[n%x], " ", dotb[x]*(n/x) + dotb[n%x]


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

sw = 0
h  = n
l  = 1

(k - 1).times do
    if sw == 0
        print "#{h} "
        h -= 1
    else
        print "#{l} "
        l += 1
    end
    sw = 1 - sw
end

if sw == 1
    l.upto(h) { |i| print "#{i} " }
else
    h.downto(l) { |i| print "#{i} " }
end

puts

n, k = gets.split.map { |i| i.to_i }
ans = []
if k == n - 1
  for i in 0...n
    print i % 2 == 0 ? i / 2 + 1 : n - i / 2
    print ' '
  end
  exit(0)
end
for i in 0...k / 2
  ans.push i, n - i - 1
end
for i in k / 2...n - k / 2
  ans.push i
end
if k % 2 == 0
  ans[0], ans[1] = ans[1], ans[0]
end
for x in ans
  print (x + 1).to_s + ' '
end


n, k = gets.split.collect{|i| i.to_i}
carts = Array.new(k) {[]}
items = Array.new(n) {gets.split.collect{|i| i.to_i}}
items.each_index{|i|
  items[i] = [i+1, items[i][0], items[i][1]]
}

items = items.sort{|a,b| b[1] <=> a[1]}
stools  = items.select{|x| x[2] == 1}
pencils = items.select{|x| x[2] == 2}

curr_k = 0
price = 0
stools.each{|s|
   carts[curr_k] << s
   curr_k += 1 if curr_k < k - 1
}

pencils.each{|p|
  carts[curr_k] << p
  curr_k += 1 if curr_k < k - 1
}

carts.each{|c|
  has_stool = c[0][2] == 1
  cart_items = c.sort{|a,b| a[1] <=> b[1]}
  cart_items.each{|item|
     price += item[1]
  }
  price -= cart_items[0][1] * 0.5 if has_stool
}

puts "%.1f" % price
carts.each{|c|
  c = c.flatten
  idxs = 0.step(c.size - 1, 3).map{|i| c[i]}
  puts [idxs.size, idxs].join(" ")
}

n,k = gets.split.map(&:to_i)
stool = []
pencil = []
sum = 0
n.times do |i|
  c,t=gets.split.map(&:to_i)
  sum += c
  if t==1
    stool.push([c,i,0])
  else
    pencil.push([c,i,1])
  end
end
stool.sort!
stool.reverse!
cart = Array.new(k){Array.new()}
cnt = [stool.length, k-1].min

cnt.times do |i|
  cart[i] = Array.new
  cart[i].push(stool[i])
end
# 残りを余りに詰める
now = cnt
while cnt < stool.size()
  cart[now].push(stool[cnt])
  cnt += 1
  now += 1
  if now >= cart.size()
    now = cart.size()-1
  end
end
cnt = 0
while cnt < pencil.size()
  cart[now].push(pencil[cnt])
  cnt += 1
  now += 1
  if now >= cart.size()
    now = cart.size()-1
  end
end

sum = 0
k.times{|i|
  cart[i].sort!
  has_stool = false
  cart[i].each{|t|
    sum += t[0]*2
    has_stool = true if t[2] == 0
  }
  if has_stool
    sum -= cart[i][0][0]
  end
}
print sum/2
print "."
if sum%2 == 1
  puts "5"
else
  puts "0"
end

k.times{|i|
  print cart[i].size()
  cart[i].each{|t|
    print " "
    print t[1]+1
  }
  puts
}

n, k = gets.split.collect{|i| i.to_i}
carts = Array.new(k) {[]}
items = Array.new(n) {gets.split.collect{|i| i.to_i}}
items.each_index{|i|
  items[i] = [i+1, items[i][0], items[i][1]]
}

items.sort{|a,b| a[1] <=> b[1]}
stools  = items.select{|x| x[2] == 1}
pencils = items.select{|x| x[2] == 2}

curr_k = 0
price = 0
stools.each{|s|
   carts[curr_k] << s
   curr_k += 1 if curr_k < k - 1
}

pencils.each{|p|
  carts[curr_k] << p
  curr_k += 1 if curr_k < k - 1
}

carts.each{|c|
  has_stool = c[0][2] == 1
  cart_items = c.sort{|a,b| a[1] <=> b[1]}
  cart_items.each{|item|
     price += item[1]
  }
  price -= cart_items[-1][1] * 0.5 if has_stool
}

puts "%.1f" % price
carts.each{|c|
  c = c.flatten
  idxs = 0.step(c.size - 1, 3).map{|i| c[i]}
  puts [idxs.size, idxs].join(" ")
}

n, k = gets.split.collect{|i| i.to_i}
carts = Array.new(k) {[]}
items = Array.new(n) {gets.split.collect{|i| i.to_i}}
items.each_index{|i|
  items[i] = [i+1, items[i][0], items[i][1]]
}

items.sort{|a,b| a[1] <=> b[1]}
stools  = items.select{|x| x[2] == 1}
pencils = items.select{|x| x[2] == 2}

curr_k = 0
price = 0
stools.each{|s|
   carts[curr_k] << s
   curr_k += 1 if curr_k < k - 1
}

pencils.each{|p|
  carts[curr_k] << p
  curr_k += 1 if curr_k < k - 1
}

carts.each{|c|
  has_stool = c[0][2] == 1
  cart_items = c.sort{|a,b| a[1] <=> b[1]}
  cart_items.each{|item|
     price += item[1]
  }
  price -= cart_items[0][1] * 0.5 if has_stool
}

puts "%.1f" % price
carts.each{|c|
  c = c.flatten
  idxs = 0.step(c.size - 1, 3).map{|i| c[i]}
  puts [idxs.size, idxs].join(" ")
}


params = gets.split(" ").collect {|v| v.to_f}
old_skill = Hash::new
params[0].to_i.times do
    old_list = gets.split(" ")
    old_skill[old_list[0]] = old_list[1].to_i
end

new_skill  = Array::new
params[1].to_i.times do
    new_skill << gets.strip
end

skills = Hash::new
old_skill.each do |k,v|
    level = (v*params[2]+0.0001).to_i
    skills[k] = level unless level<100
end

new_skill.each do |v|
    skills[v] = 0 unless skills.key?(v)
end
skills = skills.to_a.sort

puts skills.size
skills.each do |v|
    print v[0], " ", v[1].to_i, "\n"
end

input=gets.split
n,m=input[0..1].map(&:to_i)
k=(input[2].to_f*100+1e-8).to_i
res={}
res.default=0
n.times{
        input=gets.split
        input[1]=(input[1].to_i*k/100).to_i
        if input[1]>=100 then
                res.store(input[0],input[1])
        end
}
m.times{
        input=gets.split[0]
        res[input]+=0
}
puts res.length
res.sort.each do|k, v|
        puts "#{k} #{v}"
end

#!/usr/bin/ruby
# coding: utf-8
require 'scanf'
require 'bigdecimal'

n, m, k = gets.scanf '%d %d %s'
k = BigDecimal k

skills = {}
n.times do
  s, t = gets.scanf '%s %d'
  if t*k >= 100
    skills[s] = (t*k).to_i
  end
end

m.times do
  s = gets.chomp
  if not skills.has_key? s
    skills[s] = 0
  end
end

puts skills.size
puts skills.map { |k, v| "#{k} #{v}" }.sort

require 'scanf'
a = gets.scanf("%d %d %f")
n = a[0]
m = a[1]
k_100 = (a[2] * 100 + 0.5).to_i

h = Hash.new

n.times do
	a = gets.scanf("%s %d")
	if a[1] * k_100 >= 10000 then
		h[a[0]] = a[1] * k_100 / 100
	end
end

m.times do
	s = gets.chomp!
	if h[s].nil? then
		h[s] = 0
	end
end

ans = Array.new
h.each{|key, value|
	ans.push(sprintf("%s %d", key, value))
}

ans.sort!
puts ans.size
ans.each{|line|
	puts line
}

#!/usr/bin/env ruby

args = STDIN.gets.split(" ")
skill = []
args[0].to_i.times do
  skill << STDIN.gets.split(" ")
end

skillname = skill.map {|i| i[0]}

args[1].to_i.times do
  n = STDIN.gets.chomp
  if skillname.index(n)
    skill[skillname.index(n)][1] = "0" if (skill[skillname.index(n)][1].to_i*args[2].to_f*(1.0000001)).to_i < 100
  else
    skill << [n,"0"]
  end
end

skill.sort! {|a,b| a[0] <=> b[0]}
ret =[]
skill.each do |i|
  exp = (i[1].to_i*args[2].to_f*(1.0000001)).to_i
  ret << "#{i[0]} #{exp}" unless exp < 100 and i[1].to_i != 0
end

puts ret.size
ret.each do |s|
  puts s
end

input=gets.split
n,m=input[0..1].map(&:to_i)
k=(input[2].to_f*100).to_i
res={}
res.default=0
n.times{
        input=gets.split
        input[1]=(input[1].to_i*k/100).to_i
        if input[1]>=100 then
                res.store(input[0],input[1])
        end
}
m.times{
        input=gets.split[0]
        res[input]+=0
}
puts res.length
res.sort.each do|k, v|
        puts "#{k} #{v}"
end

input=gets.split
n,m=input[0..1].map(&:to_i)
k=input[2].to_f
res={}
res.default=0
n.times{
        input=gets.split
        input[1]=(input[1].to_i*k).to_i
        if input[1]>=100 then
                res.store(input[0],input[1])
        end
}
m.times{
        input=gets.split[0]
        res[input]+=0
}
res.sort.each do|k, v|
        puts "#{k} #{v}"
end

input=gets.split
n,m=input[0..1].map(&:to_i)
k=input[2].to_f
res={}
res.default=0
n.times{
        input=gets.split
        input[1]=(input[1].to_i*k).to_i
        if input[1]>=100 then
                res.store(input[0],input[1])
        end
}
m.times{
        input=gets.split[0]
        res[input]+=0
}
puts res.length
res.sort.each do|k, v|
        puts "#{k} #{v}"
end

params = gets.split(" ").collect {|v| v.to_f}
old_skill = Hash::new
params[0].to_i.times do
    old_list = gets.split(" ")
    old_skill[old_list[0]] = old_list[1].to_i
end

new_skill  = Array::new
params[1].to_i.times do
    new_skill << gets.strip
end

skills = Hash::new
old_skill.each do |k,v|
    level = (v*100*params[2]/100).to_s.to_i
    skills[k] = level unless level<100
end

new_skill.each do |v|
    skills[v] = 0 unless skills.key?(v)
end
skills = skills.to_a.sort

puts skills.size
skills.each do |v|
    print v[0], " ", v[1].to_i, "\n"
end

params = gets.split(" ").collect {|v| v.to_f}
old_skill = Hash::new
params[0].to_i.times do
    old_list = gets.split(" ")
    old_skill[old_list[0]] = old_list[1].to_i
end

new_skill  = Array::new
params[1].to_i.times do
    new_skill << gets.strip
end

skills = Hash::new
old_skill.each do |k,v|
    level = (v*100*params[2]/100).to_i
    skills[k] = level unless level<100
end

new_skill.each do |v|
    skills[v] = 0 unless skills.key?(v)
end
skills = skills.to_a.sort

puts skills.size
skills.each do |v|
    print v[0], " ", v[1].to_i, "\n"
end

params = gets.split(" ").collect {|v| v.to_f}
old_skill = Hash::new
params[0].to_i.times do
    old_list = gets.split(" ")
    old_skill[old_list[0]] = old_list[1].to_i
end

new_skill  = Array::new
params[1].to_i.times do
    new_skill << gets.strip
end

skills = Hash::new
old_skill.each do |k,v|
    level = (v*params[2]).to_i
    skills[k] = level unless level<100
end

new_skill.each do |v|
    skills[v] = 0 unless skills.key?(v)
end
skills = skills.to_a.sort

puts skills.size
skills.each do |v|
    print v[0], " ", v[1].to_i, "\n"
end

params = gets.split(" ").collect {|v| v.to_f}
old_skill = Hash::new
params[0].to_i.times do
    old_list = gets.split(" ")
    old_skill[old_list[0]] = old_list[1].to_i
end

new_skill  = Array::new
params[1].to_i.times do
    new_skill << gets.strip
end

skills = Hash::new
old_skill.each do |k,v|
    level = (v*params[2]).to_s.to_i
    skills[k] = level unless level<100
end

new_skill.each do |v|
    skills[v] = 0 unless skills.key?(v)
end
skills = skills.to_a.sort

puts skills.size
skills.each do |v|
    print v[0], " ", v[1].to_i, "\n"
end

params = gets.split(" ").collect {|v| v.to_f}
old_skill = Hash::new
params[0].to_i.times do
    old_list = gets.split(" ")
    old_skill[old_list[0]] = old_list[1].to_i
end

new_skill  = Array::new
params[1].to_i.times do
    new_skill << gets.strip
end

skills = Hash::new
old_skill.each do |k,v|
    skills[k] = v*params[2] unless v*params[2]<100
end

new_skill.each do |v|
    skills[v] = 0 unless skills.key?(v)
end
skills = skills.to_a.sort

puts skills.size
skills.each do |v|
    print v[0], " ", v[1].to_i, "\n"
end

params = gets.split(" ").collect {|v| v.to_f}
old_skill = Hash::new
params[0].to_i.times do
    old_list = gets.split(" ")
    old_skill[old_list[0]] = old_list[1].to_i
end

new_skill  = Array::new
params[1].to_i.times do
    new_skill << gets.strip
end

skills = Hash::new
old_skill.each do |k,v|
    level = (v*params[2]).to_s.to_f.to_i
    skills[k] = level unless level<100
end

new_skill.each do |v|
    skills[v] = 0 unless skills.key?(v)
end
skills = skills.to_a.sort

puts skills.size
skills.each do |v|
    print v[0], " ", v[1].to_i, "\n"
end

#!/usr/bin/env ruby

args = STDIN.gets.split(" ")
skill = []
args[0].to_i.times do
  skill << STDIN.gets.split(" ")
end

skillname = skill.map {|i| i[0]}

args[1].to_i.times do
  n = STDIN.gets.chomp
  if skillname.index(n)
    skill[skillname.index(n)][1] = "0" if skill[skillname.index(n)][1].to_i*args[2].to_f < 100
  else
    skill << [n,"0"]
  end
end

skill.sort! {|a,b| a[0] <=> b[0]}
ret =[]
skill.each do |i|
  exp = (i[1].to_i*args[2].to_f).to_i
  ret << "#{i[0]} #{exp}" unless exp < 100 and i[1].to_i != 0
end

puts ret.size
ret.each do |s|
  puts s
end


n = gets.to_i
a = gets.split.map{|x|x.to_i}
c = {}
c.default = 0
a.each do |x|
  c[x] += 1 if x != 0
end
err = false
ans = 0
c.values.each do |x|
  err = true if x > 2
  ans += 1   if x == 2
end
if err
  puts -1
else
  puts ans
end

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

gets
data = gets.split.map(&:to_i)
hash = Hash.new( 0 )

data.each do |value|
  hash[value]+=1
end

ans = 0
hash.to_a.each do |key, value|
  if value > 2 and key != 0 then
    ans = -1
    break
  end

  if value == 2 and key != 0 then
    ans+=1
  end
end

puts ans

length = $stdin.readline.to_i
ids = $stdin.readline.split(" ").map{|i| i.to_i}
raise "Input data error" if length != ids.size

counter = Hash.new(0)
ids.each{|id| counter[id]+=1}
counter.delete(0)
if counter.values.find_all{|v| v>2}.any?
	puts "-1"
else
	puts counter.values.find_all{|v| v==2}.size
end

n = gets.to_i
ids = gets.split.map(&:to_i).sort.take(n)

m = ids.reduce [-1, 0, 0] do |memo, id|
  next memo if id == 0
  if memo[0] == id
    if memo[1] > 1
      puts -1
      exit
    else
      [id, 2, memo[2] + 1]
    end
  else
    [id, 1, memo[2]]
  end
end

puts m[2]

number=Integer(gets.chomp)
str=gets.chomp
policarp=0;
flag=0;
list = Array.new
str=str.split
str.each do |c_hop|
    next if c_hop=='0'
    break if flag==1
    if list.find_all{|elem| elem==c_hop}.size == 1
        policarp=policarp+1
    elsif list.find_all{|elem| elem==c_hop}.size > 1
        flag=1;
    end
    list=list << c_hop;
 end
if flag==0
    puts policarp
else
    puts "-1"
end

begin
  ids = []
  conversations = {}
  count = 0

  n = gets.chomp

  ids = gets.chomp

  ids = ids.split(' ').map(&:to_i)

  if ids.size != n.to_i
    puts -1
    abort
  end

  ids.delete(0)

  ids.each { |id| conversations[id] ||= 0; conversations[id] += 1 }

  conversations.each { |key, value| if value > 2; puts -1; abort; end; count +=1 if value == 2  }

  puts count
rescue Exception

end

lines = ARGF.readlines

a = lines.last.split ' '
b = a.map(&:to_i).delete_if {|x| 0.eql?(x) }
b = b.uniq.map{|x| b.count(x)}
b << 0
puts (b.max > 2) ? -1 : b.count(2)

n = gets.to_i
a = gets.split(' ').map(&:to_i).reject(&:zero?).sort
b = a.uniq

if b.any? { |n| a.count(n) > 2 }
  puts -1
  exit 0
end

x = b.count do |n|
  a.count(n) == 2
end

puts x

gets.chomp # Skip the first line
id_counts = (gets.chomp.split.map { |x| x.to_i } - [0]).inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values
puts(id_counts.any? { |c| c > 2 } ? -1 : id_counts.select { |c| c > 1 }.size)

at_exit do
  n = get_numbers_from_line[0]
  ids = get_numbers_from_line[0...n]
  count = 0
  ids.select{|x| x != 0}.group_by{|x| x}.each do |_, group|
    if group.length == 2
      count += 1
    elsif group.length > 2
      count = -1
      break
    end
  end
  puts count
end

def get_numbers_from_line
  gets.strip.split.map(&:to_i)
end

hs = Hash.new 0
n = gets.to_i
gets.split(' ').map(&:to_i).map{|x| hs[x]+=1}
hs.delete(0)
hs.delete_if{|k,v| v<2}
if hs.any?{|k,v| v>2}
  puts -1
else
  puts hs.length
end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
kv = {}
arr.each do |e|
    if kv[e].nil? then
        kv[e] = 1
    else
        kv[e] = kv[e] + 1
    end
end
if kv.any? { |k,v| k>0 and v>2 } then
    puts -1
else
    puts kv.select { |k,v| k>0 and v==2 }.length
end

gets; res = 0; h = Hash.new(0)

gets.split.map.each{|i|
	next if (i == '0')

	if (h[i] == 2) then
		res = -1;
		break;
	end

	res += 1 if (h[i] == 1)
	h[i] += 1
}

p res

n = gets.to_i

t = true
wk = gets.split.map{|i| i.to_i}
asmas = {}
res = 0

n.times do |i|
    if wk[i] != 0
        if asmas[wk[i]] then
            asmas[wk[i]] += 1
            if asmas[wk[i]] == 2 then
                res +=1
            elsif asmas[wk[i]] > 2 then
                t = false
            end
        else
            asmas[wk[i]] = 1
        end
    end
end

if t then
    puts res
else
    puts "-1"
end

ids = Hash.new{0}
$stdin.each_line.to_a[1].split(" ").map(&:to_i).each do |id|
  next if id == 0
  ids[id] += 1
end

answer = 0

ids.each do |id, count|
  if count > 2
    answer = -1
    break
  end

  if count == 2
    answer += 1
  end
end

puts answer

num_calls = gets
calls = gets.split.collect {|i| i.to_i}
numbers = Hash.new(0)
calls.each do |call|
	numbers[call] += 1 unless call == 0
	if numbers[call] > 2
		puts "-1"
		exit
	end
end
puts numbers.values.count(2)

n = gets.chomp.to_i
h = {}
gets.chomp.split(' ').map {|x| if x.to_i!=0; h[x].nil? ? h[x] = 1 : h[x]+=1; end;}

v = h.values
if v.any? {|x| x > 2}
  puts -1
else
  puts v.count(2)
end

def main
  num = gets.chomp.to_i
  lines = gets.chomp.split(' ').map(&:to_i)
  return if num != lines.count

  line_count = {}

  lines.each do |line|
    next if line == 0

    if line_count.include?(line)
      line_count[line] += 1
      if line_count[line] >= 3
        puts '-1'
        return
      end
    else
      line_count[line] = 1
    end
  end

  total = 0
  line_count.each { |k,v| total += 1 if v == 2 }

  puts total
end

main

class A
  def initialize
    n = $stdin.gets.chomp.to_i
    list = $stdin.gets.chomp.split(' ').map(&:to_i)
    @list = Hash.new(0)
    count = 0

    list.each do |num|
      @list[num] += 1 if num != 0
    end

    @list.each do |key, value|
      case value
      when 1
      when 2
        count += 1
      else
        puts -1
        exit
      end
    end

    puts count
  end
end

a = A.new

hs = Hash.new 0
n = gets.to_i
gets.split(' ').map(&:to_i).map{|x| hs[x]+=1}
hs.delete_if{|k,v| v<2}
if hs.any?{|k,v| v>2}
  puts -1
else
  puts hs.length
end

n = gets.to_i
arr = gets.split(" ")
kv = {}
arr.each do |e|
    if kv[:"#{e}"].nil? then
        kv[:"#{e}"] = 1
    else
        kv[:"#{e}"] = kv[:"#{e}"] + 1
    end
end

if kv.any? { |k,v| v>2 } or kv.values.inject(&:+)>n then
    puts -1
else
    puts kv.select { |k,v| v==2 }.length
end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
kv = {}
arr.each do |e|
    if kv[e].nil? then
        kv[e] = 1
    else
        kv[e] = kv[e] + 1
    end
end
puts kv.inspect
if kv.any? { |k,v| k>0 and v>2 } then
    puts -1
else
    puts kv.select { |k,v| k>0 and v==2 }.length
end

n = gets
arr = gets.split(" ")
kv = {}
arr.each do |e|
    if kv[:"#{e}"].nil? then
        kv[:"#{e}"] = 1
    else
        kv[:"#{e}"] = kv[:"#{e}"] + 1
    end
end
if kv.any? { |k,v| v>2 } then
    puts -1
else
    puts kv.select { |k,v| v==2 }.length
end

n = gets.to_i

t = true
wk = gets.split.map{|i| i.to_i}
asmas = {}
res = 0

n.times do |i|
    if asmas[wk[i]] then
        asmas[wk[i]] += 1
        if asmas[wk[i]] == 2 then
            res +=1
        elsif asmas[wk[i]] > 2 then
            t = false
        end
    else
        asmas[wk[i]] = 1
    end
end

if t then
    puts res
else
    puts "-1"
end

ids = Hash.new{0}
$stdin.each_line.to_a[1].split(" ").map(&:to_i).each do |id|
  ids[id] += 1
end

answer = 0

ids.each do |id, count|
  if count > 2
    answer = -1
    break
  end

  if count == 2
    answer += 1
  end
end

puts answer

num_calls = gets
calls = gets.split.collect {|i| i.to_i}
numbers = Hash.new(0)
calls.each do |call|
	numbers[call] += 1
	if numbers[call] > 2
		puts "-1"
		exit
	end
end
puts numbers.values.count(2)

n = gets.chomp.to_i
h = {}
gets.chomp.split(' ').map {|x| x.to_i; h[x].nil? ? h[x] = 1 : h[x]+=1}

v = h.values
if v.any? {|x| x > 2}
  puts -1
else
  puts v.count(2)
end

n = gets.chomp.to_i
h = {}
gets.chomp.split(' ').map {|x| h[x].nil? ? h[x] = 1 : h[x]+=1}

v = h.values
if v.any? {|x| x > 2}
  puts -1
else
  puts v.count(2)
end

def main
  num = gets.chomp.to_i
  lines = gets.chomp.split(' ')
  return if num != lines.count

  line_count = {}

  lines.each do |line|
    next if line == 0

    if line_count.include?(line)
      line_count[line] += 1
      if line_count[line] >= 3
        puts '-1'
        return
      end
    else
      line_count[line] = 1
    end
  end

  total = 0
  line_count.each { |k,v| total += 1 if v == 2 }

  puts total
end

main

n = gets.to_i
a = gets.split.map{|x|x.to_i}
c = {}
c.default = 0
a.each do |x|
  c[x] += 1
end
err = false
ans = 0
c.values.each do |x|
  err = true if x > 2
  ans += 1   if x == 2
end
if err
  puts -1
else
  puts ans
end

class A
  def initialize
    n = $stdin.gets.chomp.to_i
    list = $stdin.gets.chomp.split(' ').map(&:to_i)
    @list = Hash.new(0)
    count = 0

    list.each do |num|
      @list[num] += 1
    end

    @list.each do |key, value|
      case value
      when 1
      when 2
        count += 1
      else
        puts -1
        exit
      end
    end

    puts count
  end
end

a = A.new

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

gets
data = gets.split.map(&:to_i)
hash = Hash.new( 0 )

data.each do |value|
  hash[value]+=1
end

ans = 0
hash.to_a.each do |key, value|
  if value == 2 and key != 0 then
    ans+=1
  end
end

puts ans

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

gets
data = gets.split.map(&:to_i)
hash = Hash.new( 0 )

data.each do |value|
  hash[value]+=1
end

ans = 0
hash.to_a.each do |key, value|
  next if value == 1 or key == 0

  if value%2 != 0 then
    ans = -1
    break
  end

  ans+=1
end

puts ans

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

gets
data = gets.split.map(&:to_i)
hash = Hash.new( 0 )

data.each do |value|
  hash[value]+=1
end

ans = 0
hash.to_a.each do |key, value|
  next if value == 1

  if value%2 != 0 then
    ans = -1
    break
  end

  ans+=1
end

puts ans

length = $stdin.readline.to_i
ids = $stdin.readline.split(" ").map{|i| i.to_i}
raise "Input data error" if length != ids.size

counter = Hash.new(0)
ids.each{|id| counter[id]+=1}
if counter.values.find_all{|v| v>2}.any?
	puts "-1"
else
	puts counter.values.find_all{|v| v==2}.size
end

n = gets.to_i
ids = gets.split.map(&:to_i).sort

m = ids.reduce [-1, 0, 0] do |memo, id|
  if memo[0] == id
    if memo[1] > 1
      puts -1
      exit
    else
      [id, 2, memo[2] + 1]
    end
  else
    [id, 1, memo[2]]
  end
end

puts m[2]

number=Integer(gets.chomp)
str=gets.chomp
policarp=0;
flag=0;
list = Array.new
str=str.split
str.each do |c_hop|
	next if c_hop==0;
	if list.find_all{|elem| elem==c_hop}.size == 1
	    policarp=policarp+1
	elsif list.find_all{|elem| elem==c_hop}.size > 1
	    flag=1;
		break;
	end
	list=list << c_hop;
 end
if flag==0
    puts policarp
else
    puts "-1"
end

ids = []
conversations = {}
count = 0

n = gets.chomp

ids = gets.chomp

ids = ids.split(' ').map(&:to_i)

ids.delete(0)

ids.each { |id| conversations[id] ||= 0; conversations[id] += 1 }

conversations.each { |key, value| if value > 2; puts -1; abort; end; count +=1 if value > 0  }

puts count

gets.chomp # Skip the first line
id_counts = gets.chomp.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values
puts(id_counts.any? { |c| c > 2 } ? -1 : id_counts.select { |c| c > 1 }.size)


m=gets.to_i
b=gets.split.map(&:to_i)
b.sort!.reverse!
a=[b.shift]
c=0
while b.size>0
  d=b.shift
  if c%2==0
    a.unshift d if a[0]>d
  else
    a<<d if a[-1]>d
  end
  c+=1
end
puts a.size,a*' '

#!/usr/bin/ruby

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

grouped = a.group_by {|x| x }.to_a.sort.reverse
tail = [grouped.shift[0]]
head = []
grouped.each do |val,seq|
  if seq.size >= 2
    tail << val
    head << val
  else
    tail << val
  end
end

r = head.reverse + tail
puts r.size
puts r.join(" ")

STDIN.gets
cards = STDIN.gets.strip.split.map {|x| x.to_i}
cards.sort!

stairs = []
leftovers = []
prev = 0

cards.each do |x|
  if x == prev
    leftovers << x
  else
    stairs << x
  end
  prev = x
end

leftovers.reverse.each do |x|
  stairs << x unless x == prev
  prev = x
end

STDOUT.puts stairs.size
STDOUT.puts stairs.reverse.join(" ")

m = $stdin.gets.to_i
cards = $stdin.gets.chomp.split(' ').map{|e| e.to_i}

cards.sort! {|a, b| b <=> a}
ladder = [cards.shift]
cards.each do |e|
    if e < ladder.last
        ladder.push e
    elsif e < ladder.first
        ladder.unshift e
    end
end

puts ladder.size
puts ladder.join(" ")

i = gets.to_i
cards = gets.split.map(&:to_i)

if cards.uniq.length == i
  puts i
  puts cards.sort.reverse.join ' '
else
  card_grps = cards.group_by(&:to_i).to_a.sort_by(&:first).map(&:last)
  result = []
  card_grps.each { |cs| result.push cs.pop }
  rev = card_grps.reverse
  rev.shift
  rev.each { |cs| result.push cs.pop unless cs.empty? }
  # p card_grps
  # p rev
  # p result
  puts result.count
  puts result.join ' '
end

cardNum = gets.chomp.to_i
cards = gets.chomp.split(' ').map {|i| i.to_i}
old = -1

if cards.uniq.length == cards.length
	puts "#{cards.length}"
	cards.reverse_each do |item|
		print "#{item} "
	end
else
	frontList = []
	backList = []
	cards.sort!

	cards.each do |item|
		frontList << item if old != item
		backList << item if old == item
		old = item
	end

#	print "#{frontList}"
#	print "#{backList}"

	backList.uniq!
	backList.delete_at(backList.length - 1) if frontList[frontList.length - 1] == backList[backList.length - 1]
	puts "#{frontList.length + backList.length}"

	frontList.each do |item|
		print "#{item} "
	end

	backList.reverse_each do |item|
		print "#{item} "
	end
end

STDIN.gets
cards = STDIN.gets.strip.split.map {|x| x.to_i}
cards.sort!

stairs = []
leftovers = []
prev = 0

cards.each do |x|
  if x == prev
    leftovers << x
  else
    stairs << x
  end
  prev = x
end

leftovers.reverse.each do |x|
  stairs << x unless x == prev
end

STDOUT.puts stairs.size
STDOUT.puts stairs.reverse.join(" ")

STDIN.gets
cards = STDIN.gets.strip.split.map {|x| x.to_i}
cards.sort!

stairs = []
leftovers = []
prev = 0

cards.each do |x|
  if x == prev
    leftovers << x
  else
    stairs << x
  end
  prev = x
end

leftovers.reverse.each do |x|
  stairs << x unless x == prev
  x = prev
end

STDOUT.puts stairs.size
STDOUT.puts stairs.reverse.join(" ")

STDIN.gets
cards = STDIN.gets.strip.split.map {|x| x.to_i}
cards.sort!

stairs = []
leftovers = []
prev = 0

cards.each do |x|
  if x == prev
    leftovers << x
  else
    stairs << x
  end
  prev = x
end

leftovers.reverse.each do |x|
  stairs << x unless x == prev
end

p stairs.size
p stairs.join(" ")

cardNum = gets.chomp.to_i
cards = gets.chomp.split(' ').map {|i| i.to_i}
old = -1

if cards.uniq.length == cards.length
	puts "#{cards.length}"
	cards.reverse_each do |item|
		print "#{item} "
	end
else
	frontList = []
	backList = []
	cards.sort!

	cards.each do |item|
		frontList << item if old != item
		backList << item if old == item
		old = item
	end

#	print "#{frontList}"
#	print "#{backList}"

	backList.delete_at(backList.length - 1) if frontList[frontList.length - 1] == backList[backList.length - 1]
	puts "#{frontList.length + backList.length}"

	frontList.each do |item|
		print "#{item} "
	end

	backList.reverse_each do |item|
		print "#{item} "
	end
end

cardNum = gets.chomp.to_i
cards = gets.chomp.split(' ').map {|i| i.to_i}

if cards.uniq.length == cards.length
	puts "#{cards.length}"
	cards.reverse_each do |item|
		print "#{item} "
	end
else
	tempCards = cards.uniq
	tempCards.sort!

	list = []

	tempCards.each do |item|
		#print "#{item} "
		list << item
		cards.delete_at(cards.index(item))
	end

	if list[list.length - 1] == cards[cards.length - 1]
		cards.delete_at(cards.length - 1)
	end

	cards.reverse_each do |item|
		list << item
	end

	puts "#{list.length}"
	list.each do |item|
		print "#{item} "
	end
end


num_pings = gets.to_i
num = {1 => 0, 2 => 0}
successful = {1 => 0, 2 => 0}
for i in 0...num_pings
	temp = gets.split.collect {|i| i.to_i}
	num[temp[0]] += 10
	successful[temp[0]] += temp[1]
end
(1..2).each do |n|
	if num[n]/2 <= successful[n]
		puts "LIVE"
	else
		puts "DEAD"
	end
end

n = gets.to_i
a = [[], []] # a b
n.times do
  s = gets.split.map(&:to_i)
  (s[0] == 1 ? a[0] : a[1]).push [s[1], s[2]]
end
puts (a[0].transpose[0].inject(:+) >= a[0].transpose[1].inject(:+) ? :LIVE : :DEAD)
puts (a[1].transpose[0].inject(:+) >= a[1].transpose[1].inject(:+) ? :LIVE : :DEAD)

n = gets.to_i
at = 0
ab = 0
bt = 0
bb = 0
n.times do
  t, x, y = gets.split.map(&:to_f)
  if t == 1.0
    at += x
    ab += x + y
  else
    bt += x
    bb += x + y
  end
end

if at / ab >= 0.5
  puts 'LIVE'
else
  puts 'DEAD'
end

if bt / bb >= 0.5
  puts 'LIVE'
else
  puts 'DEAD'
end

n=gets.to_i
a=[0,0]
c=[0,0]
n.times {
    t,s,f=gets.split.map(&:to_i)
    a[t-1]+=s
    c[t-1]+=1
}
for i in 0..1 do
    puts a[i]>=5*c[i] ? "LIVE" : "DEAD"
end

#! /usr/bin/ruby
$n = gets.to_i
$k, $p = [0,0,0], [0,0,0]
(1..$n).each do
  k, x, y = gets.split(' ').map(&:to_i)
  $k[k] += x
  $p[k] += 10
end
(1..2).each{ |i| if $k[i] * 2 >= $p[i] then puts "LIVE" else puts "DEAD" end }

n = gets.to_i
k = 0
x = 0.0
y = 0.0
n.times do
  a = []
  gets.split(" ").each do |st|
    a << st.to_i
  end
  if a[0] == 1 then
    x += a[1]/10.0
    k += 1
  else
    y += a[1]/10.0
  end
end
if (x/k >= 0.5) then
  puts "LIVE"
else
  puts "DEAD"
end

if (y/(n-k) >= 0.5) then
  puts "LIVE"
else
  puts "DEAD"
end

n = gets.to_i
a, c = [0,0], [0,0]
n.times do
  t,x,y = gets.split(' ').map(&:to_i)
  a[t-1] += x
  c[t-1] += 1
end
for i in 0..1 do
  puts a[i]>=5*c[i] ? "LIVE" : "DEAD"
end

n = gets.to_i
xx = Array.new(2,0)
yy = Array.new(2,0)
(1..n).each do
    t, x, y = gets.split.collect{|i| i.to_i}
    xx[t-1] += x
    yy[t-1] += y
end
(0..1).each do |i|
    if xx[i] >= yy[i]
        puts 'LIVE'
    else
        puts 'DEAD'
    end
end

n = gets.to_i

a = [0,0]
b = [0,0]

n.times do
  t,x,y = *(gets.split.map &:to_i)
  if t==1
    a[0] += x
    a[1] += x+y
  else
    b[0] += x
    b[1] += x+y
  end
end

puts a[0]>=a[1]/2 ? 'LIVE' : 'DEAD'
puts b[0]>=b[1]/2 ? 'LIVE' : 'DEAD'

n = gets.to_i
s = []
good1 = 0
good2 = 0
bad1 = 0
bad2 = 0
n.times do |i|
    s[i] = gets.split
    s[i] = s[i].collect {|j| j = j.to_i}
    if s[i][0] == 1 then
        good1 += s[i][1]
        bad1 += s[i][2]
    else
        good2 += s[i][1]
        bad2 += s[i][2]
    end
end
if good1 >= bad1 then
    puts "LIVE"
else
    puts "DEAD"
end
if good2 >= bad2 then
    puts "LIVE"
else
    puts "DEAD"
end

n = gets.to_i
s = [[0, 0], [0, 0]]
n.times do
  t, x, y = gets.split.map(&:to_i)
  s[t-1][0] += x
  s[t-1][1] += 10
end
s.each {|a| if a[0] >= a[1]/2 then puts "LIVE" else puts "DEAD" end}

n = gets.to_i
s = ["LIVE", "LIVE"]
n.times do
  t, x, y = gets.split.map(&:to_i)
  if x >= 5 then
    s[t-1] = "LIVE"
  else
    s[t-1] = "DEAD"
  end
end
s.each {|x| puts x}

n = gets.to_i
at = 0
ab = 0
bt = 0
bb = 0
n.times do
  c, t, b = gets.split.map(&:to_f)
  if c == 1.0
    at += t
    ab += b
  else
    bt += t
    bb += b
  end
end

if at / ab >= 0.5
  puts 'LIVE'
else
  puts 'DEAD'
end

if bt / bb >= 0.5
  puts 'LIVE'
else
  puts 'DEAD'
end

n = gets.to_i
s = ["DEAD", "DEAD"]
n.times do
  t, x, y = gets.split.map(&:to_i)
  if x >= 5 then
    s[t-1] = "LIVE"
  else
    s[t-1] = "DEAD"
  end
end
s.each {|x| puts x}


main = -> {
  t = int
  t.times do
      n, m = ints
      s = rep(n) { string.each_byte.to_a }
      min = INF
      s.combination(2) do |(a, b)|
          m = a.zip(b).sum { |x, y| (x - y).abs }
          min = [m, min].min
      end
      puts min
  end
}

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

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  ss = n.times.map { gets.chomp.bytes.map { |c| c - 97 } }
  answer = ss.flat_map.with_index do |s1, i|
    ss[i + 1..].map do |s2|
      m.times.map do |i|
        (s1[i] - s2[i]).abs
      end.sum
    end
  end.min
  p answer
end

def chardist(a, b)
  xs = [a.ord, b.ord].sort
  xs[1] - xs[0]
end

def dist(a, b)
  fail unless a.length == b.length
  m = a.length
  (0...m).map { |i| chardist(a[i], b[i]) }.sum
end

gets.to_i.times do
  n, _ = gets.split.map(&:to_i)
  ss = Array.new(n) { gets.strip }

  ans = nil
  n.times do |r|
    r.times do |l|
      cur = dist(ss[l], ss[r])
      ans = cur if !ans || cur < ans
    end
  end

  p ans
end

def diff(s, t)
  r = 0
  s.chars.each_with_index do |c, i|
    r += (c.ord - t[i].ord).abs
  end

  r
end

def main
  gets.to_i.times do
    n, m = gli
    a = []
    ans = 100_100_100_100
    n.times { a << gets.chomp }

    (0...n).each do |i|
      (i + 1...n).each do |j|
        ans = [ans, diff(a[i], a[j])].min
      end
    end
    puts ans
  end
end

MOD = 10**9 + 7
def gs = gets.chomp
def gi = gets.to_i
def gli = gets.split.map(&:to_i)
def cyesno(x) = x ? :Yes : :No
def uyesno(x) = x ? :YES : :NO

main


s = gets
n = s.length
sum = Array.new n + 1, 0
(2..n).each do |i|
  sum[i] += sum[i - 1]
  if s[i - 1] == s[i - 2]
    sum[i] += 1
  end
end
m = gets.to_i
m.times do
  l, r = gets.split.map &:to_i
  puts sum[r] - sum[l]
end

dp=[0]
pre=0
gets.chomp.each_byte{|b|
  dp<<dp.last+(pre==b ? 1 : 0)
  pre=b
}
gets.to_i.times{
  l,r=gets.split.map(&:to_i)
  puts dp[r]-dp[l]
}

#!/usr/bin/ruby1.9

text = STDIN.readline[0..-2]
counts = [0]
(1..text.length-1).each do |pos|
	count = counts[pos-1] + (text[pos] == text[pos-1] ? 1 : 0)
	counts.push(count)
end

query_num = STDIN.readline.to_i
query_num.times do
	start, finish = STDIN.readline.split.collect {|s| s.to_i }
	puts counts[finish-1]-counts[start-1]
end

s=gets.chomp.chars
ans=0

arr=[]
while s.size>1
	if s[0]==s[1] then
		ans=ans+1
	end
	arr<<ans
	s.shift
end
arr.unshift(0)
gets.to_i.times{
	a,b=gets.split.map{|e| e.to_i}
	puts arr[b-1]-arr[a-1]
}

string, _, *queries = STDIN.read.split("\n")

data = [0] * string.size
(1..string.size).each do |i|
  data[i] = data[i-1]
  data[i] += 1 if string[i-1] == string[i-2] && i > 1
end

queries.map! { |query| query.split.map(&:to_i) }.each do |query|
  i, j = query
  puts data[j] - data[i]
end

def main()
    st=gets.chomp
    ar=Array.new(st.length)
    for i in 0...(st.length)
        ar[i]=0
    end
    for i in 0...(st.length)
        if i>0
            ar[i]=ar[i-1]
        end
        if i+1<(st.length) && st[i].chr==st[i+1].chr
            ar[i]+=1
        end
    end
    tc=gets.chomp.to_i
    for i in 0...tc
        l,r=gets.chomp.split(" ").map{|i| i.to_i}
        l-=1
        r-=1
        if l-1==-1
            puts ar[r-1]
        else
            puts ar[r-1]-ar[l-1]
        end
    end
end
main()

def solution(s, list)
  acc = [0]
  prev = ""
  s.each_char do |char|
    acc << acc.last + (prev == char ? 1 : 0)
    prev = char
  end

  list.each do |item|
    puts acc[item[1]] - acc[item[0]]
  end
end

s = gets.strip
n = gets.strip.to_i

list = []

n.times do
  list << gets.split.map(&:to_i)
end

solution(s, list)

s = gets.split('')
a = [0]
(1..s.size - 1).each{|i| a << (s[i - 1] == s[i] ? a.last + 1 : a.last)}
m = gets.to_i
m.times do
    l, r = gets.split.map{|x| x.to_i}
    puts a[r - 1] - a[l - 1]
end

s = gets
m = gets.to_i

a = [0]
(1..s.length).each { |i| a[i] = s[i] == s[i-1] ? a[i-1] + 1 : a[i-1] }

m.times do
  l, r = gets.chomp.split(' ').map(&:to_i)
  puts a[r-1] - a[l-1]
end

s = gets.chomp
n = s.size
f = [0] * n
(1...n).each{|i|
  f[i] = ((s[i] == s[i-1])?1:0) + f[i-1]
}
gets.to_i.times {
  l, r = gets.split.map(&:to_i)
  puts f[r-1] - f[l-1]
}

s = ' ' + gets.chomp + ' '
a = Array.new(s.length+1, 0)

s.chars.each_with_index do |c, i|
  a[i] = 1 if s[i] == s[i+1]
  a[i] += a[i-1] unless i.zero?
end

1.upto(gets.to_i) do
  l, r = gets.split(' ').map(&:to_i)

  puts a[r] - a[l-1] - (a[r] - a[r-1])
end

a = gets.split('')
dem = []
dem[0] = 0
(1..a.length-1).each do |i|
    if a[i] == a[i-1]
        dem[i] = dem[i-1] + 1
    else
        dem[i] = dem[i-1]
    end
end
n = gets.to_i

(1..n).each do |k|
    b = gets.chomp.split(" ").map{|i| i.to_i}
    puts ( dem[b[1] -1] - dem[b[0]-1])
end

str = gets().chomp.split(//).unshift(' ').join
qnum = gets().to_i
qarr = []
qnum.times { qarr.push gets().chomp.split(/\ /).collect{|s| s.to_i} }
correct = []
1.upto(str.size - 2) { |i| correct[i] = true if str[i] == str[i+1] }
count = correct[1] ? [0,1] : [0,0]
2.upto(str.size - 1) { |i| count[i] = count[i-1] + (correct[i] ? 1 : 0) }
qarr.each { |arr| puts count[arr[1]-1] - count[arr[0]-1] }

str = gets()
str.chomp!
str = str.split(//).unshift(' ').join
qnum = gets().to_i
qarr = []
qnum.times do
  qarr.push gets().chomp.split(/\ /).collect{|s| s.to_i}
end
correct = []
1.upto(str.size - 2) do |i|
  correct[i] = true if str[i] == str[i+1]
end
if(correct[1])
  count = [0,1]
else
  count = [0,0]
end
2.upto(str.size - 1) do |i|
  count[i] = count[i-1] + (correct[i] ? 1 : 0)
end
qarr.each do |arr|
  puts count[arr[1]-1] - count[arr[0]-1]
end

s = gets.chomp
ps = Array.new(s.length + 1) { 0 }
s.length.times do |i|
  ps[i + 1] = ps[i] + (s[i] == s[i + 1] ? 1 : 0)
end
STDIN.readlines.drop(1).map(&:chomp).each do |str|
  q = str.split.map(&:to_i)
  p ps[q[1] - 1] - ps[q[0] - 1]
end

s = ' ' + gets.chomp + ' '
a = Array.new(s.length+1, 0)

s.chars.each_with_index do |c, i|
  a[i] = 1 if s[i] == s[i+1]
  a[i] += a[i-1] unless i.zero?
end

p a

1.upto(gets.to_i) do
  l, r = gets.split(' ').map(&:to_i)

  puts a[r] - a[l-1] - (a[r] - a[r-1])
end

s = gets()
s.chomp!
max = s.to_i
if s.size > 1 && s[0...s.size-1].to_i > max
  max = s[0...s.size-1].to_i
end
 if s.size > 2
  arr = s.split(//)
  arr.delete_at(-2)
  n = arr.join.to_i
  if n > max
    max = n
  end
end
puts max


gets.to_i.times { puts (1..gets.to_i / 2).sum { |i| i * i * 8 } }

1.upto(gets.to_i) do
  n = gets.to_i
  puts (1..n.div(2)).inject(0) { |x, i| x + 8 * i**2 }
end


GOOD = [4,8,15,16,23,42]

def solve(a)
  pos = {}
  GOOD.each { |g| pos[g] = [] }

  a.each_with_index do |e, i|
    pos[e].push(i)
  end
  # puts pos.inspect
  res = 0
  while true
    positions = []
    GOOD.each_with_index do |g, i|
      p = pos[g].shift
      while i > 0 && !p.nil? && !positions[i - 1].nil? && p < positions[i - 1]
        p = pos[g].shift
      end
      positions << p
    end
    break if positions.include?(nil)
    res += 1
  end
  a.size - (res * 6)
end

n = gets.strip.to_i
a = gets.strip.split(' ').map(&:to_i)
puts solve(a)

GOOD = [4,8,15,16,23,42]
def solve(a)
  h = {}
  GOOD.each { |g| h[g] = 0 }
  a.each do |e|
    if GOOD.include?(e)
      h[e] += 1
    end
  end
  mn = h.min_by { |k, v| v }
  return a.size if mn.nil?
  return a.size - (mn[1] * GOOD.size)
end

n = gets.strip.to_i
a = gets.strip.split(' ').map(&:to_i)
puts solve(a)


n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.chomp.reverse.to_i(2)
r_max = l_max = 10 ** 9
r_min = l_min = - 10 ** 9

4.upto(n-1) do |i|
  if b & 15 == 15
    if b & 16 == 0
      t = a[i-4,5].min - 1
      r_max = t if t < r_max
    #else
    #  t = a[i-4,5].min
    #  r_min = t if t > r_min
    end
  elsif b & 15 == 0
    if b & 16 == 16
      t = a[i-4,5].max + 1
      l_min = t if t > l_min
    #else
    #  t = a[i-4,5].max
    #  l_max = t if t < l_max
    end
  end
  b = b >> 1
  #p b.to_s(2)
end
print "#{l_min} #{r_max}"
#print "\nl is from #{l_min} to #{l_max}, r is from #{r_min} to #{r_max}"

n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.chomp.reverse.to_i(2)
r_max = l_max = 10 ** 9
r_min = l_min = - 10 ** 9

4.upto(n-1) do |i|
  if b & 15 == 15
    if b & 16 == 0
      t = a[i-4,5].min - 1
      r_max = t if t < r_max
    else
      t = a[i-4,5].min
      r_min = t if t > r_min
    end
  elsif b & 15 == 0
    if b & 16 == 16
      t = a[i-4,5].max + 1
      l_min = t if t > l_min
    else
      t = a[i-4,5].max
      l_max = t if t < l_max
    end
  end
  b = b >> 1
  #p b.to_s(2)
end
print "#{l_min} #{r_max}"
#print "\nl is from #{l_min} to #{l_max}, r is from #{r_min} to #{r_max}"


main = -> {
    t = int
    t.times do
        n, k = ints
        a = ints
        prefix_sum = [0]
        (n - 1).times do |i|
            x = (a[i] < a[i + 1] * 2) ? 1 : 0
            prefix_sum << (prefix_sum[-1] + x)
        end

        puts (0 ... n - k).count { |i| prefix_sum[i + k] - prefix_sum[i] == k }
    end
}

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

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]

def solve(a, k)
  b = []

  (0...a.size - 1).each do |i|
    if a[i] < a[i + 1] * 2
      b.push(1)
    else
      b.push(1)
      b.push(0)
    end
  end
  b.push(1)
  # p b

  (0...b.size).each do |i|
    b[i] = (i > 0 ? b[i] + b[i - 1] : b[0])
  end

  result = 0

  k += 1
  (b.size - k + 1).times do |l|
    v = (l > 0 ? b[l + k - 1] - b[l - 1] : b[l + k - 1])
    # p [b[l, k], v, l, l + k]
    if v == k
      result += 1
    end
  end

  result
end

n = gets.to_i

n.times do
  size, s = gets.split.map(&:to_i)
  array = Array.new(size)
  array = gets.split.map(&:to_i)

  p solve(array, s)
end

def solve
    q = gets.split.map(&:to_i)
    a = gets.split.map(&:to_i)
    n = q[0]
    k = q[1]
    pref = Array.new(n)
    pref[0] = 0
    i = 1
    while i < n
        pref[i] = pref[i - 1]
        if 2 * a[i] > a[i - 1]
            pref[i] = pref[i] + 1
        end
        i = i + 1
    end
    ans = 0
    i = 0
    while i + k < n
        curr = pref[i + k] - pref[i]
        if curr == k
            ans = ans + 1
        end
        i = i + 1
    end
    puts ans
end

gets.to_i.times do
    solve
end

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

  l = 0
  r = 1
  cnt = 0
  ans = 0
  flag = false
  while l < n-k
    while r < n && (r <= l || cnt < k)
      if a[r-1] >= 2*a[r]
        l = r
        cnt = 0
        flag = true
        r += 1
        break
      else
        cnt += 1
        r += 1
      end
    end
    next flag = false if flag
    ans += 1 if cnt == k
    cnt -= 1
    l += 1
  end

  puts ans
end

gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)
  counts = [0]
  xs.each_cons(2) do |a, b|
    if a < 2 * b
      counts[-1] += 1
    else
      counts << 0
    end
  end
  p counts.filter_map { |c| c - k + 1 if c >= k }.sum
end


def f n
  cur = 1
  while cur <= n
    cur = cur * 2
  end
  cur /= 2
  i = cur
  cur -= 1
  while i <= n && cur >= 0
    $a[cur] = i
    $a[i] = cur
    $ans += 2 * (cur ^ i)
    i += 1
    cur -= 1
  end
  if cur >= 1
    f cur
  end
end

n = gets.to_i
$ans = 0
$a = Array.new(n + 1, 0)
f n
p $ans
puts $a.join(' ')

h,n=[],gets.to_i+1
p n*(n-1)
now=2**(Math.log2(n-1).floor+1)-1
(0..n-1).reverse_each do |i|
  if i==now/2
    now/=2
  end
if(h[i])
  next
end
h[i]=now-i
h[now-i]=i
end
puts h.join ' '

h,n=[],gets.to_i+1
p n*(n-1)
now=2**(Math.log2(n-1).floor+1)-1
(0..n-1).reverse_each do |i|
  if i==now/2
    now/=2
  end
if(h[i])
  next
end
h[i]=now^i
h[now^i]=i
end
puts h.join ' '


n, m  = gets.split.map(&:to_i)
array = gets.split.map(&:to_i)
count = Array.new(m + 1, 0)
label = []
array.each_with_index do |x, i|
  if x <= m and count[x] < n / m
    count[x] += 1
  else
    label[i] = true
  end
end
changes = 0
(1..m).each do |i|
  while(count[i] < n / m) do
    j = label.index(true)
    array[j] = i
    label[j] = false
    count[i] += 1
    changes  += 1
  end
end
puts "#{n / m} #{changes}"
array.each { |x| print "#{x} " }
puts

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
goal = n / m
cnt = Array.new(m + 1, 0)
cgable = []
a.each_with_index do |x, i|
  if x <= m && cnt[x] < goal
    cnt[x] += 1
  else
    cgable << i
  end
end
ans = 0
(1..m).each do |i|
  while cnt[i] < goal
    a[cgable.shift] = i
    cnt[i] += 1
    ans += 1
  end
end
puts "#{goal} #{ans}"
puts a.join(' ')

n, m  = gets.split.map(&:to_i)
array = gets.split.map(&:to_i)
count, replace = Array.new(m + 1, 0), []
array.each_with_index do |x, i|
  if x <= m and count[x] < n / m
    count[x] += 1
  else
    replace[i] = true
  end
end
changes = 0
(1..m).each do |i|
  if(count[i] < n / m)
    array[j = replace.index(true)] = i
    replace[j] = false
    count[i] += 1
    changes  += 1
  end
end

puts "#{n / m} #{changes}"
array.each { |x| print "#{x} " }


t = gets.chomp().to_i

while t > 0
    n = gets.chomp().to_i
    a = Array.new(n + 1, 0)
    ind = gets.chomp.split(' ').map!{ |num| num.to_i}

    ans = -1

    ind.each do |i|
        a[i] +=  1
        if a[i] == 3
            ans = i
        end
    end

    puts ans
    t -= 1
end

t = gets.chomp().to_i

while t > 0
    n = gets.chomp().to_i
    a = Array.new(n + 1, 0)
    ind = gets.chomp.split(' ').map!{ |num| num.to_i}

    ans = -1

    ind.each do |i|
        a[i] +=  1
        if a[i] == 3
            ans = i
        end
    end

    puts ans
    a = nil
    t -= 1
end

def triple(element)
  len = element[0]
  arr = element[1]
  map = {}
  arr.each do |i|
    map.key?(i) ? map[i] += 1 : map[i] = 1
  end
  map.each do |k, v|
    if v >= 3
      return k
    end
  end
  return -1
end

t = gets.chomp.to_i

inputs = []
(0..t-1).each do |i|
  len = gets.chomp.to_i
  input = gets.chomp
  input = input.split(" ").map(&:to_i)
  inputs.push([len, input])
end

inputs.each do |element|
  p triple(element)
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  x = xs.tally.find { |x, count| count >= 3 }
  p x ? x[0] : -1
end

gets.to_i.times do
  gets
  a = gets.split.map(&:to_i).tally.sort_by { |_k, v| -v }
  if a[0][1] < 3
    puts(-1)
  else
    puts a[0][0]
  end
end


buf = gets.split(" ")
n = buf[0].to_i
k = buf[1].to_i
a = [0]
mod = 1000000007
for i in gets.split(" ")
    a << i.to_i
end
if k == 0 then
    1.upto(n) { |i|
        print a[i], " "
    }
else
    k -= 1
    now = 1
    sum = 0
    list = []
    1.upto(n) { |i|
        list << now % mod
        now *= i + k
        now /= i
    }
    1.upto(n) { |i|
        sum = 0
        i.downto(1) { |j|
            sum = sum + list[i - j] * a[j];
            sum %= mod
        }
        print sum, " "
    }
end

def modinv(num)
  a = num
  x = 0; nx = 1; b = 1000000007
  while(a>0) do
    q = b/a
    x -= q*nx
    x,nx = nx, x
    b -= q*a
    a,b = b,a
  end
  return x
end

n,k = gets.chomp.split.map(&:to_i)
array = gets.chomp.split.map(&:to_i)
mul = [1]
a = k
b = 1
1.upto(array.size - 1) do |i|
    mul[i] = (mul[i-1] * a * modinv(b)) % 1000000007
    a = a + 1
    b = b + 1
end
0.upto(array.size-1) do |i|
    ans = 0
    0.upto(i) do |j|
        ans += mul[i-j]*array[j]
    end
    print "#{ans%1000000007} "
end
puts

Mod = 1000000007
Pow = "111011100110101100101000000101"
Len = 30

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

def inverse(n)
  c = 1
  for i in 0...Len
    c = (c*c)%Mod
    if Pow[i] == '1'
      c = (c*n)%Mod
    end
  end
  return c
end

com = Array.new
com[0] = 1
s = Array.new
s[0] = a[0]
for i in 1...n
  com[i] = (com[i-1]*(k+i-1)*inverse(i))%Mod
  s[i] = 0
  for j in 0..i
    s[i] = (s[i]+a[i-j]*com[j])%Mod
  end
end

s.each {|x| print x, " "}

Mod = 1000000007
Pow = "111011100110101100101000000101"
Len = 30

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

def inverse(n)
  c = 1
  for i in 0...Len
    c = (c*c)%Mod
    if Pow[i] == '1'
      c = (c*n)%Mod
    end
  end
  return c
end

com = Array.new
com[0] = 1
s = Array.new
s[0] = a[0]
for i in 1...n
  com[i] = (com[i-1]*(k+i-1)*inverse(i))%Mod
  s[i] = 0
  for j in 0..i
    s[i] += a[i-j]*com[j]
  end
end

s.each {|x| print x, " "}

buf = gets.split(" ")
n = buf[0].to_i
k = buf[1].to_i
a = [0]
mod = 1000000007
for i in gets.split(" ")
    a << i.to_i
end
#p a
if k == 0 then
    1.upto(n) { |i|
        print a[i], " "
    }
else
    k -= 1
    now = 1
    sum = 0
    list = []
    1.upto(n) { |i|
        list << now % mod
        now /= i
        now *= i + k
    }
    1.upto(n) { |i|
        sum = 0
        i.downto(1) { |j|
            sum = sum + list[i - j] * a[j];
            sum %= mod
        }
        print sum, " "
    }
end

def modinv(num)
  a = num
  x = 0; nx = 1; b = 1000000007
  while(a>0) do
    q = b/a
    x -= q*nx
    x,nx = nx, x
    b -= q*a
    a,b = b,a
  end
  return x
end

n,k = gets.chomp.split.map(&:to_i)
array = gets.chomp.split.map(&:to_i)
mul = [1]
a = k
b = 1
1.upto(array.size - 1) do |i|
    mul[i] = (mul[i-1] * a * modinv(b)) % 1000000007
    a = a + 1
    b = b + 1
end
0.upto(array.size-1) do |i|
    ans = 0
    0.upto(i) do |j|
        ans += mul[i-j]*array[j]
    end
    print "#{ans} "
end
puts


parties = Array.new(10**5+1, nil)

n = gets.to_i

data = Array.new(n) { gets.chomp.split(' ').map(&:to_i) }

ans = 'YES'
data.each do |x, k|
    t = parties[k]
    if t
        if x > t + 1
            ans = 'NO'
            break
        elsif x == t + 1
            parties[k] = x
        end
    else
        if x == 0
            parties[k] = 0
        else
            ans = 'NO'
            break
        end
    end
end

puts ans

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

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

if k*(n/2)>=n
	puts -1
else
	puts n*k
	(1..n).each do |ind|
		(1..k).each do |ind2|
			puts "#{ind} #{(ind+ind2-1)%n+1}"
		end
	end
end

=end

array = [-1]* (10**5+1)

n=gets.to_i
i=0

while i<n
	x,k=gets.split.map(&:to_i)
	if x==array[k]+1
		array[k] = x
		i+=1
		next
	end

	if x>array[k]+1
		puts 'NO'
		exit 0
	end
	i+=1
end

puts 'YES'

prevmax = Hash.new -1
n = gets.to_i
n.times do
  x,k = gets.split.map &:to_i
  d = x - prevmax[k]
  if d == 1
    prevmax[k] += 1
  elsif d > 1
    puts "NO"
    exit
  end
end

puts "YES"

require 'set'
n = Integer gets
a = Array.new(10 ** 5)
cnt = Array.new(10 ** 5, 0)
for i in 0...10**5
  a[i] = [].to_set
end
for i in 0...n
  x, k = gets.split.map {|x| Integer x}
  k -= 1
  if !a[k].include?(x) && x != cnt[k]
    puts 'NO'
    exit
  end
  unless a[k].include?(x)
    a[k].add(cnt[k])
    cnt[k] += 1
  end
end
puts 'YES'

require 'set'
n = Integer gets
a = Array.new(10 ** 5, [].to_set)
cnt = 0
for i in 0...n
  x, k = gets.split.map {|x| Integer x}
  k -= 1
  if !a[k].include?(x) && x != cnt
    puts 'NO'
    exit
  end
  unless a[k].include?(x)
    a[k].add(cnt)
    cnt += 1
  end
end
puts 'YES'

require 'set'
n = Integer gets
a = Array.new(10 ** 5, [].to_set)
cnt = Array.new(10 ** 5, 0)
for i in 0...n
  x, k = gets.split.map {|x| Integer x}
  k -= 1
  if !a[k].include?(x) && x != cnt[k]
    puts 'NO'
    exit
  end
  unless a[k].include?(x)
    a[k].add(cnt[k])
    cnt[k] += 1
  end
end
puts 'YES'

require 'set'
n = Integer gets
a = Array.new(10 ** 5, [].to_set)
cnt = Array.new(10 ** 5, 0)
for i in 0...n
  x, k = gets.split.map {|x| Integer x}
  k -= 1
  if !a[k].include?(x) && x != cnt[k]
    puts 'NO'
    exit
  end
  unless a[k].include?(x)
    a[k].add(cnt[k])
  end
  cnt[k] += 1
end
puts 'YES'


DEBUG = false

class Board
  DELTA = [
    [ [-1, 0], [-1, 1],  [0, 1] ],
    [ [0, 1], [1, 1], [1, 0] ],
    [ [1, 0], [1, -1], [0, -1] ],
    [ [0, -1], [-1, -1], [-1, 0] ]
  ]
  attr_accessor :boards, :height, :width

  def initialize(height, width)
    self.boards = {}
    self.height = height
    self.width = width
  end

  def go(row, col)
    return false if row <= 0 || col <= 0 || row > self.height || col > self.width
    self.boards["#{row}_#{col}"] = true
    return true if self.end_game?(row, col)
    return false
  end

  def end_game?(row, col)
    if DEBUG
      puts "----- Check step -----"
      puts "row = #{row}, col = #{col}"
    end

    Board::DELTA.any? do |delta_set|

      if DEBUG
        puts "----- Check data set -----"
      end

      delta_set.all? do |delta_row, delta_col|
        if DEBUG
          puts "#{row + delta_row} #{col + delta_col}"
        end

        (row + delta_row > 0) &&
        (col + delta_col > 0) &&
        (row  + delta_row <= self.height) &&
        (col + delta_col <= self.width) &&
        (self.boards["#{row + delta_row}_#{col + delta_col}"] == true)
      end
    end
  end
end

height, width, steps = gets.strip.split(' ').map &:to_i

board = Board.new(height, width)

end_game_at = 0
1.upto(steps).each do |index|
  step_row, step_col = gets.strip.split(' ').map &:to_i
  end_game = board.go(step_row, step_col)
  if end_game
    end_game_at = index
    break
  end
end

puts end_game_at

class Board
  DELTA = [
    [ [-1, 0], [-1, 1],  [0, 1] ],
    [ [0, 1], [1, 1], [1, 0] ],
    [ [1, 0], [1, -1], [0, -1] ],
    [ [0, -1], [-1, -1], [-1, 0] ]
  ]
  attr_accessor :boards, :height, :width

  def initialize(height, width)
    self.boards = {}
    self.height = height
    self.width = width
  end

  def go(row, col)
    return false if row <= 0 || col <= 0 || row > self.height || col > self.width
    self.boards[row] ||= []
    self.boards[row][col] = true
    return true if self.end_game?(row, col)
    return false
  end

  def end_game?(row, col)
    Board::DELTA.any? do |delta_set|
      delta_set.all? do |delta_row, delta_col|
        (row + delta_row > 0) &&
        (col + delta_col > 0) &&
        (row  + delta_row <= self.height) &&
        (col + delta_col <= self.width) &&
        (!self.boards[row + delta_row].nil? && self.boards[row + delta_row][col + delta_col] == true)
      end
    end
  end
end

height, width, steps = gets.strip.split(' ').map &:to_i

board = Board.new(height, width)

end_game_at = 0
1.upto(steps).each do |index|
  step_row, step_col = gets.strip.split(' ').map &:to_i
  end_game = board.go(step_row, step_col)
  if end_game
    end_game_at = index
    break
  end
end

puts end_game_at

DEBUG = false

class Board
  DELTA = [
    [ [-1, 0], [-1, 1],  [0, 1] ],
    [ [0, 1], [1, 1], [1, 0] ],
    [ [1, 0], [1, -1], [0, -1] ],
    [ [0, -1], [-1, -1], [-1, 0] ]
  ]
  attr_accessor :boards, :height, :width

  def initialize(height, width)
    self.boards = {}
    self.height = height
    self.width = width
  end

  def go(row, col)
    return false if row <= 0 || col <= 0 || row > self.height || col > self.width
    self.boards["#{row}_#{col}".to_sym] = true
    return true if self.end_game?(row, col)
    return false
  end

  def end_game?(row, col)
    if DEBUG
      puts "----- Check step -----"
      puts "row = #{row}, col = #{col}"
    end

    Board::DELTA.any? do |delta_set|

      if DEBUG
        puts "----- Check data set -----"
      end

      delta_set.all? do |delta_row, delta_col|
        if DEBUG
          puts "#{row + delta_row} #{col + delta_col}"
        end

        (row + delta_row > 0) &&
        (col + delta_col > 0) &&
        (row  + delta_row <= self.height) &&
        (col + delta_col <= self.width) &&
        (self.boards["#{row + delta_row}_#{col + delta_col}".to_sym] == true)
      end
    end
  end
end

height, width, steps = gets.strip.split(' ').map &:to_i

board = Board.new(height, width)

end_game_at = 0
1.upto(steps).each do |index|
  step_row, step_col = gets.strip.split(' ').map &:to_i
  end_game = board.go(step_row, step_col)
  if end_game
    end_game_at = index
    break
  end
end

puts end_game_at

DELTA = [
  [ [-1, 0], [-1, 1],  [0, 1] ],
  [ [0, 1], [1, 1], [1, 0] ],
  [ [1, 0], [1, -1], [0, -1] ],
  [ [0, -1], [-1, -1], [-1, 0] ]
]
def go(boards, row, col, height, width)
  return if row <= 0 || col <= 0 || row > height || col > width
  boards[row] ||= []
  boards[row][col] = true
end

def end_game?(boards, row, col, height, width)
  DELTA.any? do |delta_set|
    delta_set.all? do |delta_row, delta_col|
      (row + delta_row > 0) &&
      (col + delta_col > 0) &&
      (row  + delta_row <= height) &&
      (col + delta_col <= width) &&
      (!boards[row + delta_row].nil? && boards[row + delta_row][col + delta_col] == true)
    end
  end
end

height, width, steps = gets.strip.split(' ').map &:to_i
boards = []
end_game_at = 0
1.upto(steps).each do |index|
  step_row, step_col = gets.strip.split(' ').map &:to_i
  go(boards, step_row, step_col, height, width)
  if end_game?(boards, step_row, step_col, height, width)
    end_game_at = index
    break
  end
end

puts end_game_at

DEBUG = false

class Board
  DELTA = [
    [ [-1, 0], [-1, 1],  [0, 1] ],
    [ [0, 1], [1, 1], [1, 0] ],
    [ [1, 0], [1, -1], [0, -1] ],
    [ [0, -1], [-1, -1], [-1, 0] ]
  ]
  attr_accessor :boards, :height, :width

  def initialize(height, width)
    self.boards = []
    self.height = height
    self.width = width
  end

  def go(row, col)
    return false if row <= 0 || col <= 0 || row > self.height || col > self.width
    self.boards[row] ||= []
    self.boards[row][col] = true
    return true if self.end_game?(row, col)
    return false
  end

  def end_game?(row, col)
    if DEBUG
      puts "----- Check step -----"
      puts "row = #{row}, col = #{col}"
    end

    Board::DELTA.any? do |delta_set|

      if DEBUG
        puts "----- Check data set -----"
      end

      delta_set.all? do |delta_row, delta_col|
        if DEBUG
          puts "#{row + delta_row} #{col + delta_col}"
        end

        (row + delta_row > 0) &&
        (col + delta_col > 0) &&
        (row  + delta_row <= self.height) &&
        (col + delta_col <= self.width) &&
        (!self.boards[row + delta_row].nil? && self.boards[row + delta_row][col + delta_col] == true)
      end
    end
  end
end

height, width, steps = gets.strip.split(' ').map &:to_i

board = Board.new(height, width)

end_game_at = 0
1.upto(steps).each do |index|
  step_row, step_col = gets.strip.split(' ').map &:to_i
  end_game = board.go(step_row, step_col)
  if end_game
    end_game_at = index
    break
  end
end

puts end_game_at

# File.open("5.txt","r") do |file|
	a=Array.new
	b=Array.new(1005)
	for i in 0..1004
		b[i]=Array.new(1005,0)
	end
	# str=file.gets.to_s
	str=gets.to_s
	a=str.split
	n=a[0].to_i
	m=a[1].to_i
	k=a[2].to_i
	h=0
	for r in 0..k
		# str=file.gets.to_s
		str=gets.to_s
		a=str.split
		i=a[0].to_i
		j=a[1].to_i
		b[i][j]=1
		if b[i-1][j-1]==1 and b[i-1][j]==1 and b[i][j-1]==1 and h==0
			h=r+1
		elsif b[i-1][j+1]==1 and b[i-1][j]==1 and b[i][j+1]==1 and h==0
			h=r+1
		elsif b[i+1][j-1]==1 and b[i+1][j]==1 and b[i][j-1]==1 and h==0
			h=r+1
		elsif b[i+1][j+1]==1 and b[i+1][j]==1 and b[i][j+1]==1 and h==0
			h=r+1
		end
	end
	if h==0
		puts 0
	else
		puts h
	end
# end

def checksquare(array,i,j)
	if array[i][j+1]==1 && array[i+1][j]==1 && array[i+1][j+1]==1
		return true
	end
	if array[i][j-1]==1 && array[i+1][j-1]==1 && array[i+1][j]==1
		return true
	end
	if array[i-1][j-1]==1 && array[i-1][j]==1 && array[i][j-1]==1
		return true
	end
	if array[i-1][j]==1 && array[i-1][j+1]==1 && array[i][j+1]==1
		return true
	end
	return false
end
n,m,k=gets.split().map{|i| Integer(i)}
pos=0
flag=1
array=Array.new
for i in 0..n
	array[i]=Array.new
	for j in 0..m
		array[i][j]=0
	end
end
start=1
while start<=k
	start+=1
	i,j=gets.split().map{|h| Integer(h)}
	array[i-1][j-1]=1
	if flag==1 && checksquare(array,i-1,j-1)==true
		pos=start-1
		flag=0
	end
end
puts pos

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

board = Array.new( h + 2 ) { |idx| Array.new( w + 2, 0 ) }

k.times do | t |
	y, x = gets.split.map( &:to_i )
	board[y][x] = 1

	2.times do | i |
		2.times do | j |
			f = true
			2.times do | k |
				2.times do | l |
					f &= board[ y - 1 + i + k ][ x - 1 + j + l ] == 1
				end
			end

			if f
				puts t + 1
				exit 0
			end
		end
	end
end

puts 0

# http://codeforces.com/contest/508/problem/A
n, m, k = gets.chomp.split.map(&:to_i)
f = []
(n + 2).times { f << Array.new(m + 2) }

ans = nil
k.times do |i|
  y, x = gets.chomp.split.map(&:to_i)
  f[y][x] = 1

  points = [
    [f[y][x], f[y][x + 1], f[y + 1][x], f[y + 1][x + 1]],
    [f[y][x], f[y][x - 1], f[y + 1][x], f[y + 1][x - 1]],
    [f[y][x], f[y][x + 1], f[y - 1][x], f[y - 1][x + 1]],
    [f[y][x], f[y][x - 1], f[y - 1][x], f[y - 1][x - 1]]
  ]

  if points.find { |arr| arr.map(&:to_i).inject(0, :+) == 4 }
    ans = i + 1 if ans.nil?
  end
end

puts ans.nil? ? 0 : ans

DEBUG = false

class Board
  DELTA = [
    [ [-1, 0], [-1, 1],  [0, 1] ],
    [ [0, 1], [1, 1], [1, 0] ],
    [ [1, 0], [1, -1], [0, -1] ],
    [ [0, -1], [-1, -1], [-1, 0] ]
  ]
  attr_accessor :boards, :height, :width

  def initialize(height, width)
    self.boards = {}
    self.height = height
    self.width = width
  end

  def go(row, col)
    return false if row <= 0 || col <= 0 || row > self.height || col > self.width
    self.boards[[row, col]] = true
    return true if self.end_game?(row, col)
    return false
  end

  def end_game?(row, col)
    if DEBUG
      puts "----- Check step -----"
      puts "row = #{row}, col = #{col}"
    end

    Board::DELTA.any? do |delta_set|

      if DEBUG
        puts "----- Check data set -----"
      end

      delta_set.all? do |delta_row, delta_col|
        if DEBUG
          puts "#{row + delta_row} #{col + delta_col}"
        end

        (row + delta_row > 0) &&
        (col + delta_col > 0) &&
        (row  + delta_row <= self.height) &&
        (col + delta_col <= self.width) &&
        (self.boards[[row + delta_row, col + delta_col]] == true)
      end
    end
  end
end

height, width, steps = gets.strip.split(' ').map &:to_i

board = Board.new(height, width)

end_game_at = 0
1.upto(steps).each do |index|
  step_row, step_col = gets.strip.split(' ').map &:to_i
  end_game = board.go(step_row, step_col)
  if end_game
    end_game_at = index
    break
  end
end

puts end_game_at

DEBUG = false

class Board
  DELTA = [
    [ [-1, 0], [-1, 1],  [0, 1] ],
    [ [0, 1], [1, 1], [1, 0] ],
    [ [1, 0], [1, -1], [0, -1] ],
    [ [0, -1], [-1, -1], [-1, 0] ]
  ]
  attr_accessor :boards, :height, :width

  def initialize(height, width)
    self.boards = {}
    self.height = height
    self.width = width
  end

  def go(row, col)
    return false if row <= 0 || col <= 0 || row > self.height || col > self.width
    self.boards["#{row}_#{col}"] = true
    if self.end_game?(row, col)
      return true
    end
    return false
  end

  def end_game?(row, col)
    if DEBUG
      puts "----- Check step -----"
      puts "row = #{row}, col = #{col}"
    end

    Board::DELTA.any? do |delta_set|

      if DEBUG
        puts "----- Check data set -----"
      end

      delta_set.all? do |delta_row, delta_col|
        if DEBUG
          puts "#{row + delta_row} #{col + delta_col}"
        end

        (row + delta_row > 0) &&
        (col + delta_col > 0) &&
        (row  + delta_row <= self.height) &&
        (row + delta_col <= self.width) &&
        (self.boards["#{row + delta_row}_#{col + delta_col}"] == true)
      end
    end
  end
end

height, width, steps = gets.strip.split(' ').map &:to_i

board = Board.new(height, width)

end_game_at = 0
1.upto(steps).each do |index|
  step_row, step_col = gets.strip.split(' ').map &:to_i
  end_game = board.go(step_row, step_col)
  if end_game
    end_game_at = index
    break
  end
end

puts end_game_at

# File.open("5.txt","r") do |file|
	a=Array.new
	b=Array.new(1005)
	for i in 0..1004
		b[i]=Array.new(1005,0)
	end
	# str=file.gets.to_s
	str=gets.to_s
	a=str.split
	n=a[0].to_i
	m=a[1].to_i
	k=a[2].to_i
	h=0
	for r in 0..k
		# str=file.gets.to_s
		str=gets.to_s
		a=str.split
		i=a[0].to_i
		j=a[1].to_i
		b[i][j]=1
		if b[i-1][j-1]==1 and b[i-1][j]==1 and b[i][j-1]==1
			h=r+1
		elsif b[i-1][j+1]==1 and b[i-1][j]==1 and b[i][j+1]==1
			h=r+1
		elsif b[i+1][j-1]==1 and b[i+1][j]==1 and b[i][j-1]==1
			h=r+1
		elsif b[i+1][j+1]==1 and b[i+1][j]==1 and b[i][j+1]==1
			h=r+1
		end
	end
	if h==0
		puts 0
	else
		puts h
	end
# end


t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  res = [4 * n]
  while res.size < n
    cur = res[-1]
    if cur <= 1
      break
    end
    while not(res.all? {|el| el.gcd(cur) > 1} &&
        res.all? {|el| el % cur > 0 && cur % el > 0})
      cur -= 1
    end
    res << cur
  end
  puts res.join(' ')
end

# frozen_string_literal: true

gets.chomp.to_i.times do |_m|
  n = gets.chomp.to_i
  (2 * n + 2).step(4 * n, 2) { |i| print i.to_s + ' ' }
  puts
end

$\ = " "
gets.to_i.times do
  n = gets.to_i
  cur = 2 * n + 2
  while cur <= 4 * n do
    print cur
    cur += 2
  end
  puts
end


gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i).sort
  ans=Float::INFINITY
  a.each_cons(3) do |i,j,k|
    ans=[ans,j-i+k-j].min
  end
  puts ans
end

def main
  t = inp.to_i
  t.times do
    n = inp.to_i
    a = mapint(inarr).sort!
    ans = 1 << 32
    0.upto(n - 1) do|i|
      (i + 1).upto(n - 1) do|j|
        (j + 1).upto(n - 1) do|k|
          dif = a[j] - a[i] + a[k] - a[j]
          ans = [ans, dif].min
        end
      end

    end
    puts ans

  end

end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

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

main


n, m, x, y = gets.split.map(&:to_i)
a = Array.new(n){gets.chomp.chars.to_a}
a = a.transpose.map{|_| _.count('#')}
w = [0] + [n * m] * m
b = [0] + [n * m] * m
x.upto(m) do |i|
    c = 0
    (i - 1).downto([i - y, 0].max) do |j|
        c += a[j]
        if i - j >= x
            w[i] = [w[i], b[j] + c].min
            b[i] = [b[i], w[j] + (i - j) * n - c].min
        end
    end
end
p [w.last, b.last].min


n = gets.to_i
str = gets.chomp
res = Array.new
if n%2 != 0
  k = i =1
  ctr =1
  res[n/2] = str[0]
  while(k <= n/2)
      if (i == 1)
        res[n/2-k] = str[ctr]
        i = -1
      else
        res[n/2+k] = str[ctr]
        i=1
      end
      ctr +=1
      if(ctr%2 != 0)
        k = k+1
      end
  end
else
  k=1
  i=1
  ctr = 0
  while(k <= n/2)
      if (i == 1)
        res[n/2-k] = str[ctr]
        i = -1
      else
        res[n/2+k-1] = str[ctr]
        i=1
      end
      ctr +=1
      if(ctr%2 == 0)
        k = k+1
      end
  end
end

print res.join("")

gets
s = gets.chomp

a = []
i = 0
if s.length % 2 != 0
  a.push(s[i])
  i += 1
end

while i < s.length
  a.unshift(s[i])
  a.push(s[i + 1])
  i += 2
end

puts a.join

n = gets.to_i
s = gets.chomp.chars

i = n
ans = ''
while !s.empty? do
  ss = s.shift
  if i.odd?
    ans += ss
  else
    ans = ss + ans
  end
  i += 1
end

puts ans

def g(ps)
	if ps.empty? == true then
		return []
	else
		p=ps.pop
		return (f(ps).unshift(p))
	end
end

def f(ps)
	if ps.empty? == true then
		return []
	else
		p=ps.pop
		return (g(ps).push(p))
	end
end

n=gets.to_i
s=gets.chomp

ps=(0..(n-1)).map{|i| i}
ps2=f(ps)
ps2.each{|i|
	print s[i]
}
print "\n"

n=gets.to_i
s=gets
r=''
for i in 0...n
    c=s[i]
    if (n+i).odd?
        r<<c
    else
        r=c+r
    end
end
puts r

n = gets.to_i
s = gets
a = []
i, d, sgn = (n - 1)/2, 0, n%2 == 0 ? -1 : 1
(0...n).each do |j|
  a[i] = s[j]
  d += 1
  sgn *= -1
  i += sgn*d
end
puts a.join

n = gets.chomp.to_i
word = gets.chomp
empty = " "*n
middle = n%2 == 0 ? (n/2) - 1: n / 2
left = middle - 1
right = middle + 1
empty[ middle ] = word[0]

if n % 2 == 1
    for i in 1 ... n
        if i % 2 == 1
            empty[ left ] = word[i]
            left -= 1
        else
            empty[ right ] = word[i]
            right += 1
        end
    end
else
    for i in 1 ... n
        if i % 2 == 0
            empty[ left ] = word[i]
            left -= 1
        else
            empty[ right ] = word[i]
            right += 1
        end
    end
end

puts empty

n = gets.to_i
s = gets.chomp.reverse
x, y = "", ""
s.chars.each_with_index do |c, i|
  if i % 2 == 0
    x << c
  else
    y << c
  end
end
puts y + x.reverse

n=gets.to_i
s=gets
r=''
for i in 0...n
    c=s[i]
    if i.odd?
        r=c+r
    else
        r<<c
    end
end
puts r

n = gets.to_i
s = gets
a = []
i, d, sgn = (n - 1)/2, 0, 1
(0...n).each do |j|
  a[i] = s[j]
  d += 1
  sgn *= -1
  i += sgn*d
end
puts a.join

n = gets.to_i
s = gets
a = [nil]*n
i, d, sgn = (n - 1)/2, 0, 1
(0...n).each do |j|
  a[i] = s[j]
  d += 1
  sgn *= -1
  i += sgn*d
end
puts a.join

n = gets.to_i
s = gets.strip
a = [nil]*n
i, d, sgn = (n - 1)/2, 0, 1
(0...n).each do |j|
  a[i] = s[j]
  d += 1
  sgn *= -1
  i += sgn*d
end
puts a.join


s = gets.strip
depth = 0
s.scan(/<(\/)?.*?>/) do
  depth -= 1 if $1
  puts " "*(2*depth)+$&
  depth += 1 unless $1
end

str=gets
input=str[1..str.length-3].split("><")
depth=0
input.each{|a|
        if a[0]=="/" then
                depth-=1
                print "  "*depth
        else
                print "  "*depth
                depth+=1
        end
        print "<"+a+">\n"
}

line = STDIN.gets()

def rec(arr)
  def _rec(_arr, ret, depth)
    if _arr.empty?
      return ret
    end

    e = _arr.shift.shift

    if e.start_with?('/') then
      depth -= 1
      ret << (['  '] * depth).join('') + '<' + e + '>'
      return _rec(_arr,ret,depth)
    else
      ret << (['  '] * depth).join('') + '<' + e + '>'
      return _rec(_arr,ret,depth+1)
    end

  end

  return _rec(arr, [], 0)
end

rec(line.scan(/<([a-z\/]+)>/)).each do |l|
  puts l
end

xml = gets.strip!

de = []
while xml.size > 0
  r = xml.index ">"
  if xml[1] == "/"
    puts " " * (de.size - 1) * 2 + xml[0..r]
    de.pop
  else
    puts " " * (de.size) * 2 + xml[0..r]
    de << 1
  end
  xml = xml[r + 1..-1]
end

str = gets.chomp
h = []

while str.size > 0
  res = str.index ">"
  if str[1] == "/"
    puts " " * (h.size - 1) *2 + str[0..res]
    h.pop
  else
    puts " " * (h.size) *2 + str[0..res]
    h << 1
  end
  str = str[res + 1..-1]
end

xml = gets.strip!

de = []
while xml.size > 0
  r = xml.index ">"
  if xml[1] == "/"
    puts " " * (de.size - 1) * 2 + xml[0..r]
    de.pop
  else
    puts " " * (de.size) * 2 + xml[0..r]
    de << 1
  end
  xml = xml[r + 1..-1]
  puts xml
  puts xml.size
end


t = gets.to_i
t.times do
  n, k = gets.split.map(&:to_i)
  puts (n >= k * k && (n - k) % 2 == 0) ? "YES" : "NO"
end

lines = []
for i in 0...gets.to_i
	lines << gets
end
def summable(n, k)
	return false if n - (k ** 2) < 0
	if n % 2 != k % 2
		return false
	end
	return true
end
def problem1(lines)
	for i in lines
		i = i.split(" ")
		summable(i[0].to_i, i[1].to_i) ? (puts "YES") : (puts "NO")
	end
end
problem1(lines)

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

  puts (n.modulo(2) == k.modulo(2) && n >= k ** 2) ? 'YES' : 'NO'
end

def main
  t = gets.chomp.to_i
  t.times { solve }
end

def solve
  # n = gets.chomp.to_i
  n, k = gets.chomp.split.map(&:to_i)
  # S = gets.chomp.chars

  if (k == 1)
    # Here, we should check the oddness of n
    if (n%2 == 1)
      print "YES\n"
      return
    else
      print "NO\n"
      return
    end
  end

  # Here, k >= 2
  # minimum k-1 sum is k_sum. [0, k-2]
  k_sum = (k-1)*(k-1)
  remain = n - k_sum
  if (remain % 2 == 1 && remain > (2*k - 3))
    print "YES\n"
  else
    print "NO\n"
  end
end

main

t = gets.to_i
t.times do
  n, k = gets.split.map(&:to_i)
  puts (n >= k && (n - k) % 2 == 0) ? "YES" : "NO"
end

lines = []
for i in 0...gets.to_i
	lines << gets
end
def summable(n, k)
	firstKSum = k ** 2
	if (n - k) % 2 == 1
		return false
	end
	return true
end
def problem1(lines)
	for i in lines
		i = i.split(" ")
		summable(i[0].to_i, i[1].to_i) ? (puts "YES") : (puts "NO")
	end
end
problem1(lines)

lines = []
for i in 0...gets.to_i
	lines << gets
end
def summable(n, k)
	if k == 1
		if n % 2 == 0
			return false
		end
		return true
	end
	for i in 2..k
		return true if n % i == k % i
	end
	return false
end
def problem1(lines)
	for i in lines
		i = i.split(" ")
		summable(i[0].to_i, i[1].to_i) ? (puts "YES") : (puts "NO")
	end
end
problem1(lines)

lines = []
for i in 0...gets.to_i
	lines << gets
end
def summable(n, k)
	if n % 2 != k % 2
		return false
	end
	return true
end
def problem1(lines)
	for i in lines
		i = i.split(" ")
		summable(i[0].to_i, i[1].to_i) ? (puts "YES") : (puts "NO")
	end
end
problem1(lines)


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

now = 1
a.each { |i| now += 1 if i >= now }
puts now - 1

len = gets.to_i
arr = gets.split(' ').map(&:to_i).sort()
for x in 1..len
    n = x
    while n > 0 && arr.length > 0
        n -= arr[0]
        arr.shift()
    end
    while arr.length > 0 && arr[0] <= x
        arr.shift()
    end
    if arr.length == 0
        puts "#{x}"
        break
    end
end
if arr.length > 0
    puts "#{len}"
end