number = gets.chomp.to_i
vectors = []

i = 0
while i < number
	x = gets.chomp
	vectors << x.split(" ")
	i += 1
end

x = 0
y = 0
z = 0
vectors.each do |vector|
	x += vector[0].to_i
	y += vector[1].to_i
	z += vector[2].to_i
end

if x == 0 && y == 0 && z == 0
	print "YES"
else
	print "NO"
end

n = gets.to_i
ar = [0, 0, 0]
n.times do
  gets.split(' ').map.with_index(0){|x,id| ar[id]+=x.to_i}
end
if ar.all?{|x| x==0}
  puts "YES"
else
  puts "NO"
end

matrix = Array.new
gets.to_i.times do
matrix.push(gets.split(" ").map(&:to_i))
end

for lines in matrix.transpose
  res = lines.inject { |sum, i| sum += i }
  (res == 0 )?(res):(puts "NO"; exit)
end
puts "YES"

    n = gets.to_i
    a = 0
    b = 0
    c = 0
    n.times do
        nums = gets.split(" ").map(&:to_i)
        a+=nums[0]
        b+=nums[1]
        c+=nums[2]
    end

    if(a==0 and b==0 and c==0)
        puts("YES")
    else
        puts("NO")
    end

n = gets.to_i

sumx = 0
sumy = 0
sumz = 0

n.times do |i|
    s = gets.split.map{|i| i.to_i}
    sumx += s[0]
    sumy += s[1]
    sumz += s[2]
end

if sumx != 0 || sumy != 0 || sumz != 0 then
    puts "NO"
else
    puts "YES"
end

forces_count = gets.to_i

force_vectors = []

forces_count.times do
  force_vectors.push(gets.split.map(&:to_i))
end

resultant_force = [0, 0, 0]

force_vectors.each do |force|
  resultant_force[0] += force[0]
  resultant_force[1] += force[1]
  resultant_force[2] += force[2]
end

if resultant_force[0] | resultant_force[1] | resultant_force[2] == 0
  puts "YES"
else
  puts "NO"
end

x, y, z = 0, 0, 0

gets.to_i.times do
  a, b, c = gets.split.map(&:to_i)
  x += a
  y += b
  z += c
end

puts [0, 0, 0] == [x, y, z] ? :YES : :NO

x, y, z = 0, 0, 0
gets.to_i.times do
	a, b, c = gets.split.map(&:to_i)
	x += a
	y += b
	z += c
end
puts [x, y, z] == [0, 0, 0] ? :YES : :NO

puts( Array.new(gets.to_i) do
  gets.split.map(&:to_i)
end.transpose.all?{ |r| r.inject(:+).zero? } ? :YES : :NO )

n = gets.to_i
p = [0, 0, 0]
n.times do
    a = gets.split.map(&:to_i)
    3.times {|i| p[i] += a[i]}
end
puts p == [0, 0, 0] ? "YES" : "NO"

n = gets.to_i
x0, y0, z0= 0,0,0
n.times do
  x, y, z = gets.split.map(&:to_i)
  x0, y0, z0 = x0 + x, y0 + y, z0 + z
end

if x0 == 0 && y0 == 0 && z0 == 0
  puts "YES"
else
  puts "NO"
end

q = []
a = gets.to_i
for i in 0..(a-1)
q[i] = gets.split(" ")
end
w = 0
e = 0
r = 0

q.each do |n|

  w = w + n[0].to_i
  e = e + n[1].to_i
  r = r + n[2].to_i

end

if w == 0 and e == 0 and r == 0
  puts "YES"
else
  puts "NO"
end

num_of_lines = gets.chomp.to_i
x = 0
y = 0
z = 0
num_of_lines.times do
  line = gets.chomp.split(" ")
  x += line[0].to_i
  y += line[1].to_i
  z += line[2].to_i
end
puts  (x == 0 && y == 0 && z == 0)? "YES" : "NO"

forces = gets.to_i

def solve(n_forces)
  all_forces = Array.new(3,0)

  (1..n_forces).each do |i|
    a = gets.split(/\s+/).map(&:to_i)
    all_forces = all_forces.zip(a).map(&:sum)
  end

  all_forces == [0,0,0] ? "YES" : "NO"
end

puts solve(forces)

n = gets.to_i
x = 0
y = 0
z = 0
for i in 1..n
	str = gets.chomp
	xi, yi, zi = str.split(" ").map { |num| num.to_i }
	x += xi
	y += yi
	z += zi
end
if x == 0 && y == 0 && z == 0
	puts "YES"
else
	puts "NO"
end

n=gets.chomp.to_i
a=[]
suma=0
sumb=0
sumc=0
n.times do
	b=gets.chomp.split.map(&:to_i)
	a.push(b)
	suma+=b[0]
	sumb+=b[1]
	sumc+=b[2]
end
if suma==0 && suma==sumb && sumb==sumc
	puts "YES"
else
	puts "NO"
end

STDIO = $stdin
STDIO.readline
puts STDIO.readlines.map(&:strip).map(&:split).transpose.map { |b| b.map(&:to_i).inject(0) { |acc, x| acc+x } }.eql?([0,0,0]) ? "YES" : "NO"

n = gets.to_i

r = [0, 0, 0]

n.times do
  s = gets.chomp.split(' ').map &:to_i

  r = r.zip(s).map {|a| a.inject(:+)}
end

if r.sort == [0,0,0].sort
  puts "YES"
else
  puts "NO"
end

n = gets.to_i
a = Array.new()
b = Array.new()
c = Array.new()

lo,xo,fo = 0,0,0
for i in 0..n-1
 a[i],b[i],c[i] = gets.split.map(&:to_i)
 lo+=a[i]
 xo+=b[i]
 fo+=c[i]
end

if [fo, lo, xo] == [0, 0, 0]
    puts "YES"
else
    puts "NO"
end

n = $stdin.readline.to_i
dx,dy,dz = 0,0,0
n.times  do
    x,y,z = $stdin.readline.split.map(&:to_i)
    dx,dy,dz = x+dx, y+dy, z+dz
end

puts (dx==0 && dy==0 && dz==0) ? "YES" : "NO"

arr = []
gets.to_i.times do
  arr.push(gets.chomp.split.map &:to_i)
end
arr = arr.transpose.map { |x| x.inject(:+) }
if arr == [0, 0, 0]
  puts "YES"
else
  puts "NO"
end

n = gets.chomp.to_i
x,y,z = 0,0,0
n.times do |i|
  a,b,c = gets.split.map(&:to_i)
  x+=a ; y+=b ; z+=c
end
if [x,y,x] == [0,0,0]
  puts "YES"
else
  puts "NO"
end

#!/usr/bin/env ruby

while true
    break if STDIN.eof?
    n = gets.to_i

    a = b = c = 0
    n.times {
        x, y, z = gets.split.map(&:to_i)

        a += x; b += y; c += z
    }

    puts [a, b, c] == [0, 0, 0] ? 'YES' : 'NO'
end

no_forces = gets.chomp.to_i
x_sum = 0
y_sum = 0
z_sum = 0
no_forces.times do
    forces = gets.chomp.split(/ /).map(&:to_i)
    x_sum += forces[0]
    y_sum += forces[1]
    z_sum += forces[2]
end
if x_sum == 0 && y_sum == 0 && z_sum == 0
    puts "YES"
else
    puts "NO"
end

x=0
y=0
z=0
gets.to_i.times{
	a,b,c=gets.split.map{|e| e.to_i}
	x=x+a
	y=y+b
	z=z+c
}
puts (x|y|z)==0?"YES":"NO"

#Redirecting STDIN to file
#$stdin=File.open("input.txt", "r")
#$stdout=File.open("output.txt", "w")
#END

t=gets.to_i
data=[0,0,0]

for i in 1..t do
  x, y, z=gets.split.map(&:to_i)
  data[0]+=x
  data[1]+=y
  data[2]+=z
end

if data[0]==0 and data[1]==0 and data[2]==0 then
  puts "YES"
else
  puts "NO"
end

a=[]
x=y=z=0
gets.to_i.times{
	a=gets.split
	x+=a[0].to_i
	y+=a[1].to_i
	z+=a[2].to_i
}

puts(if x==y && y==z && z==0 then
		'YES'
	else
		'NO'
	end)

n = gets.to_i
x,y,z = 0,0,0
(1..n).each do
  a, b, c = gets.split.map(&:to_i)
  x += a; y += b; z += c
end
puts x==0 && y==0 && z==0 ? 'YES' : 'NO'

x,y,z = 0,0,0

gets.to_i.times do
  v = gets.split.map(&:to_i)
  x += v[0]; y+= v[1]; z+= v[2];
end

puts [x,y,z].all? {|i| i == 0} ? "YES":  "NO"

n = gets.to_i
av=bv=cv = 0
n.times{
    a,b,c = gets.split.map(&:to_i)
    av = av + a
    bv = bv + b
    cv = cv + c
}
puts (av**2+bv**2+cv**2 == 0 ? 'YES' : 'NO')

x,y,z=0,0,0;gets.to_i.times{a,b,c=gets.split.map &:to_i;x+=a;y+=b;z+=c;};puts [x,y,z]==[0,0,0]?:YES: "NO"

#puts "Enter the number of vector\n"
n = gets.to_i
a = [n]
b = [n]
c = [n]
for i in 0...n do
    a[i], b[i], c[i] = gets.split.map(&:to_i)
end
x = y = z = 0
for j in 0...n do
    x += a[j]
    y += b[j]
    z += c[j]
end
if x == 0 && y == 0 && z == 0
    puts "YES\n"
else
    puts "NO\n"
end

x_sum = 0
y_sum = 0
z_sum = 0
n = gets.to_i
n.times do
  x,y,z = gets.split(" ").map(&:to_i)
  x_sum += x
  y_sum += y
  z_sum += z
end
puts x_sum == 0 && y_sum == 0 && z_sum == 0 ? "YES" : "NO"

x, y, z = [0, 0, 0]
gets.chomp.to_i.times {
  xi, yi, zi = gets.split.map(&:to_i)
  x += xi
  y += yi
  z += zi
}
puts (x == 0 and y == 0 and z == 0) ? "YES" : "NO"

require 'matrix'

n = gets.to_i

res = Vector[0, 0, 0]

n.times do
	res += Vector.elements(gets.split.map(&:to_i))
end

puts "YES" if res == Vector[0, 0, 0]
puts "NO" unless res == Vector[0, 0, 0]

n = gets.chomp.to_i
init_vector = [0,0,0]
n.times do
  vec = gets.chomp
  vec = vec.split(' ')

  init_vector.map!.with_index {|val, i| val += vec[i].to_i }
end
puts init_vector.all? {|v| v == 0 } ? "YES" : "NO"

puts ( gets.to_i.times.map{gets.split.map(&:to_i)}.transpose.map{|x| x.reduce(&:+)} == [0,0,0] )? "YES" : "NO"

require 'scanf'

def main()
  vectors_count = gets.to_i
  vectors = []
  vectors_count.times do
    vectors << gets.scanf("%d %d %d")
  end
  check_vectors(vectors) ? puts("YES") : puts("NO")
end

def check_vectors(vectors)
  result_sum = 0
  i = 0
  vectors[0].size.times do
    if result_sum == 0 then
      result = 0
      result = vectors.inject(0) {|result, item| result = result + item[i]}
      result_sum += result
    end
    i += 1
  end
  return result_sum == 0
end
main()

n = $stdin.gets
sumx = 0
sumy = 0
sumz = 0
while line = $stdin.gets
  x,y,z = line.strip.split(' ').map{|v| v.to_i}
  sumx += x
  sumy += y
  sumz += z
end
if(sumx == 0 && sumy == 0 && sumz == 0)
  print "YES"
else
  print "NO"
end

n = gets.chomp.to_i
total_x, total_y, total_z = 0, 0, 0
n.times do
    x, y, z = gets.chomp.split(" ").map(&:to_i)
    total_x += x
    total_y += y
    total_z += z
end

if total_x == 0 && total_y == 0 && total_z == 0
    puts "YES"
else
    puts "NO"
end

puts gets.to_i.times.map{gets.split.map(&:to_i)}.transpose.all?{|a|a.inject(0){|r,j|r+j}==0} ? "YES" : "NO"

n = gets.chomp.to_i

a = b = c = 0

n.times{
	input = gets.chomp.split(" ")
	a += input[0].to_i
	b += input[1].to_i
	c += input[2].to_i
}

if a==0 && b==0 && c==0
	puts "YES"
else
	puts "NO"
end

n = readline.to_i
sum = [0,0,0]
i = 0
while i < n do
  a = readline.split(' ')
  sum[0] += a[0].to_i
  sum[1] += a[1].to_i
  sum[2] += a[2].to_i
  i += 1
end
puts (if(sum[0] == 0 && sum[1] == 0 && sum[2] == 0)
  begin
    "YES"
  end
else
  begin
    "NO"
  end
end)

n = gets.to_i
a =[]
b=[]
res = 0
d = 0

n.times do
  a = gets.split().map(&:to_i)
  b.push(a)
end

def check(c,d)
  sum = 0
  sum = sum+c[d]
end

b.each do |i|
    res = res + check(i,0)
    d = d+1
end

if res == 0
  puts "YES"
else
  puts "NO"
end

# https://codeforces.com/problemset/problem/69/A
n = gets.chomp.to_i
forcex = 0
forcey = 0
forcez = 0
1.upto(n) do |force|
    x, y, z = gets.chomp.split(" ").map(&:to_i)
    forcex += x
    forcey += y
    forcez += z
end

if forcex == 0 && forcey == 0 && forcez == 0
    puts "YES"
else
    puts "NO"
end

class Euler
  n=gets.to_i;
  k1=0;
  k2=0;
  k3=0;
  (1..n).each do
    a=gets;
    m=a.split(" ");
    k1+=m[0].to_i;
    k2+=m[1].to_i;
    k3+=m[2].to_i;
  end
  if (k1==k2 && k2==k3 && k3==0)
  puts("YES");
else
  puts("NO");
  end
end

x=0
y=0
z=0

gets.to_i.times{

x+=gets(' ').to_i
y+=gets(' ').to_i
z+=gets.to_i

}

if x == 0 && y == 0 && z == 0
print "YES\n"
else
print "NO\n"
end

n = gets.to_i

result = [0, 0, 0]

1.upto(n) do
  vec = gets.split(' ').map(&:to_i)
  0.upto(2) { |i| result[i] += vec[i] }
end

puts result.all? { |n| n == 0 } ? 'YES' : 'NO'

sum = [0, 0, 0]

n = gets.to_i

for str in $stdin.each_line.take(n)
  arr = str.split(' ').map &:to_i
  for i in 0..2
    sum[i] += arr[i]
  end
end

if sum.all? { |x| x == 0 }
  puts "YES"
else
  puts "NO"
end

n = gets.to_i
s1 = 0
s2 = 0
s3 = 0
while (n > 0)
    a,b,c = gets.split(' ').map{|s| s.to_i}
    s1 += a
    s2 += b
    s3 += c
    n -= 1
end

if (s1 == 0 && s2 == 0 && s3 == 0) then puts 'YES'
else puts 'NO'
end

t = gets.to_i
sum = [0,0,0]
for i in 1..t
 b = gets.chomp.split().map { |e| e.to_i  }
 for i in 0..2
     sum[i] +=  b[i]
 end
end

puts sum.uniq.size <= 1 && sum.inject(0, :+) == 0 ? 'YES' : 'NO'

n = gets.chomp.to_i
vec = [0, 0, 0]
n.times.each do |_i|
  force = gets.chomp.split(/ /).map(&:to_i)
  vec[0] += force[0]
  vec[1] += force[1]
  vec[2] += force[2]
end
puts vec.uniq.count > 1 ? 'NO' : vec[0] == 0 ? 'YES' : 'NO'

gets
puts $<.map{|_|_.split}.transpose.all?{|_|0==eval(_*?+)}? "YES" : "NO"

require"matrix"
gets
puts $<.map{|_|Vector[*_.split.map(&:to_i)]}.inject(:+).r==0? "YES" : "NO"

gets; input, x, y, z = STDIN.read.split("\n").map{ |i| i.split.map! &:to_i  }, 0, 0, 0

input.each { |i| x += i[0]; y += i[1]; z += i[2] }
print [x, y, z].join == "000" ? :YES : :NO

x,y,z=0,0,0
gets.to_i.times do
    a=gets.split
    x+=a[0].to_i
    y+=a[0].to_i
    z+=a[0].to_i
end
if x==0&&y==0&&z==0
    puts "YES"
else
    puts "NO"
end

n = gets.to_i
sum = [0, 0, 0]
n.times do
	f = gets.split.map { |x| x.to_i }
	3.times do |i|
		sum[i] += f[i]
	end
end

if sum.count(0) == 3
	puts 'YES'
else
	puts 'NO'
end

vectorNum = gets.chomp.to_i

totals = Array.new(3) { |elem| elem = 0 }

vectorNum.times do
	vector = gets.chomp.split(' ').map(&:to_i)
	for i in (0...vector.length)
		totals[i] += vector[i]
	end
end

# print totals

ans = if totals.all? { |elem| elem == 0 }
	'YES'
else
	'NO'
end

puts ans

puts %w[YES NO][STDIN.readlines.drop(1).reduce([0, 0, 0]) { |r, x| r.zip(x.split(' ').map(&:to_i)).map { |a, b| a + b } } == [0, 0, 0] ? 0 : 1]

n = gets.to_i;s=0
for i in(1..n)
    a = gets.split.map(&:to_i)
    s += (a[0]+a[1]+a[2])
end
puts s==0 ? "SI" : "NO"

n = gets.to_i;s=0
for i in(1..n)
    a = gets.split.map(&:to_i)
    s += (a[0]+a[1]+a[2])
end
puts s==0 ? "YES" : "NO"

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

n.times do
  a = gets.split().map(&:to_i)
  b.push(a)
end

b.each do |i|
    i.each do |j|
        sum = sum +j
    end
end

if sum == 0
  puts "YES"
else
  puts "NO"
end

n = gets.to_i
a =[]
b=[]
sum = 0
res = 0

n.times do
  a = gets.split().map(&:to_i)
  b.push(a)
end

b.each do |i|
    i.each do |j|
        sum = sum +j
    end
     res = res + sum
end

if res == 0
  puts "YES"
else
  puts "NO"
end

# https://codeforces.com/problemset/problem/69/A
n = gets.chomp.to_i
forcex = 0
forcey = 0
forcez = 0
1.upto(n) do |force|
    x, y, z = gets.chomp.split(" ").map(&:to_i)
    forcex += x
    forcey += y
    forcez += z
end

if (forcex + forcey + forcez) == 0
    puts "YES"
else
    puts "NO"
end

sum = [0, 0, 0]

n = gets.to_i

for str in $stdin.each_line.take(n)
  arr = str.split(' ').map &:to_i
  (sum.zip(arr)).map do |x|
    x.reduce { :+ }
  end
end

if sum.all? { |x| x == 0 }
  puts "YES"
else
  puts "NO"
end

t = gets.to_i
sum = [0,0,0]
for i in 1..t
 b = gets.chomp.split().map { |e| e.to_i  }
 for i in 0..2
     sum[i] +=  b[i]
 end
end

puts sum.inject(0, :+) == 0 ? 'YES' : 'NO'

n = gets.chomp.to_i
vec = [0, 0, 0]
n.times.each do |_i|
  force = gets.chomp.split(/ /).map(&:to_i)
  vec[0] += force[0]
  vec[1] += force[1]
  vec[2] += force[2]
end
sum = vec.inject(0) { |sum, x| sum + x }
puts sum.zero? ? 'YES' : 'NO'

n = gets.chomp.to_i
vec = [0, 0, 0]
n.times.each do |_i|
  force = gets.chomp.split(/ /).map(&:to_i)
  vec[0] += force[0]
  vec[1] += force[1]
  vec[2] += force[2]
end
puts vec.uniq.count > 1 ? 'NO' : 'YES'

vectorNum = gets.chomp.to_i

totals = Array.new(3) { |elem| elem = 0 }

vectorNum.times do
	vector = gets.chomp.split(' ').map(&:to_i)
	for i in (0...vector.length)
		totals[i] += vector[i]
	end
end

ans = if totals.all? { |elem| elem = 0 }
	'YES'
else
	'NO'
end

puts ans

def equ(*array)
  if array.length < 4 and array.length > 0
    k = 0
    array.each do |element|
      k = k + element
    end
    if k == 0
      puts "YES"
    else
      puts "NO"
    end
  else
  puts "Enter more than 0 and less than 4 input"
  end
end

equ()

def equ(*array)
  if array.length < 4 and array.length > 0
    k = 0
    array.each do |element|
      k = k + element
    end
    if k == 0
      puts "YES"
    else
      puts "NO"
    end
  else
  puts "NO"
  end
end

a = 3

q = "4 -1 7".split(" ")
w = "-5 2 -4".split(" ")
e = "2 -1 -3".split(" ")
res = 0

for i in 0..2
  r = q[i].to_i + w[i].to_i + e[i].to_i
  res = res + r
end

if res == 0
  puts "YES"
else
  puts "NO"
end

a = gets
q = gets.split(" ")
w = gets.split(" ")
e = gets.split(" ")

res = 0

for i in 0..2
  r = q[i].to_i + w[i].to_i + e[i].to_i
  res = res + r
end

if res == 0
  puts "YES"
else
  puts "NO"
end

forces = gets.to_i

def solve(n_forces)
  all_forces = Array.new(3,0)

  (1..n_forces).each do |i|
    a = gets.split(/\s+/).map(&:to_i)
    c = all_forces.zip(a).map(&:sum)
  end

  all_forces == [0,0,0] ? "YES" : "NO"
end

puts solve(forces)

sum = 0
gets.to_i.times do
  sum += gets.chomp.split.map { |x| x.to_i }.inject(:+)
end
if sum == 0
  puts "YES"
else
  puts "NO"
end

def sum( forces )
    sum = 0
    forces.each do |force|
        sum += force
    end
    sum
end
no_forces = gets.chomp.to_i
sum_forces = 0
no_forces.times do
    forces = gets.chomp.split(/ /).map(&:to_i)
    sum_forces += sum forces
end
if sum_forces == 0
    puts "YES"
else
    puts "NO"
end

number = gets.chomp.to_i
vectors = []

i = 0
while i < number
	x = gets.chomp
	vectors << x.split(" ")
	i += 1
end

equilibrium = 0
vectors.each do |vector|
	equilibrium += vector[0].to_i + vector[1].to_i + vector[2].to_i
end

if equilibrium == 0
	print "YES"
else
	print "NO"
end

require 'scanf'

def main()
  vectors_count = gets.to_i
  vectors = []
  vectors_count.times do
    vectors << gets.scanf("%d %d %d")
  end
  check_vectors(vectors) ? puts("YES") : puts("NO")
end

def check_vectors(vectors)
  result_sum = 0
  i = 0
  vectors[0].size.times do
    if result_sum == 0 then
      result = 0
      result = vectors.inject(0) {|result, item| result = result + item[i]}
      result_sum += result
    end
    p i
    i += 1
  end
  return result_sum == 0
end
main()

require 'scanf'

def main()
  vectors_count = readline.to_i
  vectors = []
  vectors_count.times do
    vectors << readline.scanf("%d %d %d")
  end
  result_sum = 0
  i = 0
  vectors_count.times do
    if result_sum == 0 then
      result = 0
      vectors.inject(0) {|result, item| result = result + item[i]}
      result_sum += result
    end
    i += 1
  end
  result_sum == 0 ? puts("YES") : puts("NO")
end

main

require 'scanf'

def main()
  vectors_count = readline.to_i
  vectors = []
  vectors_count.times do
    vectors << readline.scanf("%d %d %d")
  end
  result = 0
  vectors.inject(0) do |result, item|
    result = result + item[0]
  end
  #p result
  vectors.inject(0) {|result, item| result = result + item[1]} if result == 0
  #p result
  vectors.inject(0) {|result, item| result = result + item[2]} if result == 0
  #p result
  result == 0 ? puts("YES") : puts("NO")
end

main

require 'scanf'

def main()
  vectors_count = gets.to_i
  vectors = []
  vectors_count.times do
    vectors << gets.scanf("%d %d %d")
  end
  check_vectors(vectors_count, vectors) ? puts("YES") : puts("NO")
end

def check_vectors(vectors_count, vectors)
  return false unless vectors_count == vectors.size
  result_sum = 0
  i = 0
  vectors_count.times do
    if result_sum == 0 then
      result = 0
      result = vectors.inject(0) {|result, item| result = result + item[i]}
      result_sum += result
    end
    i += 1
  end
  return result_sum == 0
end
def check_vectors(vectors_count, vectors)
  result_sum = 0
  i = 0
  vectors[0].size.times do
    if result_sum == 0 then
      result = 0
      result = vectors.inject(0) {|result, item| result = result + item[i]}
      result_sum += result
    end
    p i
    i += 1
  end
  return result_sum == 0
end
main()

require 'scanf'

def main()
  vectors_count = readline.to_i
  vectors = []
  vectors_count.times do
    vectors << readline.scanf("%d %d %d")
  end
  result = 0
  vectors.inject(0) do |result, item|
    result = result + item[0]
  end
  vectors.inject(0) {|result, item| result = result + item[1]} if result == 0
  vectors.inject(0) {|result, item| result = result + item[2]} if result == 0
  result == 0 ? puts("YES") : puts("NO")
end

main


n,d=gets.chomp!.split(" ").map(&:to_i)
s,c=gets.chomp!.split(" ").map(&:to_i),0
(n-1).times{|i|t=s[i]-s[i+1]
if t>=0 then
  t=t/d+1
  c+=t
  s[i+1]+=t*d end}
puts c

s = gets.split(" ")
n = s[0].to_i
d = s[1].to_i

s = gets.split(" ")
mas = Array.new()
for i in 0..n-1
  mas[i] = s[i].to_i
end

count = 0
for i in 1..n-1
  if (mas[i] <= mas[i-1])
    delta = ((mas[i-1] - mas[i]) / d) + 1
    mas[i] = mas[i] +d * delta
    count += delta
  end
end

puts count

n,d = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
s = 0
1.upto(b.size - 1).each do |i|
	if b[i] <= b[i-1] then
		t = (b[i-1] - b[i]) / d + 1
		s += t
		b[i] += t * d
	end
end
p s

n, d = gets.split.map &:to_i
b = gets.split.map &:to_i
ans = 0
(1...n).each do |i|
  t = [(b[i - 1] - b[i])/d + 1, 0].max
  b[i] += t*d
  ans += t
end
puts ans

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

b = gets.split.map{|i| i.to_i}

res = 0

(n-1).times do |i|
    k = b[i] - b[i+1]
    if k >= 0 then
        m = k/d + 1
        res += m
        b[i+1] += m*d
    end
end

puts res

n, d = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
s = 0
1.upto(b.size - 1).each do |i|
	next if b[i] > b[i - 1]
	t = (b[i - 1] - b[i]) / d + 1
	s += t
	b[i] += t * d
end
p s

d=gets.split[1].to_i
a=gets.split.map(&:to_i)
ans=0
1.upto(a.length-1){|i|
    r=a[i-1]-a[i]+1
    if r>0 then
        ans+=(r+d-1)/d
        a[i]+=(r+d-1)/d*d
    end
}
puts ans

#=================================
# Better be sure than sorry.
#=================================
$stdin=File.open("./input.txt","r") if ENV["LOGNAME"]=="skydos"
require 'prime'
require 'mathn'
require 'matrix'
include Math
#=================================

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

ans=0
for i in 1...n
	if data[i]<=data[i-1]
		k=((data[i-1]-data[i]+1)*1.0/d).ceil
		data[i]+=k*d
		ans+=k
	end
end

puts ans

#=================================
# Better be sure than sorry.
#=================================
$stdin=File.open("./input.txt","r") if ENV["LOGNAME"]=="skydos"
require 'prime'
require 'mathn'
#=================================

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

ans=0
i=n-2
loop do
	break if i<0

	if data[i]>=data[i+1]
		(i+1).upto(n-1){|j|
			data[j]+=d
			ans+=1
		}
	else
		i-=1
	end
end

puts ans

s = gets.split(" ")
n = s[0].to_i
d = s[1].to_i

s = gets.split(" ")
mas = Array.new()
for i in 0..n-1
  mas[i] = s[i].to_i
end

count = 0
for i in 1..n-1
  if (mas[i] <= mas[i-1])
    delta = ((mas[i-1] - mas[i]) / d) + 1
    mas[i] = mas[i-1] +d * delta
    count += delta
  end
end

puts count


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

dragon = []
n.times do |i|
    dragon << gets.split.map{|j| j.to_i}
end

dragon = dragon.sort{|a,b| a[0] <=> b[0]}

t = true
n.times do |i|
    if s > dragon[i][0] then
        s += dragon[i][1]
    else
        t = false
    end
end

if t then
    puts "YES"
else
    puts "NO"
end

power, enemy_count = gets.chop.split.map(&:to_i)

dragons = []
enemy_count.times { dragons << gets.chop.split.map(&:to_i) }

loop do
  weak_dragons = dragons.select { |dragon| power > dragon[0] }
  break if dragons.count.zero? || weak_dragons.count.zero?
  power += weak_dragons.reduce(0) { |bonus, dragon| bonus += dragon[1] }
  dragons -= weak_dragons
end

dragons.count.zero? ? puts('YES') : puts('NO')

#!/usr/bin/env ruby

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

x = n.times.map { gets.split.map(&:to_i) }

yes = 1
n.times do
  id = -1
  x.each_with_index do |arr, i|
    if arr[0] < s
      id = i
      break
    end
  end
  if id == -1
    yes = 0
    break
  end
  s += x[id][1]
  x.delete_at(id)
end

puts(yes == 1 ? 'YES' : 'NO')

#!/usr/bin/env ruby

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

x = n.times.map { gets.split.map(&:to_i) }.sort

yes = true
x.each do |a,b|
  if s <= a
    yes = false
    break
  end
  s += b
end

puts(yes ? 'YES' : 'NO')

s, n = gets.split.map(&:to_i)
a = []
n.times do
  b = gets.split.map(&:to_i)
  a << b
end
a.sort!
ans = 1
n.times do |i|
  if s <= a[i][0]
    ans = 0
    break
  end
  s += a[i][1]
end
puts ans == 0 ? "NO" : "YES"

s, n = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}.sort
ans = true
n.times do |i|
  if s <= a[i][0]
    ans = false
    break
  end
  s += a[i][1]
end
puts ans ? "YES" : "NO"

 a=gets.chomp.split(" ")
pow=a[0].to_i
drag=a[1].to_i
dragary=[]
drag.times do
temp=gets.chomp.split(" ")
temp[0]=temp[0].to_i
temp[1]=temp[1].to_i
dragary<< temp
end
dragary.sort!
i=0
loop do
if dragary[i][0]<pow
pow+=dragary[i][1]
i+=1
else
puts "NO"
break
end
break if i>drag-1
end
if i>drag-1
puts "YES"
end

class Dragon
  attr_accessor :x, :y

  def initialize(x, y)
    @x = x
    @y = y
  end

  def <=>(other)
    unless self.x == other.x
      self.x - other.x
    else
      other.y - self.y
    end
  end
end

s, n = gets.split.map &:to_i
ar = Array.new
n.times do
  ar << Dragon.new(*(gets.split.map &:to_i))
end
ar.sort!
ar.each do |dragon|
  if dragon.x < s
    s += dragon.y
  else
    puts "NO"
    exit
  end
end
puts "YES"

s,n=gets.split.map(&:to_i)
puts n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+y<s+=y} ?"YES":"NO"

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

a = 1.upto(n).map do
  gets.split(' ').map(&:to_i)
end.sort { |x, y| x.first <=> y.first }

success = true
a.inject(s) do |strength, dragon|
  if strength <= dragon.first
    success = false
    break
  end

  strength += dragon.last
end

puts success ? 'YES' : 'NO'

first_line = gets.chomp.split(' ')
s = first_line[0].to_i
n = first_line[1].to_i
dragons = []
(0...n).each do |i|
	line = gets.chomp.split(' ')
	dragons[i] = {
		strength: line[0].to_i,
		bonus: line[1].to_i
	}
end

dragons.sort! { |a, b| a[:strength] <=> b[:strength] }
defeated = false
dragons.each do |elem|
	if s <= elem[:strength]
		defeated = true
		break
	end
	s += elem[:bonus]
end

if defeated
	puts "NO"
else
	puts "YES"
end

class Pair
  attr_accessor :x, :y

  def initialize(x = 0, y = 0)
    @x = x
    @y = y
  end

end

s, n = gets.chomp.split(' ')

s = s.to_i
n = n.to_i

dragons = [];

n.times {
  x, y = gets.chomp.split(' ')
  x = x.to_i
  y = y.to_i
  dragons << Pair.new(x, y)
}

dragons.sort! { |a, b| a.x <=> b.x }

isDead = false
dragons.each { |dragon|

  if s <= dragon.x
    isDead = true
    break
  end

  s += dragon.y
}

if isDead
  puts "NO"
else
  puts "YES"
end

class Kirito
  def initialize(strength)
    @strength = strength
  end

  def attack(dragon)
    if @strength > dragon.strength
      @strength += dragon.bonus
      return true
    else
      return false
    end
  end
end

class Dragon
  attr_reader :strength, :bonus
  def initialize(strength, bonus)
    @strength = strength
    @bonus = bonus
  end
end

s, n = gets.split.map(&:to_i)
kirito = Kirito.new(s)
dragons = []
n.times do
  dragons << Dragon.new(*gets.split.map(&:to_i))
end
dragons.sort!{|a, b| a.strength <=> b.strength}
dragons.each do |d|
  unless kirito.attack(d)
    puts "NO"
    exit
  end
end
puts "YES"

s, n = gets.split(' ').map(&:to_i)
ar = []
ans = "YES"
n.times do
  ar << gets.split(' ').map(&:to_i)
end
ar.sort_by(&:first).each do |x,y|
  ans = "NO" if s <= x
  s += y
end
puts ans

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

all_dragons_dead = true

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

dragons = dragons.sort do |d1, d2|
  d1[0] <=> d2[0]
end

dragons.each do |dragon|
  dragon_s, bonus = dragon

  if s <= dragon_s
    all_dragons_dead = false
    break
  end

  s += bonus
end

output = all_dragons_dead ? "YES" : "NO"
puts output

class Dragon
  attr_accessor :hp
  attr_accessor :bonus
  def initialize(h, b)
    @hp = h
    @bonus = b
  end
end
line1 = gets.chomp.split(" ")
s = line1[0].to_i
n = line1[1].to_i

enemy = []

for i in 1..n
  line = gets.chomp.split(" ").map {|x| x.to_i}
  enemy << Dragon.new(line[0], line[1])
end

enemy = enemy.sort_by {|obj| obj.hp}

win = true
for e in enemy
  if s > e.hp
    s += e.bonus
  else
    win = false
    break
  end
end

if win
  puts "YES"
else
  puts "NO"
end

s, n = gets.split(" ").map(&:to_i)
a = []

n.times do |i|
  a[i] = gets.split(" ").map(&:to_i)
end

a.sort!{|x,y| x[0] <=> y[0]}

a.each do |el|
  if s <= el[0]
    puts "NO"
    exit
  end
  s += el[1]
end

puts "YES"

class Dragon
	attr_reader :life, :bonus
	def initialize(life, bonus)
		@life = life
		@bonus = bonus
	end
end

input = gets.split
kirito = input[0].to_i
n = input[1].to_i

d = Array.new
for i in 0...n
	input = gets.split
	d[i] = Dragon.new(input[0].to_i, input[1].to_i)
end
d.sort! {|x, y| x.life <=> y.life}

die = false
for i in 0...n
	if kirito <= d[i].life
		puts "NO"
		die = true
		break
	end
	kirito += d[i].bonus
end

if not die then puts "YES" end

#!/usr/bin/ruby

s, n = STDIN.readline.split.collect {|s| s.to_i }
dragons = []
n.times do
	x, y = STDIN.readline.split.collect {|s| s.to_i }
	dragons.push([x, y])
end
dragons.sort!
dragons.each do |x, y|
	if s <= x
		puts 'NO'
		exit
	end
	s += y
end
puts 'YES'

s,n=gets.chomp.split(' ').map(&:to_i)
a=[]
b=[]
n.times do
	x,y=gets.chomp.split(' ').map(&:to_i)
	a.push(x)
	b.push(y)
end
check=0
n.times do
	i=0
	check=nil
	while i<n do
		if a[i]!=0 && s>a[i]
			check=1
			a[i]=0
			s+=b[i]
			break
		end
		i+=1
	end
end
if check
	puts "YES"
else
	puts "NO"
end

s, _ = gets.split.map(&:to_i)
readlines.map(&:strip).map { |x| x.split.map(&:to_i) }.sort_by! { |x| x[0]}.each do |x|
  if s <= x[0]
    puts "NO"
    exit
  else
    s += x[1]
  end
end
puts "YES"

s, n = gets.chomp.split(" ").map(&:to_i)
a = []

n.times do
  q = gets.chomp.split(" ").map(&:to_i)
  a.push(q)
end

a.sort_by!{|x, y| x}

a.each do |x, y|
  if s > x
    s += y
  else
    puts "NO"
    exit
  end
end

puts "YES"

# cook your code here
energy,dragons = gets.split(' ').map(&:to_i)
arr = []
dragons.times do |i|
    arr[i]= gets.split(' ').map(&:to_i)
end

arr.sort!
done = true
dragons.times do |i|
    if arr[i][0] < energy
        energy += arr[i][1]
    else
        done = false
        break
    end

end

puts "YES" if done
puts "NO" unless done

s, n = gets.split.map &:to_i
arr = []
n.times do
  arr.push(gets.split.map &:to_i)
end
d, b = arr.transpose
n.times do
  w = d.index { |x| x < s }
  if w == nil
    puts "NO"
    exit
  else
    s += b[w]
    d.delete_at(w)
    b.delete_at(w)
  end
end
puts "YES"

r=->(){gets.to_s.split.map &:to_i}
s,n=*r.call()
a=[];n.times {a<<r.call()}
a.sort_by! {|a| a[0]}
ok=true
a.each do |x|
  ok=false if x[0]>=s
  s+=x[1]
end
print ok ? 'YES' : 'NO'

def r() gets.to_s.split.map &:to_i end
s,n=*r
a=[];n.times {a<<r}
a.sort.each do |x|
  if x[0]>=s then print 'NO';exit(0);end
  s+=x[1]
end
print 'YES'

s, n = gets.chomp.split(/ /).map(&:to_i)
dragons = []
winner = true
n.times do
    dragons << gets.chomp.split(/ /).map(&:to_i)
end
dragons.sort!
kirito = s
dragons.each do |dragon|
    if kirito > dragon[0]
        kirito += dragon[1]
    else
        winner = false
        break
    end
end
if winner
    puts "YES"
else
    puts "NO"
end

#!/usr/bin/env ruby
first_line = gets
strength = first_line.split(' ')[0].to_i
count_of_dragons = first_line.split(' ')[1].to_i

dragons = []

(1..count_of_dragons).each do |i|
	dragon = {}
	drag_str = gets
	dragon["strength"] = drag_str.split(' ')[0].to_i
	dragon["bonus"] = drag_str.split(' ')[1].to_i
	dragons.push(dragon)
end

dragons = dragons.sort_by{ |d| d["strength"] }

dragons.each do |d|
	if strength > d["strength"] then
		strength+= d["bonus"]
	else
		puts "NO"
		exit
	end
end

puts "YES"
exit

n,m=gets.split.map{|e| e.to_i}
x=[]
m.times{|i|
	a=gets.split.map{|e| e.to_i}
	x<<a
}
x.sort!
ans="YES"
while !x.empty?
	if n<=x[0][0] then
		ans="NO"
	end
	n=n+x[0][1]
	x.shift
end
puts ans

s,n = gets.split.map &:to_i
a = []
n.times do |i|
a[i] = gets.split.map(&:to_i)
end
a.sort_by!{|x,y| x }
b = true
for i in a
	if i[0] < s
		s += i[1]
	else
		b = false
	end
end
if b == false
	puts 'NO'
else
	puts 'YES'
end

a=[]
s,n=gets.split.map{|i|i.to_i}
n.times{|i| a[i]=gets.split.map{|i|i.to_i}}
a.sort!
n.times{|i|
	if s <= a[i][0] then
		puts 'NO'
		exit
	else
		s+=a[i][1]
	end
}
puts 'YES'

s,n = gets.split(" ").map(&:to_i)
win = true
dragons = []
n.times do
  dragons << gets.split(" ").map(&:to_i)
end
dragons.sort! do |a,b|
  if a[0] != b[0]
    a[0] <=> b[0]
  else
    b[1] <=> a[1]
  end
end
dragons.each { |dragon|
  if s > dragon[0]
    s += dragon [1]
  else
    win = false
    break
  end
}
puts win ? "YES" : "NO"

s, n = gets.split.map(&:to_i)
d = []
n.times {
  d << gets.split.map(&:to_i)
}
d = d.sort_by{ |x| x[0]}
flag = true
d.each do |x|
  if s <= x[0]
    flag = false
  else
    s += x[1]
  end
end

puts flag ? "YES" : "NO"

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

all_dragons_dead = true

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

dragons = dragons.sort do |d1, d2|
  d1[0] <=> d2[0]
end

dragons.each do |dragon|
  dragon_s, bonus = dragon

  if s < dragon_s
    all_dragons_dead = false
    break
  end

  s += bonus
end

output = all_dragons_dead ? "YES" : "NO"
puts output

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

all_dragons_dead = true

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

dragons = dragons.sort do |d1, d2|
  d1[0] <=> d2[0]
end

dragons.each do |dragon|
  dragon_s, bonus = dragon

  if s < dragon_s
    all_dragons_dead = false
    break
  end

  s += bonus
  s -= dragon_s
end

output = all_dragons_dead ? "YES" : "NO"
puts output

class Dragon
  attr_accessor :hp
  attr_accessor :bonus
  def initialize(h, b)
    @hp = h
    @bonus = b
  end
end
line1 = gets.chomp.split(" ")
s = line1[0].to_i
n = line1[1].to_i

line_hp = []
line_bonus = []
if n == 1
  line = gets.chomp.split(" ").map {|x| x.to_i}
  line_hp[0] = line[0]
  line_bonus[0] = line[1]
else
  line_hp = gets.chomp.split(" ").map {|x| x.to_i}
  line_bonus = gets.chomp.split(" ").map {|x| x.to_i}
end
enemy = []

for i in 1..n
  enemy << Dragon.new(line_hp[i-1], line_bonus[i-1])
end

enemy.sort_by {|obj| obj.hp}

win = true
for e in enemy
  if s > e.hp
    s += e.bonus
  else
    win = false
    break
  end
end

if win
  puts "YES"
else
  puts "NO"
end

s, n = gets.split(" ").map(&:to_i)
a = []

n.times do |i|
  a[i] = gets.split(" ").map(&:to_i)
end

a.sort!{|x,y| x[0] <=> y[0]}

a.each do |el|
  if s < el[0]
    puts "NO"
    exit
  end
  s += el[1]
end

puts "YES"

s, _ = gets.split.map(&:to_i)
readlines.map(&:strip).map { |x| x.split.map(&:to_i) }.sort_by! { |x| x[0]}.each do |x|
  if s < x[0]
    puts "NO"
    exit
  else
    s += x[1]
  end
end
puts "YES"

a=gets.chomp.split(" ")
pow=a[0].to_i
drag=a[1].to_i
dragary=[]
drag.times do
temp=gets.chomp.split(" ")
temp[0]=temp[0].to_i
temp[1]=temp[1].to_i
dragary<< temp
end
dragary.sort!
i=0
loop do
if dragary[i][0]<=pow
pow+=dragary[i][1]
i+=1
else
puts "NO"
break
end
break if i>drag-1
end
if i>drag-1
puts "YES"
end

s, n = gets.chomp.split(/ /).map(&:to_i)
kirito = s
winner = true
n.times do
    x, y = gets.chomp.split(/ /).map(&:to_i)
    if kirito > x
        kirito += y
    else
        winner = false
        break
    end
end
if winner
    puts "YES"
else
    puts "NO"
end

#!/usr/bin/env ruby
first_line = gets
strength = first_line.split(' ')[0].to_i
count_of_dragons = first_line.split(' ')[1].to_i

dragons = []

(1..count_of_dragons).each do |i|
	dragon = {}
	drag_str = gets
	dragon["strength"] = drag_str.split(' ')[0].to_i
	dragon["bonus"] = drag_str.split(' ')[1].to_i
	dragons.push(dragon)
end

dragons.sort_by!{ |d| d["strength"] }

dragons.each do |d|
	if strength > d["strength"] then
		strength+= d["bonus"]
	else
		puts "NO"
		exit
	end
	puts "YES"
	exit
end

#!/usr/bin/env ruby
first_line = gets
strength = first_line.split(' ')[0].to_i
count_of_dragons = first_line.split(' ')[1].to_i

dragons = []

(1..count_of_dragons).each do |i|
	dragon = {}
	drag_str = gets
	dragon["strength"] = drag_str.split(' ')[0].to_i
	dragon["bonus"] = drag_str.split(' ')[1].to_i
	dragons.push(dragon)
end

dragons = dragons.sort_by{ |d| d["strength"] }

dragons.each do |d|
	if strength > d["strength"] then
		strength+= d["bonus"]
	else
		puts "NO"
		exit
	end
	puts "YES"
	exit
end

s,n = gets.split.map &:to_i
a = []
n.times do |i|
a[i] = gets
a.sort!
end
for i in a
	x,y = i.split.map &:to_i
	if x < s
		s += y
	else
		s = -1
	end
end
if s == -1
	puts 'NO'
else
	puts 'YES'
end

s,n = gets.split(" ").map(&:to_i)
win = true
dragons = []
n.times do
  dragons << gets.split(" ").map(&:to_i)
end
dragons.sort! do |a,b|
  if a[0] != b[0]
    a[0] <=> b[0]
  else
    b[1] <=> a[1]
  end
end
dragons.each { |dragon|
  if s >= dragon[0]
    s += dragon [1]
  else
    win = false
    break
  end
}
puts win ? "YES" : "NO"

s, n = gets.split.map(&:to_i)
d = []
n.times {
  d << gets.split.map(&:to_i)
}
d = d.sort_by{ |x| x[0]}
puts d.to_s
flag = true
d.each do |x|
  if s <= x[0]
    flag = false
  else
    s += x[1]
  end
end

puts flag ? "YES" : "NO"

#!/usr/bin/env ruby

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

x = n.times.map { gets.split.map(&:to_i) }

yes = 1
n.times do
  id = -1
  x.each_with_index do |arr, i|
    if arr[0] <= s
      id = i
      break
    end
  end
  if id == -1
    yes = 0
    break
  end
  s += x[id][1]
  x.delete_at(id)
end

puts(yes == 1 ? 'YES' : 'NO')

s, n = gets.split.map(&:to_i)
a = []
n.times do
  b = gets.split.map(&:to_i)
  a << b
end
a.sort!
ans = 1
n.times do |i|
  if s < a[i][0]
    ans = 0
    break
  end
  s += a[i][1]
end
puts ans == 0 ? "NO" : "YES"

first_line = gets.chomp.split(' ')
s = first_line[0].to_i
n = first_line[1].to_i
dragons = []
(0...n).each do |i|
	line = gets.chomp.split(' ')
	dragons[i] = {
		strength: line[0].to_i,
		bonus: line[1].to_i
	}
end

dragons.sort { |a, b| a[:strength] <=> b[:strength] }
defeated = false
dragons.each do |elem|
	if s <= elem[:strength]
		defeated = true
		break
	end
	s += elem[:bonus]
end

if defeated
	puts "NO"
else
	puts "YES"
end


gets.strip.to_i.times do
  n = gets.strip.to_i
  sticks = gets.strip.split(' ').map(&:to_i)
  h = Hash.new(0)
  sticks.each do |s|
    h[s] += 1
  end

  if h.any? { |k, v| (v % 2) != 0 }
    puts "NO"
  else
    vals = []
    h.each do |k,v|
      h[k] = v / 2
      (v / 2).times { vals << k }
    end
    vals.sort!
    i , j = 0, vals.size - 1
    last = nil
    res = true
    while i < j
      if vals[i] * vals[j] != last && i != 0
        puts "NO"
        res = false
        break
      end
      last = vals[i] * vals[j]
      i += 1
      j -= 1
    end
    puts "YES" if res
  end
end

q = gets.to_i
ans = []
q.times do
  n = gets.to_i
  pp = gets.split.map(&:to_i).sort{|a,b|b<=>a}

  vv = []
  can = true

  0.step(n*4-2,2) do |i|
    # ペアがないとダメ
    unless pp[i]==pp[i+1]
      can = false
      break
    end

    vv << pp[i]
  end
  #puts vv.to_s #d

  if can
    e1 = vv.shift
    e2 = vv.pop
    s = e1*e2
    while vv.size > 1 do
      e1 = vv.shift
      e2 = vv.pop
      if e1*e2 != s
        can = false
        break
      end
    end
  end

  ans << (can ? 'YES' : 'NO')
end
puts ans

t = gets.to_i
t.times do |index|
    n = (gets.to_i)*4
    a = gets.split(" ").map(&:to_i)
    b = []
    a.sort!
    i = 0
    ans = true
    while i < n
        if a[i] != a[i+1]
            ans = false
        end
        b.push a[i]
        i += 2
    end
    i = 0
    j = n/2 - 1
    area = b[i]*b[j]
    while i <= j
        if (b[i])*(b[j]) != area
            ans = false
        end
        i += 1
        j -= 1
    end
    puts ans ? "YES" : "NO"
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i).tally
	if a.any? { |k, v| v.odd? }
		puts 'NO'
	else
		b = a.reduce([]) { |acc, (k, v)| acc.concat([k] * (v / 2)) }.sort
		c = (0...n).map { |i| b[i] * b[~i] }
		puts c == c.rotate(1) ? "YES" : "NO"
	end
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  a.sort!
  s = a[0] * a.last
  ok = true
  (0...n).each do |i|
    ok = false if a[2 * i] != a[2 * i + 1] or a[4 * n - 2 * i - 1] != a[4 * n - 2 * i - 2] or a[2 * i] * a[4 * n - 2 * i - 1] != s
  end
  unless ok
    puts "NO"
  else
    puts "YES"
  end
end

q = gets.to_i
ans = []
q.times do
  n = gets.to_i
  pp = gets.split.map(&:to_i).sort{|a,b|b<=>a}

  vv = []
  can = true

  0.step(n*4-2,2) do |i|
    # ペアがないとダメ
    unless pp[i]==pp[i+1]
      can = false
      break
    end

    vv << pp[i]
  end
  #puts vv.to_s #d

  if can
    s = vv.inject(:*) / n
    while vv.size > 1 do
      e1 = vv.shift
      idx = pp.index(s/e1)
      if idx.nil?
        can = false
        break
      end
      vv.delete_at(idx)
    end
  end

  ans << (can ? 'YES' : 'NO')
end
puts ans


gets.to_i.times do
    n = gets.to_i
    a = gets.split.map(&:to_i)
    f = true
    ans = 0
    a.each do |x|
        (ans += 1) if (x != 0 && f)
        f = x == 0 ? true : false
    end
    puts (ans > 2)?2 : ans
end

# gets.to_i
# gets.split.map(&:to_i)

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

  ans = 0
  flag = false
  n.times do |i|
    if !flag && a[i] != 0
      ans += 1
      flag = true
    elsif a[i] == 0
      flag = false
    end
  end

  puts [ans,2].min
end

gets.to_i.times do
  gets
  as = gets.split.map(&:to_i)
  bs = as.drop_while { |a| a == 0 }.reverse.drop_while { |a| a == 0 }.reverse
  if bs.empty?
    p 0
  elsif bs.include?(0)
    p 2
  else
    p 1
  end
end

# gets.to_i
# gets.split.map(&:to_i)

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

  ans = 0
  flag = false
  n.times do |i|
    if !flag && a[i] != 0
      ans += 1
      flag = true
    elsif a[i] == 0
      flag = false
    end
  end

  puts ans
end

# gets.to_i
# gets.split.map(&:to_i)

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  list = []

  k = []
  n.times do |i|
    if a[i] != 0
      k << a[i]
    else
      list << k if !k.empty?
      k = []
    end
  end
  list << k if !k.empty?

  puts list.size
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  p xs.slice_when { |a, b| (a == 0) != (b == 0) }.count { |xs| xs.max > 0 }
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  p xs.drop_while { |x| x == 0 }.reverse.drop_while { |x| x == 0 }.reverse.slice_when { |x| x == 0 }.count { |xs| xs.max > 0 }
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map(&:to_i)
    f = true
    ans = 0
    a.each do |x|
        (ans += 1) if (x != 0 && f)
        f = x == 0 ? true : false
    end
    puts ans
end


H,W = gets.split.map(&:to_i)
pic = []
cell = 0
H.times do
  s = gets.chomp
  cell += s.count("*")
  pic << s
end

flg = false
ver = false
(H-2).times do |i|
  1.upto(W-2) do |j|
    if pic[i][j] == "*"
      pic[i][j] = "."
      k = i+1
      while k < H && pic[k][j] == "*"
        pic[k][j] = "."
        if !ver && k < H-1 && pic[k][j-1] == "*" && pic[k][j+1] == "*" && pic[k+1][j] == "*"
          l,r = j-1,j+1
          while l >= 0 && pic[k][l] == "*"
            pic[k][l] = "."
            l -= 1
          end
          while r < W && pic[k][r] == "*"
            pic[k][r] = "."
            r += 1
          end
          ver = true
        end
        k += 1
      end
      flg = true
      break
    end
  end
  break if flg
end
emp = true
H.times do |i|
  W.times do |j|
    emp = false if pic[i][j] == "*"
  end
end
#p pic
puts emp && ver ? "YES" : "NO"

h, w = gets.split.map(&:to_i)
f = h.times.map{gets.chomp}
memo_left = (0..h).map{[0] * w}.to_a
memo_up = (0..h).map{[0] * w}.to_a
memo_right = (0..h).map{[0] * w}.to_a
memo_down = (0..h).map{[0] * w}.to_a
cnt = f.map{|s| s.count('*')}.inject(:+)

h.times{|i|
  w.times{|j|
    next if f[i][j] != '*'
    memo_left[i][j] = (i > 0 ? memo_left[i - 1][j] : 0) + 1
    memo_up[i][j] = (j > 0 ? memo_up[i][j - 1] : 0) + 1
  }}
(h-1).downto(0).each{|i|
  (w-1).downto(0).each{|j|
    next if f[i][j] != '*'
    memo_right[i][j] = (i < h - 1 ?  memo_right[i + 1][j] : 0) + 1
    memo_down[i][j] = (j < w - 1 ? memo_down[i][j + 1] : 0) + 1
  }}

h.times{|i|
  w.times{|j|
    next if f[i][j] != '*'
    a = memo_left[i][j]
    b = memo_right[i][j]
    c = memo_up[i][j]
    d = memo_down[i][j]
    if a > 1 and b > 1 and c > 1 and d > 1 and a + b + c + d - 3 == cnt
      puts :YES
      exit
    end
  }
}
puts :NO

h, w = gets.split.map(&:to_i)
f = h.times.map{gets.chomp}
cnt = f.map{|s| s.count('*')}.inject(:+)
h.times{|i|
  w.times{|j|
    a = f[i].chars.drop_while{|x|x==?.}.take_while{|x|x==?*}.size
    b = (0...h).drop_while{|i|f[i][j]==?.}.take_while{|i|f[i][j]==?*}.size
    if a + b - 1 == cnt
      puts :YES
      exit
    end
  }
}
puts :NO

h, w = gets.split.map(&:to_i)
f = h.times.map{gets.chomp}
cnt = f.map{|s| s.count('*')}.inject(:+)
h.times{|i|
  w.times{|j|
    a = i.downto(0).take_while{|ii| f[ii][j] == ?*}.size
    b = i.upto(h-1).take_while{|ii| f[ii][j] == ?*}.size
    c = j.downto(0).take_while{|jj| f[i][jj] == ?*}.size
    d = j.upto(w-1).take_while{|jj| f[i][jj] == ?*}.size
    if a + b + c + d - 3 == cnt
      puts :YES
      exit
    end
  }
}
puts :NO


# frozen_string_literal: true

def distance(numbers)
  a, b = numbers.minmax
  res = 0
  while a * 2 < b
    res += 1
    a *= 2
  end
  res
end

test = gets.to_i
test.times do
  n = gets.chomp.to_i
  numbers = gets.chomp.split.map(&:to_i)
  result = (0..n-2).reduce(0) do |total, index|
    total + distance(numbers[index..index+1])
  end
  puts result
end

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  q=a[0]
  c=0
  a.each do |aa|
    x,y=[q,aa].sort
    while x*2<y
      x*=2
      c+=1
    end
    q=aa
  end
  puts c
end

alias g gets;g.to_i.times{g;a=g.split.map &:to_i;p (a.zip(a[1..])[..-2].map &:sort).map{|x,y|(-1-y/-x).to_s(2).size-1}.sum}

alias g gets;g.to_i.times{g;a=g.split.map &:to_i;p (a.zip(a[1..])[..-2].map &:sort).map{|x, y|(-1-y/-x).to_s(2).size-1}.sum}

t = gets.chomp.to_i

def getNumInsertions(smaller, bigger)
    nextElement = smaller
    count = 0
    until nextElement * 2 >= bigger
        nextElement *= 2
        count += 1
    end
    return count
end

t.times {
    n = gets.chomp.to_i
    a = gets.chomp.split.map(&:to_i)

    count = 0
    0.upto(n - 2) { |i|
        smaller = a[i] < a[i + 1] ? a[i] : a[i + 1]
        bigger = a[i] > a[i + 1] ? a[i] : a[i + 1]

        count += getNumInsertions(smaller, bigger)
    }

    puts count
}

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i)
	puts a.each_cons(2).sum { |x, y|
			x, y = [x, y].sort
			res = 0
			while x * 2 < y
				res += 1
				x *= 2
			end
			res
	     }
end

def method(x, y)
  x, y = [x, y].minmax
  0.step do |i|
    break i if x * 2 >= y
    x *= 2
  end
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  puts a.each_cons(2).map { |x, y| method(x, y) }.sum
end


gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i)
	x, y = a.count(1), a.count(2)
	x -= 2 if y.odd?
	puts x.odd? || x < 0 ? 'NO' : 'YES'
end

def is_fair_division?(candies, n)
  return true if n == 0

  ones_count = 0
  twos_count = 0
  candies.each do |candy|
    if candy == 1
      ones_count += 1
    elsif candy == 2
      twos_count += 1
    end
  end

  if ones_count % 2 == 1
    return false
  else
    if ones_count >= 2 || twos_count % 2 == 0
      return true
    else
      return false
    end
  end
end

t = gets.chomp().to_i
t.times do |_|
  n = gets.chomp().to_i
  candies = gets.chomp().split("").map(&:to_i)
  if is_fair_division?(candies, n)
    puts "YES"
  else
    puts "NO"
  end
end

gets.to_i.times do
n=gets.to_i
a=gets.split(' ').map(&:to_i).sort
if a.reduce(:+) % 2 == 1 || a[0] == 2 && n % 2 == 1
puts 'NO'
else
puts 'YES'
end
end

gets.to_i.times {
  gets
  a = 0; b = 0
  gets.split.map!(&:to_i).each { _1 == 1 ? (a += 1) : (b += 1) }

  if b.odd?
    if a < 2
      puts "No"
      next
    end

    b += 1
    a -= 2
  end

  if a.odd?
    puts "No"
    next
  end

  puts "Yes"
}

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

  one = a.count(1)
  two = n - one
  # STDERR.puts [one, two].inspect

  possible = (0 .. two).any? { |x|
    (two - 2 * x).abs * 2 <= one and one.even?
  }
  puts possible ? "YES" : "NO"
end

t = gets.to_i
t.times do
  solve
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map { |v| v.to_i }
  puts a.sum.even? && (!a.count(1).zero? || a.count(2).even?) ? "YES" : "NO"
end

a = gets.to_i
for i in 1..a do
    x = gets.to_i
    b = gets.chop.split(/ /).map(&:to_i)
    c = b.sum
    if c % 2 == 1
        print("NO", "\n")
    else
        if b.include? 1
            print("YES", "\n")
        else
            if x % 2 == 0
                print("YES", "\n")
            else
                print("NO", "\n")
            end
        end
    end
end

t = gets.to_i

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

  a[1] ||= 0
  a[2] ||= 0

  if a[2].odd?
    if a[1] > 0 && a[1].even?
      puts 'yes'
    else
      puts 'no'
    end
  else
    if a[1].odd?
      puts 'no'
    else
      puts 'yes'
    end
  end
end

#!/usr/bin/env ruby

gets.to_i.times {
    n = gets.to_i
    sweets = gets.split.map &:to_i
    tv1 = sweets.select{|v| v == 1}.size
    tv2 = sweets.select{|v| v == 2}.size
    res = false
    (tv1 + 1).times do |v1|
        (tv2 + 1).times do |v2|
            g1 = v1 + v2 * 2
            g2 = (tv1 - v1) + (tv2 - v2) * 2
            #puts [g1, g2].to_s
            if g1 == g2
                res = true
            end
        end
    end

    puts res ? "YES" : "NO"
}

gets.to_i.times do
  n =  gets.to_i
  a = gets.split.map &:to_i
  cnt = [0] * 3
  a.each do |i|
    cnt[i] += 1
  end
  if cnt[2] % 2 == 1 && (cnt[1] < 2 || (cnt[1] - 2) % 2 == 1) || cnt[2] % 2 == 0 && cnt[1] % 2 == 1
    puts "NO"
  else
    puts "YES"
  end
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    hash = Hash.new(0)
    ans = 0
    a.each do |i|
        hash[i] += 1
        ans += i
    end
    if ans % 2 != 0
        puts "NO"
    else
        if hash[1] >= 1
            puts "YES"
        elsif hash[2] % 2 != 0
            puts "NO"
        else
            puts "YES"
        end
    end
end

require 'prime'
t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  if arr.sum % 2 != 0 || arr.sum % 2 == 0 && arr.count(1) == 0 && arr.count(2) % 2 != 0
    puts "NO"
  else
    puts "YES"
  end
end

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  s=a.sum
  if s.odd?
    puts "NO"
    next
  end
  s2=s/2
  if s2.even?
    puts "YES"
  elsif a.include?(1)
    puts "YES"
  else
    puts "NO"
  end
end

def is_fair_division?(candies, n)
  return true if n == 0

  ones_sum = 0
  twos_sum = 0
  candies.each do |candy|
    if candy == 1
      ones_sum += 1
    elsif candy == 2
      twos_sum += 2
    end
  end

  return (ones_sum % 2 == 0) && (twos_sum % 2 == 0)
end

t = gets.chomp().to_i
t.times do |_|
  n = gets.chomp().to_i
  candies = gets.chomp().split("").map(&:to_i)
  if is_fair_division?(candies, n)
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.to_i

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

  a[1] ||= 0
  a[2] ||= 0

  if (2 * a[2] + a[1]).odd?
    puts 'no'
  elsif a[2] <= a[1]
    puts 'yes'
  elsif (a[2] - 2 * a[1]).even?
    puts 'yes'
  else
    puts 'no'
  end
end

t = gets.to_i

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

  a[1] ||= 0
  a[2] ||= 0

  if a[1].even? && a[2].even?
    puts 'yes'
  else
    puts 'no'
  end
end


gets
$<.each{|e|n,k=e.split.map &:to_i;puts n.times.map{|i|(97+i%k).chr}*''}

ALPHA = ('a'..'z').to_a
def solution
  count = read_int
  count.times do
    n, k = read_ints
    puts ALPHA[0...k].cycle.first(n).join("")
  end

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

# Functions
def handle(n, k)
  sample = ""
  k.times { |i| sample << (97 + i).chr }
  s = sample * (n / k)
  s << sample.slice(0, n % k)
  return s
end

# MAIN
t = gets.to_i # 1 <= t <= 100

t.times do |i|
  str = gets.split(" ")
  n = str[0].to_i # 1 <= n <= 100
  k = str[1].to_i # 1 <= k <= min(n_i, 26)
  puts handle(n, k)
end

t = gets.chomp.to_i
alph = "abcdefghijklmnopqrstuvwxyz"
t.times do |testcase|
  (n,k) = gets.split.map(&:to_i)
  cur = 0
  res = ""
  n.times do |i|
    if cur == k
      cur = 0
    end
    res<<alph[cur]
    cur+=1
  end
  puts res
end

alp_arr = ('a'..'z').to_a
gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  rep = n / k
  mod = n % k
  ans = alp_arr[0..k-1].join * rep
  if mod > 0
    ans += alp_arr[0..mod-1].join
  end
  puts ans
end

n = gets.to_i
(1..n).each do |i|
  a, b = gets.split.map(&:to_i)
  puts (('a'..('a'.ord + b - 1).chr).to_a.join * 100)[0..a - 1]
end

s = "abcdefghijklmnopqrstuvwxyz"
gets.to_i.times do
    n,k = gets.split.map(&:to_i)
    ans = ""
    (n/k).times do
        ans += s[0,k]
    end
    ans += s[0,n%k]
    puts ans
end

# https://codeforces.com/problemset/problem/1092/A

def solution(n, k)
  s = ""
  j = 0
  n.times do |i|
    s += ((i % k) + 97).chr
  end
  return s
end

q = gets.chomp.to_i
q.times do
  n, k = gets.chomp.split(" ").map(&:to_i)
  puts solution(n, k)
end

t = gets.to_i
t.times do
    n, k = gets.split.map(&:to_i)
    s = ''
    i = 0
    n.times do
        s += ('a'.ord + i).chr
        i += 1
        i %= k
    end
    puts s
end

#

def f(n,k)
    code_a=97
    ans=[]
    for i in 0..n-1
        ans<<i%k+code_a
    end
    ans=ans.pack("C*")
    print(ans,"\n")
end

m=gets().to_i
for i in 1..m
    n,k=gets.chomp().split(" ").map(&:to_i)
    f(n,k)
end

n = gets.to_i
(1..n).each do |i|
  a, b = gets.split.map(&:to_i)
  puts (('a'..('a'.ord + b).chr).to_a.join * 100)[0..a]
end

n = gets.to_i
(1..n).each do |i|
  a, b = gets.split.map(&:to_i)
  puts (('a'..('a'.ord + b).chr).to_a.join * 100)[0..a - 1]
end

#

def f(n,k)
    code_a=97
    ans=[]
    for i in 0..n-1
        ans<<i%k+code_a
    end
    ans=ans.pack("C*")
    p ans
end

m=gets().to_i
for i in 1..m
    n,k=gets.chomp().split(" ").map(&:to_i)
    f(n,k)
end


n = gets.to_i
a = gets.split.collect{|i| i.to_i}
if a.inject(0){|sum,i| sum+i}%n == 0
    puts n
else
    puts n-1
end

n = gets.to_i
a = gets.split.map(&:to_i)
s = a.inject(:+) / n
puts a.inject(0) {|acc, x| acc + x - s }.zero? ? n : n-1

n = gets.to_i
a = gets.split.map &:to_i
sum = a.inject{|sum, el| sum+el}
puts sum%n==0 ? n : n-1

n = gets.to_i
a = gets.split.map { |x| x.to_i }
puts(a.inject(:+) % n == 0 ? n : n - 1)

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

sum = 0
a.each{|x| sum += x}

if sum % n == 0
	puts n
else
	puts n-1
end

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

sum = a.reduce(:+)
if sum % n == 0
	puts n
else
	puts n - 1
end

gets.chomp
sum = 0
a = gets.chomp.split(" ").map {|x| x.to_i}.sort
for i in a
  sum += i
end
if sum % a.length == 0
  puts a.length
else
  puts a.length - 1
end

n=gets.to_i
sum=0
gets.split.map(&:to_i).each{|el|sum+=el}
(n==1) ?  (puts 1) : ((sum%n ==0)? ( puts n) : (puts n-1))

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|n| n.to_i}
sum = a.inject{|sum, n| sum += n}

if sum % n == 0
  puts n
else
  puts [n - 1, 1].max
end

sum = 0
gets.chomp
min = 100000
check = false
a = gets.chomp.split(" ").each {|x|
  x = x.to_i
  sum += x
  min = x if x < min
  if x < 0
    check = true
  end
}
sum += -min * a.length if check
if sum / a.length == 0
  puts a.length
else
  puts sum / a.length
end

sum = 0
gets.chomp
min = 100000
check = false
a = gets.chomp.split(" ").each {|x|
  x = x.to_i
  sum += x
  min = x if x < min
  if x < 0
    check = true
  end
}
sum += -min * a.length if check
if sum % a.length == 0
  puts a.length
else
  puts sum / a.length
end

sum = 0
gets.chomp
gets.chomp.split(" ").each {|x| sum += x.to_i}
puts sum / 2

n=gets.to_i
sum=0
gets.split.map(&:to_i).each{|el|sum+=el}
puts sum/2

n = gets.to_i
a = gets.split.map(&:to_i).sort
while a[-1]-a[0] >= 2 do
  a[0]+=1; a[-1]-=1; a.sort!
  #p a
end
p a.uniq.map {|x| a.count(x) }.inject(:+)-1

n = gets.to_i
a = gets.split.map(&:to_i).sort
while a[-1]-a[0] >= 2 do
  a[0]+=1; a[-1]-=1; a.sort!
end
puts a.uniq.map {|x| a.count(x) }.max


cases = gets.chomp.to_i

cases.times do |i|
  n = gets.chomp.to_i
  hs = gets.chomp.split(" ").map(&:to_i)

  failed = false
  hs.each_with_index do |h, idx|
    if h < idx
      failed = true
      break
    else
      hs[(idx + 1)] += h - idx if hs[(idx + 1)]
    end
  end

  puts failed ? "NO" : "YES"
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    need, cur, ok = 0, 0, true
    n.times do |i|
        need += i
        cur += a[i]
        ok = false if cur < need
    end
    puts ok ? "YES" : "NO"
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    n, h, res = 0, 0, true
    n.times do |i|
        n += i
        h += a[i]
        res = false if h < n
    end
    puts res ? "YES" : "NO"
end


t = gets.to_i

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

  ans = 0
  n.times do |i|
    if a[i] > m
      ans += a[i]-m
      m += a[i]-m
    end

    m -= a[i]
  end

  puts ans
end

gets.to_i.times do
    n, m = gets.split.map(&:to_i)
    puts [gets.split.map(&:to_i).sum - m, 0].max
end

gets.to_i.times do
    n, m = gets.split.map(&:to_i)
    puts([gets.split.map(&:to_i).sum - m, 0].max)
end

gets.to_i.times do
    n, m = gets.split.map(&:to_i)
    x = 0
    w = gets.split.map(&:to_i)
    for item in w
        x += item
    end
    puts((x <= m )? 0 : x - m)
end

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  as = gets.split.map(&:to_i)
  p [as.sum - m, 0].max
end

def solve(xs, m)
   total = xs.sum
   [total - m, 0].max
end

t = gets.chomp.to_i

results = []

t.times do
    n, m = gets.chomp.split.map(&:to_i)
    xs = gets.chomp.split.map(&:to_i)
    results << solve(xs, m)
end

puts results

gets.to_i.times do
  n,m = gets.chomp.split.map(&:to_i)
  a = gets.chomp.split.map(&:to_i)
  ans = 0
  puts [a.reduce(:+) - m,0].max
end


def solve(a, b)
  return -1 unless a.reduce(&:+) == b.reduce(&:+)
  ans = []
  0.upto (a.size - 1) do |i|
    if a[i] < b[i]
      diff = b[i] - a[i]
      for j in (i+1)..(a.size - 1) do
        break if diff == 0
        next if a[j] < b[j]
        cnt = [diff, a[j] - b[j]].min
        cnt.times do
          ans << [j+1, i+1]
          diff -= 1
          a[i] += 1
          a[j] -= 1
        end
      end

    elsif a[i] > b[i]
      diff = a[i] - b[i]
      for j in (i+1)..(a.size - 1) do
        break if diff == 0
        next if a[j] > b[j]
        cnt = [diff, b[j] - a[j]].min
        cnt.times do
          ans << [i+1, j+1]
          diff -= 1
          a[i] -= 1
          a[j] += 1
        end
      end
    end
  end

  ans
end

t = gets.chomp.to_i
t.times do
  gets
  a = gets.chomp.split(' ').map(&:to_i)
  b = gets.chomp.split(' ').map(&:to_i)

  ans = solve(a, b)
  if ans == -1
    puts "-1\n"
  else
    puts "#{ans.size}\n"
    for i, j in ans do
      puts "#{i} #{j}\n"
    end
  end
end

def read_array()
  gets.split(" ").map { |s| s.to_i  }
end
t = gets.to_i
for z in 1..t
  n = gets.to_i
  a, b = read_array, read_array
  m, p = [], []
  for i in 0...n
    x = a[i] - b[i]
    if a[i] > b[i]
      m << [i + 1, a[i] - b[i]]
    elsif a[i] < b[i]
      p << [i + 1, b[i] - a[i]]
    end
  end
  res = []
  while p.length() > 0 && m.length() > 0
    x = [p[-1][1], m[-1][1]].min
    p[-1][1] -= x
    m[-1][1] -= x
    while x > 0
      x -= 1
      res << [m[-1][0], p[-1][0]]
    end
    if p[-1][1] == 0
      p.pop()
    end
    if m[-1][1] == 0
      m.pop()
    end
  end
  if p.length() != 0 || m.length() != 0
    puts(-1)
    next
  end
  puts res.length()
  for i in res
      puts i[0].to_s + ' ' + i[1].to_s
  end
end


def gao(i, j, m, n)
	m /= n
	m > 0 && ($a[i / m][j / m] == '*' || gao(i % m, j % m, m, n))
end

I = open('input.txt')
O = open('output.txt', 'w')
n, k = I.gets.split.map(&:to_i)
$a = n.times.map{I.gets}
m = n ** k
O.puts m.times.map{|i| m.times.map{|j| gao(i, j, m, n) ? '*' : '.'} * ''} * "\n"

def rec_paint(canvas,shape,x1,y1,x2,y2,n,dim)
	scale=n**dim
	if dim==0
		0.upto(n-1){|y|
		0.upto(n-1){|x|
			#puts"%d %d, %d %d"%[y1+y,x1+x,y,x]
			canvas[y1+y][x1+x]=shape[y][x]
		}}
	else
		0.upto(n-1){|y|
		0.upto(n-1){|x|
			xx1=x1+scale*x
			yy1=y1+scale*y
			xx2=xx1+n-1
			yy2=yy1+n-1
			rec_paint(canvas,shape,xx1,yy1,xx2,yy2,n,dim-1) unless shape[y][x]==:*
		}}

	end
end
f,g=open("input.txt","r"),open("output.txt","w")
#f,g=$stdin,$stdout
n,k=f.gets.split.map(&:to_i)
scale=n**k
shape=[]
n.times{shape<<f.gets.chomp.split(//).map(&:to_sym)}
canvas=(1..n**k).map{[nil]*(n**k)}
rec_paint(canvas,shape,0,0,scale-1,scale-1,n,k-1)
g.puts(canvas.map{|syms|syms.map{|sym|sym ? sym.to_s : :*}*""}*$/)
f.close
g.close

#!/usr/bin/env ruby

open("input.txt"){|f|open("output.txt","w"){|g|

n, k = f.gets.split.map(&:to_i)
t = n.times.map { f.gets.chomp }

(n**k).times do |ii|
  (n**k).times do |jj|
    i, j = ii, jj
    result = '.'
    k.times do
      if t[i % n][j % n] == '*'
        result = '*'
        break
      end
      i /= n
      j /= n
    end
    g.print result
  end
  g.puts
end
}}


up = Integer gets
down = Integer gets
height = up + down + 1
arry = []
arry[up] = height
for i in 0..up-1
  if i == 0
    arry[0] = 1
  else
    arry[i] = arry[i-1]+1
  end
end

for j in up+1..down+up
  arry[j] = arry[j-1]-1
end

for k in 0..down+up-1
  printf("#{arry[k]} ")
end
puts arry[down+up]

a = gets.to_i
b = gets.to_i
c = b + 1
print "#{c} "
a.times do
  c += 1
  print "#{c} "
end
c = b
b.times do
  print "#{c} "
  c -= 1
end

#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/491/A

up = gets.to_i
i = down = gets.to_i

max = up + down + 1

answer = ""

answer << "#{current = max - up} "

while i != 0 do
  answer << "#{current -= 1} "
  i -= 1
end

current += down

while up != 0 do
  answer << "#{current += 1} "
  up -= 1
end

puts answer

a = gets.to_i
b = gets.to_i
c = 100000
print "#{c} "
a.times do
  c += 1
  print "#{c} "
end
b.times do
  c -= 1
  print "#{c} "
end

a = gets.to_i
b = gets.to_i
c = b + 1
print "#{c} "
a.times do
  c += 1
  print "#{c} "
end
b.times do
  c -= 1
  print "#{c} "
end

up = Integer gets
down = Integer gets
height = up + down + 1
arry = []
if up > down
  arry[0] = 1
  arry[1] =3
  for i in 2..up
    arry[i] = arry[i-1]+1
  end
  for j in up+1..down+up
    arry[j] = arry[j-1]-1
    arry[j] = arry[j] == 3 ? 2 : arry[j]
  end
else
  arry[0] = 2
  for i in 1..up
    arry[i] = arry[i-1]+1
  end

  for j in up+1..down+up
    arry[j] = arry[j-1]-1
    arry[j] = arry[j] == 2 ? 1 : arry[j]
  end
end

for k in 0..down+up-1
  printf("#{arry[k]} ")
end
puts arry[down+up]

up = Integer gets
down = Integer gets
height = up + down + 1
arry = []
if up > down
  arry[0] = 1
  arry[1] =3
  for i in 2..up
    arry[i] = arry[i-1]+1
  end
  for j in up+1..down+up
    arry[j] = arry[j-1]-1
  end
else
  arry[0] = 2
  for i in 1..up
    arry[i] = arry[i-1]+1
  end
  for j in up+1..down+up
    arry[j] = arry[j-1]-1
  end
end

for k in 0..down+up-1
  printf("#{arry[k]} ")
end
puts arry[down+up]


DBG = true
q = gets.to_i
for i in 0...q
  n,a,b = gets.split.map{|z| z.to_i}
  if 2*a <= b
    puts "#{n*a}"
  else
    puts "#{b*(n/2)+a*(n%2)}"
  end
end

gets.to_i.times do
	n, a, b = gets.split.map(&:to_i)
	p [a * n, b * (n / 2) + a * (n % 2)].min
end

def solution
  q = read_int

  q.times do
    n, a, b = read_ints

    one_liter = n * a
    two_liter = (n / 2) * b + (n.odd? ? a : 0)

    p [one_liter, two_liter].min

  end

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

q = gets.to_i
ans = 0
q.times do
  n, a, b = gets.split.map(&:to_i)
  if a <= b / 2
    puts n * a
  else
    puts (n/2) * b + (n - (n/2*2)) * a
  end
end

q = gets.strip.to_i
q.times do
  n, a, b = gets.strip.split.map(&:to_i)
  first = n * a
  second = (n / 2) * b
  second += a if n.odd?
  ans = [first, second].min
  puts ans
end

gets.to_i.times do
    n,a,b = gets.split.map(&:to_i)
    if a*2 < b
        puts n*a
    else
        puts n/2*b+(n%2)*a
    end
end

def solve(n,a,b)
 [n*(a),b*(n/2) + a*(n%2)].min
 end
t = gets.to_i
t.times{
n,a,b = gets.split(" ").map(&:to_i)
puts solve(n,a,b)
}

# n - required amount of water (in liters)
# a - 1L bottle cost
# b - 2L bottle cost
def price(n, a, b)
  if 2 * a <= b
    n * a
  else
    n / 2 * b + n % 2 * a
  end
end

def respond_n_queries(n)
  n.times do
    n, a, b = gets.chomp.split.map(&:to_i)
    puts price(n, a, b)
  end
end

queries = gets.to_i
respond_n_queries(queries)

def solve(n,a,b)
 ans = 0
 if(n==1)
  return a
  end
 if(b<=a)
  if(n%2==0)
    return b*(n/2)
  end
  return b*(n/2)+a
 end
 return a*n
 end

t = gets.to_i

t.times{

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

puts solve(n,a,b)

}

# n - required amount of water (in liters)
# a - 1L bottle cost
# b - 2L bottle cost
def price(n, a, b)
  if a <= b
    n * a
  else
    n / 2 * b + n % 2 * a
  end
end

def respond_n_queries(n)
  n, a, b = gets.chomp.split.map(&:to_i)
  puts price(n, a, b)
end

queries = gets.to_i
respond_n_queries(queries)

# n - required amount of water (in liters)
# a - 1L bottle cost
# b - 2L bottle cost
def price(n, a, b)
  if a <= 2 * b
    n * a
  else
    n / 2 * b + n % 2 * a
  end
end

def respond_n_queries(n)
  n.times do
    n, a, b = gets.chomp.split.map(&:to_i)
    puts price(n, a, b)
  end
end

queries = gets.to_i
respond_n_queries(queries)

# n - required amount of water (in liters)
# a - 1L bottle cost
# b - 2L bottle cost
def price(n, a, b)
  if a <= b
    n * a
  else
    n / 2 * b + n % 2 * a
  end
end

def respond_n_queries(n)
  n.times do
    n, a, b = gets.chomp.split.map(&:to_i)
    puts price(n, a, b)
  end
end

queries = gets.to_i
respond_n_queries(queries)


n = gets.to_i
a = gets.split.map(&:to_i)
b = Array.new(n,0)
for i in 1..n-1
    b[i] = b[i-1]+a[i-1]
end
mn = b.min
for i in 0..n-1
    b[i] -= mn-1
end
c = Hash.new(false)
for i in 0..n-1
    if c[b[i]] || b[i] > n
        puts -1
        exit
    end
    c[b[i]] = true
end
puts b.join(" ")

def main
  n = gets.chomp.to_i
  q = gets.chomp.split.map(&:to_i)

  qp = [0]
  for k in q
    qp << qp.last + k
  end

  diff = 1 - qp.min
  result = []
  visited = {}
  for k in qp
    if k + diff < 1 or k + diff > n or visited.key?(k + diff)
      puts -1
      return
    else
      result << k + diff
      visited[k + diff] = true
    end
  end

  puts result.join(" ")
end

main

N = gets.to_i
Q = gets.split.map &:to_i
ps = [0]
Q.each{ |q| ps << (ps[-1] + q) }
mx = ps.max
mn = ps.min
if mx - mn == N-1 && ps.uniq.size == N
  puts ps.map{ |r| r - mn + 1 }.join(" ")
else
  p -1
end

n = gets.to_i
a = gets.split.map(&:to_i)
b = Array.new(n,0)
for i in 1..n-1
    b[i] = b[i-1]+a[i-1]
end
mn = b.min
for i in 0..n-1
    b[i] -= mn-1
end
c = Hash.new(false)
for i in 0..n-1
    if c[b[i]] || b[i] > n
        puts -1
        exit
    end
end
puts b.join(" ")

def main
  n = gets.chomp.to_i
  q = gets.chomp.split.map(&:to_i)

  diff = q.inject(0) { |sum, x| sum + x }
  for i in (1..n)
    next if i + diff < 1 or i + diff > n

    visited = {i => true, i + diff => true}
    result = [i]

    for j in q
      x = result.last + j
      break if x < 1 or x > n or visited.key?(x)
      visited[x] = true
      result << x
    end

    result << i + diff
    if result.length == n
      puts result.join(" ")
      return
    end
  end

  puts -1
end

main

def main
  n = gets.chomp.to_i
  q = gets.chomp.split.map(&:to_i)

  diff = q.inject(0) { |sum, x| sum + x }
  for i in (1..n)
    next if i + diff < 1 or i + diff > n

    visited = {i => true, i + diff => true}
    result = [i]

    for j in q
      x = result.last + j
      break if x < 1 or x > n or visited.key?(x)
      visited[x] = true
      result << x
    end

    result << i + diff
    break if visited.key?(i + diff)

    if result.length == n
      puts result.join(" ")
      return
    end
  end

  puts -1
end

main

def main
  n = gets.chomp.to_i
  q = gets.chomp.split.map(&:to_i)

  diff = q.last - q.first
  for i in (1..n)
    visited = {i => true, i + diff => true}
    result = [i]

    for j in q
      x = result.last + j
      break if x < 1 or visited.key?(x)
      visited[x] = true
      result << x
    end

    if result.length == n
      puts result.join(" ")
      return
    end
  end

  puts -1
end

main


n = gets.to_i
a = gets.split.map(&:to_i)
ans = 0
for i in 2..n-1
    if a[i] == 1 && a[i-1] == 0 && a[i-2] == 1
        ans += 1
        a[i] = 0
    end
end
puts ans

n = gets.strip.to_i
flats = gets.strip.gsub(' ', '')
k = 0
until !flats.include?("10101")
  flats.sub!("10101", "10001")
  k += 1
end

until !flats.include?("101")
  flats.sub!("101", "100")
  k += 1
end
puts k


n=gets.to_i
points=[]
n.times do
	points<<gets.chomp.split.map(&:to_i)
end
count,ctr=0,0
points.each { |x, y|
  ctr+= 1 if points.any? {|a, b| b == y && a < x } && points.any? {|a, b| b == y && a > x } && points.any? {|a, b| a == x && b < y } && points.any? {|a, b| a == x && b > y }
}
puts ctr

gets; input = STDIN.read.split("\n")
input.map! {|i| i.split(" ").map(&:to_i)}

count = 0

input.each do |i|
  c = [false, false, false, false]
  input.each do |j|
    if i != j

      c[0] = true if i[0]  > j[0] && i[1] == j[1]
      c[1] = true if i[0]  < j[0] && i[1] == j[1]
      c[2] = true if i[0] == j[0] && i[1]  < j[1]
      c[3] = true if i[0] == j[0] && i[1]  > j[1]

      if c.count(true) == 4
        count += 1
        break
      end
    end
  end
  c = [false, false, false, false]
end

print count

class SupercentralPoint
  # To change this template use File | Settings | File Templates.

end

number = STDIN.readline.to_i

#p number

z = []

point = {:x => 0, :y => 0}

number.times do
  v = STDIN.readline.split(' ')
  point[:x] = v[0].to_i
  point[:y] = v[1].to_i
  z << point.dup
end

supercentral_points = []

z.each do |i|
  z.each do |j|
    if i[:x] == j[:x]
      if i[:y] > j[:y]
        i[:bot] = true
        j[:top] = true
      elsif i[:y] < j[:y]
        j[:bot] = true
        i[:top] = true
      end
    end

    if i[:y] == j[:y]
      if i[:x] > j[:x]
        i[:left] = true
        j[:right] = true
      elsif i[:x] < j[:x]
        j[:left] = true
        i[:right] = true
      end

    end
  end
end

z.each do |i|

  if i[:top] && i[:bot] && i[:left] && i[:right]
    supercentral_points << i
  end
end

p supercentral_points.count

n = gets.to_i
hs = Hash.new 0
n.times{ hs[ gets.split(' ').map(&:to_i) ] = 0 }
ar = hs.keys
ar.product(ar).map(&:flatten).each do |x0,y0,x1,y1|
  vec = [x0,y0]
  if x1 == x0
    if y1 > y0
      hs[vec] |= 1
    elsif y1 < y0
      hs[vec] |= 2
    end
  elsif y1 == y0
    if x1 > x0
      hs[vec] |= 4
    elsif x1 < x0
      hs[vec] |= 8
    end
  end
end
puts hs.select{|k,v| v == 15}.count

sin = STDIN
sout = STDOUT
# sin = File.open("test.in", "r")
# sout = File.open("test.out", "w")

n = sin.readline.to_i
a = []
n.times {
    |i| a[i] = sin.readline.split.map{|e| e.to_i}
}
sout.puts a.find_all{ |e| a.find{|x| x[0] < e[0] && x[1] == e[1]} != nil && a.find{|x| x[0] > e[0] && x[1] == e[1]} != nil \
                         && a.find{|x| x[0] == e[0] && x[1] > e[1]} != nil && a.find{|x| x[0] == e[0] && x[1] < e[1]} != nil}.size

n = gets.chomp.to_i
c = []
xh = Hash.new()
yh = Hash.new()
for i in 0..n-1
  a = gets.chomp.split(/ /).map!{|x| x.to_i}
  c << a
  if xh.key?(a[0])
    xh[a[0]] << a
  else
    xh[a[0]] = [a]
  end
  if yh.key?(a[1])
    yh[a[1]] << a
  else
    yh[a[1]] = [a]
  end
end
xh.each_key{|key| xh[key].sort!{|a1,a2| a1[1] <=> a2[1] } }
yh.each_key{|key| yh[key].sort!{|a1,a2| a1[0] <=> a2[0] } }

ret = 0
xh.each_key{|key|
  aa = xh[key]
  if aa.length >= 3
    for i in 1..aa.length-2
      yk = aa[i][1]
      if yk > aa[0][1] && yk < aa[aa.length-1][1]
        bb = yh[yk]
        if bb.length >= 3
          if key > bb[0][0] && key < bb[bb.length-1][0]
            ret += 1
          end
        end
      end
    end
  end
}
puts ret

n = gets.to_i
points = []
n.times do
        points << gets.split.map(&:to_i)
end
ans = 0
points.each do |pts1|
        check = 0
        points.each do |pts2|
                check |= 1 if pts1[0] < pts2[0] && pts1[1] == pts2[1]
                check |= 2 if pts1[0] > pts2[0] && pts1[1] == pts2[1]
                check |= 4 if pts1[1] < pts2[1] && pts1[0] == pts2[0]
                check |= 8 if pts1[1] > pts2[1] && pts1[0] == pts2[0]
        end
        ans += 1 if check == 15
end
p ans

n = gets.to_i
mas = []
n-=1
for i in (0..n) do
    mas[i] = gets.split.map(&:to_i)
end
count = 0
mas.each do |m1|
    q,w,e,r = false
    mas.each do |m2|
        if m1 != m2
            if m1[0] > m2[0] && m1 [1] == m2[1]
                q = true
            end
            if m1[0] < m2[0] && m1 [1] == m2[1]
                w = true
            end
            if m1[0] == m2[0] && m1 [1] < m2[1]
                e = true
            end
            if m1[0] == m2[0] && m1 [1] > m2[1]
                r = true
            end
        end
    end
    if q && w && e && r
        count+=1
    end
end
puts count

n = gets.chomp.to_i

points = []
n.times do
  x, y = gets.chomp.split(" ").map{|e| e.to_i}
  points << [x, y]
end
supercentrals = []

points.each do |point|
  x, y = point[0], point[1]
  right, left, up, down = false, false, false, false

  points.each do |point2|
    if point == point2
      next
    end
    x2, y2 = point2[0], point2[1]

    if x2 > x and y == y2
      right = true
    elsif x2 < x and y == y2
      left = true
    elsif x == x2 and y2 > y
      up = true
    elsif x == x2 and y2 < y
      down = true
    end
  end

  if right and left and up and down
    supercentrals << point
  end
end

puts supercentrals.length

n = gets.chomp.to_i
points = []
n.times do
    points << gets.chomp.split(/ /).map(&:to_i)
end
ans = 0
n.times do |i|
    point_1 = points[i]
    x = point_1[0]
    y = point_1[1]
    lower_n = upper_n = left_n = right_n = false
    n.times do |j|
        next if i == j
        point_2 = points[j]
        x_1 = point_2[0]
        y_1 = point_2[1]
        right_n = true if x_1 > x && y_1 == y && !right_n
        left_n = true if x_1 < x && y_1 == y && !left_n
        lower_n = true if x_1 == x && y_1 < y && !lower_n
        upper_n = true if x_1 == x && y_1 > y && !upper_n
    end
    ans += 1 if upper_n && lower_n && left_n && right_n
end
puts ans

hsX={}
hsY={}
n=gets.to_i
n.times{|i|
	x,y=gets.split.map{|e| e.to_i}
	if hsX.member?(x)==false
		hsX[x]=[]
	end
	if hsY.member?(y)==false
		hsY[y]=[]
	end
	hsX[x]<<[y,i]
	hsY[y]<<[x,i]
}
hsX=hsX.select{|k,v|
	v.size>2
}
hsY=hsY.select{|k,v|
	v.size>2
}
ans=0

vx=hsX.values.map{|xs|
	xs.sort!
	xs.shift
	xs.pop
	xs
}
vy=hsY.values.map{|ys|
	ys.sort!
	ys.pop
	ys.shift
	ys
}
vx.each{|vx1|
	vy.each{|vy1|
		vx1.each{|e1|
			vy1.each{|e2|
				ans+=1 if e1[1]==e2[1]
			}
		}
	}
}
puts ans

n = gets.to_i

a = Array.new

n.times do |i|
	a << gets.split(" ").map{|x| x.to_i}
end

#puts a.to_s

sum = 0

for i in 0...a.length
	next if a.take(i).count{|x,y| a[i][0] == x} > 0
	b = a.select{|x,y| x == a[i][0]}
	b.sort!{|x,y| x[1] <=> y[1]}
	b = b.drop(1)
	b.pop
	#puts b.to_s
	for j in 0...b.length do
		if(a.count{|x,y| b[j][1] == y && b[j][0] < x} > 0 &&
			a.count{|x,y| b[j][1] == y && b[j][0] > x} > 0)
			then
			sum += 1
		end
	end
end

puts sum

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

n = $stdin.gets.to_i
h = Hash.new { Array.new }
v = Hash.new { Array.new }
ps = []
n.times do
  x, y = *$stdin.gets.chomp.split.map(&:to_i)
  ps.push [x, y]
  h[x] += [y]
  v[y] += [x]
end

h.keys.each do |x|
  h[x].sort!
end
v.keys.each do |y|
  v[y].sort!
end

ans = 0
ps.each do |x, y|
  i = h[x].find_index y
  j = v[y].find_index x
  if 0 < i and i < h[x].size-1 and 0 < j and j < v[y].size-1
    ans += 1
  end
end
puts ans

class Point
	attr_accessor :x,:y

	def initialize(_x,_y)
		self.x = _x
		self.y = _y
	end
end

def left?(p)
	@points.any?{|a| a.x < p.x && p.y == a.y}
end

def right?(p)
	@points.any?{|a| a.x > p.x && p.y == a.y}
end

def upper?(p)
	@points.any?{|a| a.x == p.x && p.y < a.y}
end

def lower?(p)
	@points.any?{|a| a.x == p.x && p.y > a.y}
end

n = gets.to_i
@points = []

n.times do
	x,y = gets.split(" ").map(&:to_i)
	@points << Point.new(x,y)
end

count = 0
@points.each do |p|
	count += 1 if left?(p) && right?(p) && upper?(p) && lower?(p)
end
puts count

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
ans = a.count do |x, y|
  check = Array.new(4, false)
  a.each do |xx, yy|
    if xx == x
      check[2] ||= yy < y
      check[3] ||= yy > y
    elsif yy == y
      check[0] ||= xx < x
      check[1] ||= xx > x
    end
  end
  check.all?
end
puts ans

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
c = 0
a.each do |x, y|
  check = [0, 0, 0, 0]
  a.each do |xx, yy|
    if xx == x
      check[2] = 1 if yy < y
      check[3] = 1 if yy > y
    elsif yy == y
      check[0] = 1 if xx < x
      check[1] = 1 if xx > x
    end
  end
  c += 1 if check.all?{|x| x == 1}
end
puts c

n = gets.to_i
xmi, xmx, ymi, ymx = [1000, -1000, 1000, -1000]
a = n.times.map{gets.split.map(&:to_i)}
c = 0
a.each do |x, y|
  check = [0, 0, 0, 0]
  a.each do |xx, yy|
    if xx == x
      check[2] = 1 if yy < y
      check[3] = 1 if yy > y
    elsif yy == y
      check[0] = 1 if xx < x
      check[1] = 1 if xx > x
    end
  end
  c += 1 if check.all?{|x| x == 1}
end
puts c

xs = Array.new(2011) { Array.new }
ys = Array.new(2011) { Array.new }

n = gets.to_i
a = []
Point = Struct.new(:x, :y)

(0...n).each { |i|
  x, y = gets.split.map(&:to_i)
  a << Point.new(x, y)
  xs[y+1000] << x
  ys[x+1000] << y
}
(0...2011).each { |i|
  xs[i].sort!
  ys[i].sort!
}

res = 0
a.each { |p|
  if xs[p.y+1000][0] < p.x && p.x < xs[p.y+1000][-1]
    if ys[p.x+1000][0] < p.y && p.y < ys[p.x+1000][-1]
      res += 1
    end
  end
}
p res

points = []
for _ in 1..Integer(gets.chomp)
  points << gets.chomp.split.map { |x| x.to_i }
end

count = 0
points.each { |x, y|
  count += 1 if points.any? {|a, b| b == y && a < x } && points.any? {|a, b| b == y && a > x } && points.any? {|a, b| a == x && b < y } && points.any? {|a, b| a == x && b > y }
}
puts count

pts=gets.to_i.times.map{gets.split.map(&:to_i)}
ansx=[]
ansy=[]
pts.group_by{|x,y|x}.each{|_x,ary|ary.sort_by{|x,y|y}[1..-2].each{|pt|ansx<<pt} if ary.size>2}
pts.group_by{|x,y|y}.each{|_y,ary|ary.sort_by{|x,y|x}[1..-2].each{|pt|ansy<<pt} if ary.size>2}
p (ansx&ansy).size

n = gets.chomp.to_i
points = []
n.times do
    points << gets.chomp.split(/ /).map(&:to_i)
end
ans = 0
n.times do |i|
    point_1 = points[i]
    x = point_1[0]
    y = point_1[1]
    lower_n = upper_n = left_n = right_n = false
    n.times do |j|
        next if i == j
        point_2 = points[j]
        x_1 = point_2[0]
        y_1 = point_2[1]
        right_n = true if x_1 > x && y_1 == y && !right_n
        left_n = true if x_1 < x && y_1 == y && !left_n
        lower_n = true if x_1 == x && y_1 < y && !lower_n
        upper_n = true if x_1 == x && y_1 > y && !upper_n
    end
    if upper_n && lower_n && left_n && right_n
        puts point_1.inspect
    end
    ans += 1 if upper_n && lower_n && left_n && right_n
end
puts ans

n = gets.chomp.to_i
points = []
n.times do
    points << gets.chomp.split(/ /).map(&:to_i)
end
ans = 0
n.times do |i|
    point_1 = points[i]
    x = point_1[0]
    y = point_1[1]
    neighbor = 0
    n.times do |j|
        next if i == j
        point_2 = points[j]
        x_1 = point_2[0]
        y_1 = point_2[1]
        neighbor += 1 if x_1 > x && y_1 == y
        neighbor += 1 if x_1 < x && y_1 == y
        neighbor += 1 if x_1 == x && y_1 < y
        neighbor += 1 if x_1 == x && y_1 > y
    end
    ans += 1 if neighbor >= 4
end
puts ans

n=gets.to_i
hsX=Hash.new{|hash,key| hash[key]=[]}
hsY=Hash.new{|hash,key| hash[key]=[]}
n.times{|i|
	x,y=gets.split.map{|e| e.to_i}
	hsX[x]<<i
	hsY[y]<<i
}
hsX=hsX.select{|k,v|
	v.size>2
}
hsY=hsY.select{|k,v|
	v.size>2
}
ans=0
hsX.values.each{|vx|
	hsY.values.each{|vy|
		vx.each{|e|
			ans+=1 if vy.index(e)!=nil
		}
	}
}
puts ans

points = Hash.new { |h, k| h[k] = [] }
for _ in 1..Integer(gets.chomp)
  x, y = gets.chomp.split.map { |x| x.to_i }
  points[x] << y
end

count = 0
points.values.each { |v|
  count += v.size - 2 if v.size > 2
}
puts count

pts=gets.to_i.times.map{gets.split.map(&:to_i)}
ans=[]
pts.group_by{|x,y|x}.each{|_x,ary|ary.sort_by{|x,y|y}[1..-2].each{|pt|ans<<pt} if ary.size>2}
pts.group_by{|x,y|y}.each{|_y,ary|ary.sort_by{|x,y|x}[1..-2].each{|pt|ans<<pt} if ary.size>2}
p ans.uniq.size

gets; input = STDIN.read.split("\n")
input.map! {|i| i.split(" ").map(&:to_i)}

count = 0

input.each do |i|
  c = 0
  input.each do |j|
    if i != j
      if (i[0]  > j[0] && i[1] == j[1]) ||
         (i[0]  < j[0] && i[1] == j[1]) ||
         (i[0] == j[0] && i[1]  < j[1]) ||
         (i[0] == j[0] && i[1]  > j[1])
        c += 1
      end
      if c == 4
        c = 0
        count += 1
        break
      end
    end
  end
end

print count


n=gets.to_i;
a=gets.split.map{|x| x.to_i; }
sum=a.inject{|sum,i| sum+i }
res=[];
a.length.times{ |x|
  if a[x]*a.length==sum then
    res.push(x+1);
  end
}
puts(res.length);
res.map{|x| print(x," "); }

input = STDIN.read.split("\n")

n = input[0].to_i
a = input[1].split(" ").map(&:to_i)

avg = a.inject(0.0) {|sum, e| sum + e} / n

str = ""
count = 0

a.each.with_index do |v, k|
	if v.to_f == avg
		str << (k + 1).to_s << " "
		count += 1
	end
end

STDOUT.puts count
STDOUT.puts str.chop

n=gets.chomp.to_i
a=[]
b=[]
sum=0
a=gets.chomp.strip.split(' ')
i=0
while i<a.length do
	a[i]=a[i].to_i
	sum+=a[i]
	i+=1
end
i=0
while i<a.length do
	if a[i]*(n-1)==(sum-a[i])
		b.push(i+1)
		#puts "YES"
	end
	i+=1
end
puts b.length
b.each do |x|
	print x.to_s+' '
end

def average_numbers
  count = gets.to_i
  nums  = gets.split(' ').map{|i| i.to_i}
  sum = nums.inject(0){|sum, i| sum+i}

  idx = []
  nums.each_with_index do |n, ind|
    idx << ind+1 if (n.to_f == sum/count.to_f)
  end
  puts idx.size
  puts idx.join ' '
end

average_numbers

n=gets.to_i;
a=gets.split.map{|x| x.to_i};
sum=a.inject{|sum,i| sum+i };
res=[];
for i in 0..(a.length-1) do
  if a[i]*a.length == sum then
    res.push(i);
  end
end
puts(res.length);
for i in 0..(res.length-1) do
  print( res[i]+1," ");
end
puts("");

n=gets.to_i
a=gets.split.map(&:to_i)
s=a.inject(:+)
m=s/n
puts s%n>0 ? 0 : [a.count(m),a.each_with_index.select{|a,i|a==m}.map{|a,i|i+1}*" "]

sum=gets.to_i*0
a=gets.split.map(&:to_i)
a.each{|s| sum+=s}
ans=[]
a.length.times{|i|
        if a[i]*(a.length-1)==(sum-a[i])
                ans.push(i)
        end
}
p ans.length
ans.each{|i| print "#{i+1} "}
puts

n=gets.chomp.to_i
a=[]
b=[]
sum=0
a=gets.chomp.strip.split(' ')
i=0
while i<a.length do
	a[i]=a[i].to_i
	sum+=a[i]
	i+=1
end
i=0
while i<a.length do
	if a[i]==(sum-a[i])/(n-1)
		b.push(i+1)
		#puts "YES"
	end
	i+=1
end
puts b.length
b.each do |x|
	print x.to_s+' '
end

def average_numbers
  count = gets.to_i
  nums  = gets.split(' ').map{|i| i.to_i}
  sum = nums.inject(0){|sum, i| sum+i}

  idx = []
  nums.each_with_index do |n, ind|
    idx << ind+1 if (n == sum/count)
  end
  puts idx.size
  puts idx.join ' '
end

average_numbers

sum=gets.to_i*0
a=gets.split.map(&:to_i)
a.each{|s| sum+=s}
ans=[]
a.length.times{|i|
        if a[i]==(sum-a[i])/(a.length-1)
                ans.push(i)
        end
}
p ans.length
ans.each{|i| print "#{i+1} "}
puts


A = a = gets.to_i
b = [2, 13]

if a == 2
	x = 0
	y = 0
else
	x = 0
	while a % 12 == 0
		a /= 12
		x += 1
	end

	y = 1
	while b[y] < a
		b.push 13 * b[-1] - 12 * b[-2]
		y += 1
	end
end

if b[y] != a
	puts :NO
else
	puts :YES
	p 1
	p x + y + 1
	if A == 1729
		puts "1\n156"
	else
		puts "0"
	end
end

# 题目看不懂 >v<


def solve(n, a, b)
    s = 0
    arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}
    m = (arr + [0]).min
    res = (0...n).select {|i| arr[i] == m }
    res << 0 if m == 0
    res
end

n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
r = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}
puts "#{r.size}\n#{r.join(' ')}"

#!/usr/bin/ruby

def solve(n, a, b)
    fuel = a[0]
    arr = [fuel]
    (n-1).times { |i|
        fuel -= b[i]
        arr << fuel
        fuel += a[i+1]
    }
    min = arr.min
    ret = []
    if min >= 0
        ret << 1
        min = 0
    end
    arr.each_with_index { |v, i|
        ret << i+1 if v == min
    }
    ret
end

n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
b_rev = b.reverse
b_rev = b_rev[1..-1] + [b_rev[0]]
a1 = solve(n, a, b)
a2 = solve(n, a.reverse, b_rev).map{|v| n+1-v}
ans = (a1+a2).sort.uniq
puts "#{ans.size}\n#{ans.join(' ')}"

#!/usr/bin/ruby

def solve(n, a, b)
    fuel = a[0]
    arr = [fuel]
    (n-1).times { |i|
        fuel -= b[i]
        arr << fuel
        fuel += a[i+1]
    }
    min = arr.min
    ret = []
    if min < 0
        a.each_with_index { |v,i|
            ret << i+1 if arr[i] == min
        }
    else
        ret << 1
        a.each_with_index { |v,i|
            ret << i+1 if arr[i] == 0
        }
    end
    ret
end

n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
b_rev = b.reverse
b_rev = b_rev[1..-1] + [b_rev[0]]
a1 = solve(n, a, b)
a2 = solve(n, a.reverse, b_rev).map{|v| n+1-v}
ans = (a1+a2).sort.uniq
puts "#{ans.size}\n#{ans.join(' ')}"

#!/usr/bin/ruby

def solve(n, a, b)
    fuel = a[0]
    arr = [fuel]
    (n-1).times { |i|
        fuel -= b[i]
        arr << fuel
        fuel += a[i+1]
    }
    min = arr.min
    ret = []
    if min < 0
        ret << 1
        min = 0
    end
    a.each_with_index { |v,i|
        ret << i+1 if arr[i] == 0
    }
    ret
end

n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
b_rev = b.reverse
b_rev = b_rev[1..-1] + [b_rev[0]]
a1 = solve(n, a, b)
a2 = solve(n, a.reverse, b_rev).map{|v| n+1-v}
ans = (a1+a2).sort.uniq
puts "#{ans.size}\n#{ans.join(' ')}"


n,m = gets.split.map(&:to_i)
d = (m+1)/2
i = d
for k in 1..n
  p i
  if i==d and m.odd? then i=d-1
  elsif i==d and !m.odd? then i=d+1
  elsif i>d then i=m-i
  elsif i<d then i=m-i+1
  end
  if i<1 or i>m then i=d end
end

n,m = gets.split.map{|e| e.to_i}
for i in (1..n) do
  k = i.modulo(m)
  if k==0 then k=m end
  r = k/2
  na = k.modulo(2)
  sr = (m+1)/2
  if na==0 then na=-1 end
  if m.modulo(2)==0 then na*=-1 end
  ans=sr+na*r
  puts ans
end

(n,m) = gets.split.map(&:to_i)
if(m % 2 ==0)
th = (m/2).ceil
w = 0
  n.times do |i|
    th += w
    if(w > 0)
      w +=1
    else
      w -=1
    end
    w *=-1
    puts th
    if(th == m)
      th = (m/2).ceil
      w = 0
    end
  end
else
  th = (m/2).ceil+1
  w = 0
    n.times do |i|
      th -= w
      if(w > 0)
        w +=1
      else
        w -=1
      end
      w *=-1
      puts th
      if(th == m)
        th = (m/2).ceil+1
        w = 0
      end
   end
end

#!/usr/bin/ruby
n, m = gets.chomp.split(' ').map{|a| a.to_i}
if m == 1
	n.times do
		puts 1
	end
	exit
end
if m == 2
	n.times do |i|
		puts (i+1)%2 == 1 ? 1:2
	end
	exit
end
if m%2 == 1
	k = (m/2.0).ceil
	for i in 1..n
		if i > m
			i = i % m;
			if i == 0
				i = m
			end
		end
		if i%2 == 0
			puts k-((i+2)/2)+1;
		end
		if i%2 == 1
			puts k+((i+1)/2)-1;
		end
	end
else
	k = m/2
	for i in 1..n
		if i > m
			i = i % m;
			if i == 0
				i = m
			end
		end
		if i%2 == 1
			puts k-(i+1)/2+1;
		end
		if i%2 == 0
			puts k+(i/2);
		end
	end
end

n,m = gets.split.map(&:to_i)
i=0
while i<n
    k=i%m
    if m % 2 == 0
        if k % 2 == 0
            puts (m-k-1)/2+1
        else
            puts (m+k)/2+1
        end
    else
        if k % 2 == 0
            puts (m+k-1)/2+1
        else
            puts (m-k-2)/2+1
        end
    end
    i = i + 1
end

require 'Set'
arr = Set.new
n,m = gets.split(" ").map(&:to_i)

val = (m+1) / 2
arr << val
arr << val + 1 if m.even?
i = 1
while arr.length < m
	if m.odd?
		val1 = val + i
	else
		val1 = val + i + 1
	end
	val2 = val - i
	arr << val2 if val2 >= 1
	arr << val1 if val1 <= m
	i += 1
end
arr = arr.to_a
n.times do |i|
	puts arr[i % m]
end

a = []
gets.split.each { |_| a.push(_.to_i)}
# a[0] - мячи
# a[1] -корзины

if a[1].odd?
  #нечетное
  plu = (a[1]/2.0).ceil
  for i in 1..a[0] do
    k = i % a[1]
    k = a[1] if k == 0 # костыль если количество мячей больше корзин начать счет снова при пересечении рубежа равенства количеству корзин
    if k.odd?
      p plu + (k-1)/2 if k != 1 # ---
      p plu if k == 1 # Костыль на начальное число которое не поддерживает изменений при первом мяче
    else
      p plu - k/2
    end
  end
else
  #четное
  plu = (a[1]/2.0).ceil
  for i in 1..a[0] do
    k = i % a[1]
    k = a[1] if k == 0 # костыль если количество мячей больше корзин начать счет снова при пересечении рубежа равенства количеству корзин
    if k.odd?
      p plu - (k-1)/2 if k != 1 # ---
      p plu if k == 1 # Костыль на начальное число которое не поддерживает изменений при первом мяче
    else
      p plu + k/2
    end
  end

end

#!/usr/bin/ruby
n, m = gets.chomp.split(' ').map{|a| a.to_i}
if m == 1
	n.times do
		puts 1
	end
	exit
end
if m == 2
	n.times do |i|
		puts (i+1)%2 == 1 ? 1:2
	end
	exit
end
if m%2 == 1
	k = (m/2.0).ceil
	for i in 1..n
		if i > m
			i = i % m;
			if i == 0
				i = m
			end
		end
		if i%2 == 0
			puts k-((i+2)/2)+1;
		end
		if i%2 == 1
			puts k+((i+1)/2)-1;
		end
	end
else
	k = m/2.0
	for i in 1..n
		if i > m
			i = i % m;
			if i == 0
				i = m
			end
		end
		if i%2 == 1
			puts k-i+1;
		end
		if i%2 == 2
			puts k+((i+1)/2)-1;
		end
	end
end

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

val = (m+1) / 2
arr << val
i = 1
while arr.length < m
	val1 = val + i
	val2 = val - i
	arr << val2 if val2 >= 1
	arr << val1 if val1 <= m
	i += 1
end

n.times do |i|
	puts arr[i % m]
end

require 'Set'
arr = Set.new
n,m = gets.split(" ").map(&:to_i)

val = (m+1) / 2
arr << val
arr << val + 1 if m.even?
i = 1
while arr.length < m
	val1 = val + i
	val2 = val - i
	arr << val2 if val2 >= 1
	arr << val1 if val1 <= m
	i += 1
end
arr = arr.to_a
n.times do |i|
	puts arr[i % m]
end

a = []
gets.split.each { |_| a.push(_.to_i) }

b = [0] * a[1] # количество корзин и все корзины

def get_indexes(a_,v_) # получаем все индексы массива a_ по значению v_
  ha = []
  a_.each_with_index do |v,i|
    ha.push(i) if v == v_
  end
  ha
end

a[0].times do #делаем по количеству мячей
  ins = get_indexes(b, b.min) # просим индексы
  if ins.count > 1 # если индексов больше одного то необходимо выбрать ближайшую к центру
    h = ins.inject(Hash.new(0)) { |h,v| h[v] = ((a[1]+1)/2-(v+1)).abs; h } # получаем индекс каждой корзины
    p h.key(h.values.min) + 1 # выбираем наименьший индекс или ближайшую корзину
    b[h.key(h.values.min)] += 1 # клдем мяч
  else
    p b.index(b.min) + 1
    b[b.index(b.min)] += 1
  end
end

a = []
gets.split.each { |_| a.push(_.to_i) }

b = [0] * a[1] # количество корзин и все корзины

def get_indexes(a_,v_) # получаем все индексы массива a_ по значению v_
  ha = []
  a_.each_with_index do |v,i|
    ha.push(i) if v == v_
  end
  ha
end

a[0].times do #делаем по количеству мячей
  ins = get_indexes(b, b.min) # просим индексы
  if ins.count > 1 # если индексов больше одного то необходимо выбрать ближайшую к центру
    h = ins.inject(Hash.new(0)) { |h,v| h[v] = (a[1]+1/2-(v+1)).abs; h } # получаем индекс каждой корзины
    p h.key(h.values.min) + 1 # выбираем наименьший индекс или ближайшую корзину
    b[h.key(h.values.min)] += 1 # клдем мяч
  else
    p b.index(b.min) + 1
    b[b.index(b.min)] += 1
  end
end


(1..gets.to_i).each {
  s = gets.chomp.chars
  a = [1, 2, 3, 4, 6, 12].select { |v| u = 12/v; t = s.each_slice(u); (0...u).any? { |i| t.all? { |r| r[i] == 'X' } } }
  out = a.size.to_s
  out += ' ' + a.map { |v| "#{v}x#{12/v}" } * ' ' unless a.empty?
  puts out
}

#!/usr/bin/ruby
gets.to_i.times{
	s=gets.chomp.split('')
	n=s.size
	h={}
	n.times{|i|h[i]=1 if s[i]=='X'}
	r=(1..n).select{|i|n%i==0 && (n/i).times.any?{|j|
		i.times.all?{|k|h[k*(n/i)+j]}
	}}.map{|e|'%dx%d'%[e,n/e]}
	puts r.size.to_s+' '+r*' '
}

t = gets.to_i

def check(row,s)
    arr = s.split('').each_slice(row).to_a.transpose

    arr.each_with_index do |r,ri|
         bool = true
         r.each_with_index do |rri,rj|
            if r[rj]=='O'
                bool = false
            end
        end

        if bool
        return true

        end
    end
    return false
end

t.times do |i|
    s = gets.chomp
    fact = [12,6,4,3,2,1]
    ans = []
    fact.each_with_index do |f,i|
        if check(f,s)
            a = (s.length/f).to_s+'x'+f.to_s
            ans << a
        end
    end
    puts ans.length.to_s+' '+ans.join(' ')
end

def run
  n = $stdin.gets.to_i
  $stdin.readlines.each do |line|
    chars = line.chomp.split(//)
    len = chars.length
    ans = []
    1.upto(len) do |i|
      f = [true] * (len / i)
      next if len % i != 0
      len.times do |j|
        f[j % (len / i)] = false if chars[j] == 'O'
      end
      ans << "#{i}x#{chars.length / i}" if f.find_index(true)
    end
    puts "#{ans.length} #{ans.join(" ")}"
  end
end

run if $0 == __FILE__

gets
while s = gets
  r = []
  for a in 1..12
    if 12 % a == 0
      b = 12 / a
      ok = [ true ] * b
      s.chars.each_with_index do |c, i|
        ok[i % b] = false if c == 'O'
      end
      if ok.find_index(true)
        r << "#{a}x#{b}"
      end
    end
  end
  puts "#{r.size} #{r.join(' ')}"
end

tests = gets.to_i
tests.times do
  s = gets
  result = []
  for a in 1..12
    if 12 % a == 0
      b = 12 / a
      ok = [ true ] * b
      s.split(//).each_with_index do |c, i|
        ok[i % b] = false if c == 'O'
      end
      if ok.find_index(true)
        result << "#{a}x#{b}"
      end
    end
  end
  puts "#{result.size} #{result.join(' ')}"
end

class ProblemA

  def initialize
    @dats = STDIN.read.split("\n")
    @num_of_dat = @dats[0]
    @final_str = ""
  end

  def solve(line)
    out_line = ""
    num_of_ways = 0
    return if line.length != 12
    [1,2,3,4,6,12].each do |a|
      b = 12/a
      has_way = false
      str = ""
      (0..(b-1)).each do |col_idx|
        is_x = true
        (0..(a-1)).each do |row_idx|
          if line[col_idx+(row_idx)*b] != "X"
            is_x = false
          end
        end

        if is_x
          has_way = true
          num_of_ways += 1
          break
        end
      end
      out_line += " #{a}x#{b}" if has_way
    end
    @final_str += "#{num_of_ways} #{out_line}\n"
  end

  def solve_all
    (1..@num_of_dat.to_i).each do |i|
      solve(@dats[i])
    end
    puts @final_str
  end
end

solver = ProblemA.new
solver.solve_all

a=gets.chomp.to_i
ans= ["1x12 ", "2x6 ", "3x4 ", "4x3 ", "6x2 "]

def chk (a,b)
x=0
y=(12/b)
temp=[]
0.upto(y-1) do |i|
temp=[]
0.upto(b-1) do|j|
temp<<a[i+j*y]
end
if temp.count("X")==b
x=1
end
break if x==1
end
if x==1
return 1
else
return 0
end
end

a.times do
temp=gets.chomp
d=temp.count("X")

if d==0
puts "0"

elsif d==1
puts "1 1x12"

elsif d==12
puts "6 1x12 2x6 3x4 4x3 6x2 12x1"

elsif d==2
p=1
p+=chk(temp,2)
if p==1
puts "1 1x12"
else
puts "2 1x12 2x6"
end

elsif d==3
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=4 && d<6
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=6 && d<12
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
p=chk(temp,6)
if p==1
ansa<<ans[4]
end
printf "#{ansa.length} "
puts"#{ansa.join}"
end
end

n = gets.to_i
n.times do
	s = ?0 + gets
	ans = []
	1.upto(12) do |i|
		if 12 % i == 0 ;
			flag = false
			1.upto(12/i) do |j|
				tflag = true
				1.upto(i) do |k|
					if s[(k-1)*(12/i)+j] == ?O ;
						tflag = false;
					end
				end
				flag = true if tflag == true
			end
			ans << i if flag == true
		end
	end
	print ans.size
	ans.each do |x|
		print ' ', x, ?x, 12/x
	end
	print ?\n
end

a=gets.chomp.to_i
ans= ["1x12 ", "2x6 ", "3x4 ", "4x3 ", "6x2 "]

def chk (a,b)
x=0
y=(12/b)
temp=[]
0.upto(y-1) do |i|
temp=[]
0.upto(b-1) do|j|
temp<<a[i+j*y]
end
if temp.count("X")==b
x=1
end
break if x==1
end
if x==1
return 1
else
return 0
end
end

a.times do
temp=gets.chomp
d=temp.count("X")

if d==0
puts "0"

elsif d==1
puts "1 1x12"

elsif d==12
puts "6 1x12 2x6 3x4 4x3 6x2 12x1"

elsif d==2
p=1
p+=chk(temp,2)
if p==1
puts "1 1x12 2x6"
else
puts "1 1x12 2x6"
end

elsif d==3
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=4 && d<6
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=6 && d<12
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
p=chk(temp,6)
if p==1
ansa<<ans[4]
end
printf "#{ansa.length} "
puts"#{ansa.join}"
end
end

a=gets.chomp.to_i
ans= ["1x12 ", "2x6 ", "3x4 ", "4x3 ", "6x2 "]

def chk (a,b)
x=0
y=(12/b)
temp=[]
0.upto(y-1) do |i|
temp=[]
0.upto(b-1) do|j|
temp<<a[i+j*y]
end
if temp.count("X")==b
x=1
end
break if x==1
end
if x==1
return 1
else
return 0
end
end

a.times do
temp=gets.chomp
d=temp.count("X")

if d==0
puts "0"

elsif d==1
puts "1 1x12"

elsif d==12
puts "6 1x12 2x6 3x4 4x3 6x2 12x1"

elsif d==2
p=1
p+=chk(temp,2)
if p==1
puts "1 1x12 2x6"
else
puts "1 1x12"
end

elsif d==3
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=4 && d<6
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=6 && d<12
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
p=chk(temp,6)
if p==1
ansa<<ans[4]
end
printf "#{ansa.length} "
puts"#{ansa.join}"
end
end

a=gets.chomp.to_i
ans= ["1x12 ", "2x6 ", "3x4 ", "4x3 ", "6x2 "]

def chk (a,b)
x=0
y=(12/b)
temp=[]
0.upto(y-1) do |i|
temp=[]
0.upto(b-1) do|j|
temp<<a[i+j*y]
end
if temp.count("X")==b
x=1
end
break if x==1
end
if x==1
return 1
else
return 0
end
end

a.times do
temp=gets.chomp
d=temp.count("X")

if d==0
puts "0"

elsif d==1
puts "1 1x12"

elsif d==12
puts "6 1x12 2x6 3x4 4x3 6x2 12x1"

elsif d==2
p=1
p+=chk(temp,2)
if p==1
puts "1 1x12"
else
puts "1 1x12 2x6"
end

elsif d==3
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=4 && d<6
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
printf "#{ansa.length} "
puts"#{ansa.join}"

elsif d>=6 && d<12
ansa=[]
ansa<<ans[0]
p=1
p=chk(temp,2)
if p==1
ansa<<ans[1]
end
p=chk(temp,3)
if p==1
ansa<<ans[2]
end
p=chk(temp,4)
if p==1
ansa<<ans[3]
end
p=chk(temp,6)
if p==1
ansa<<ans[4]
end
printf "#{ansa.length} "
puts"#{ansa.join}"
end
end