n = gets.to_i
a = n.times.map{gets.chomp}
ans = true
n.times do |i|
  n.times do |j|
    c = 0
    c += 1 if i != 0 && a[i - 1][j] == 'o'
    c += 1 if j != 0 && a[i][j - 1] == 'o'
    c += 1 if i != n - 1 && a[i + 1][j] == 'o'
    c += 1 if j != n - 1 && a[i][j + 1] == 'o'
    ans = false if c % 2 != 0
  end
end
puts ans ? "YES" : "NO"

a=gets.chomp.to_i
array=[]
a.times do
array<<gets.chomp.split("")
end
api=0
i=0
loop do
k=0
loop do
temp=0
if (i-1!=-1) &&   array[i-1][k]=="o"
temp+=1
end
if (i+1!=a) && array[i+1][k]=="o"
temp+=1
end
if (k-1!=-1) && array[i][k-1]=="o"
temp+=1
end
if (k+1!=a) && array[i][k+1]=="o"
temp+=1
end
if temp%2!=0
api=1
break
end
k+=1
break if k==a
end
i+=1
break if i==a
break if api==1
end
if api==1
puts "NO"
else
puts "YES"
end

n = gets.chomp.to_i
board = []
n.times do
    line = gets.chomp.split("")
    board << line
end
adjacent = [[0, -1], [0, 1], [-1, 0], [1, 0]]
valid = true
(0...n).each do |row|
    break if !valid
    (0...n).each do |col|
        count = 0
        adjacent.each do |adj|
            i = row + adj[0]
            j = col + adj[1]
            next if i < 0 || j < 0 || i == n || j == n
            count += 1 if board[i][j] == 'o'
        end
        if count.odd?
            valid = false
            break
        end
    end
end
if valid
    puts "YES"
else
    puts "NO"
end

def f(x,y,m,n)
	if x<0 || x>=n || y<0 || y>=n
		return 0
	end
	return 1 if m[y][x]=="o"
	return 0
end

n=gets.to_i
m=[]
n.times{
	s=gets.chomp
	m<<s
}
ans="YES"
n.times{|y|
	n.times{|x|
		c1=f(x+1,y,m,n)+f(x,y+1,m,n)+f(x-1,y,m,n)+f(x,y-1,m,n)
		if c1%2==1 then
			ans="NO"
		end
	}
}
puts ans

n = gets.to_i
arr = []
ans = true

for i in 0...n
    arr[i] = gets.chomp
end

for i in 0...n
    count = 0
    for j in 0...n
        count += 1 if(j + 1 < n && arr[i][j + 1] == 'o')
        count += 1 if(j - 1 >= 0 && arr[i][j - 1] == 'o')
        count += 1 if(i + 1 < n && arr[i + 1][j] == 'o')
        count += 1 if(i - 1 >= 0 && arr[i - 1][j] == 'o')

        ans = false if count.odd?
    end
end

puts (ans ? "YES" : "NO")

n=gets.to_i
arr=[]
for i in 1..n
  arr[i]=gets
end
for i in 1..n
  for j in 0..n-1
    cnt=0
    if i>1&&arr[i-1][j]=='o'
      cnt+=1
    end
    if i<n&&arr[i+1][j]=='o'
      cnt+=1
    end
    if arr[i][j-1]=='o'
      cnt+=1
    end
    if arr[i][j+1]=='o'
      cnt+=1
    end
    if cnt&1==1
      puts "NO"
      exit(0)
    end
  end
end
puts "YES"

#$stdin = File.open "input.txt"

n = gets.to_i
a = gets(p).split "\n"

n.times do |i|
	n.times do |j|
		am = 0

		am += 1 if (i > 0) && (a[i - 1][j] == 'o')
		am += 1 if (i < n - 1) && (a[i + 1][j] == 'o')

		am += 1 if (j > 0) && (a[i][j - 1] == 'o')
		am += 1 if (j < n - 1) && (a[i][j + 1] == 'o')

		if am.odd?
			puts "NO"
			exit 0
		end
	end
end

puts "YES"

class Array
  def get_element(i, j, n)
    if i >= 0 && i < n && j >= 0 && j < n
      self[i][j]
    else
      nil
    end
  end
end

def is_good(i, j, s, n)
  t = 0
  t += 1 if s.get_element(i + 1, j, n) == 'o'
  t += 1 if s.get_element(i - 1, j, n) == 'o'
  t += 1 if s.get_element(i, j + 1, n) == 'o'
  t += 1 if s.get_element(i, j - 1, n) == 'o'
  # p [i, j, t]
  t % 2 == 0
end
def main
  n = gets.to_i
  s = n.times.map{gets}
  n.times do |i|
    n.times do |j|
      # p [i, j]
      return :NO unless is_good(i, j, s, n)
    end
  end
  :YES
end

puts main

t=gets.chomp.to_i
board=[]
def solve(board)
  len=board.size
  for i in 0...len
  	for j in 0...len
  		s=0
  	   if i-1>=0
         s+=1 if board[i-1][j]=='o'
  	   end
  	   if i+1<=len-1
  	   	 s+=1 if board[i+1][j]=='o'
  	   end
  	   if j-1>=0
  	   	 s+=1 if board[i][j-1]=='o'
  	   end
  	   if j+1<=len-1
  	   	s+=1 if board[i][j+1]=='o'
  	   end
  	   return 'NO' if s%2!=0
  	end
  end
  return "YES"
end
for i in 0...t
  board<<gets.chomp.split("").map {|x| x.to_s}

end
puts solve(board)

n = gets.to_i
matrix = []
n.times{
  matrix << gets.chomp.split("")
}
res = true
matrix.each_with_index do |row,i|
  row.each_with_index do |ele,j|
    adj_arr = []
    adj_arr << matrix[i+1][j] if matrix[i +1]
    adj_arr << matrix[i-1][j] if i-1 >= 0 && matrix[i-1]
    adj_arr << matrix[i][j+1]
    adj_arr << matrix[i][j-1]  if j-1 >= 0
    # puts "#{adj_arr}"
    res = false and break unless adj_arr.compact.count("o") % 2 == 0
  end
end

puts res ? "YES" : "NO"

require 'pp'
n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  j = 0
  while j < n && result
    c = 0
    SHIFT.each do |(k,z)|
      k += i
      z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  n.times do |j|
    c = 0
    c += 1 if i != 0 && a[i - 1][j] == 'o'
    c += 1 if j != 0 && a[i][j - 1] == 'o'
    c += 1 if i != n - 1 && a[i + 1][j] == 'o'
    c += 1 if j != n - 1 && a[i][j + 1] == 'o'
    if c % 2 != 0
      ans = false
    end
  end
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  break if !ans
  n.times do |j|
    c = 0
    c += 1 if i != 0 && a[i - 1][j] == 'o'
    c += 1 if j != 0 && a[i][j - 1] == 'o'
    c += 1 if i != n - 1 && a[i + 1][j] == 'o'
    c += 1 if j != n - 1 && a[i][j + 1] == 'o'
    if c % 2 != 0
      ans = false
      break
    end
  end
end
puts ans ? "YES" : "NO"

n=gets.to_i
arr=Array.new()
for i in 0...n do
    arr[i]=gets.to_s.chomp
end

flag=1
for i in 0...n do
    for j in 0...n do
        count=0
        if (i>0 && arr[i-1][j]=='o')
            count+=1;
        end
        if (i<n-1 && arr[i+1][j]=='o')
            count+=1;
        end
        if(j>0 && arr[i][j-1]=='o')
            count+=1;
        end
        if(j<n-1 && arr[i][j+1]=='o')
            count+=1;
        end

        if(count%2!=0)
            flag*=0
        end
    end
end

if (flag==1)
    puts "YES"
else
    puts "NO"
end

STEP_X = [0, 1, 0, -1]
STEP_Y = [1, 0, -1, 0]

n = gets.to_i
b = Array.new n
n.times do |i|
  b[i] = gets
end
valid = true
n.times do |i|
  n.times do |j|
    cnt = 0
    4.times do |k|
      tx = i + STEP_X[k]
      ty = j + STEP_Y[k]
      if 0 <= tx && tx < n then
        if 0 <= ty && ty < n then
          cnt += 1 if b[tx][ty] == 'o'
        end
      end
    end
    valid = false if cnt % 2 == 1
  end
end
if valid then
  puts 'YES'
else
  puts 'NO'
end

n=gets.to_i
board=[]
def solve(board)
	len=board.size
	for i in 0...len
		for j in 0...len
			s=0
			if i-1>=0
				s+=1 if board[i-1][j]=='o'
			end
			if i+1<=len-1
				s+=1 if board[i+1][j]=='o'
			end
			if j-1>=0
				s+=1 if board[i][j-1]=='o'
			end
			if j+1<=len-1
				s+=1 if board[i][j+1]=='o'
			end
			return 'NO' if s%2!=0
		end
	end
	return "YES"
end
n.times do
  board.push(gets.chomp.split('').map(&:to_s))
end
puts solve(board)

require 'pp'

n = STDIN.gets.chomp.to_i
m = []

n.times do |row|
  m.push STDIN.gets.chomp.split(//)
end

n.times do |i|
  n.times do |j|
    count = 0
    count += 1 if i != 0 &&   m[i-1][j] == 'o'
    count += 1 if i != n-1 && m[i+1][j] == 'o'
    count += 1 if j != 0 &&   m[i][j-1] == 'o'
    count += 1 if j != n-1 && m[i][j+1] == 'o'

    if count %2 != 0
      puts "NO"
      exit(0)
    end
  end
end
puts "YES"

require 'pp'

n = STDIN.gets.chomp.to_i
m = []

n.times do |row|
  m.push STDIN.gets.chomp.split(//)
end

n.times do |i|
  n.times do |j|
    count = 0
    count += 1 if i != 0 &&   m[i-1][j] == 'o'
    count += 1 if i != n-1 && m[i+1][j] == 'o'
    count += 1 if j != 0 &&   m[i][j-1] == 'o'
    count += 1 if j != n-1 && m[i][j+1] == 'o'

    if count %2 != 0
      p "NO"
      exit(0)
    end
  end
end
p "YES"

n = gets.to_i
arr = []
n.times do
  arr << gets.chomp
end
arr.each do |x|
  if x.scan(/o/).empty?
    puts "NO"
    exit
  end
end
puts "YES"

n=gets.to_i
ok="YES"
n.times{|i|
	s=gets.chomp
	n.times{|j|
		ok="NO" if (i+j)%2==1 && s[j]=="o"
	}
}
puts ok

n=gets.to_i
ok="YES"
n.times{|i|
	s=gets.chomp
	if i==0 || i==n-1 then
		ok="NO" if /.o./.match(s)
	else
		ok="NO" if /^o|o$/.match(s)
	end
}
puts ok

n=gets.to_i
ans="NO"
s=STDIN.read.split

hit=true
n.times{|i|
	hit=false if s[i][i]=="x"
}
ans="YES" if hit

hit=true
n.times{|i|
	hit=false if s[i][n-i-1]=="x"
}
ans="YES" if hit
puts ans

n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  while j < n && result do
    c = 0
    SHIFT.each do |(k,z)|
      k += i; z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

require 'pp'
n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

pp array
result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  while j < n && result
    c = 0
    SHIFT.each do |(k,z)|
      k += i; z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

require 'pp'
n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  while j < n && result
    c = 0
    SHIFT.each do |(k,z)|
      k += i; z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  n.times do |j|
    if a[i][j] == 'x'
      c = 0
      c += 1 if i != 0 && a[i - 1][j] == 'o'
      c += 1 if j != 0 && a[i][j - 1] == 'o'
      c += 1 if i != n - 1 && a[i + 1][j] == 'o'
      c += 1 if j != n - 1 && a[i][j + 1] == 'o'
      if c % 2 != 0
        ans = false
        break
      end
    end
  end
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  n.times do |j|
    if a[i][j] == 'o'
      c = 0
      c += 1 if i != 0 && a[i - 1][j] == 'x'
      c += 1 if j != 0 && a[i][j - 1] == 'x'
      c += 1 if i != n - 1 && a[i + 1][j] == 'x'
      c += 1 if j != n - 1 && a[i][j + 1] == 'x'
      if c % 2 != 0
        ans = false
        break
      end
    end
  end
end
puts ans ? "YES" : "NO"

(mx = (0...gets.to_i).map { |_| gets.chomp.split('') }).each_with_index { |a, i|
  a.each_with_index { |_, j|
    if [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]].map { |k| mx[k[0]] && mx[k[0]][k[1]] == 'o' ? 1 : 0 }.inject(:+).odd?
      puts 'NO'
      exit
    end
  }
}

puts 'YES'


def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length).map { |num| factors_of_two(num) }
  index_factors.sort!

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# code for tests:

number_of_tests = gets.chomp.to_i

number_of_tests.times do
  n = gets.chomp.to_i
  num_array = gets.chomp.split.map(&:to_i)

  puts solve(n, num_array)
end

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

def log2(n)
  cnt = 0
  while n % 2 == 0
    n /= 2
    cnt += 1
  end
  cnt
end

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

  rem = max(0, n - a.sum{ |ai| log2(ai) })

  ans = (1..n).map{ |i| log2(i) }.sort.reverse.sum do |i|
    if rem != 0 && rem - i >= 0
      rem -= i
      1
    else
      0
    end
  end

  puts rem == 0 ? ans : -1
end

def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length).map { |num| factors_of_two(num) }
  index_factors.sort

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# code for tests:

number_of_tests = gets.chomp.to_i

number_of_tests.times do
  n = gets.chomp.to_i
  num_array = gets.chomp.split.map(&:to_i)

  puts solve(n, num_array)
end

def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length).map { |num| factors_of_two(num) }
  index_factors.sort

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# # code for tests:

# number_of_tests = gets.chomp.to_i

# number_of_tests.times do
#   n = gets.chomp.to_i
#   num_array = gets.chomp.split.map(&:to_i)

#   puts solve(n, num_array)
# end

p solve(2, [3, 2])

def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length - 1).map { |num| factors_of_two(num) }
  index_factors.sort

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# code for tests:

number_of_tests = gets.chomp.to_i

number_of_tests.times do
  n = gets.chomp.to_i
  num_array = gets.chomp.split.map(&:to_i)

  puts solve(n, num_array)
end


n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
if !a.empty? && k>0
  puts 2+k,a.shift
  a.delete 2+k
end
puts a.shift,a.shift until a.empty?

n, k = gets.chomp.split.map(&:to_i)
ary = (1..2 * n).to_a
i = 0
k.times do
  ary[i], ary[i + 1] = ary[i + 1], ary[i]
  i += 2
end
puts ary.join(' ')

n, k = gets.split.map(&:to_i)
a = Array.new(2*n) { |i| i+1 }

k.times do |i|
  a[2*i] += 1
  a[2*i+1] -= 1
end

puts a.join(' ')

n,k=gets.chomp.split.map(&:to_i)
for i in 1..k
	print "#{2*i} #{2*i-1} "
end
for i in (k+1)..n
	print "#{2*i-1} #{2*i} "
end
puts

n,k = gets.split(" ").map(&:to_i)
arr = (1..(2 * n)).to_a
(1..k).each do |i|
	l,m = 2 * i - 1,(2 * i) - 2
	arr[l],arr[m] =  arr[m],arr[l]
end
puts arr.join(" ")

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

if k == 0 then
    a = (1..n*2).to_a.reverse
else
    a = [1, k+1] | (1..n*2).to_a.reverse
end

a.each do |x| print "#{x} " end and puts
#
# s = ms = 0
#
# a.each_index { |i|
#     if i%2 == 0 then
#         s += a[i] - a[i+1]
#         ms += (a[i]-a[i+1]).abs
#     end
# }
#
# print ms-s.abs, " ", k.*(2)

n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
puts a.delete(2+k),a.shift if !a.empty?&&k>0
puts a

n, k = gets.split.map { |x| 2 * x.to_i }
puts n.downto(1).each_slice(2).map { |x| x[0] <= k ? x.reverse : x } * ' '

line = gets.chomp.split(" ").map {|e| e.to_i}
second = 1
first = nil
nums = [second]
if line[1] != 0
  first = line[1] + 1
  nums = [first, second]
end

for i in 2..(line[0] * 2)
  if i != first
    nums.push i
  end
end

puts nums.join(" ")

n, k = gets.chomp.split.map(&:to_i)
ary = (1..2 * n).to_a
i = 0
k.times do
  ary[i], ary[i + 1] = ary[i + 1], ary[i]
  i += 2
end
p ary.join(" ")

n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
if !a.empty? && k>0
  puts a.shift,2+k
  a.delete 2+k
end
puts a.pop,a.pop until a.empty?

n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
if !a.empty? && k>0
  puts 2+k,a.shift
  a.delete 2+k
end
puts a.pop,a.pop until a.empty?


n,k=gets.split.map{|e| e.to_i}
xs=gets.split.map{|e| e.to_i}.sort.reverse
ans=0
while xs.empty? == false
	ans+=(xs.take(k).shift-1)*2
	xs=xs.drop(k)
end
puts ans

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

def solve(f, k)
    sum = 0
    cur = []
    while f.size > 0
        if f.size > k
            cur = f[0...k]
            f = f[k...f.size]
        else
            cur = f
            f = []
        end

        cur << 1
        #p cur

        floor = 1
        cur.each do |newfloor|
            sum += (newfloor - floor).abs
            floor = newfloor
        end
    end
    return sum
end

p [solve(f.dup, k), solve(f.reverse, k)].min

nums = gets.chomp.split
n = Integer(nums[0])
k = Integer(nums[1])
nums2 = gets.chomp.split
f = []
for i in 0...n
    f[i] = Integer(nums2[i])
end

f.sort!
m = n - 1
total = 0
while m >= 0 do
    total += 2*(f[m] - 1)
    m -= k
end
print total

nk=gets.chomp.split(" ").map {|x| x.to_i}
line=gets.chomp.split(" ").map {|x| x.to_i}
line.sort!
last=nk[0]
times=0
while last>0
  times+=(line[last-1]-1)*2
  last-=nk[1]
end
puts times

n, limit = gets.split.map(&:to_i)
goal = gets.split.map(&:to_i).sort
step = 0
position = 1
n.times do
	limit.times do
		step += (goal[-1] - position).abs
		position = goal[-1]
		goal.pop
		break if goal == []
	end
	step += position - 1
	break if goal == []
	position = 1
end
puts step

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

_, k = read_next_line
sum = 0
read_next_line.sort.reverse.each_slice(k) { |p|
  sum += (p.first - 1) * 2
}
puts sum

n, k = gets.split.map(&:to_i)
f_list = gets.split.map(&:to_i)
f_list.sort!.reverse!
sum = 0
((f_list.size - 1) / k + 1).times do |i|
  sum += (f_list[i * k, k].max - 1) * 2
end
puts sum

def read_line; gets.chomp.split.map(&:to_i);end
a,b = read_line
s = 0
read_line.sort.reverse.each_slice(b){|p| s+=(p[0]-1)*2}
puts s

a,b=0,gets.split.map(&:to_i)
gets.chomp.split.map(&:to_i).sort.reverse.each_with_index{|x,i| a+=(x-1)*2 if i%b[1]==0}
puts a

n, k = STDIN.readline.split.map {|s| s.to_i }
targets = STDIN.readline.split.map {|s| s.to_i }.sort.reverse
cost = 0
pos = 0
while pos < n
	cost += 2*targets[pos] - 2
	pos += k
end
puts cost

nk=gets.chomp.split(" ").map {|x| x.to_i}
line=gets.chomp.split(" ").map {|x| x.to_i}
line.sort!
last=nk[0]
times=0
while last>=0
  times+=(line[last-1]-1)*2
  last-=nk[1]
end
puts times


def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end

  if i & (i-1) == 0
    z+=2
  end

  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

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

def foo(x)
	return 0 if(x==0)
	xlog = (Math.log(x)/Math.log(2)).to_i
	ans=0
	if(xlog%2==1)
		#our last number is odd
		asd = (xlog-1)/2
		numberofodds = (((4**(asd+1))-1)/3)
		numberofevens = x-numberofodds
		ans += numberofevens**2 + numberofevens
		ans += numberofodds**2
		return ans
	else
		#our last number is even
		asd = (xlog-2)/2
		numberofevens = 2*(((4**(asd+1))-1)/3)
		numberofodds = x-numberofevens
		ans += numberofevens**2 + numberofevens
		ans += numberofodds**2
		return ans
	end
end

lr = gets.chomp.split(" ").map(&:to_i)

l = lr[0]
r = lr[1]
puts (foo(r)-foo(l-1))%1000000007

require 'scanf'
class T
  attr_accessor :lb
  attr_accessor :le
  attr_accessor :vb
  attr_accessor :ve
  attr_accessor :val
  def initialize(a1,a2,a3,a4,a5)
    @lb,@le,@vb,@ve,@val=a1,a2,a3,a4,a5
  end
end
dt=[]
$shit=1000000007
dt[1]=T.new(1,1,1,1,1)
dt[2]=T.new(2,3,2,4,6)
def size(a1,a2,a3)
  return (((a1+a2)*a3)/2)%$shit
end
for i in 3..100
  dt[i]=T.new(dt[i-1].le+1,2**i-1,dt[i-2].ve+2,dt[i-2].ve+2**i,1)
  dt[i].val=size(dt[i].vb,dt[i].ve,dt[i].le-dt[i].lb+1)
end
a,b=scanf("%d%d")
a-=1
l,r=0,0
for i in 1..100
  if dt[i].le<a
    l+=dt[i].val
  else
    l+=size(dt[i].vb,dt[i].vb+2*(a-dt[i].lb),a-dt[i].lb+1)
    break
  end
end
for i in 1..100
  if dt[i].le<b
    r+=dt[i].val
  else
    r+=size(dt[i].vb,dt[i].vb+2*(b-dt[i].lb),b-dt[i].lb+1)
    break
  end
end
x=r-l
while x<$shit
  x+=$shit
end
#puts l
#puts r
puts x%$shit

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  return 10 if i == 4
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  puts z
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  q = Math.log2(i).ceil
  z = 0
  q.times do
    puts z
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end

  p(i) + p(i-z)
end

l, r= gets.split(/\s+/).map(&:to_i)
puts (S(l)-S(r-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  puts z
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  return 10 if i == 4
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007


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

  max_j = [m, n/k].min

  max_j -= (m - max_j).div(k-1) + ((m - max_j).modulo(k-1).zero? ? 0 : 1)

  puts max_j
end

cases = gets.to_i
res = []
cases.times do |i|
  n, m, k = gets.split.map(&:to_i)
  c = n/k
  if c >= m
    res << m
  else
    res << c - (m-c)/(k-1) - ( ((m-c)%(k-1) > 0) ? 1 : 0)
  end
end

res.each { |r| p r }

gets.to_i.times do
    n, m, k = gets.split.map &:to_i
    x = [n/k, m].min
    y = (m - x + k - 2)/(k - 1)
    puts x - y
end

t = gets.to_i

1.upto t do
  n, m, k = gets.split.map(&:to_i)
  a = [m, n/k].min
  b = m - a
  puts (a - ((b + k - 2)/(k-1)))
end

t = gets.to_i

1.upto t do
  n, m, k = gets.split.map(&:to_i)
  if m == 0
    puts 0
  else
    if (n/k == 1)
      if m == 1
        puts 1
      else
        puts 0
      end
    else
      if (m > n/k)
        z = n/k
        y = m-z
        if y % (k-1) == 0
           puts (z - y/(k+1))
        else
           puts (z - (y/(k-1)+1))
        end
      else
        puts m
      end
    end
  end
end


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

a, b, c, d, s = 0, 0, 0, 0, 0

q.each.with_index do |e, i|
  c += e if i.even?
  d += e if i.odd?
end

q.each.with_index do |e, i|
  c -= e if i.even?
  d -= e if i.odd?

  s += 1 if a + c == b + d

  b += e if i.even?
  a += e if i.odd?
end

puts s

def good(sweets)
  count = 0
  evens = 0
  odds  = 0

  sweets.each_with_index do |x, i|
    if i.even? then evens += x else odds += x end
  end

  sweets.each_with_index do |x, i|
    if i.even? then evens -= x else odds -= x end

    count += 1 if evens == odds

    if i.even? then odds += x else evens += x end
  end
  count
end

gets
sweets = gets.chomp.split.map(&:to_i)
puts(good(sweets))


class Advisor
  def self.get_advice(src)
    @@norm_work_cnt=src[0][1]
    src[1].unshift @@norm_work_cnt
    walks=src[1].map {|x| x=*x}.each_cons(2).to_a.each {|x| x[1][0]= get_walks_needed(x[0][0],x[1][0])}.
        map {|x| x=x[1]}.flatten
    src[1].shift
    result = [] << walks.zip(src[1]).map {|x,y| x-y}.reduce(:+) << walks
    print_result result
    result
  end
  def self.get_walks_needed(walks_prev,walks_current)
    tmp = @@norm_work_cnt-walks_prev-walks_current
    (tmp>=0)?(tmp+walks_current) : walks_current
  end
  def self.print_result(res)
    puts res[0],res[1].join(' ')
  end
end

if __FILE__ == $0
  src = $stdin.each_line.map {|x| x.split.map {|x| x=(x.to_i)}}
  Advisor.get_advice(src)
end

n,k,*a=$<.read.split.map(&:to_i)
b=a[s=0]
(1...n).each{|i|
if b+a[i]<k
s+=k-b-a[i]
a[i]=k-b
end
b=a[i]
}
p s
puts a*' '

n, k = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)
inc = 0
(0...n).each_cons(2).each do |i, j|
  if a[i] + a[j] < k
    inc += k - a[i] - a[j]
    a[j] = k - a[i]
  end
end
puts inc
puts a.join(" ")

n,k=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
arr=[]
sum=0
while x.size>1
	d=x[0]+x[1]
	if k>d then
		x[1]+=(k-d)
		sum+=(k-d)
	end
	arr<<x[0]

	x.shift
end
arr<<x[0]

puts sum
puts arr*" "

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

res=0
b=[a[0]]
for i in 1..n-1
    t=k-a[i]-b[i-1]
    b[i]=a[i]
    if t>0
        res+=t
        b[i]+=t
    end
end
puts res
puts b*' '

n,k = gets.split(' ').map {|e| e.to_i}
arr = gets.split(' ').map {|e| e.to_i}
i = 1
tmp = 0
while i < n
	if arr[i]+arr[i-1]<k
		tmp1 = arr[i]
		arr[i]+=k-arr[i-1]-arr[i]
		tmp+=k-tmp1-arr[i-1]
	end
	i+=1
end
puts tmp
puts arr.join(' ')

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

ad = 0
a.each_with_index do |t, i|
  next if i == 0
  dif = k > (a[i - 1] + a[i]) ? k - (a[i - 1] + a[i]) : 0
  ad += dif
  a[i] += dif
end
puts ad
puts a.join " "

def solution(arr, k)
  counter = 0
  for i in 0...arr.size-1 do
    add = k - arr[i+1] - arr[i]
    if  add > 0
      arr[i+1] += add
      counter += add
    end
  end
  puts counter
  puts arr
end

_, k = gets.split.map{ |i| i.to_i }
arr = gets.split.map(&:to_i)

solution(arr, k)

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 0
(n - 1).times do |i|
  c = k - a[i] - a[i + 1]
  if c > 0
    ans += c
    a[i + 1] += c
  end
end
puts ans, a.join(' ')

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 0
(n - 1).times do |i|
  if a[i] + a[i + 1] < k
    c = k - a[i] - a[i + 1]
    ans += c
    a[i + 1] += c
  end
end
puts ans, a.join(' ')

n,k=gets.split(" ").map(&:to_i)
a=gets.split(" ").map(&:to_i)
b=[a[0]]
delta=0
for i in 1...n
	b<<[a[i],k-b[i-1]].max
	if k-b[i-1]>a[i]
		delta+=k-b[i-1]-a[i]
	end
end
puts delta
puts b.join(" ")

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

tot = 0
for i in 0 ... n - 1
  if a[i] + a[i + 1] < k
    add = k - a[i] - a[i + 1]
    a[i + 1] += add
    tot += add
  end
end
puts tot
puts a.join(' ')

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

(0...n).each_cons(2).each do |i, j|
  if a[i] + a[j] < k
    a[j] = k - a[i]
  end
end
puts a.join(" ")

n,k=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
arr=[]
sum=0
while x.size>1
	d=x[0]+x[1]
	if k>d then
		x[1]+=(k-d)
		sum+=(k-d)
	end
	arr<<x[0]

	x.shift
end
if n>1 then
	arr<<x[0]
else
	if x[0]>=k then
		arr<<x[0]
	else
		sum=k-x[0]
		arr<<k
	end
end
puts sum
puts arr*" "


n, m, k = gets.split(" ").map(&:to_i)
a = gets.split(" ").map(&:to_i)
b = gets.split(" ").map(&:to_i)
ans = 0
ind = Array.new(n + 1)
for i in 0...n
  ind[a[i]] = i
end
b.each do |x|
  j = ind[x]
  ans += (j / k) + 1
  if j != 0
    a[j - 1], a[j] = a[j], a[j - 1]
    ind[a[j]] = j
    ind[a[j - 1]] = j - 1
  end
end
puts ans

n, m, k = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}
mark = Hash.new
(0...a.size).each{|i| mark[a[i]] = i}
ans = 0
gets.split.each do |b|
    tmp = mark[b.to_i]
    if tmp != 0
        ans += tmp/k+1
        mark[a[tmp]] -= 1
        mark[a[tmp-1]] += 1
        a[tmp-1], a[tmp] = a[tmp], a[tmp-1]
    else
        ans += 1
    end
end
puts ans

n, m, k = gets.split.map &:to_i
a = gets.split.map &:to_i
b = gets.split.map &:to_i

h = {}

a.each_with_index { |value, index| h[ value ] = index }

ans = 0
b.each do |v|
  pos = h[ v ]
  page = (pos + k) / k
  ans += page

  if pos != 0
    a[pos], a[pos - 1] = a[pos - 1], a[pos]
    h[ a[pos] ] = pos
    h[ a[pos - 1] ] = pos - 1
  end
end
puts ans

if __FILE__ == $0
  END { main }
end

def main
  app_count, up_app_count, apps_per_screen = gets.strip.split(" ").map(&:to_i)
  apps = gets.strip.split(" ").map(&:to_i)
  up_apps = gets.strip.split(" ").map(&:to_i)

  app_orders = get_app_orders(apps)

  gesture_count = 0

  up_apps.each do |app|
    app_order = get_app_order(app, app_orders)
    gesture_count += get_gesture_count(app_order, apps_per_screen)
    up_app(app, apps, app_orders)
  end

  puts gesture_count
end

def get_app_orders(apps)
  orders = []
  apps.each_with_index do |app, index|
    orders[app-1] = index
  end
  orders
end

def get_gesture_count(app_order, apps_per_screen)
  app_order / apps_per_screen + 1
end

def up_app(app, apps, app_orders)
  app_order = get_app_order(app, app_orders)
  return if app_order == 0

  prev_app = apps[app_order - 1]
  app_orders[prev_app - 1] += 1
  app_orders[app - 1] -= 1

  apps[get_app_order(app, app_orders)] = app
  apps[get_app_order(prev_app, app_orders)] = prev_app
end

def get_app_order(app, app_orders)
  app_orders[app - 1]
end

n, m, k = gets.split(" ").map(&:to_i)
a = gets.split(" ").map(&:to_i)
b = gets.split(" ").map(&:to_i)
ans = 0
ind = Array.new(n + 1)
for i in 0...n
  ind[a[i]] = i
end
b.each do |x|
  j = ind[x]
  ans += (j / k) + 1
  a[j - 1], a[j] = a[j], a[j - 1] if j != 0
  ind[a[j]] = j
  ind[a[j - 1]] = j - 1
end
puts ans

n, m, k = gets.split(" ").map(&:to_i)
a = gets.split(" ").map(&:to_i)
b = gets.split(" ").map(&:to_i)
ans = 0
ind = Array.new(n + 1)
for i in 0...n
  ind[a[i]] = i
end
m.times do |i|
  ans += (ind[b[i]] / k) + 1
  tmp = a[ind[b[i]] - 1]
  a[ind[b[i]] - 1] = a[ind[b[i]]]
  a[ind[b[i]]] = tmp
  ind[b[i]] -= 1
  ind[tmp] += 1
end
puts ans


input = File.open("input.txt", "r")
output = File.open("output.txt", "w")

n = input.gets.to_i
x, a, b, c = input.gets.split.map(&:to_i)

team = Array.new(n)
for i in 0...n
  team[i] = input.gets.split
end

team.sort! {|x, y| y[1].to_i <=> x[1].to_i}

sep = n/4
for i in 0...n/4 do
  output.puts "Group #{('A'.ord+i).chr}:"
  x = (x*a + b)%c
  t1 = x%sep
  output.puts team[t1][0]

  x = (x*a + b)%c
  t2 = sep + x%sep
  output.puts team[t2][0]

  x = (x*a + b)%c
  t3 = sep*2 + x%sep
  output.puts team[t3][0]

  x = (x*a + b)%c
  t4 = sep*3 + x%sep
  output.puts team[t4][0]

  team.delete_at(t1)
  team.delete_at(t2-1)
  team.delete_at(t3-2)
  team.delete_at(t4-3)

  sep -= 1
end

input.close
output.close

if ENV['USER'] != 'watashi'
  $stdin = open('input.txt')
  $> = open('output.txt', 'w')
end

class RNG
  def initialize(x, a, b, c)
    @x = x
    @f = ->(x){(x * a + b) % c}
  end

  def next
    @x = @f[@x]
  end
end

n = gets.to_i
r = RNG.new(*gets.split.map(&:to_i))
a = Array.new(n){
  x, y = gets.split
  [y.to_i, x]
}
n /= 4
a = a.sort.reverse.each_slice(n).to_a
g = 'Group A:'
n.downto(1){|m|
  puts g
  g.succ!
  a.each{|b|
    t = b[r.next % m]
    puts t[1]
    b.delete(t)
  }
}


gets.to_i.times do
  s = gets.chomp
  as = 0
  s.chars.each do |ch|
    case ch
    when "A" then as += 1
    when "B" then as -= 1
    end
    break if as < 0
  end
  ok = as >= 0 && s[-1] == "B"
  puts ok ? "YES" : "NO"
end

gets.to_i.times do
  a = gets.chomp.split('')
  flag = (a[a.size-1] == 'B')
  bcount = 0
  acount = 0
  0.upto(a.size-1) do |i|
    if (a[i]=='B') then
      bcount += 1
    else
      acount += 1
    end
    flag = false if bcount>acount
  end
  puts ((bcount > 0) && flag) ? "YES" : "NO"
end

gets.to_i.times do
  a = gets.chomp.split('')
  flag = true
  bcount = 0
  acount = 0
  0.upto(a.size-1) do |i|
    if (a[i]=='B') then
      bcount += 1
      if (bcount>acount) then
        flag = false
      end
    else
      acount += 1
    end
  end
  puts ((bcount > 0 ) && flag) ? "YES":"NO"
end

gets.to_i.times do
  a = gets.chomp.split('')
  flag = true
  bcount = 0
  acount = 0
  0.upto(a.size-1) do |i|
    if (a[i]=='B') then
      bcount += 1
      if (bcount>acount)
        flag = false
      end
    else
      acount += 1
    end
  end
  puts ((bcount > 0 ) & flag) ? "YES":"NO"
end


n = gets.to_i

gs = gets.chomp

origingold = false
inagold = false
if gs[0]=="G"
  inagold=true
  origingold = true
end

newarr = []

count = 1

gs.each_char.with_index do |ch,i|
  next if i==0
  if inagold
    if ch=="S"
      newarr.push count
      count = 1
      inagold = false
    else
      count+=1
    end
  else
    if ch=="G"
      newarr.push count
      count = 1
      inagold = true
    else
      count += 1
    end
  end
end

newarr.push count

def checkifgold(i,origin)
  if origin
    return i%2==0
  else
    return i%2==1
  end
end

maxgold = 0
maxtwogold = 0
prevgold = 0
wasaone = false

#p newarr

newarr.each_with_index do |x,i|

#  puts wasaone
#  puts x
#  puts i
#  puts checkifgold(i,origingold)

  if checkifgold(i,origingold)
    maxgold = x if x>maxgold
    if wasaone
      asd = prevgold+x+1
      maxtwogold = asd if asd>maxtwogold
    end
    prevgold = x
  else
    if x==1
      wasaone = true
    else
      wasaone = false
    end
  end

end

if newarr.length==1
  if origingold
    puts maxgold
  else
    puts 0
  end
  exit
end

if newarr.length==2
  puts maxgold
  exit
end

if newarr.length==3
  if not origingold
    puts maxgold
    exit
  end
end

if newarr.length<5 or (newarr.length==5 and not origingold)
  puts [maxgold+1,maxtwogold-1].max
  exit
end
#puts maxgold
#puts maxtwogold
puts [maxgold+1,maxtwogold].max

gets
a=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)
res=0
1.step(a.size, 2) do |i|
    break if !a[i]
    t=a[i]+(a.size>4 ? 1 : 0)
    res=a[i] if res<a[i]
    if a[i+2]
        t=[a[i], a[i+2]].max+1
        res=t if res<t
        t=a[i]+(a[i+1]==1 ? a[i+2] : 1)
        res=t if res<t
        res=t+1 if a[i+1]==1 && a.size>6 && res<t+1
    end
end
puts res

def length(streak)
  streak[1] - streak[0] + 1
end

def solution
  read_int
  medals = read_string

  streaks = []
  crt_streak = nil

  medals.chars.each.with_index do |c, i|
    if c == ?G
      if crt_streak
        crt_streak[1] = i
      else
        crt_streak = [i, i]
        streaks << crt_streak
      end
    else
      crt_streak = nil
    end
  end

  if streaks.size == 0
    puts 0
  elsif streaks.size == 1
    puts length(streaks.first)
  else

    max = 0

    (0...streaks.size).each do |i|
      crt = streaks[i]
      next_streak = streaks[i + 1]

      crt_max = length(crt) + 1

      if next_streak && next_streak[0] == crt[1] + 2
        combined = length(crt) + length(next_streak) + 1
        if streaks.size == 2
          # have to reuse a Gold medal from one of the current streaks
          combined -= 1
        end

        crt_max = combined
      end

      max = [max, crt_max].max

    end

    puts max

  end

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

def length(streak)
  streak[1] - streak[0] + 1
end

def solution
  read_int
  medals = read_string

  streaks = []
  crt_streak = nil

  medals.chars.each.with_index do |c, i|
    if c == ?G
      if crt_streak
        crt_streak[1] = i
      else
        crt_streak = [i, i]
        streaks << crt_streak
      end
    else
      crt_streak = nil
    end
  end

  if streaks.size == 0
    puts 0
  elsif streaks.size == 1
    puts length(streaks.first)
  else

    max = 0

    (0...streaks.size).each do |i|
      crt = streaks[i]
      next_streak = streaks[i + 1]

      crt_max = length(crt)

      if next_streak && next_streak[0] == crt[1] + 2
        combined = length(crt) + length(next_streak) + 1
        if streaks.size == 2
          # have to reuse a Gold medal from one of the current streaks
          combined -= 1
        end

        crt_max = combined
      end

      max = [max, crt_max].max

    end

    puts max

  end

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

gets
a=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)
res=0
1.step(a.size, 2) do |i|
    break if !a[i]
    t=a[i]+(a.size>4 ? 1 : 0)
    res=a[i] if res<a[i]
    if a[i+2]
        t=a[i]+(a[i+1]==1 ? a[i+2] : 1)
        res=t if res<t
        res=t+1 if a[i+1]==1 && a.size>6 && res<t+1
    end
end
puts res

gets
a=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)
res=0
1.step(a.size, 2) do |i|
    break if !a[i]
    t=a[i]+(a.size>4 ? 1 : 0)
    res=a[i] if res<a[i]
    if a[i+2]
        t=a[i]+(a.size>6 && a[i+1]==1 ? a[i+2]+1 : 1)
        res=t if res<t
    end
end
puts res

gets
a=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)
res=0
1.step(a.size, 2) do |i|
    break if !a[i]
    res=a[i] if res<a[i]
    if a[i+2]
        t=a[i]+a[i+2]+(a.size>5 ? 1 : 0)
        res=t if res<t
    end
end
puts res

gets
a=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)
res=0
1.step(a.size, 2) do |i|
    break if !a[i]
    res=a[i] if res<a[i]
    if a[i+2]
        t=a[i]+a[i+2]+(a.size>5 && a[i+1]==1 ? 1 : 0)
        res=t if res<t
    end
end
puts res

gets
a=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)
res=0
1.step(a.size, 2) do |i|
    break if !a[i]
    t=a[i]+(a.size>4 ? 1 : 0)
    res=a[i] if res<a[i]
    if a[i+2]
        t=a[i]+a[i+2]+(a.size>6 && a[i+1]==1 ? 1 : 0)
        res=t if res<t
    end
end
puts res


n = gets.to_i
ans = 0
exp = 0

p = gets.split.map &:to_f
p.each do |p|
  ans += p*(2*exp + 1)
  exp = p*(exp+1)
end
puts ans

n = gets.to_i
e = 0
a = 0
f = gets.split.map(&:to_f)
f.each { |d|
    a += d*(2*e+1)
    e = (e+1)*d
}
p a

n=Integer(gets());
q = gets().split(" ").map(&:to_f);
e_sz = e = 0; q.each { |p| e=(1-p)*e+p*e+2*p*e_sz + p; e_sz=p*(e_sz+1);}
printf "%.10f\n", e

n = gets.to_i
p = gets.split.map(&:to_f)
a = 0.0
e = 0.0
p.each {|x|
  a += x*(2*e+1)
  e = x*(e+1)
}
print a

n = gets.to_i
e = 0
a = 0
f = (gets.split)
f.each { |d|
    a += d.to_f*(2*e+1)
    e = (e+1)*d.to_f
}
p a


require 'prime'

n = gets.to_i
knights = gets.split.map(&:to_i)
divs = (1..n).to_a.select{|v| n%v == 0}.select{|v| v <= n/3}

divs.each do |i|
	#if arr[i] == 1
		#check
		if n % i == 0
			(0..(i-1)).each do |start|
				ok = true
				pos = start
				(n/i).times do
					if knights[pos] == 0
						ok = false
						break
					end
					pos = (pos+i) % n
				end
				if ok
					puts "YES"
					exit
				end
			end
		end

=begin
		(2..i+1).each do |k|
			if (i+1) % k == 0
				arr[(i+1)/k-1] = 0
			end
		end
=end
#	end
end
puts "NO"

n = gets.chomp
n = n.to_i

knights = gets.chomp
knights = knights.split(' ')

ok = false;

for i in 1..n
	if i * 3 > n; break; end;
	if n % i != 0; next; end;

	for j in 0..i-1
		ok = true;
		k = j;
		while k < n
			if knights[k] == "0"
				ok = false;
				break;
			end
			k += i;
		end
		if ok == true
			break;
		end
	end
	if ok
		break;
	end
end

if ok
	puts "YES";
else
	puts "NO";
end

#!/usr/bin/env/ruby

def exec(debug = false)
  length = gets.chomp.to_i
  elements = gets.chomp.split(/\s/).map{|s| s.to_i}
  return puts "NO" unless length == elements.size

  max = length / 3
  p "max: #{max}" if debug
  max.times do |i|
    ii = i + 1
    p "ii: #{ii}" if debug
    next if length % ii != 0
    elements.each_with_index do |e, index|
      break if index == max
      success = true
      next if e == 0
      next if index >= ii
      p "index:#{index}" if debug
      (index + ii).step(length - 1, ii) do |j|
        p "j: #{j}" if debug
        unless elements[j] == 1
          p "break" if debug
          success = false
          break
        end
      end
      return puts("YES") if success
    end
  end
  puts "NO"
end

#exec(debug = true)
exec

n=gets.to_i
a=gets.split.map(&:to_i)
3.upto(n){|i|
        if n%i==0 then
                k=n/i
                k.times{|j|
                        ok=1
                        now=j
                        i.times{
                                if a[now]==0 then
                                        ok=0
                                        break
                                end
                                now+=k
                        }
                        if ok==1 then
                                puts "YES"
                                exit
                        end
                }
        end
}
puts "NO"

require 'prime'

n = gets.to_i
knights = gets.split.map(&:to_i)
divs = n.prime_division.map{|a| a[0]-1}.select{|v| v+1<=n/3} + [0]

divs.each do |i|
	#if arr[i] == 1
		#check
		if n % (i+1) == 0
			(0..i).each do |start|
				ok = true
				pos = start
				(n/(i+1)).times do
					if knights[pos] == 0
						ok = false
						break
					end
					pos = (pos+i+1) % n
				end
				if ok
					puts "YES"
					exit
				end
			end
		end

=begin
		(2..i+1).each do |k|
			if (i+1) % k == 0
				arr[(i+1)/k-1] = 0
			end
		end
=end
#	end
end
puts "NO"

#!/usr/bin/env/ruby

def exec(debug = false)
  length = gets.chomp.to_i
  elements = gets.chomp.split(/\s/).map{|s| s.to_i}
  return puts "NO" unless length == elements.size

  max = length / 3
  p "max: #{max}" if debug
  max.times do |i|
    ii = i + 1
    p "ii: #{ii}" if debug
    next if length % ii != 0
    elements.each_with_index do |e, index|
      break if index == max
      success = true
      next if e == 0
      next if index >= ii
      p "index:#{index}" if debug
      (index + ii).step(length, ii) do |j|
        p "j: #{j}" if debug
        unless elements[j] == 1
          p "break" if debug
          success = false
          break
        end
      end
      return puts("YES") if success
    end
  end
  puts "NO"
end

exec(debug = true)
#exec

#!/usr/bin/env/ruby

def exec
  length = gets.chomp.to_i
  elements = gets.chomp.split(/\s/).map{|s| s.to_i}
  return puts "NO" unless length == elements.size

  max = length / 3
  #p "max: #{max}"
  max.times do |i|
    ii = i + 1
    #p "ii: #{ii}"
    return puts("NO") unless length % ii == 0
    elements.each_with_index do |e, index|
      break if index == max
      success = true
      next if e == 0
      #p "index:#{index}"
      (index + ii).step(max, ii) do |j|
        #p "j: #{j}"
        unless elements[j] == 1
          #p "break"
          success = false
          break
        end
      end
      return puts("YES") if success
    end
  end
  puts "NO"
end

exec

#!/usr/bin/env/ruby

def exec(debug = false)
  length = gets.chomp.to_i
  elements = gets.chomp.split(/\s/).map{|s| s.to_i}
  return puts "NO" unless length == elements.size

  max = length / 3
  p "max: #{max}" if debug
  max.times do |i|
    ii = i + 1
    p "ii: #{ii}" if debug
    elements.each_with_index do |e, index|
      break if index == max
      success = true
      next if e == 0
      p "index:#{index}" if debug
      (index + ii).step(max, ii) do |j|
        p "j: #{j}" if debug
        unless elements[j] == 1
          p "break" if debug
          success = false
          break
        end
      end
      return puts("YES") if success
    end
  end
  puts "NO"
end

#exec(debug = true)
exec

#!/usr/bin/env/ruby

def exec(debug = false)
  length = gets.chomp.to_i
  elements = gets.chomp.split(/\s/).map{|s| s.to_i}
  return puts "NO" unless length == elements.size

  max = length / 3
  p "max: #{max}" if debug
  max.times do |i|
    ii = i + 1
    p "ii: #{ii}" if debug
    next if length % ii != 0
    elements.each_with_index do |e, index|
      break if index == max
      success = true
      next if e == 0
      next if index >= ii
      p "index:#{index}" if debug
      (index + ii).step(length - 1, ii) do |j|
        p "j: #{j}" if debug
        unless elements[j] == 1
          p "break" if debug
          success = false
          break
        end
      end
      return puts("YES") if success
    end
  end
  puts "NO"
end

exec(debug = true)
#exec


n,_ = gets.split
arr = gets.split.map(&:to_i)
arr.sort_by!{|a| -a}

total = arr.inject(&:+)
needed = 0

(0...arr.size).each do |i|
    if i == arr.size-1
        needed += [1,arr[i]].max
    elsif arr[i] > arr[i+1]
        needed += arr[i] - arr[i+1]
    else
        needed +=1
        arr[i+1] = arr[i]-1
    end
end

puts total-needed

n, m = gets.split.map{|x| x.to_i}
a = gets.split.map{|x| x.to_i}.sort.reverse
m = a.max
d = 0
(0...n-1).each do |i|
    if a[i] == a[i + 1]
        d += a[i] if a[i] > 1
        a[i + 1] = [a[i] - 1, 1].max
    elsif a[i] > a[i + 1]
        d += a[i + 1]
    else
        d += a[i] if a[i] > 1
        d += a[i + 1] - a[i]
        a[i + 1] = [a[i] - 1, 1].max
    end
end
puts d


def E_GCD(a,b)
  if b==0
    return 1,0,a
  else
    x,y,d = E_GCD(b, a%b)
    return y,x-y*(a/b),d
  end
end

  a,b,c = gets.split.map(&:to_i)
  x,y,d = E_GCD(a,b)

  if c%d==0
    puts "#{-x*c/d} #{-y*c/d}"
  else
    puts -1
  end

def extgcd(a, b)
	if b == 0
		[a, 1, 0]
	else
		g, x, y = extgcd(b, a % b)
		[g, y, x - a / b * y]
	end
end

a, b, c = gets.split.map(&:to_i)
g, x, y = extgcd(a, b)

if c % g == 0
	c /= -g
	puts "#{x*c} #{y*c}"
else
	p -1
end


#!/usr/bin/env ruby
require 'bigdecimal'
n = STDIN.gets.chomp.to_i
x = 10 ** (n - 1) / 210

a = x * 210
b = (x + 1) * 210
if a.to_s.length == n && a > 0
  puts a
elsif b.to_s.length == n && b > 0
  puts b
else
  puts -1
end

n=gets.chomp.to_i

res=0
a=[2,3,5,7]

((10**(n-1))..(10**n-1)).each do |i|
flag=1
a.each do |j|
if i%j!=0
flag=0
break
end
end

if flag==1
res=i
break
end

end

res=-1 if res==0

puts res

n = gets.to_i
m = 2*3*5*7
if (n < 3)
	puts -1
elsif n == 3
	puts m
else
	k = 10**(n-1)
	r = k % m
	puts k + m - r
end

def t(n)
    if n % 2 == 0 then
        return true
    else
        return false
    end
end

def th(n)
    if n % 3 == 0 then
        return true
    else
        return false
    end
end

def f(n)
    if n % 5 == 0 then
        return true
    else
        return false
    end
end

def s(n)
    if n % 7 == 0 then
        return true
    else
        return false
    end
end

n = gets.to_i

num = 10**(n-1)
h = 1

while !t(num) do
    num += h
end

h = 2

while !th(num) do
    num += h
end

h *= 3

while !f(num) do
    num += h
end

h *= 5

while !s(num) && num < 10**n do
    num += h
end

if !s(num) then
    num = -1
end

puts num

a=10**(gets.to_i-1)
if a<100 then
    puts -1
else
    while a%7!=0 do
        a+=1
    end
    while a%2!=0 || a%3!=0 || a%5!=0 do
        a+=7
    end
    puts a
end

a=10**(gets.to_i-1)
if a<100 then
    puts -1
else
    while a%7!=0 do
        a+=1
    end
    while a%2!=0 || a%3!=0 || a%5!=0 || a%7!=0 do
        a+=7
    end
    puts a
end

n = gets.to_i
if n < 3 then
  print -1
  exit
else
  x = 10**(n-1) +20
  while x%7 != 0 do x += 30 end
  print x
end

n = gets.to_i
if n < 3 then
  print -1
  exit
end
Num = 2*3*5*7
k = 10**(n-1)
while k%Num != 0 do
  k += 10
end
print k

n=gets.chomp.to_i
def s n
  if n<3
    puts -1
    return
  end
  s = 210
  z = 10**(n-1)
  d = z/s
  puts s*(d+1)
end
s n

n = gets.to_i

num = 10**(n-1)
while (num % 3) != 0 || (num % 7) != 0 do
    num += 10
end

puts num

a=10**(gets.to_i-1)
if a<100 then
    puts -1
else
    while a%7!=0 do
        a+=1
    end
    while a%2!=0 || a%3!=0 || a%5!=0 do
        a+=1
    end
    puts a
end

a=10**(gets.to_i-1)
if a<1000 then
	puts -1
else
	while a%7!=0 do
		a+=1
	end
	while a%2!=0 || a%3!=0 || a%5!=0 do
		a+=7
	end
	puts a
end

#!/usr/bin/env ruby
require 'bigdecimal'
n = STDIN.gets.chomp.to_i

log = n - 1 - Math::log(2 * 3 * 5 * 7, 10)
x = (BigDecimal.new('10.0') ** log).to_i
a = x * 210
b =  (x + 1) * 210

if a.to_s.length == n && a > 0
  puts a
elsif b.to_s.length == n && b > 0
  puts b
else
  puts "-1"
end

#!/usr/bin/env ruby
require 'bigdecimal'
n = STDIN.gets.chomp.to_i

log = n - 1 - Math::log(2 * 3 * 5 * 7, 10)
if log < 0
  puts -1
  exit
end
x = (BigDecimal.new('10.0') ** log).to_i
a = x * 210
b =  (x + 1) * 210
c =  (x + 2) * 210

if a.to_s.length == n
  puts a
elsif b.to_s.length == n
  puts b
elsif c.to_s.length == n
else
  puts "-1"
end

#!/usr/bin/env ruby
require 'bigdecimal'
n = STDIN.gets.chomp.to_i
x = 10 ** (n - 1) / 210

if x < 1
  puts -1
  exit
end
a = x * 210
b = (x + 1) * 210
if a.to_s.length == n
  puts a
elsif b.to_s.length == n
  puts b
else
  puts -1
end


input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
log_stream = Array.new
while line = gets do
    log_stream.push(line)
end

x = n / 3600
y = (n % 3600) / 60
z = n - 3600 * x - 60 * y

def time_back(log, x, y, z)
    year = log[0..3].to_i
    month = log[5..6].to_i
    date = log[8..9].to_i
    hour = log[11..12].to_i
    minute = log[14..15].to_i
    second = log[17..18].to_i
    if second - z >= 0
        second = second - z
    else
        second = second - z + 60
        y += 1
    end
    if minute - y >= 0
        minute = minute - y
    else
        minute = minute - y + 60
        x += 1
    end
    if hour - x >= 0
        hour = hour - x
    else
        hour = hour - x + 24
        date -= 1
        if date == 0
            month -= 1
            if [1,3,5,7,8,10].include? month
                date = 31
            elsif [4,6,9,11].include? month
                date = 30
            elsif month == 0
                date = 31
                month = 12
                year -= 1
            elsif month == 2
                if year % 4 == 0
                    date = 29
                else
                    date = 28
                end
            end
        end
    end
    if month < 10
        month = "0" + month.to_s
    end
    if date < 10
        date = "0" + date.to_s
    end
    if hour < 10
        hour = "0" + hour.to_s
    end
    if minute < 10
        minute = "0" + minute.to_s
    end
    if second < 10
        second = "0" + second.to_s
    end
    return "#{year}-#{month}-#{date} #{hour}:#{minute}:#{second}"
end

moment = -1
for i in m-1...log_stream.length
    if time_back(log_stream[i], x, y, z) < log_stream[i-m+1][0..18]
        moment = log_stream[i]
        break
    end
end
if moment != -1
    puts moment[0..18]
else
    puts moment
end

require 'date'

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

l = n + 10
counts = Array.new(l) do |i|
  [0, 0]
end

first = 0
last = prev = 0
total = 0
ans = -1
while line=gets
  time_str = line[0..18]
  time = DateTime.parse(time_str).to_time
  total += 1
  if first != last && time == counts[prev][0]
    counts[prev][1] += 1
  else
    while first != last && counts[first][0] <= time - n
      total -= counts[first][1]
      first += 1
      first = 0 if first == l
    end
    counts[last][0] = time
    counts[last][1] = 1
    prev = last
    last += 1
    last = 0 if last == l
  end
  if total >= m
    ans = time_str
    break
  end
end

puts ans

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s[0, 19]) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_s.gsub(/T/, ' ').gsub(/\+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip).new_offset(0) }
puts logs.map { |l| l.to_time }
puts logs.map { |l| l.to_time.to_s }
puts logs.map { |l| l.to_time.to_s.gsub(/ \+.*/, '_') }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
puts logs
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
puts logs.map { |l| l.to_time }
puts logs.map { |l| l.to_time.to_s }
puts logs.map { |l| l.to_time.to_s.gsub(/ \+.*/, '_') }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].new_offset(0).to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_s.gsub(/T/, '').gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip).new_offset("+0000") }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip).new_offset("+0000") }
puts logs.map { |l| l.to_time }
puts logs.map { |l| l.to_time.to_s }
puts logs.map { |l| l.to_time.to_s.gsub(/ \+.*/, '_') }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s[0, 19].strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_s.gsub(/T/, ' ').gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  puts logs[i]
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_s.gsub(/T/, ' ').gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_s.gsub(/T/, ' ').gsub(/\+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].new_offset(0).to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

require 'date'

n, m = gets.split.map &:to_i
logs = $<.map { |s| DateTime.parse(s.strip) }
(m - 1).upto(logs.size - 1) do |i|
  if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n
    puts logs[i].to_time.to_s.gsub(/ \+.*/, '')
    exit
  end
end

puts -1

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
log_stream = Array.new
while line = gets do
    log_stream.push(line)
end

x = n / 3600
y = (n % 3600) / 60
z = n - 3600 * x - 60 * y

def time_back(log, x, y, z)
    year = log[0..3].to_i
    month = log[5..6].to_i
    date = log[8..9].to_i
    hour = log[11..12].to_i
    minute = log[14..15].to_i
    second = log[17..18].to_i
    if second - z >= 0
        second = second - z
    else
        second = second - z + 60
        y += 1
    end
    if minute - y >= 0
        minute = minute - y
    else
        minute = minute - y + 60
        x += 1
    end
    if hour - x >= 0
        hour = hour - x
    else
        hour = hour - x + 24
        date -= 1
        if date == 0
            month -= 1
            if [1,3,5,7,8,10].include? month
                date = 31
            elsif [4,6,9,11].include? month
                date = 30
            elsif month == 0
                date = 31
                month = 12
                year -= 1
            elsif month == 2
                if year % 4 == 0
                    date = 29
                else
                    date = 28
                end
            end
        end
    end
    if month < 10
        month = "0" + month.to_s
    end
    if date < 10
        date = "0" + date.to_s
    end
    if hour < 10
        hour = "0" + hour.to_s
    end
    if minute < 10
        minute = "0" + minute.to_s
    end
    if second < 10
        second = "0" + second.to_s
    end
    return "#{year}-#{month}-#{date} #{hour}:#{minute}:#{second}"
end

moment = -1
puts log_stream.length
for i in m-1...log_stream.length
    puts
    puts time_back(log_stream[i], x, y, z)
    puts log_stream[i-m+1][0..18]
    if time_back(log_stream[i], x, y, z) < log_stream[i-m+1][0..18]
        moment = log_stream[i]
        break
    end
end
if moment != -1
    puts moment[0..18]
else
    puts moment
end


T = gets.to_i

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

  if a.sum == m
    puts 'YES'
  else
    puts 'NO'
  end
end

gets.chomp.to_i.times do |_i|
  n, m = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)
  puts (m == a.inject(0, :+) ? 'YES' : 'NO')
end

t = gets.chomp.to_i
t.times do
  n, m = gets.chomp.split(' ').map(&:to_i)
  c = gets.chomp.split(' ').map(&:to_i)
  if c.sum == m
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.to_i

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

  if(a.sum == m)
    puts "YES"
  else
    puts "NO"
  end
end

test_cases = gets.chomp

test_cases.to_i.times do
 x = gets.chomp.split(" ").map(&:to_i)[1]
 y = gets.chomp.split(" ").map(&:to_i)
 puts y.sum == x ? "YES" : "NO"

end


n, m = readline.split(' ').collect(&:to_i)
arr = Array.new(m) {Array.new}
n.times do
  name, region, mark = readline.split(' ')
  region, mark = region.to_i, mark.to_i
  arr[region - 1].push([mark, name])
end
arr.each do |a|
  a.sort!.reverse!
  if a.length <= 2 || a[2][0] < a[1][0]
    print a[0][1], " ", a[1][1]
    puts
  else
    puts "?"
  end
end

a=gets.chomp.split(" ")
input=a[0].to_i
city=a[1].to_i
max2=Array.new(city){Array.new(2,0)}
name=Array.new(city) {Hash.new { |hash, key| hash[key] = [] } }
input.times do
temp=gets.chomp.split(" ")
tc=temp[1].to_i
ts=temp[2].to_i
if ts>max2[tc-1][0]
max2[tc-1][1]=max2[tc-1][0]
max2[tc-1][0]=ts
elsif ts>max2[tc-1][1]
max2[tc-1][1]=ts
end
name[tc-1][ts]<<temp[0]
end
0.upto(city-1) do |i|
if max2[i][0]==max2[i][1]
if name[i][max2[i][0]].length>2
puts "?"
else
puts name[i][max2[i][0]].join(" ")
end
else
if name[i][max2[i][0]].length + name[i][max2[i][1]].length>2
puts "?"
else
printf "#{ name[i][max2[i][0]][0]} "
puts "#{ name[i][max2[i][1]][0]}"
end
end
end

n, m = gets.split.map(&:to_i)
a = Array.new(m).map{Array.new}
n.times do
  na, re, sc = gets.split
  re = re.to_i - 1
  sc = sc.to_i
  a[re].push([sc, na])
end

m.times do |i|
  t = a[i].sort{ |x, y| y[0] <=> x[0] }
  if t.length < 3
    puts "#{t[0][1]} #{t[1][1]}"
  elsif t[1][0] == t[2][0]
    puts "?"
  else
    puts "#{t[0][1]} #{t[1][1]}"
  end
end

def update(g, str, score)
	if !$a[g][0] || score >= $a[g][0]
		$a[g][2] = $a[g][1]
		$a[g][1] = $a[g][0]
		$a[g][0] = score
		$name[g][2] = $name[g][1]
		$name[g][1] = $name[g][0]
		$name[g][0] = str
	elsif !$a[g][1] || score >= $a[g][1]
		$a[g][2] = $a[g][1]
		$a[g][1] = score
		$name[g][2] = $name[g][1]
		$name[g][1] = str
	elsif !$a[g][2] || score >= $a[g][2]
		$a[g][2] = score
		$name[g][2] = str
	end
end

buf=gets.split(' ')
n,m=buf[0].to_i, buf[1].to_i
$a=Array.new(m+1)
$name=Array.new(m+1)
for i in 1..m
	$a[i]=Array.new(3)
	$name[i]=Array.new(3)
end
for i in 1..n
	buf=gets.split(' ')
	update(buf[1].to_i, buf[0], buf[2].to_i)
end
for i in 1..m
	if $a[i][1] == $a[i][2]
		puts '?'
	else
		puts $name[i][0] + ' ' + $name[i][1]
	end
end

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

region = Array.new(m) { |r| r = [] }
ans = Array.new(m) { |r| r = [] }
n.times do
    info = gets.chomp.split(' ')
    region[info[1].to_i - 1] << [info[0], info[2].to_i]
end

region.each do |r|
    r.sort! { |a, b| if a[1] > b[1] then -1 elsif a[1] < b[1] then 1 else 0 end }
end

region.each_with_index do |r, i|
    sameWithMax = r.count { |p| p[1] == r[0][1] }
    sameWithSubSequent = 0

    if (sameWithMax < r.length)
        sameWithSubSequent = r.count { |p| p[1] == r[sameWithMax][1] }
    end

    if (sameWithMax >= 3 || (sameWithMax < 2 && sameWithMax + sameWithSubSequent >= 3))
        ans[i] << '?'
    else
        ans[i] << "#{r[0][0]} #{r[1][0]}"
    end
end

ans.each do |a|
    puts a
end

#!/usr/bin/ruby
n,m=gets.split.map(&:to_i)
h=Hash.new{|h,k|h[k]=[]}
n.times{
	a,b,c=gets.split
	h[b.to_i-1]<<[-c.to_i,a]
}
m.times{|i|
	h[i].sort!
	if h[i].size<=2 || h[i][1][0]!=h[i][2][0]
		puts h[i][0,2].map(&:last)*' '
	else
		puts '?'
	end
}

n, m = gets.chomp.split.map(&:to_i)
memo = Hash.new {|h, k| h[k] = [] }
Player = Struct.new(:name, :point)

n.times do
  name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }
  memo[region] << Player.new(name, point)
end

memo.keys.sort.each do |key|
  players = memo[key].sort_by { |p| -p.point }
  selected = players.take(2)

  if players.length <= 2 || players[1..2].map(&:point).uniq.length == 2
    puts selected.map(&:name).join(' ')
  else
    puts "?"
  end
end

n, m = gets.chomp.split.map(&:to_i)
memo = Hash.new {|h, k| h[k] = [] }
Player = Struct.new(:name, :point)

n.times do
  name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }
  memo[region] << Player.new(name, point)
end

memo.keys.sort.each do |key|
  players = memo[key].sort_by { |p| -p.point }
  selected = players.take(2)

  if players.length <= 2 || players[1..2].map(&:point).uniq == 1
    puts selected.map(&:name).join(' ')
  else
    puts "?"
  end
end

n, m = gets.chomp.split.map(&:to_i)
memo = Hash.new {|h, k| h[k] = [] }
Player = Struct.new(:name, :point)

n.times do
  name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }
  memo[region] << Player.new(name, point)
end

memo.keys.sort.each do |key|
  players = memo[key]
  max = players.map(&:point).max
  max_players = players.select { |p| p.point == max }
  selected = players.sort_by { |p| -p.point }.take(2)
  if max_players.length > 2 || selected.map(&:name).uniq.length == 1
    puts "?"
  else
    puts selected.map(&:name).join(' ')
  end
end

n, m = gets.chomp.split.map(&:to_i)
memo = Hash.new {|h, k| h[k] = [] }
Player = Struct.new(:name, :point)

n.times do
  name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }
  memo[region] << Player.new(name, point)
end

memo.keys.sort.each do |key|
  players = memo[key]
  max = players.map(&:point).max
  if players.map(&:point).select { |p| p == max }.length > 2
    puts "?"
  else
    puts players.sort_by { |p| -p.point }.take(2).map(&:name).join(' ')
  end
end

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

region = Array.new(m) { |r| r = [] }
ans = Array.new(m) { |r| r = [] }
n.times do
    info = gets.chomp.split(' ')
    region[info[1].to_i - 1] << [info[0], info[2].to_i]
end

region.each do |r|
    r.sort! { |a, b| if a[1] > b[1] then -1 elsif a[1] < b[1] then 1 else 0 end }
end

region.each_with_index do |r, i|
    sameWithMax = r.count { |p| p[1] == r[0][1] }
    sameWithSubSequent = 0

    if (sameWithMax < r.length)
        sameWithSubSequent = r.count { |p| p[1] == r[sameWithMax][1] }
    end

    if (sameWithMax + sameWithSubSequent >= 3)
        ans[i] << '?'
    else
        ans[i] << "#{r[0][0]} #{r[1][0]}"
    end
end

ans.each do |a|
    puts a
end


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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
        m[t[i]-1] += 1
      end
      t[head] = num_i + 1
      head = m[num_i]
    elsif m[num_i] == head
      head +=  1
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
        m[t[i]-1] = i
      end
      t[head] = num_i + 1
      head = m[num_i]
    elsif m[num_i] == head
      head +=  1
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      while(t[head] <= num_i)
        head += 1
      end
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
        m[t[i]-1] += 1
      end
      t[head] = num_i + 1
      head = m[num_i]
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head] = num_i + 1
      head = m[num_i]
    elsif (m[num_i] == head)
      head += 1
    end
    break if head == (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  barrier = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1+barrier)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head] = num_i + 1
      head = m[num_i]
    elsif (m[num_i] == head)
      head += 1
      barrier = 0
    elsif (m[num_i] == head - 1)
      barrier = 1
    end
    break if head == (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
        m[t[i]-1] = i
      end
      t[head] = num_i + 1
      head = m[num_i]
    else
      head = m[num_i] + 1
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      while(t[head] <= num_i and head<num_i)
        head += 1
      end
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
        m[t[i]-1] += 1
      end
      t[head] = num_i + 1
      head = m[num_i]
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      while(t[head] <= num_i and head<(num_i-1))
        head += 1
      end
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
        m[t[i]-1] += 1
      end
      t[head] = num_i + 1
      head = m[num_i]
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      while(m[head] <= num_i and head<num_i)
        head += 1
      end
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head] = num_i + 1
      head = m[num_i]
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  barrier = false
  n.times do |num_i|
    if m[num_i] > head
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head] = num_i + 1
      head = m[num_i]
    elsif (m[num_i] == head or m[num_i] == head - 1)
      head += 1
    end
    break if head == (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head] = num_i + 1
      head = m[num_i]
      break if head == (n-1)
    elsif (m[num_i] == head)
      head += 1
    end
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  barrier = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1+barrier)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head+barrier] = num_i + 1
      head = m[num_i]
    elsif (m[num_i] == head or m[num_i] == head - 1)
      barrier = 1
    end
    break if head == (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
        m[t[i]-1] = i
      end
      t[head] = num_i + 1
      head = m[num_i]
    else
      head +=  1
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  n.times do |num_i|
    if m[num_i] > head
      while(t[head] <= num_i and head<num_i)
        head += 1
      end
      ((head+1)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head] = num_i + 1
      head = m[num_i]
    end
    break if head >= (n-1)
  end
  puts t.join(" ")
end

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

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

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

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

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

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

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

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

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

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

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

q = inp[0]
q.times do
  n = inp[0]
  t = inp
  m = na(n,0)
  n.times do |i|
    m[t[i]-1] = i
  end
  head = 0
  barrier = 0
  n.times do |num_i|
    if m[num_i] > head
      ((head+1+barrier)..m[num_i]).reverse_each do |i|
        t[i] = t[i-1]
      end
      t[head+barrier] = num_i + 1
      head = m[num_i]
      barrier = 0
    elsif (m[num_i] == head or m[num_i] == head - 1)
      barrier = 1
    end
    break if head == (n-1)
  end
  puts t.join(" ")
end


a=n=gets.to_i;s=gets
n.times{|i|a-=i%2^s[i].hex}
p [a,n-a].min

a=[0,0]
gets.to_i.times{|i|a[STDIN.read(1).hex^i%2]+=1}
p a.min

a=[0,0]
gets.to_i.times{|i|c=STDIN.read(1).ord-48;a[(c+i)%2]+=1}
p a.min

a=[0,0]
gets.to_i.times{|i|a[(STDIN.read(1).ord-48+i)%2]+=1}
p a.min

n=gets.to_i;s=gets;a=0
n.times{|i|a+=(i%2)^s[i].to_i}
p [a,n-a].min

n=gets.to_i;s=gets;a=0
n.times{|i|a+=i%2^s[i].to_i}
p [a,n-a].min


DBG = true
s = gets.chomp
st = 0
for i in 0...s.size

  if st == 0
    if s[i] == '0'
      puts "3 4"
      st = 2
    else
      puts "1 1"
      st = 1
    end

  elsif st == 1
    if s[i] == '0'
      puts "3 4"
      st = 3
    else
      puts "1 3"
      st = 0
    end

  elsif st == 2
    if s[i] == '0'
      puts "1 4"
      st = 0
    else
      puts "1 1"
      st = 3
    end

  else #if st == 3
    if s[i] == '0'
      puts "1 4"
      st = 1
    else
      puts "1 3"
      st = 2
    end
  end
end

# Input
s = gets.strip

# TACTIC:
# We will place all the horizontal tiles in top row, so every second horizontal
# tile will make this row clear again.
# All the vertical tiles will be placed in last two rows (3 1, 3 2, 3 3, 3 4),
# so ever y 4th tile will clear both this lines.
# Such tactic must guarantee that we will be able to place any tile any time.

# Processing
result = []
h_count, v_count = 0, 0

s.each_char do |chr|
  if chr == "1"
    # Horizontal tile
    result << "1 #{h_count * 2 + 1}"
    h_count = (h_count + 1) % 2
  else
    # Vertical tile
    result << "3 #{v_count + 1}"
    v_count = (v_count + 1) % 4
  end
end

# Output
puts result

s = gets.chomp.split('').map(&:to_i)
N = 4
arr = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
s.each do |c|
  arr.each_with_index do |row, i|
    f = true
    row.each do |ee|
      f &= ee == 1
    end
    if (f)
      arr[i] = [0, 0, 0, 0]
    end
  end
  N.times do |i|
    f = true
    N.times do |ii|
      f &= arr[ii][i] == 1
    end
    if (f)
      arr[0][i] = 0
      arr[1][i] = 0
      arr[2][i] = 0
      arr[3][i] = 0
    end
  end
  if (c == 0)
    b = false
    N.times do |row|
      break if b
      N.times do |col|
        if (arr[col] && arr[col][row] == 0 && arr[col + 1] && arr[col + 1][row] == 0)
          arr[col][row] = 1
          arr[col + 1][row] = 1
          puts [col + 1, row + 1].join(' ')
          b = true
          break
        end
      end
    end
  end
  if (c == 1)
    b = false
    N.times do |row|
      break if b
      N.times do |col|
        #arr[1][0] arr[1][1]
        if (arr[row] && arr[row][col] == 0 && arr[row] && arr[row][col + 1] == 0)
          arr[row][col] = 1
          arr[row][col + 1] = 1
          puts [row + 1, col + 1].join(' ')
          b = true
          break
        end
      end
    end
  end
end

s = gets.chomp.split('').map(&:to_i)
N = 4
arr = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
s.each do |c|
  arr.each_with_index do |row, i|
    f = true
    row.each do |ee|
      f &= ee == 1
    end
    if (f)
      arr[i] = [0, 0, 0, 0]
    end
  end
  N.times do |i|
    f = true
    N.times do |ii|
      f &= arr[ii][i] == 1
    end
    if (f)
      arr[0][i] = 0
      arr[1][i] = 0
      arr[2][i] = 0
      arr[3][i] = 0
    end
  end
  if (c == 0)
    b = false
    N.times do |row|
      break if b
      N.times do |col|
        #arr[0][1] arr[1][1]
        if (arr[col][row] == 0 && arr[col + 1][row] == 0)
          arr[col][row] = 1
          arr[col + 1][row] = 1
          puts [row + 1, col + 1].join(' ')
          b = true
          break
        end
      end
    end
  end
  if (c == 1)
    b = false
    N.times do |row|
      break if b
      N.times do |col|
        #arr[1][0] arr[1][1]
        if (arr[row][col] == 0 && arr[row][col + 1] == 0)
          arr[row][col] = 1
          arr[row][col + 1] = 1
          puts [col+ 1, row + 1].join(' ')
          b = true
          break
        end
      end
    end
  end
end


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

n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)

xorarr=[]
a = 0
(0..n-1).each do |i|
	a = a^arr[i]
	xorarr.push a
end

evenhash = Hash.new(0)
oddhash = Hash.new(0)
xorarr.insert(0,0)

(0..n).each do |i|
	if(i%2)==0
		evenhash[xorarr[i]]+=1
	else
		oddhash[xorarr[i]]+=1
	end
end

#p evenhash
#p oddhash

=begin
p xorarr
p evenhash
p oddhash
=end
ans = 0
evenhash.each do |k,v|
	ans+=(v*(v-1))/2
end

oddhash.each do |k,v|
	ans+=(v*(v-1))/2
end

puts ans

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

dp=[]
currXorSum = 0
(0..n-1).each do |i|
	currXorSum = currXorSum^a[i]
	dp.push currXorSum
end

arr1 = [Hash.new(0),Hash.new(0)]
dp.insert(0,0)

(0..n).each do |i|
	arr1[i%2][dp[i]] += 1
end

ans = 0
arr1[0].each do |k,ct|
	ans += ((ct-1)*ct)/2
end

arr1[1].each do |k,ct|
	ans += ((ct-1)*ct)/2
end

puts ans


def gao(v, p)
	$e[v].delete(p)
	n, x, y, z = $e[v].size, 1, 1, 1
	dp = [1] + [0] * n
	$e[v].each do |w|
		a, b, c = gao(w, v)
		x *= a
		y = [y, Rational(b, a)].max
		z *= a
		n.downto(1) do |i|
			dp[i] = [dp[i - 1] * c, dp[i] * a].max
		end
		dp[0] *= a
	end
	x, y = (x * y).to_i, 0
	0.upto(n) do |i|
		x = [x, dp[i] * (i + 1)].max
		y = [y, dp[i] * (i + 2)].max
	end
	return x, y, z
end

n = gets.to_i
$e = Array.new(n){[]}
(n - 1).times do
	a, b = gets.split.map{|_| _.to_i - 1}
	$e[a] << b
	$e[b] << a
end
p gao(0, -1)[0]

N = gets.to_i
adj = Array.new(N) { Array.new }
(N - 1).times {
  a, b = gets.split.map { |x| x.to_i - 1 }
  adj[a] << b
  adj[b] << a
}

dp = Array.new(N) { Array.new 2, 0 }
def dfs u, fa, adj, dp
  return dp[u] if dp[u][0] != 0
  dp[u] = [1, 1]
  ch = adj[u].reject { |v| v == fa }
  return dp[u] if ch.size == 0
  dp[u][0] = ch.map { |v| dfs(v, u, adj, dp)[1] } .reduce(1) { |o, v| o * v }
  f, l = dp[u][0], 1
  dp[u][1] = ch.map { |v| # left one right one
    dfs v, u, adj, dp
  } .sort { |x, y|
    x[1] * y[0] <=> x[0] * y[1]
  } .map { |a|
    l += 1
    f *= l
    f *= a[0]
    f /= a[1]
    f /= l - 1
    f
  } .max
  dp[u][1] = [
    dp[u][1],
    ch.map { |c|
      f, l = dp[u][0] * 2 * dfs(c, u, adj, dp)[0] / dfs(c, u, adj, dp)[1], 2
      adj[c].reject { |q| q == u } .map { |gc|
        dfs gc, c, adj, dp
      } .sort { |x, y|
        x[1] * y[0] <=> x[0] * y[1]
      } .map { |a|
        l += 1
        f *= l
        f *= a[0]
        f /= a[1]
        f /= l - 1
        f
      } .max || 1
    } .max || 1
  ].max
  return dp[u]
end

dfs 0, -1, adj, dp
p dp[0][1]

def gao(v, p)
	$e[v].delete(p)
	n, x, y = $e[v].size, 1, 1
	dp = [1] + [0] * n
	$e[v].each do |w|
		a, b = gao(w, v)
		x *= a
		y = [y, Rational(b, a)].max
		n.downto(1) do |i|
			dp[i] = [dp[i - 1], dp[i] * a].max
		end
		dp[0] *= a
	end
	x, y = (x * y).to_i, 0
	0.upto(n) do |i|
		x = [x, dp[i] * (i + 1)].max
		y = [y, dp[i] * (i + 2)].max
	end
	return x, y
end

n = gets.to_i
$e = Array.new(n){[]}
(n - 1).times do
	a, b = gets.split.map{|_| _.to_i - 1}
	$e[a] << b
	$e[b] << a
end
p gao(0, -1)[0]


input = STDIN.gets.chomp.split(" ").map{|i| i.to_i}

N = input[0]
M = input[1]
K = input[2]

A = STDIN.gets.chomp.split(" ").map.with_index{|a, i| [i, a.to_i]}
sorted_a = A.sort_by{|a| a[1]}.reverse.slice(0, M * K).sort_by{|a| a[0]}

total = 0
slices = []
sorted_a.each_with_index{|a, i|
    if i != (M * K - 1) && (i % M) == (M - 1)
        slices.push(a[0] + 1)
    end
    total = total + a[1]
}
puts total
puts slices.join(" ")

# A.map.with_index{|a, b|
#     p a
#     p b
#     return 23
# }

n, m, k = gets.chomp.split(' ').map(&:to_i)
i = -1
array = gets.chomp.split(' ').map(&:to_i)
l_array = array.map{|n| i+=1; [n.to_i, i]}

l_array = l_array.sort{
	|x,y|
	if(x[0] == y[0]) then x[1]<=>y[1]
	else y<=>x end
}[0...(k*m)].sort{|x,y| x[1]<=>y[1]}

sum = 0
l_array.each{|x| sum+=x[0]}

puts sum

#~ puts
#~ print l_array
#~ puts
i = -1
(k-1).times{
	|x|
	x+=1
	i += m
	if(x != k-1) then print("#{l_array[i][1]+1} ")
	else puts("#{l_array[i][1]+1}") end
}


#input = File.open('input.txt','r')
n = gets.to_i
graph = Array.new(n)
coordinates = Array.new(n)
n.times do |i|
	coordinates[i] = Array.new(2)
	graph[i] = Array.new
	coordinates[i][0], coordinates[i][1] = gets.split
	coordinates[i][0] = coordinates[i][0].to_i - 1
	coordinates[i][1] = coordinates[i][1].to_i - 1
end
n.times do |i|
	#print i.to_s + ": "
	(n-i-1).times do |j|
		#print (i+j+1).to_s + " "
		if coordinates[i][0] == coordinates[i+j+1][0] or coordinates[i][1] == coordinates[i+j+1][1]
			graph[i].push(i+j+1)
			graph[i+j+1].push(i)
			#print "ALALAH!"
		end
	end
	#puts
end
visited = Array.new(n,false)
require 'thread'
queue = Queue.new
components = -1
n.times do |i|
	unless visited[i]
		visited[i] = true
		queue << i
		while !queue.empty?
			v = queue.pop
			graph[v].each do |j|
				unless visited[j]
					visited[j] = true
					queue << j
				end
			end
		end
		components += 1
	end
end
puts components

#assign a value to a variable only if that variable isn't already set:
#@variable ||= "default value"

#x, y = y, x will interchange the values of x and y

#names2 = %w{  ann richard william susan pat }

#puts "Enrollments will now Stop" if participants > 2500

#Negative index values count from the end of the array

n = gets.to_i
arr = []

def dfs(start,array,n)
	return if @visited[start]

	@visited[start] = true

	(0...n).each do |i|
		if !@visited[i] && (array[i][0] == array[start][0] || array[i][1] == array[start][1])
			# puts "here"
			dfs(i,array,n)
		end
	end
end
n.times do
	arr << gets.split(" ").map(&:to_i)
end

@visited = Hash.new(false)
comp = 0
(0...n).each do |i|
	 unless @visited[i]
	 	comp += 1
	 	dfs(i,arr,n)
	 end
end
puts comp-1

class Node
  attr_accessor :visited, :nearest, :x, :y

  def initialize(x, y)
    @visited = false
    @nearest = []
    @x = x
    @y = y
  end

  def add(node)
    @nearest<< node if node.is_a? Node
  end

  def to_s
    puts "x:#{@x},y:#{@y}"
  end
end

class Graph
  attr_accessor :nodes

  def initialize
    @nodes = []
  end

  def add_node(node)
    @nodes << node if node.is_a? Node
  end

  def has_not_visited
    index = @nodes.index{|el| el.visited==false}
    index.nil? ? false : @nodes[index]
  end
end

def bfs(start)
  start.visited = true
  start.nearest.each{|el| bfs(el) unless el.visited}
end

n = gets.to_i
gr = Graph.new

n.times do
  x, y = gets.split.map(&:to_i)
  node = Node.new(x, y)

  gr.nodes.each do |n|
    if n.x == node.x || n.y ==node.y
      n.add(node)
      node.add(n)
    end
  end
  gr.add_node(node)
end

counter = -1
while gr.has_not_visited do
  counter +=1
  bfs(gr.has_not_visited)
end

puts counter

n = gets.to_i
li = []
(1..n).each do
    x, y = gets.split.collect{|i| i.to_i}
    li.push([[x],[y]])
end
ans = 0
for i in (n-1).downto(0)
    for j in 0...i
        unless (li[j][0]&li[i][0]).empty? and (li[j][1]&li[i][1]).empty?
            li[j][0] |= li[i][0]
            li[j][1] |= li[i][1]
            ans -= 1
            break
        end
    end
    ans += 1
end
puts ans-1

n = gets.to_i
li = []
(1..n).each do
    x, y = gets.split.collect{|i| i.to_i}
    li.push([[x],[y]])
end
for i in (n-1).downto(0)
    for j in 0...i
        unless (li[j][0]&li[i][0]).empty? and (li[j][1]&li[i][1]).empty?
            li[j][0] |= li[i][0]
            li[j][1] |= li[i][1]
            li.delete_at(i)
            break
        end
    end
end
puts li.size-1

# Create reachability matrix
def reach(coordinates)
    reachability = Array.new(coordinates.length) {[]}
    for i in 0..(coordinates.length - 1) do
        root = coordinates[i]
        for j in (i + 1)..(coordinates.length - 1) do
            target = coordinates[j]
            if (root[0] == target[0]) || (root[1] == target[1])
                reachability[i].push(j)
                reachability[j].push(i)
            end
        end
    end
    return reachability
end

# Run DFS
def dfs(root, reachability, traversal)
    traversal.push(root)
    for child in reachability[root] do
        unless traversal.include?(child)
            dfs(child, reachability, traversal)
        end
    end
end

# Initialize variables
coordinates = []; nodes = []; segments = 0;

# Read Input
n = gets.strip.to_i
n.times {coordinates.push(gets.strip.split.map{|s| s.to_i})}

# Create reachability matrix
reachability = reach(coordinates)
nodes = (0..n - 1).to_a

# Count connected segments
until nodes.empty?
    traversal = []; root = nodes[0];
    dfs(root, reachability, traversal)
    nodes = nodes - traversal
    segments = segments + 1;
end

# Return the number of nodes needed
puts segments - 1

def dfs(index)
  return true if @dp[index] != nil
  @dp[index] = 1
  (1..@n).each do |i|
    dfs(i) if @arr[index][i] == 1
  end
end

@n = gets.to_i
@arr = Array.new(@n+1) {Array.new(@n+1)}
nodes = []
@dp = []
(1..@n).each do |index|
  x,y = gets.split(' ').map(&:to_i)
  nodes.each_with_index do |node, i|
    if node[0] == x || node[1]== y
      @arr[index][i+1] = 1
      @arr[i+1][index] = 1
    end
  end
  nodes << [x,y]
end
components = 0
(1..@n).each do |index|
  if(@dp[index] == nil)
    components+= 1
    dfs(index)
  end
end
puts components-1

class Drift
  attr_reader :x, :y
  attr_accessor :zone, :adj
  def initialize(x, y)
    @x = x
    @y = y
    @zone = nil
    @adj = Array.new
  end
end

class Map
  attr_accessor :d, :n
  def initialize
    @d = Array.new
    @n = 0
  end
  def add(x, y)
    d << Drift.new(x, y)
    for i in 0...@n
      if d[i].x == d[@n].x || d[i].y == d[@n].y
        d[i].adj << @n
        d[@n].adj << i
      end
    end
    @n += 1
  end
  def at(n)
    @d[n]
  end
  def dfs(n, z)
    d[n].zone = z
    for i in d[n].adj
      dfs(i, z) if d[i].zone == nil
    end
  end
end

n = gets.to_i
map = Map.new
for i in 0...n
  x, y = gets.split(" ").map(&:to_i)
  map.add(x, y)
end
z = 0
for i in 0...map.n
  if map.at(i).zone == nil
    map.dfs(i, z)
    z += 1
  end
end
print z-1

ySet, connectedGraph=Array.new(gets.to_i){gets.scan(/(\d+) (\d+)/).first}.inject({}){|h, n|h.update({n[0]=>Array[n[1]]}){|k, sv, ov|sv+ov}}.values, []
connectedGraph.empty? || ySet.reject{|v|(connectedGraph.last&v).empty?}.each{|v|connectedGraph[-1]|=ySet.delete(v)}.empty? ? connectedGraph<<ySet.shift : next while !ySet.empty?
print connectedGraph.size-1

xMap=Hash.new([])
gets.to_i.times{
  gets.scan(/(\d+) (\d+)/)
  xMap[$1.to_i]+=Array[$2.to_i]
}

disjointCount=0
ySet=xMap.delete(xMap.keys.first)
while !xMap.empty?
  next if !xMap.values.select{|v|!(ySet&v).empty?}.each{|v|ySet|=xMap.delete(xMap.key(v))}.empty?
  disjointCount+=1
  ySet=xMap.delete(xMap.keys.first)
end
print disjointCount

ySet=Array.new(gets.to_i){gets.scan(/(\d+) (\d+)/).first}.inject({}){|h, n|h.update({n[0]=>Array[n[1]]}){|k, sv, ov|sv+ov}}.values
connectedGraph = [ySet.shift]
ySet.reject{|v|(connectedGraph.last&v).empty?}.each{|v|connectedGraph[-1]|=ySet.delete(v)}.empty? ? connectedGraph<<ySet.shift : next while !ySet.empty?
print connectedGraph.size-1

ySet=Array.new(gets.to_i){gets.scan(/(\d+) (\d+)/).first}.map{|v|{v[0]=>Array[v[1]]}}.inject{|h, n|h.update(n){|k, sv, ov|sv+ov}}.values
disjointCount=0
connectedGraph=ySet.shift
while !ySet.empty?
  next if !ySet.reject{|v|(connectedGraph&v).empty?}.each{|v|connectedGraph|=ySet.delete(v)}.empty?
  disjointCount+=1
  connectedGraph=ySet.shift
end
print disjointCount

ySet=Array.new(gets.to_i){gets.scan(/(\d+) (\d+)/).first}.inject({}){|h, n|h.update({n[0]=>Array[n[1]]}){|k, sv, ov|sv+ov}}.values
disjointCount=0
connectedGraph=ySet.shift
while !ySet.empty?
  next unless ySet.reject{|v|(connectedGraph&v).empty?}.each{|v|connectedGraph|=ySet.delete(v)}.empty?
  disjointCount+=1
  connectedGraph=ySet.shift
end
print disjointCount

xMap=Hash.new([])
gets.to_i.times{
  Hash[*gets.scan(/(\d+) (\d+)/)]
  xMap[$1.to_i]+=Array[$2.to_i]
}

disjointCount=0
set=xMap.delete(xMap.keys.first)
while !xMap.empty?
  next if !xMap.values.select{|v|!(set&v).empty?}.each{|v|set|=xMap.delete(xMap.key(v))}.empty?
  disjointCount+=1
  set=xMap.delete(xMap.keys.first)
end
print disjointCount

class Drift
  attr_reader :x, :y
  attr_accessor :zone
  def initialize(x, y)
    @x = x
    @y = y
    @zone = nil;
  end
end

n = gets.to_i
map = Array.new(n)
for i in 0...n
  input = gets.split
  map[i] = Drift.new(input[0].to_i, input[1].to_i)
end

z = 0
for i in 0...n
  if map[i].zone == nil
    map[i].zone = z
    for j in (i+1)...n
      map[j].zone = z unless map[i].x != map[j].x && map[i].y != map[j].y
    end
    z += 1
  end
end
print z-1

ys=Array.new(gets.to_i){gets.scan(/(\d+) (\d+)/).first}.map{|v|{v[0]=>Array[v[1]]}}.inject{|h, n|h.update(n){|k, sv, ov|sv+ov}}.values
print ys.map{|v|ys.reject{|ov|(v&ov).empty?}.inject(:|)}.uniq.size-1

n = gets.to_i
li = []
(1..n).each do
    x, y = gets.split.collect{|i| i.to_i}
    li.push([[x],[y]])
end
ans = 0
for i in (n-1).downto(0)
    for j in 0...i
        unless (li[j][0]&li[i][0]).empty? and (li[j][1]&li[i][1]).empty?
            li[i][0] |= li[j][0]
            li[i][1] |= li[j][1]
            ans -= 1
            break
        end
    end
    ans += 1
end
puts ans-1


tc = gets.to_i

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

    i = 0
    while(i < n)
        if(arr[i] > 1)
            i += 1
            break
        end
        i += 1
    end
    i -= 1
    if(i% 2 == 0)
        puts "First"
    else
        puts "Second"
    end
end

t = gets.to_i

1.upto t do |z|
  n = gets.to_i
  a = gets.split.map(&:to_i)
  c = 0

  if n == 1
    c = 0
  else
    0.upto n-1 do |i|
      if a[i] == 1
        c += 1
      else
        break
      end
    end
  end

  if c == n
    if c.odd?
      puts "First"
    else
      puts "Second"
    end
  else
    if c.even? or c == 0
      puts "First"
    else
      puts "Second"
    end
  end
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    k = 0
    k += 1 while k < n && a[k] == 1
    if k == n
        res = k % 2 == 0 ? "Second" : "First"
    else
        res = k % 2 == 0 ? "First" : "Second"
    end
    puts res
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    k = 0
    k += 1 while k < n && a[k] == 1
    if k == n
        res = k % 2 == 0 ? "Secobd" : "First"
    else
        res = k % 2 == 0 ? "First" : "Second"
    end
    puts res
end


s = [nil] + gets.chomp.chars
n = gets.to_i
c = Array.new(s.size / 2 + 1, 0)
a = gets.split.map(&:to_i).each{|x| c[x] += 1}
for i in 2...c.size do
  c[i] += c[i - 1]
end
for i in 1...c.size do
  if c[i] % 2 == 0
    next
  else
    s[i], s[s.size - i] = s[s.size - i], s[i]
  end
end
puts s.join("")

s = gets.chomp.split('')
m = gets.to_i
cnt = Array.new((s.length+1)/2+1,0)
gets.split.collect{|i| cnt[i.to_i]+=1}
for i in 1..(s.length+1)/2
    cnt[i] = (cnt[i-1]+cnt[i])%2
    if cnt[i] > 0
        s[i-1], s[s.length-i] = s[s.length-i], s[i-1]
    end
end
puts s.join

# f = File.open 'input.txt', 'r'
f = STDIN

def compute(s, positions)
  marked_positions = Hash.new(0)
  positions.each do |pos|
    marked_positions[pos] += 1
  end
  be_changed = false
  1.upto(s.length/2) do |index|
    # index - 1 && s.length - index
    be_changed = !be_changed if marked_positions[index].odd?
    if be_changed
      tmp = s[index - 1]
      s[index - 1] = s[s.length - index]
      s[s.length - index] = tmp
    end
  end
  s
end

while true
  raw_s = f.gets
  break unless raw_s
  s = raw_s.chomp
  m = f.gets.chomp.to_i
  positions = f.gets.chomp.split.map &:to_i
  puts compute(s, positions)
end

s = [nil] + gets.chomp.chars
n = gets.to_i
c = Array.new(s.size / 2 + 1, 0)
a = gets.split.map(&:to_i).each{|x| c[x] += 1}
for i in 2...c.size do
  c[i] += c[i - 1]
end
p c
for i in 1...c.size do
  if c[i] % 2 == 0
    next
  else
    s[i], s[s.size - i] = s[s.size - i], s[i]
  end
end
puts s.join("")

# f = File.open 'input.txt', 'r'
f = STDIN

def compute(s, positions)
  marked_positions = Hash.new(0)
  positions.each do |pos|
    marked_positions[pos] += 1
  end
  be_changed = false
  1.upto(s.length/2) do |index|
    # index - 1 && s.length - index
    be_changed = !be_changed if marked_positions[index].odd?
    if be_changed
      tmp = s[index - 1]
      s[index - 1] = s[s.length - index]
      s[s.length - index] = tmp
    end
  end
  s
end

while true
  raw_s = f.gets
  break unless raw_s
  s = raw_s.chomp
  m = f.gets.chomp.to_i
  positions = f.gets.chomp.split.map &:to_i
  p compute(s, positions)
end