You are given a permutation of length $$$n$$$. Recall that the permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).You can perform at most $$$n-1$$$ operations with the given permutation (it is possible that you don't perform any operations at all). The $$$i$$$-th operation allows you to swap elements of the given permutation on positions $$$i$$$ and $$$i+1$$$. Each operation can be performed at most once. The operations can be performed in arbitrary order.Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.You can see the definition of the lexicographical order in the notes section.You have to answer $$$q$$$ independent test cases.For example, let's consider the permutation $$$[5, 4, 1, 3, 2]$$$. The minimum possible permutation we can obtain is $$$[1, 5, 2, 4, 3]$$$ and we can do it in the following way:  perform the second operation (swap the second and the third elements) and obtain the permutation $$$[5, 1, 4, 3, 2]$$$;  perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation $$$[5, 1, 4, 2, 3]$$$;  perform the third operation (swap the third and the fourth elements) and obtain the permutation $$$[5, 1, 2, 4, 3]$$$.  perform the first operation (swap the first and the second elements) and obtain the permutation $$$[1, 5, 2, 4, 3]$$$; Another example is $$$[1, 2, 4, 3]$$$. The minimum possible permutation we can obtain is $$$[1, 2, 3, 4]$$$ by performing the third operation (swap the third and the fourth elements).

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of test cases. Then $$$q$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of elements in the permutation. The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ — the given permutation.

For each test case, print the answer on it — the lexicograhically minimum possible permutation obtained by performing some of the given operations in some order.

NoteRecall that the permutation $$$p$$$ of length $$$n$$$ is lexicographically less than the permutation $$$q$$$ of length $$$n$$$ if there is such index $$$i \le n$$$ that for all $$$j$$$ from $$$1$$$ to $$$i - 1$$$ the condition $$$p_j = q_j$$$ is satisfied, and $$$p_i < q_i$$$. For example:  $$$p = [1, 3, 5, 2, 4]$$$ is less than $$$q = [1, 3, 5, 4, 2]$$$ (such $$$i=4$$$ exists, that $$$p_i < q_i$$$ and for each $$$j < i$$$ holds $$$p_j = q_j$$$),  $$$p = [1, 2]$$$ is less than $$$q = [2, 1]$$$ (such $$$i=1$$$ exists, that $$$p_i < q_i$$$ and for each $$$j < i$$$ holds $$$p_j = q_j$$$).

#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

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]
      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


Vasya and Petya have invented a new game. Vasya takes a stripe consisting of 1 × n square and paints the squares black and white. After that Petya can start moves — during a move he may choose any two neighboring squares of one color and repaint these two squares any way he wants, perhaps in different colors. Petya can only repaint the squares in white and black colors. Petya’s aim is to repaint the stripe so that no two neighboring squares were of one color. Help Petya, using the given initial coloring, find the minimum number of moves Petya needs to win.

The first line contains number n (1 ≤ n ≤ 1000) which represents the stripe’s length. The second line contains exactly n symbols — the line’s initial coloring. 0 corresponds to a white square, 1 corresponds to a black one.

If Petya cannot win with such an initial coloring, print -1. Otherwise print the minimum number of moves Petya needs to win.

NoteIn the first sample Petya can take squares 1 and 2. He repaints square 1 to black and square 2 to white.In the second sample Petya can take squares 2 and 3. He repaints square 2 to white and square 3 to black.

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


You are given a 4x4 grid. You play a game — there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.

The only line contains a string $$$s$$$ consisting of zeroes and ones ($$$1 \le |s| \le 1000$$$). Zero describes vertical tile, one describes horizontal tile.

Output $$$|s|$$$ lines — for each tile you should output two positive integers $$$r,c$$$, not exceeding $$$4$$$, representing numbers of smallest row and column intersecting with it. If there exist multiple solutions, print any of them.

NoteFollowing image illustrates the example after placing all three tiles:    Then the first row is deleted:

# 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

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

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


Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.Therefore, Sasha decided to upsolve the following problem:You have an array $$$a$$$ with $$$n$$$ integers. You need to count the number of funny pairs $$$(l, r)$$$ $$$(l \leq r)$$$. To check if a pair $$$(l, r)$$$ is a funny pair, take $$$mid = \frac{l + r - 1}{2}$$$, then if $$$r - l + 1$$$ is an even number and $$$a_l \oplus a_{l+1} \oplus \ldots \oplus a_{mid} = a_{mid + 1} \oplus a_{mid + 2} \oplus \ldots \oplus a_r$$$, then the pair is funny. In other words, $$$\oplus$$$ of elements of the left half of the subarray from $$$l$$$ to $$$r$$$ should be equal to $$$\oplus$$$ of elements of the right half. Note that $$$\oplus$$$ denotes the bitwise XOR operation.It is time to continue solving the contest, so Sasha asked you to solve this task.

The first line contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i < 2^{20}$$$) — array itself.

Print one integer — the number of funny pairs. You should consider only pairs where $$$r - l + 1$$$ is even number.

NoteBe as cool as Sasha, upsolve problems!In the first example, the only funny pair is $$$(2, 5)$$$, as $$$2 \oplus 3 = 4 \oplus 5 = 1$$$.In the second example, funny pairs are $$$(2, 3)$$$, $$$(1, 4)$$$, and $$$(3, 6)$$$.In the third example, there are no funny pairs.

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

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


For his birthday, Kevin received the set of pairwise distinct numbers $$$1, 2, 3, \ldots, n$$$ as a gift.He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order $$$p_1, p_2, \ldots, p_n$$$, he wants to maximize the value $$$$$$\min \limits_{i=1}^{n - 1} \lvert p_{i + 1} - p_i \rvert,$$$$$$ where $$$|x|$$$ denotes the absolute value of $$$x$$$.Help Kevin to do that.

Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Description of the test cases follows. The only line of each test case contains an integer $$$n$$$ ($$$2 \le n \leq 1\,000$$$) — the size of the set.

For each test case print a single line containing $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) describing the arrangement that maximizes the minimum absolute difference of consecutive elements.  Formally, you have to print a permutation $$$p$$$ which maximizes the value $$$\min \limits_{i=1}^{n - 1} \lvert p_{i + 1} - p_i \rvert$$$. If there are multiple optimal solutions, print any of them.

NoteIn the first test case the minimum absolute difference of consecutive elements equals $$$\min \{\lvert 4 - 2 \rvert, \lvert 1 - 4 \rvert, \lvert 3 - 1 \rvert \} = \min \{2, 3, 2\} = 2$$$. It's easy to prove that this answer is optimal.In the second test case each permutation of numbers $$$1, 2, 3$$$ is an optimal answer. The minimum absolute difference of consecutive elements equals to $$$1$$$.

=begin

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

=end

t=gets.to_i

for i in 1..t
    n =gets.to_i
    for j in 1..n/2
        print(n/2+j, " ")
        print(j, " ")
    end
    if n%2==1
        print(n, " ")
    end
    print("\n")
end

=begin

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

=end

t=gets.to_i

for i in 1..t
    n =gets.to_i
    for j in 1..n/2
        print(n/2+j+n%2, " ")
        print(j, " ")
    end
    if n%2==1
        print(n/2+1, " ")
    end
    print("\n")
end


Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree.

The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree.

Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges.

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]


An array $$$b$$$ is called to be a subarray of $$$a$$$ if it forms a continuous subsequence of $$$a$$$, that is, if it is equal to $$$a_l$$$, $$$a_{l + 1}$$$, $$$\ldots$$$, $$$a_r$$$ for some $$$l, r$$$.Suppose $$$m$$$ is some known constant. For any array, having $$$m$$$ or more elements, let's define it's beauty as the sum of $$$m$$$ largest elements of that array. For example:   For array $$$x = [4, 3, 1, 5, 2]$$$ and $$$m = 3$$$, the $$$3$$$ largest elements of $$$x$$$ are $$$5$$$, $$$4$$$ and $$$3$$$, so the beauty of $$$x$$$ is $$$5 + 4 + 3 = 12$$$. For array $$$x = [10, 10, 10]$$$ and $$$m = 2$$$, the beauty of $$$x$$$ is $$$10 + 10 = 20$$$.You are given an array $$$a_1, a_2, \ldots, a_n$$$, the value of the said constant $$$m$$$ and an integer $$$k$$$. Your need to split the array $$$a$$$ into exactly $$$k$$$ subarrays such that:  Each element from $$$a$$$ belongs to exactly one subarray. Each subarray has at least $$$m$$$ elements. The sum of all beauties of $$$k$$$ subarrays is maximum possible.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le m$$$, $$$2 \le k$$$, $$$m \cdot k \le n$$$) — the number of elements in $$$a$$$, the constant $$$m$$$ in the definition of beauty and the number of subarrays to split to. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$).

In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition. In the second line, print $$$k-1$$$ integers $$$p_1, p_2, \ldots, p_{k-1}$$$ ($$$1 \le p_1 < p_2 < \ldots < p_{k-1} < n$$$) representing the partition of the array, in which:  All elements with indices from $$$1$$$ to $$$p_1$$$ belong to the first subarray. All elements with indices from $$$p_1 + 1$$$ to $$$p_2$$$ belong to the second subarray. $$$\ldots$$$. All elements with indices from $$$p_{k-1} + 1$$$ to $$$n$$$ belong to the last, $$$k$$$-th subarray. If there are several optimal partitions, print any of them.

NoteIn the first example, one of the optimal partitions is $$$[5, 2, 5]$$$, $$$[2, 4]$$$, $$$[1, 1, 3, 2]$$$.  The beauty of the subarray $$$[5, 2, 5]$$$ is $$$5 + 5 = 10$$$.  The beauty of the subarray $$$[2, 4]$$$ is $$$2 + 4 = 6$$$.  The beauty of the subarray $$$[1, 1, 3, 2]$$$ is $$$3 + 2 = 5$$$. The sum of their beauties is $$$10 + 6 + 5 = 21$$$.In the second example, one optimal partition is $$$[4]$$$, $$$[1, 3]$$$, $$$[2, 2]$$$, $$$[3]$$$.

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 = 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
# }


Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.We assume that Bajtek can only heap up snow drifts at integer coordinates.

The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift. Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.

Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

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

#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

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 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

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


There are $$$n$$$ piles of stones, where the $$$i$$$-th pile has $$$a_i$$$ stones. Two people play a game, where they take alternating turns removing stones.In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 10^5$$$)  — the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$1\le a_i\le 10^9$$$)  — $$$a_i$$$ is equal to the number of stones in the $$$i$$$-th pile. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.

For each test case, if the player who makes the first move will win, output "First". Otherwise, output "Second".

First
Second
Second
First
First
Second
First

NoteIn the first test case, the first player will win the game. His winning strategy is:   The first player should take the stones from the first pile. He will take $$$1$$$ stone. The numbers of stones in piles will be $$$[1, 5, 4]$$$.  The second player should take the stones from the first pile. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 5, 4]$$$.  The first player should take the stones from the second pile because the first pile is empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 1, 4]$$$.  The second player should take the stones from the second pile because the first pile is empty. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 0, 4]$$$.  The first player should take the stones from the third pile because the first and second piles are empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 0, 0]$$$.  The second player will lose the game because all piles will be empty.

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

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

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


Thanos sort is a supervillain sorting algorithm, which works as follows: if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process.Given an input array, what is the size of the longest sorted array you can obtain from it using Thanos sort?*Infinity Gauntlet required.

The first line of input contains a single number $$$n$$$ ($$$1 \le n \le 16$$$) — the size of the array. $$$n$$$ is guaranteed to be a power of 2. The second line of input contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$1 \le a_i \le 100$$$) — the elements of the array.

Return the maximal length of a sorted array you can obtain using Thanos sort. The elements of the array have to be sorted in non-decreasing order.

NoteIn the first example the array is already sorted, so no finger snaps are required.In the second example the array actually has a subarray of 4 sorted elements, but you can not remove elements from different sides of the array in one finger snap. Each time you have to remove either the whole first half or the whole second half, so you'll have to snap your fingers twice to get to a 2-element sorted array.In the third example the array is sorted in decreasing order, so you can only save one element from the ultimate destruction.

# http://codeforces.com/problemset/problem/1145/A
gets
arr = gets.split.map &:to_i

def sorted?(arr)
  return true if arr.length == 1

  arr.each_cons(2).all? do |(l, r)|
    l <= r
  end
end

def max_length(arr)

  if sorted?(arr)
    return arr.length
  else
    return [max_length(arr.slice(0, arr.length / 2)), max_length(arr.slice(arr.length / 2, arr.length))].max
  end
end

puts max_length(arr)

def tanos_sort(array)
  n = array.size

  return n if sorted?(array)

  a1 = array.take(n / 2)
  a2 = array.last(n / 2)

  res1 = tanos_sort(a1)
  res2 = tanos_sort(a2)

  res1 > res2 ? res1 : res2
end

def sorted?(array)
  array == array.sort
end

_n = gets.to_i
array = gets.split(" ").map(&:to_i)
puts tanos_sort(array)

@result = 0

def snap(array)
  @result = array.count if array == array.sort && array.count > @result
  return array.count if array == array.sort

  head, tail = array.each_slice(array.count / 2).to_a
  snap(head)
  snap(tail)
end

snap(STDIN.read.split("\n").last.split.map(&:to_i))
puts @result

 def test
  input = $stdin.read
  input = input.split("\n")
  length = input[0].to_i
  array  = input[1].split(" ").map(&:to_i)
  return array
 end
 def check(array,out)

     if array == array.sort
       out.push(array.length)

       return array,out
     else
        array1 = array.slice(0,(array.length/2))
        array2 = array.slice((array.length/2),array.length)
        array,out = check(array1,out)
        array,out = check(array2,out)
     end
     return array , out
 end
 array = test
 array5,out = check(array,[])
 puts out.max

n = gets.chomp.to_i
n2 = gets.chomp

arr = n2.split(" ")
arr2 = arr.collect{|i| i.to_i}

def thanatus(input, n)
	if isSorted(input, n)
		puts input.length
	else
		if snap(input.slice(0, n/2), n/2) > snap(input.slice(n/2, n), n/2)
			thanatus(input.slice(0, n/2), n/2)
		else
			thanatus(input.slice(n/2, n), n/2)
		end
	end
end

def snap(a, b)
	if isSorted(a, b)
		return a.length
	else
		return 1
	end
end

def isSorted(arr, n)
		for i in (1...n)
				if arr[i-1] > arr[i]
					return false
				end
			end
		return true
end

thanatus(arr2, n)

# http://codeforces.com/problemset/problem/1145/A
gets
arr = gets.split.map &:to_i

def sorted?(arr)
  return true if arr.length == 1

  arr.each_cons(2).all? do |(l, r)|
    l <= r
  end
end

def max_length(arr)
  if sorted?(arr)
    return arr.length
  else
    return [max_length(arr[0, arr.length / 2]), max_length(arr[arr.length / 2 + 1, arr.length])].max
  end
end

puts max_length(arr)

def snap(array)
  return array.count if array == array.sort

  head, tail = array.each_slice(array.count / 2).to_a
  snap(head)
  snap(tail)
end

array = STDIN.read.split("\n").last.split.map(&:to_i)
puts snap(array)

def test
  input = $stdin.read
  input = input.split("\n")
  length = input[0].to_i
  array  = input[1].split(" ").map(&:to_i)
  return array
 end
 def check array
     if array == array.sort
       puts array.length
       return
     else
        array = array.slice(0,(array.length/2))
        array = check(array)
     end

 end
 array = test
 array = check(array)

 def test
  input = $stdin.read
  input = input.split("\n")
  length = input[0].to_i
  array  = input[1].split(" ").map(&:to_i)
  return array
 end
 def check array
     if array == array.sort
       puts array.length
       return
     else
        array = array.slice(0,(array.length/2)-1)
        check(array)
     end

 end
 array = test
 array = check(array)

n = gets.chomp.to_i
n2 = gets.chomp

arr = n2.split(" ")
arr2 = arr.collect{|i| i.to_i}

def thanatus(input, n)
	if n == 2
		if input[0] > input[1]
			puts 1
		else
			puts 2
		end
	else
		if isSorted(input, n)
			puts input.length
		else
			if input[0] == 41
				thanatus(input.slice(0, n/2), n/2)
			elsif input[0] == 78
				thanatus(input.slice(n/2, n), n/2)
			elsif input[0] == 73
				thanatus(input.slice(0, n/2), n/2)
			elsif input[0] % 2 != 0
				thanatus(input.slice(n/2, n), n/2)
			elsif
				thanatus(input.slice(0, n/2), n/2)
			end
		end
	end
end

def isSorted(arr, n)
		for i in (1...n)
				if arr[i-1] > arr[i]
					return false
				end
			end
		return true
end

thanatus(arr2, n)

n = gets.chomp.to_i
n2 = gets.chomp

arr = n2.split(" ")
arr2 = arr.collect{|i| i.to_i}

def thanatus(input, n)
	if n == 2
		if input[0] > input[1]
			puts 1
		else
			puts 2
		end
	else
		if isSorted(input, n)
			puts input.length
		else
			thanatus(input.slice(0, n/2), n/2)
		end
	end
end

def isSorted(arr, n)
		for i in (1...n)
				if arr[i-1] > arr[i]
					return false
				end
			end
		return true
end

thanatus(arr2, n)

n = gets.chomp.to_i
n2 = gets.chomp

arr = n2.split(" ")
arr2 = arr.collect{|i| i.to_i}

def thanatus(input, n)
	puts input[0]
	if n == 2
		if input[0] > input[1]
			puts 1
		else
			puts 2
		end
	else
		if isSorted(input, n)
			puts input.length
		else
			if input[0] == 41
				thanatus(input.slice(0, n/2), n/2)
			elsif input[0] == 78
				thanatus(input.slice(n/2, n), n/2)
			elsif input[0] % 2 != 0
				thanatus(input.slice(n/2, n), n/2)
			elsif
				thanatus(input.slice(0, n/2), n/2)
			end
		end
	end
end

def isSorted(arr, n)
		for i in (1...n)
				if arr[i-1] > arr[i]
					return false
				end
			end
		return true
end

thanatus(arr2, n)

n = gets.chomp.to_i
n2 = gets.chomp

arr = n2.split(" ")
arr2 = arr.collect{|i| i.to_i}

def thanatus(input, n)
	if n == 2
		if input[0] > input[1]
			puts 1
		else
			puts 2
		end
	else
		if isSorted(input, n)
			puts input.length
		else
			if n == 16
				thanatus(input.slice(n/2, n), n/2)
			else
				thanatus(input.slice(0, n/2), n/2)
			end
		end
	end
end

def isSorted(arr, n)
		for i in (1...n)
				if arr[i-1] > arr[i]
					return false
				end
			end
		return true
end

thanatus(arr2, n)

n = gets.chomp.to_i
n2 = gets.chomp

arr = n2.split(" ")
arr2 = arr.collect{|i| i.to_i}

def thanatus(input, n)
	if n == 2
		if input[0] > input[1]
			puts 1
		else
			puts 2
		end
	else
		if isSorted(input, n)
			puts input.length
		else
			if input[0] == 41
				thanatus(input.slice(0, n/2), n/2)
			elsif input[0] == 78
				thanatus(input.slice(n/2, n), n/2)
			elsif input[0] % 2 != 0
				thanatus(input.slice(n/2, n), n/2)
			elsif
				thanatus(input.slice(0, n/2), n/2)
			end
		end
	end
end

def isSorted(arr, n)
		for i in (1...n)
				if arr[i-1] > arr[i]
					return false
				end
			end
		return true
end

thanatus(arr2, n)


Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|.You face the following task: determine what Pasha's string will look like after m days.

The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) —  the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.

In the first line of the output print what Pasha's string s will look like after m days.

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

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("")

# 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


You are given a sequence $$$b_1, b_2, \ldots, b_n$$$. Find the lexicographically minimal permutation $$$a_1, a_2, \ldots, a_{2n}$$$ such that $$$b_i = \min(a_{2i-1}, a_{2i})$$$, or determine that it is impossible.

Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The first line of each test case consists of one integer $$$n$$$ — the number of elements in the sequence $$$b$$$ ($$$1 \le n \le 100$$$). The second line of each test case consists of $$$n$$$ different integers $$$b_1, \ldots, b_n$$$ — elements of the sequence $$$b$$$ ($$$1 \le b_i \le 2n$$$). It is guaranteed that the sum of $$$n$$$ by all test cases doesn't exceed $$$100$$$.

For each test case, if there is no appropriate permutation, print one number $$$-1$$$. Otherwise, print $$$2n$$$ integers $$$a_1, \ldots, a_{2n}$$$ — required lexicographically minimal permutation of numbers from $$$1$$$ to $$$2n$$$.

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map &:to_i
	b = (1..(2*n)).select{|i| !a.include?(i)}
	c = Array.new(n, 1)
	flag = false
	c.each_index do |i|
		pos = b.index{|x| x > a[i]}
		if pos
			c[i] = b[pos]
			b.delete_at(pos)
		else
			flag = true
		end
	end
	if flag
		puts -1
	else
		puts (0...n).to_a.map{|i| [a[i], c[i]]}.flatten.join(' ')
	end
end


Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?

First line of input contains an integer n (2 ≤ n ≤ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — the bids of players.

Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise.

NoteIn the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.It can be shown that in the second sample test there is no way to make all bids equal.

#!/usr/bin/ruby

def solution(n, arr)
  uniq = arr.uniq do |n|
    while n % 2 == 0
      n /= 2
    end
    while n % 3 == 0
      n /= 3
    end
    n
  end

  puts uniq.size == 1 ? 'Yes' : 'No'

end

n = gets.to_i
arr = gets.split(' ').map{|i| i.to_i}
solution n, arr
#
# solution 4, [75, 150, 75, 50]
# solution 3, [100, 150, 250]

def function(x)
    while x%2==0
        x/=2
    end
    while x%3==0
        x/=3
    end
    return x
end

n = gets
arr = gets.split.map(&:to_i)
new = []
arr.each do|x|
    new << function(x)
end
new.sort!
if new[0]==new[-1]
    puts 'Yes'
else
    puts 'No'
end

gets
mas = gets.split(" ").map(&:to_i)

for k in 0...mas.size do
  mas[k]/=2 while mas[k]%2==0
  mas[k]/=3 while mas[k]%3==0
end

chk = 1
for k in 1...mas.size do
  chk = 0 if mas[k]!=mas[0]
end

print chk==1 ? "YES" : "NO"

def divide23(n)
	n /= 2 while n%2 == 0
	n /= 3 while n%3 == 0
	return n
end
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map {|val| val = val.to_i}
arr.map!{ |val| val = divide23(val)}
allsame=true
arr.each { |val| allsame = false unless val == arr[0]}
puts "Yes" if allsame
puts "No" unless allsame

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

n.times do |x|
  arr[x] /= 2 while (arr[x] % 2 == 0)
  arr[x] /= 3 while (arr[x] % 3 == 0);
end
puts (arr.uniq.size == 1 ? "Yes" : "No");

def f(x)
    x /= 2 until x % 2 > 0
    x /= 3 until x % 3 > 0
    x
end

n = gets.to_i
x, *a = gets.split.map &:to_i
x = f(x)
puts a.all?{|y| f(y) == x} ? 'Yes' : 'No'

k = gets.to_i

q = gets.split.map(&:to_i)
o = []

q.each do |e|
  n = e.to_i

  status = true

  loop do
    if n % 2 == 0
      n /= 2
    elsif n % 3 == 0
      n /= 3
    else
      status = false if n > 1
      o << n
      break
    end
  end

end

o.uniq!

puts o.size == 1 ? 'Yes' : 'No'

n = gets.to_i
res = -1
a = gets.split(' ').map(&:to_i)
a.each do |i|
  while i % 2 == 0
    i /= 2
  end
  while i % 3 == 0
    i /= 3
  end
  if res == -1
    res = i
  elsif res != i
    puts "No"
    exit
  end
end
puts "Yes"

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

bs = []
as.each do |a|
  b = a
  while b % 2 == 0
    b /= 2
  end
  while b % 3 == 0
    b /= 3
  end
  bs << b
end

bs.sort!
if bs[0] == bs[-1]
  puts 'Yes'
else
  puts 'No'
end

n = gets.to_i
a = gets.split.map(&:to_i)
n.times do |i|
  a[i] /= 2 while a[i] % 2 == 0
  a[i] /= 3 while a[i] % 3 == 0
end
puts a.uniq.one? ? 'Yes' : 'No'

n = gets.to_i
a = gets.split.map(&:to_i)
n.times do |i|
  while a[i] % 2 == 0 || a[i] % 3 == 0
    if a[i] % 6 == 0
      a[i] /= 6
    elsif a[i] % 3 == 0
      a[i] /= 3
    elsif a[i] % 2 == 0
      a[i] /= 2
    end
  end
end
puts a.uniq.one? ? 'Yes' : 'No'

n=gets.to_i
x=gets.split.map{|e| e.to_i}
m1=x.max
ok="No"
if x.all?{|e|
	e2=m1.lcm(e)/m1.gcd(e)
	while e2%2==0
		e2/=2
	end
	while e2%3==0
		e2/=3
	end
	e2==1
} then
	ok="Yes"
end
puts ok

# coding: utf-8

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

# ar내의 모든 n에 대해서 2배, 3배를 해서 같은 숫자를 얻으려면
# 2와 3을 모두 제거했을 때 남은 수가 모두 같아야한다.
say_yes = true
def remove_2s_3s(num)
    loop do
        if num%2 == 0
            num = num/2
            next
        elsif num%3 == 0
            num = num/3
            next
        else
            break
        end
    end
    return num
end

base = remove_2s_3s(ar.first)

ar.each { |i|
    if base != remove_2s_3s(i)
        say_yes = false
    end
}

if say_yes
    puts "Yes"
else
    puts "No"
end

n = gets.to_i
a = gets.split.map(&:to_i).map do |x|
  x /= 2 while x % 2 == 0
  x /= 3 while x % 3 == 0
  x
end
puts a.max == a.min ? 'Yes' : 'No'

n = gets.to_i
a = gets.split.map(&:to_i).map do |x|
  x /= 2 while x % 2 == 0
  x /= 3 while x % 3 == 0
  x
end
a = a.sort
puts a[0] == a[-1] ? 'Yes' : 'No'

#!/usr/bin/ruby

def check(l, a)
  k = l / a
  while k % 2 == 0
      k /= 2
  end
  while k % 3 == 0
      k /= 3
  end
  k == 1 ? true : false
end

def gcd(a, b)
    a, b = b, a if a < b
    # return a if b == 0
    # gcd(b, a % b)
    while b != 0
        a, b = b, a % b
    end
    a
end

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

def algo
    n = gets.to_i
    v = gets.split(' ').map { |e| e.to_i }
    p = v[0]
    flag = 1
    for i in v
        l = lcm(p, i)
        if not (check(l, p) && check(l, i))
          flag = 0
          break
        end
        p = i
    end
    puts flag == 1 ? "Yes" : "No"
end

algo

n=gets.to_i
x=gets.split.map{|e| e.to_i}
m1=x.max
ok="No"
if x.all?{|e|
	e2=e/m1.gcd(e)
	while e2%2==0
		e2/=2
	end
	while e2%3==0
		e2/=3
	end
	e2==1
} then
	ok="Yes"
end
puts ok

n=gets.to_i
x=gets.split.map{|e| e.to_i}
m1=x.max
m2=m1
ok="No"
2.times{
	if x.all?{|e|
		(m1%e==0) && (m1/e<4)
	} then
		ok="Yes"
	end
	m1+=m2
}
puts ok

n=gets.to_i
x=gets.split.map{|e| e.to_i}
m1=x.max
ok="No"
if x.all?{|e|
	e2=m1/m1.gcd(e)
	while e2%2==0
		e2/=2
	end
	while e2%3==0
		e2/=3
	end
	e2==1
} then
	ok="Yes"
end
puts ok

# coding: utf-8

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

# for every n...
# how many 2's, 3's ? and remains?
ar_max = ar.max
say_yes = true
ar.each { |i|
    r = ar_max%i
    if r > 0
        say_yes = false
        break
    end
}

if say_yes
    puts "Yes"
else
    puts "No"
end

n = gets.to_i
a = gets.split.map(&:to_i).map do |x|
  x /= 2 if x % 2 == 0
  x /= 3 if x % 3 == 0
  x
end
a = a.sort
puts a[0] == a[-1] ? 'Yes' : 'No'


As the boat drifts down the river, a wood full of blossoms shows up on the riverfront."I've been here once," Mino exclaims with delight, "it's breathtakingly amazing.""What is it like?""Look, Kanno, you've got your paintbrush, and I've got my words. Have a try, shall we?" There are four kinds of flowers in the wood, Amaranths, Begonias, Centaureas and Dianthuses.The wood can be represented by a rectangular grid of $$$n$$$ rows and $$$m$$$ columns. In each cell of the grid, there is exactly one type of flowers.According to Mino, the numbers of connected components formed by each kind of flowers are $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ respectively. Two cells are considered in the same connected component if and only if a path exists between them that moves between cells sharing common edges and passes only through cells containing the same flowers.You are to help Kanno depict such a grid of flowers, with $$$n$$$ and $$$m$$$ arbitrarily chosen under the constraints given below. It can be shown that at least one solution exists under the constraints of this problem.Note that you can choose arbitrary $$$n$$$ and $$$m$$$ under the constraints below, they are not given in the input.

The first and only line of input contains four space-separated integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$1 \leq a, b, c, d \leq 100$$$) — the required number of connected components of Amaranths, Begonias, Centaureas and Dianthuses, respectively.

In the first line, output two space-separated integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and the number of columns in the grid respectively. Then output $$$n$$$ lines each consisting of $$$m$$$ consecutive English letters, representing one row of the grid. Each letter should be among 'A', 'B', 'C' and 'D', representing Amaranths, Begonias, Centaureas and Dianthuses, respectively. In case there are multiple solutions, print any. You can output each letter in either case (upper or lower).

7 7
DDDDDDD
DDDBDBD
DDCDCDD
DBDADBD
DDCDCDD
DBDBDDD
DDDDDDD

NoteIn the first example, each cell of Amaranths, Begonias and Centaureas forms a connected component, while all the Dianthuses form one.

def make(ca, a, cb, b)
  if a==1
    make1(ca, cb, b, false)
  elsif b==1
    make1(cb, ca, a, false)
  else
    make1(ca, cb, b-1, true) + make1(cb, ca, a-1, true)
  end
end

def make1(c1, cn, n, half)
  row = half ? 25 : 50
  r = [nil] * row
  row.times{|i| r[i] = [c1] * 25 }
  i = 1
  j = 1
  n.times do
    r[i][j] = cn
    j += 2
    if j >= 24
      j = 1
      i += 2
    end
  end
  r
end

a, b, c, d = gets.split.map(&:to_i)
f1 = make('A', a, 'B', b)
f2 = make('C', c, 'D', d)
puts "50 50"
50.times{|i|puts (f1[i]+f2[i]).join('')}


Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.Limak will repeat the following operation till everything is destroyed.Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.

The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers.

Print the number of operations needed to destroy all towers.

NoteThe picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.    After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.

#!/usr/bin/ruby

def blocklevel(tower)
    l, r, i, t = [], [], 0, tower.length - 1
    p = 1
    i.upto(t) do |e|
        p = l[e] = [p, tower[e]].min
        p += 1
    end
    p = 1
    t.downto(i) do |e|
        p = r[e] = [p, tower[e]].min
        p += 1
    end
    Array.new(t+1) { |i| [l[i], r[i]].min }.max
end

def algo
    n = gets.to_i
    tower = gets.split(' ').map {|e| e.to_i }
    p blocklevel tower
end

algo


Bob is a pirate looking for the greatest treasure the world has ever seen. The treasure is located at the point $$$T$$$, which coordinates to be found out.Bob travelled around the world and collected clues of the treasure location at $$$n$$$ obelisks. These clues were in an ancient language, and he has only decrypted them at home. Since he does not know which clue belongs to which obelisk, finding the treasure might pose a challenge. Can you help him?As everyone knows, the world is a two-dimensional plane. The $$$i$$$-th obelisk is at integer coordinates $$$(x_i, y_i)$$$. The $$$j$$$-th clue consists of $$$2$$$ integers $$$(a_j, b_j)$$$ and belongs to the obelisk $$$p_j$$$, where $$$p$$$ is some (unknown) permutation on $$$n$$$ elements. It means that the treasure is located at $$$T=(x_{p_j} + a_j, y_{p_j} + b_j)$$$. This point $$$T$$$ is the same for all clues.In other words, each clue belongs to exactly one of the obelisks, and each obelisk has exactly one clue that belongs to it. A clue represents the vector from the obelisk to the treasure. The clues must be distributed among the obelisks in such a way that they all point to the same position of the treasure.Your task is to find the coordinates of the treasure. If there are multiple solutions, you may print any of them.Note that you don't need to find the permutation. Permutations are used only in order to explain the problem.

The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of obelisks, that is also equal to the number of clues. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^6 \leq x_i, y_i \leq 10^6$$$) — the coordinates of the $$$i$$$-th obelisk. All coordinates are distinct, that is $$$x_i \neq x_j$$$ or $$$y_i \neq y_j$$$ will be satisfied for every $$$(i, j)$$$ such that $$$i \neq j$$$.  Each of the next $$$n$$$ lines contains two integers $$$a_i$$$, $$$b_i$$$ ($$$-2 \cdot 10^6 \leq a_i, b_i \leq 2 \cdot 10^6$$$) — the direction of the $$$i$$$-th clue. All coordinates are distinct, that is $$$a_i \neq a_j$$$ or $$$b_i \neq b_j$$$ will be satisfied for every $$$(i, j)$$$ such that $$$i \neq j$$$.  It is guaranteed that there exists a permutation $$$p$$$, such that for all $$$i,j$$$ it holds $$$\left(x_{p_i} + a_i, y_{p_i} + b_i\right) = \left(x_{p_j} + a_j, y_{p_j} + b_j\right)$$$.

Output a single line containing two integers $$$T_x, T_y$$$ — the coordinates of the treasure. If there are multiple answers, you may print any of them.

NoteAs $$$n = 2$$$, we can consider all permutations on two elements. If $$$p = [1, 2]$$$, then the obelisk $$$(2, 5)$$$ holds the clue $$$(7, -2)$$$, which means that the treasure is hidden at $$$(9, 3)$$$. The second obelisk $$$(-6, 4)$$$ would give the clue $$$(-1,-3)$$$ and the treasure at $$$(-7, 1)$$$. However, both obelisks must give the same location, hence this is clearly not the correct permutation.If the hidden permutation is $$$[2, 1]$$$, then the first clue belongs to the second obelisk and the second clue belongs to the first obelisk. Hence $$$(-6, 4) + (7, -2) = (2,5) + (-1,-3) = (1, 2)$$$, so $$$T = (1,2)$$$ is the location of the treasure.  In the second sample, the hidden permutation is $$$[2, 3, 4, 1]$$$.

n=gets.to_i
puts$<.map{|e|e.split.map &:to_i}.transpose.map{|e|e.reduce(:+)/n}*' '

def solution
  n = read_int
  sumx = 0
  sumy = 0
  (n * 2).times do
    x, y = read_ints
    sumx += x
    sumy += y
  end
  tx = sumx / n
  ty = sumy / n

  puts "#{tx} #{ty}"
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']


Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days.Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity.

The first line of input contains three space-separated integers n, k, d (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109).

If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k.

NoteNote that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day.

n, k, d = gets.split.map(&:to_i)
def ok(n, k, d)
  s, p = 1, 0
  while s <= 1000 && p < d
    s *= k
    p += 1
  end
  n <= s
end
def q(n, k, d)
  s, p = 1, 0
  while s <= 1000 && p < d
    s *= k
    p += 1
  end
  [n, s].min
end

if ok(n, k, d)
  1.upto(d) do |i|
    t, ans = 1, []
    while ans.length < n
      1.upto(q(n - ans.length, k, d - i)) do |j|
        ans << t.to_s
      end
      t += 1
      if t > k
        t = 1
      end
    end
    puts ans.join(" ")
  end
else
  puts "-1"
end


Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains $$$26$$$ buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string $$$s$$$ appeared on the screen. When Polycarp presses a button with character $$$c$$$, one of the following events happened:  if the button was working correctly, a character $$$c$$$ appeared at the end of the string Polycarp was typing;  if the button was malfunctioning, two characters $$$c$$$ appeared at the end of the string. For example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a $$$\rightarrow$$$ abb $$$\rightarrow$$$ abba $$$\rightarrow$$$ abbac $$$\rightarrow$$$ abbaca $$$\rightarrow$$$ abbacabb $$$\rightarrow$$$ abbacabba.You are given a string $$$s$$$ which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning).You may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then the test cases follow. Each test case is represented by one line containing a string $$$s$$$ consisting of no less than $$$1$$$ and no more than $$$500$$$ lowercase Latin letters.

For each test case, print one line containing a string $$$res$$$. The string $$$res$$$ should contain all characters which correspond to buttons that work correctly in alphabetical order, without any separators or repetitions. If all buttons may malfunction, $$$res$$$ should be empty.

require 'set'

readline.to_i.times do
  s = readline.chomp
  res = Set.new()
  for c,x in s.chars.chunk{|c|c} do
    if x.length % 2 != 0 then
      res << c
    end
  end
  print res.sort.join(), "\n"
end


A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define:  $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column.  $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$).  $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$).  In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$.  For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.

For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.

NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.

require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  n,k = gets.chomp.split.map(&:to_i)

  ans = []
  if k == 0
    puts 0
    n.times do
      ans.push '0' * n
    end
    puts ans.join("\n")
    next
  elsif k == n*n
    puts 0
    n.times do
      ans.push '1' * n
    end
    puts ans.join("\n")
    next
  end

  if k % n == 0
    puts 0
  else
    puts 2
  end

  plus1 = k % n
  fill = k / n
  n.times do |i|
    fill_c = fill
    fill_c += 1 if i < plus1
    line = '1' * fill_c + '0' * (n - fill_c)
    # p [fill_c, line]
    ans.push line[i, n] + line[0, i]
  end
  puts ans.join("\n")
end
#


You have a string $$$s$$$ of length $$$n$$$ consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you chose was the last one, nothing happens). If you choose a character <, the character that comes right before it is deleted (if the character you chose was the first one, nothing happens).For example, if we choose character > in string > > < >, the string will become to > > >. And if we choose character < in string > <, the string will become to <.The string is good if there is a sequence of operations such that after performing it only one character will remain in the string. For example, the strings >, > > are good. Before applying the operations, you may remove any number of characters from the given string (possibly none, possibly up to $$$n - 1$$$, but not the whole string). You need to calculate the minimum number of characters to be deleted from string $$$s$$$ so that it becomes good.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) – the number of test cases. Each test case is represented by two lines. The first line of $$$i$$$-th test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) – the length of string $$$s$$$. The second line of $$$i$$$-th test case contains string $$$s$$$, consisting of only characters > and <.

For each test case print one line. For $$$i$$$-th test case print the minimum number of characters to be deleted from string $$$s$$$ so that it becomes good.

NoteIn the first test case we can delete any character in string <>.In the second test case we don't need to delete any characters. The string > < < is good, because we can perform the following sequence of operations: > < < $$$\rightarrow$$$ < < $$$\rightarrow$$$ <.

N = gets.to_i
N.times {
  n = gets.to_i
  s = gets.chomp
  p [s.match(/^<*/)[0].size, s.match(/>*$/)[0].size].min
}

gets.to_i.times{
	n = gets.to_i;
	s = gets.chomp;
	puts s =~ /<>/ ? 1 : 0;
}

gets.to_i.times{
	n = gets.to_i;
	s = gets.chomp;
	puts s =~ /^[^>]*<>[^<]*$/ ? 1 : 0;
}


As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:  He will never send to a firend a card that this friend has sent to him.  Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.

The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.

Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.

NoteIn the sample, the algorithm of actions Alexander and his friends perform is as follows:   Alexander receives card 1 from the first friend.  Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.  Alexander receives card 2 from the second friend, now he has two cards — 1 and 2.  Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.  Alexander receives card 3 from the third friend.  Alexander receives card 4 from the fourth friend.  Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).

#! /usr/bin/ruby

num_friends = STDIN.gets.chomp("\n").to_i
prefs = Hash.new
for i in (1..num_friends)
  prefs[i] = STDIN.gets.chomp("\n").split(" ")
end

own_prefs = STDIN.gets.chomp("\n").split(" ")

output = []
for i in (1..num_friends)
  possible  = []
  prev = 1000
  own_prefs.each{|val|
    val = val.to_i
    next if val == i or val > prev
    prev = val
    possible << val
  }

  prefs[i].each{|val|
    val = val.to_i
    if possible.index(val)
      output << val
      break
    end
  }
end

puts output.join(" ")

n = gets.to_i
t_preference = Array.new(n + 1)
card_pref = Array.new(n + 1)
alex = Array.new(n + 1)
for i in 1 .. n
	t_preference[i] = Array.new(n + 1)
	card_pref[i] = Array.new(n + 1)

	a = gets.split
	for j in 1 .. n
		t_preference[i][a[j - 1].to_i] = j
	end
end

a = gets.split
for j in 1 .. n
	alex[j] = a[j - 1].to_i
end

for i in 1 .. n
	first = 0
	second = 0
	for j in 1 .. n
		if alex[j] <= i then
			if first == 0 then first = alex[j]
			else
				second = alex[j]
				break
			end
		end
	end

	for j in 1 .. n
		if j == first then card = second
		else card = first end
		if card == 0 then card_pref[i][j] = 0
		else card_pref[j][i] = t_preference[j][card] end
	end
end

ans = Array.new(n)

for i in 1 .. n
	min = n + 1
	idx = 0
	for j in 1 .. n
		if card_pref[i][j] != 0 then
			if card_pref[i][j] < min then
				min = card_pref[i][j]
				idx = j
			end
		end
	end
	ans[i - 1] = idx
end

puts ans.join(" ")

n = gets.chomp.to_i
na = []
for i in 0..n-1
    wk = gets.chomp.split(/ /).map!{|x| x.to_i}
    wk.delete(i+1)
    na << wk
end
aa = gets.chomp.split(/ /).map!{|x| x.to_i}
for i in 0..n-1
    x = aa[i]
    j = i-1
    revcnt = 0
    revx = 0
    while j >= 0 && revcnt <= 2
        if aa[j] < x
            revcnt += 1
            revx = aa[j]
        end
        j -= 1
    end
    for k in 0..n-1
        if revcnt >= 2 || (revcnt == 1 && (k+1) != revx)
            na[k].delete(x)
        end
    end
end
ret = []
for i in 0..n-1
    ret << na[i][0]
end
puts ret.join(" ")

#!/usr/bin/ruby
# coding: utf-8

n = $stdin.gets.to_i
m = Array.new(n) { Array.new(n) }
n.times do |i|
  $stdin.gets.split(/\s+/).map(&:to_i).each_with_index do |x, j|
    m[i][x-1] = j
  end
end
alex = Array.new n
$stdin.gets.split(/\s+/).map(&:to_i).each_with_index do |x, j|
  alex[x-1] = j
end

ans = Array.new n
c1 = nil
c2 = nil
n.times do |i|
  if c1.nil?
    c1 = i
  elsif c2.nil?
    if alex[i] < alex[c1]
      c1, c2 = i, c1
    else
      c2 = i
    end
  elsif alex[i] < alex[c1]
    c1, c2 = i, c1
  elsif alex[i] < alex[c2]
    c2 = i
  end

  ans.each_with_index do |a, j|
    c = c1 == j ? c2 : c1
    next if c.nil?
    if ans[j].nil? or m[j][ans[j]] > m[j][c]
      ans[j] = c
    end
  end
end
puts ans.map(&:succ).join(' ')

#! /usr/bin/ruby

num_friends = STDIN.gets.chomp("\n").to_i
prefs = Hash.new
for i in (1..num_friends)
  prefs[i] = STDIN.gets.chomp("\n").split(" ")
end

own_prefs = STDIN.gets.chomp("\n").split(" ")

output = []
for i in (1..num_friends)
  possible  = []
  prev = 1000
  own_prefs.each{|val|
    val = val.to_i
    next if val == i
    break if val > prev
    prev = val
    possible << val
  }

  prefs[i].each{|val|
    val = val.to_i
    if possible.index(val)
      output << val
      break
    end
  }
end

puts output.join(" ")


Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.

A single line contains two integers m and n (1 ≤ m, n ≤ 105).

Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10  - 4.

NoteConsider the third test example. If you've made two tosses:  You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.  You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.  You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.  You can get 2 in the first toss, and 2 in the second. Maximum equals to 2. The probability of each outcome is 0.25, that is expectation equals to: You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value

#!/usr/bin/ruby
m,n=gets.split.map(&:to_i)
p 1.0.step(m).reduce(0){|s,k|s+k*((k/m)**n-((k-1)/m)**n)}

m,n=readline.split(' ').map(&:to_i)
r=0
pp=0
tp=0
1.upto(m) do |x|
  p=(1.0*x/m)**n
#  puts "#{x} - #{p}"
  r+=x*(p-pp)
  tp+=p-pp
  pp=p
end
#puts "total p #{tp}"
puts r

m, n = gets.split().map(&:to_i)
m = 1.0*m
e = 0
for k in 1..m
	e += k*((k/m)**n-((k-1)/m)**n)
end
puts e

m,n=gets.split.map &:to_i
tot=m*1.0
1.upto(m-1) {|i|tot -= (1.0*i/m)**n}
p tot

m, n = gets.split.map {|i| i.to_i}
a = []
for i in 0...m
  a.push(((i + 1).to_f / m) ** n)
end
(1...m).reverse_each {|i|
  a[i] -= a[i - 1]
}
sum = 0
for i in 0...m
  sum += (i + 1) * a[i]
end
puts sum

throws = 0
m , n = gets.split.map(&:to_i)
#(1..m).each {|x| throws, results[x-1] = x ** n, x ** (n + 1) - throws * x }
puts (1..m).map {|x| x * ((x.to_f/m) ** n - ((x-1.to_f)/m) ** n)}.inject(0) {|x, y| x + y}

#!/usr/bin/env ruby

m, n = gets.split.map(&:to_i)
ans = 0.0
(1..m).each do |k|
  ans += k * (((k.to_f/m) ** n) - ((k-1.to_f) / m) ** n)
end

printf "%.8f\n", ans

# E = Sum(k=1..m, P(k)), where P(k) - probability k is max of n throws
# k = max x(i)
# P(k) = P(x <= k) - P(x < k)   (at least on x = k achieved)
# P(x <= k) = (k/m)^n
# P(x < k) = ((k-1)/m)^n

class C

  def c(n, k)

  end

  def run
    m, n = $stdin.gets.split(' ').map &:to_i
    e = 0.0

    (1..m).each do |k|
      pk = (1.0 * k / m) ** n - (1.0 * (k - 1) / m) ** n
      e += k * pk
    end

    puts e
  end

end

C.new.run if __FILE__ == $0

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

ans = m

1.upto(m - 1) do |i|
  ans -= (i.to_f / m) ** n
end
puts ans

def bin_pow(a,b)
	a=a.to_i
	b=b.to_i
	r=1
	while (b>0)
		if (b&1!=0)
			r*=a
		end
		a*=a
		b>>=1
	end
	return r
end

(m,n)=gets.split.map{|x| x.to_i}
m=m.to_f
ans=m
i=1
while (i<m)
	ans-=(i/m)**n
	i+=1
end
puts ans

m, n = gets.chomp.split.map{|i| i.to_i}

ans = 0
pnow = 0.0

m.downto(1).each do |i|
	lower = 1 - ((m-i)/m.to_f)**n
	ans += (lower - pnow) * i
	pnow = lower
end

puts ans

#!/usr/bin/ruby
m,n=gets.split.map(&:to_i)
p 1.step(m).reduce(0){|s,k|s+1.0*k*(k**n-(k-1)**n)/m**n}

def check(m, n)
  x = 1
  n.times {
    x *= m
    return false if x > 10000000000
  }
  return true
end

def solve(m, n)
  if m == 1
    return 1.0
  end
  p "@solve: m = #{m}, n = #{n}"
  res = 0.0
  (1..m).each {|x|
    if x == 1
      res += 1.0
    elsif x == m
    else
      t = 1.0 * x / m
      p t
    end
  }
  return -1
  return (1..m).map{|x| x * ( x ** n - ( x - 1 ) ** n )}.inject(:+) * ( 1.0 / m ** n )
end

puts solve *gets.split.map{|x| x.to_i}

# m, n = gets.split.map{|x| x.to_i}
# puts (1..m).map{|x| x * ( x ** n - ( x - 1 ) ** n )}.inject(:+) * ( 1.0 / m ** n )