p,q=gets.split.map(&:to_i)
n=gets.to_i
a=[]
a=gets.split.map(&:to_i)
s=1
b=0
i = n
while i>0
i=i-1
ks=b
kb=s
s=kb*a[i]+ks
b=kb
end
if s*q==b*p
print "YES"
end
if s*q!=b*p
puts "NO"
end

require"mathn"
p,q=gets.split.map(&:to_i)
n=gets.to_i
a=gets.split.map(&:to_i)
frac=a.last
a[0..-2].reverse.each{|aa|frac=aa+1/frac}
puts p/q==frac ? "YES" : "NO"

require"mathn"
p, q = gets.split(' ').map { |i| i.to_i }
n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
r = a.last
a[0..-2].reverse.each { |e|  r = e + 1 / r;}
puts p / q == r ? "YES" : "NO"

require 'mathn'

class B
  def initialize
    p, q = gets.chomp.split(' ').map(&:to_i)
    n = gets.chomp
    num_list = gets.chomp.split(' ').map(&:to_i)

    res = calc(num_list.shift, num_list)
    if res == p/q
      puts "YES"
    else
      puts "NO"
    end
  end

  def calc(a, b)
    if b.size == 0
      return a
    else
      a + 1/calc(b.shift, b)
    end
  end
end

b = B.new

require "mathn"

loop do
  line = gets
  break if line.nil?
  p,q = line.split(" ").map(&:to_i)
  n = gets.to_i
  as = gets.split(" ").map(&:to_i)
  cal = as.reverse.inject(0){ |res, val|
    res **= -1 unless res.zero?
    res + val
  }
  if cal == p/q
    puts "YES"
  else
    puts "NO"
  end
end

p, q = gets.split.map(&:to_i)
n = gets.to_i
a = gets.split.reverse.map(&:to_i)

x = Rational(0)
for i in 0 .. n-2 do
    x += Rational(a[i], 1)
    x = Rational(1, x)
end
x += Rational(a[n-1], 1)

if Rational(p, q) == x then
    puts "YES"
else
    puts "NO"
end

p,q = gets.chomp.split(" ").collect {|x| x.to_i}
n = gets.to_i
arr = gets.chomp.split(" ").collect {|x| x.to_i}

if arr[-1]==1 and arr.length>1
  arr[-2] += 1
  arr.delete_at(n-1)
end

ok = true
nt = p
aux = -1
arr.each { |v|
  if q == 0
    ok = false
    break
  end
  nt = p/q
  if (nt!=v)
    ok = false
    break
  end
  aux = p%q
  p = q
  q = aux

}

if ok and aux == 0
  puts "YES"
else
  puts "NO"
end

def rec(ary)
	return Rational(1, ary.last) if ary.size == 1
	Rational(1, ary.first + rec(ary.drop(1)))
end

f1 = Rational(*gets.split.map(&:to_i))
gets
f2 = rec(gets.split.map(&:to_i))

if f1 == Rational(f2.denominator, f2.numerator)
	puts "YES"
else
	puts "NO"
end

def solve(a, p, q)
  if a.size == 1
    return p == q * a.first ? "YES" : "NO"
  end
  t = a.shift
  solve(a,q, (p - t * q))
end

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

puts solve(a,p,q)

p, q = gets.split.map(&:to_i)
d = p.gcd(q)
p, q = p / d, q / d
n = gets.to_i
a = gets.split.map(&:to_i)
x, y = a[n-1], 1
(n-2).downto(0) do |i|
	x, y = y, x
	x = x + a[i] * y
end
if x == p && y == q
	puts "YES"
else
	puts "NO"
end

#!/usr/bin/ruby

class Fraction
  @num
  @den

  def initialize(a, b)
    @num = a
    @den = b
  end

  def num=(num)
    @num = num
  end

  def num
    @num
  end

  def den=(den)
    @den = den
  end

  def den
    @den
  end
end

def cf(pos)
  return Fraction.new(1, $nums[pos]) if pos + 1 == $n

  res = cf(pos + 1)
  temp = Fraction.new(res.num + res.den * $nums[pos], res.den)
  return Fraction.new(temp.den, temp.num)
end

tokens = gets.split(" ")
p = tokens[0].to_i
q = tokens[1].to_i
$n = gets.to_i

tokens = gets.split(" ")
$nums = Array.new($n)

0.upto(tokens.length - 1) do |i|
  $nums[i] = tokens[i].to_i
end

result = cf(0)

g = p.gcd(q)
#puts g

f = result.num.gcd(result.den)
#puts f

p /= g
q /= g

a = result.den / f
b = result.num / f

if b == q and a == p
  puts "YES"
else
  puts "NO"
end

(p,q) = gets.split(' ').map{|i| i.to_i()}
n = gets.to_i()
a = gets.split(' ').map{|i| i.to_i()}
x,y = 1,0

a.reverse_each{|i| x,y = y+x*i,x}

puts ((x*q==y*p)?"YES":"NO")

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

r = Rational(0)
for b in a.reverse
  r = 1 / (b + r)
end

if r == Rational(q, p)
  puts "YES"
else
  puts "NO"
end

require"mathn"
p,q = gets.split().map {|x| x.to_i}
n = gets.to_i
a = gets.split().map {|x| x.to_i}
a = a.reverse
res = a[0];
a[1..-1].each {|x| res = x+1/res}

puts (res == p/q)? "YES" : "NO"

p,q = gets.split().map {|x| x.to_i}
n = gets.to_i
a = gets.split().map {|x| x.to_i}
a.reverse!
res = a[0];
a[1..-1].each {|x| res = x+1.0/res}
puts (res == 1.0*p/q)? "YES" : "NO"

p,q = gets.split().map {|x| x.to_i}
n = gets.to_i
a = gets.split().map {|x| x.to_i}
a.reverse!
res = a[0];
a[1..-1].each {|x| res = x+1/res}
puts (res == p/q)? "YES" : "NO"

p, q = gets.split(' ').map { |i| i.to_f }
n = gets.to_i
a = gets.split(' ').map { |i| i.to_f }
r = a.last
a[0..-2].reverse_each { |e|  r = e + 1.0 / r;}
puts p / q == r ? "YES" : "NO"

p, q = gets.split(' ').map {|i| i.to_i}
n = gets.to_i
a = gets.split(' ').map {|i| i.to_i}
def fun(arr, ind)
    if(ind == arr.size - 1)
        return [1, arr[ind]]
    end
    ret = fun(arr, ind + 1)
    return [ret[1], ret[0] + arr[ind] * ret[1]]
end
ret = fun(a, 0)
if p == ret[1] and q == ret[0] then puts "YES"
else puts "NO"
end

p, q = gets.split(' ').map { |i| i.to_f }
n = gets.to_i
a = gets.split(' ').map { |i| i.to_f }
r = a.last
a[0..-2].reverse.each { |e|  r = e + 1 / r;}
puts p / q == r ? "YES" : "NO"

class B
  def initialize
    p, q = gets.chomp.split(' ').map(&:to_f)
    n = gets.chomp
    num_list = gets.chomp.split(' ').map(&:to_f)

    res = calc(num_list.shift, num_list)
    if res == p/q
      puts "YES"
    else
      puts "NO"
    end
  end

  def calc(a, b)
    if b.size == 0
      return a
    else
      a + 1/calc(b.shift, b)
    end
  end
end

b = B.new

#!/usr/bin/ruby

class Fraction
  @num
  @den

  def initialize(a, b)
    @num = a
    @den = b
  end

  def num=(num)
    @num = num
  end

  def num
    @num
  end

  def den=(den)
    @den = den
  end

  def den
    @den
  end
end

def cf(pos)
  return Fraction.new(1, $nums[pos]) if pos + 1 == $n

  res = cf(pos + 1)
  temp = Fraction.new(res.num + res.den * $nums[pos], res.den)
  return Fraction.new(temp.den, temp.num)
end

tokens = gets.split(" ")
p = tokens[0].to_i
q = tokens[1].to_i
$n = gets.to_i

tokens = gets.split(" ")
$nums = Array.new($n)

0.upto(tokens.length - 1) do |i|
  $nums[i] = tokens[i].to_i
end

result = cf(0)

if result.num == q and result.den == p
  puts "YES"
else
  puts "NO"
end


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

res = 0
last = h[0]
tmp = []
idx = 0

while idx < a.size
  if idx.zero?
    tmp << a[idx]
    idx += 1
    next
  end
  if h[idx] != h[idx - 1]
    res += tmp.size.zero? ? 0 : tmp.sort.reverse.slice(0, k).inject(:+)
    tmp = [a[idx]]
  else
    tmp << a[idx]
  end
  idx += 1
end
if tmp.size > 0
  res += tmp.size.zero? ? 0 : tmp.sort.reverse.slice(0, k).inject(:+)
end
puts res

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

#use moving window to get the sum
nk = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]
a = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp

arr = [0]
breakpoints = 2
(1..n-1).each do |i|
	if s[i]!=s[i-1]
		breakpoints+=1
		arr.push i
	end
end
arr.push n

totsum = 0
#bigarr = []
=begin

(0..breakpoints-2).each do |st|
	newarr = []
	newlen = 1
	min = 1.0/0.0
	sum = 0
	max = 0
	(arr[st]..arr[st+1]-1).each do |range|
		this = a[range]
#		p newlen
		if(newlen<=k)
			newarr.push this
			sum+=this
			min = this if this<min
			newlen+=1
		else
			if (this>min)
				sum-=min
				sum+=this
				newarr.push this
				newarr.delete_at(newarr.index(min))
				min = newarr.min
			end
		end
		max = sum if sum>max
	end
	totsum += sum
end
=end
#p arr
(0..breakpoints-2).each do |st|
	newarr = a[arr[st]..arr[st+1]-1].sort
#	p newarr
	initi = newarr.length-1
	i = initi
	min = arr[st]
	sum = 0
#	p initi
	while true
		break if (initi-i+1)>k
		break if i<0
#		p i
		sum+=newarr[i]
		i -= 1
#		p sum
	end
#	p sum
	totsum += sum
end

#p arr
puts totsum

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

#use moving window to get the sum
nk = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]
a = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp

arr = [0]
breakpoints = 2
(1..n-1).each do |i|
	if s[i]!=s[i-1]
		breakpoints+=1
		arr.push i
	end
end
arr.push n

bigarr = []
(0..breakpoints-2).each do |st|
	newarr = []
	newlen = 1
	min = 1.0/0.0
	sum = 0
	(arr[st]..arr[st+1]-1).each do |range|
		this = a[range]
#		p newlen
		if(newlen<=k)
			newarr.push this
			sum+=this
#			min = this if this<min
			newlen+=1
		else
			if (this>min)
				sum-=newarr.pop
				sum+=this
				newarr.push this
#				newarr.delete_at(newarr.index(min))
#				min = newarr.min
			end
		end
		newarr.sort!
		bigarr.push sum
	end
end

totsum = 0
(0..breakpoints-2).each do |st|
	totsum += bigarr[arr[st]..arr[st+1]-1].max
end

#p arr
#p bigarr
puts totsum


a=gets.chomp.split(" ")
n=a[0].to_f
p=a[1].to_f
j=a[2].to_f
s=gets.chomp

sc1=0
sc1c=0
sc1n=n
loop do
    sc1n=sc1n-p
	break if sc1n <= 0
	if sc1n%j==0
		sc1+=1
	end
	sc1c+=1
	break if sc1n%j==0
end

sc2=0
sc2c=0
sc2n=n
loop do
    sc2n=sc2n-j
	break if sc2n <= 0
	if sc2n%p==0
		sc2+=1
	end
	sc2c+=1
	break if sc2n%p==0
end

if n==p+j
   puts 2
   puts s[0..(p-1)]
   puts s[p..(s.length-1)]
elsif n%p==0
    split=n/p
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end

elsif n%j==0
    split=n/j
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc1>0
	puts (sc1c+(n-sc1c*p)/j ).to_i
	c1=0
	c2=0
	loop do
		break if c2==sc1c
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end

	c2=0
	loop do
		break if c2==(n-sc1c*p)/j
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc2>0
	puts (sc2c+(n-sc2c*j)/p ).to_i

	c2=0
	loop do
		break if c2==sc2c
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
	c1=0
	c2=0
	loop do
		break if c2==(n-sc2c*j)/p
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end
else
	puts -1
end

#!/usr/bin/ruby
n,a,b=gets.split.map(&:to_i)
j=0.upto(n/a).find{|i|(n-i*a)%b==0}
if !j
	p -1
else
	k=(n-j*a)/b
	s=gets.chomp.chars.to_a
	p j+k
	j.times{puts s.shift(a)*''}
	k.times{puts s.shift(b)*''}
end

def count(n, p, q, s)
	i = 0

	while (n - i*q) >=0
		if (n-i*q)%p == 0
			res = Array.new
 			i.times do |e|
 				res << s[e*q...(e+1)*q]
			end

			head = i*q

			(((n-i*q)/p).to_i()).times do |e|
				res << s[head+e*p...head+(e+1)*p]
			end

			puts i+(n-i*q)/p.to_i
			puts res
			return 1
		end
		i += 1

	end
	return -1
end

n, p, q = gets.strip.split.map(&:to_i)
s = gets.strip
p, q = q, p if p > q
if count(n,p,q, s) == -1
	puts -1
end

#!/usr/bin/env ruby

def split n, p, q
  max_left_coeff = n / p
  (0..max_left_coeff).each do |a|
    left = a * p
    right = n - left
    if right % q == 0
      return [a, (right / q)]
    end
  end
  [-1, 0]
end

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

a, b = split(n, p, q)

puts (a + b)
a.times do
  puts s[0, p]
  s = s[p..s.size-1]
end
b.times do
  puts s[0, q]
  s = s[q..s.size-1]
end

line1 = gets.to_s.split(' ').collect{ |x| x.to_i}
n = line1[0]
p = line1[1]
q = line1[2]

s = gets

x = n / p

y = n / q
total = -1
tmp = -1
(0..x).each do |index|
  ci = n - index * p
  if ci % q == 0
    total = index + ci / q
    tmp = index
    break
  end
end

puts total
if total != -1
  cur = 0
  (1..tmp).each do
    puts s[cur...cur+p]
    cur+=p
  end
  (1..total - tmp).each do
    puts s[cur...cur+q]
    cur += q
  end
end

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

a = b = -1
for i in 0..100
  for j in 0..100
    if p*i + q*j == n
      a = i
      b = j
    end
  end
end

if a == -1
  puts a
  exit
end

puts a+b

idx = 0
(1..a).each do
  puts s[idx, p]
  idx += p
end

(1..b).each do
  puts s[idx, q]
  idx += q
end

n,p,q=gets.split.map(&:to_i)
s=gets.chomp.split(//)
a=b=0

0.upto(100){|k|
	m=n-k*p
	break if m<0
	if m%q==0
		a=k
		b=m/q
		break
	end
}
if a==0&&b==0
	puts -1
else
	puts a+b
	a.times{puts s.shift(p).join}
	b.times{puts s.shift(q).join}
end

class Solver
	def main
		n, a, b = gets.split.map { |x| x.to_i }
		str = gets.chomp
		0.upto(n) do |l|
			r = n - l
			if l % a == 0 && r % b == 0
				puts l / a + r / b
				0.upto(l / a - 1) do |i|
					puts str[i * a, a]
				end
				0.upto(r / b - 1) do |i|
					puts str[l + i * b, b]
				end
				return
			end
		end
		puts -1
	end
end
Solver.new.main

a=gets.chomp.split(" ")
n=a[0].to_f
p=a[1].to_f
j=a[2].to_f
s=gets.chomp

sc1=0
sc1c=0
sc1n=n
loop do
    sc1n=sc1n-p
	break if sc1n <= 0
	if sc1n%j==0
		sc1+=1
	end
	sc1c+=1
	break if sc1n%j==0
end

sc2=0
sc2c=0
sc2n=n
loop do
    sc2n=sc2n-j
	break if sc2n <= 0
	if sc2n%p==0
		sc2+=1
	end
	sc2c+=1
	break if sc2n%p==0
end

if n==p+j
   puts 2
   puts s[0..(p-1)]
   puts s[p..(s.length-1)]
elsif n%p==0
    split=n/p
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end

elsif n%j==0
    split=n/j
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc1>0
	puts (sc1c+(n-sc1c*p)/j ).to_i
	c1=0
	c2=0
	loop do
		break if c2==sc1c
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end
	c1=0
	c2=0
	loop do
		break if c2==(n-sc1c*p)/j
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc2>0
	puts (sc2c+(n-sc2c*j)/p ).to_i
	c1=0
	c2=0
	loop do
		break if c2==sc2c
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
	c1=0
	c2=0
	loop do
		break if c2==(n-sc2c*j)/p
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end
else
	puts -1
end

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

if s.length%p != 0 && s.length%q != 0 && s.length%(p+q) != 0
  puts -1
  exit
end

arr = []
if s.length%p == 0
  arr = [p]
  puts s.length/p
elsif s.length%(p+q) == 0
  arr = [p, q]
  puts (s.length/(p+q))*2
elsif s.length%q == 0
  arr = [q]
  puts s.length/q
end

count = 0
idx = 0
while count != s.length
  puts s[count, arr[idx%arr.length]]
  count += arr[idx%arr.length]
  idx += 1
end


n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx+i
  #p [i,idx]
end

#p match
min = n/2
(n/2).times do |i|
  break if (i+1) > [match[i],min].min
  ans += 1
  min = match[i] if min > match[i]
end
p ans

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
arr.sort!
p1, p2 = 0, N / 2
answer = 0
while p2 < N
	break if p1 == N / 2
	if arr[p2] - arr[p1] >= Z
		answer += 1
		p1 += 1
		p2 += 1
	else
		p2 += 1
	end
end
puts answer

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx
end

matchable = n/2
(n/2).times do |i|
  matchable = match[i]+i if matchable > match[i]+i
  break if (i+1) > matchable
  ans += 1
end
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
used = [false]*n
ans = 0
n.times do |i|
  if !used[i]
    idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
    if !idx.nil?
      used[i] = true
      used[idx] = true
      ans += 1
    end
  end
end
#p used
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx+i
end

#p match
(n/2).times do |i|
  break if (i+1) > match[i]
  ans += 1
end
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx
end

ans = 0
tmp = n/2
(n/2).times do |i|
  c = tmp < match[i]+i ? tmp : match[i]+i
  break if (i+1) > c || match[i] < 0
  ans += 1
end
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx
end

ans = n/2
(n/2).times do |i|
  t = ans < match[i]+i ? ans : match[i]+i
  break if (i+1) > t
  ans = t
end
p ans

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).sort
p1, p2 = 0, 1
answer = 0
while p2 < N
	if arr[p1]
		if arr[p2]
			if arr[p2] - arr[p1] >= Z
				answer += 1
				arr[p2] = arr[p1] = nil
				p1 += 1
				p2 += 1
			else
				p2 += 1
			end
		else
			p2 += 1
		end
	else
		p1 += 1
	end
end
puts answer

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
arr.sort!
p1, p2 = 0, N / 2
answer = 0
while p2 < N
	if arr[p2] - arr[p1] >= Z
		answer += 1
		p1 += 1
		p2 += 1
	else
		p2 += 1
	end
end
puts answer

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).sort
p1, p2 = 0, 1
answer = 0
while p2 < N
	if arr[p2]
		if arr[p1]
			if arr[p2] - arr[p1] >= Z
				answer += 1
				arr[p2] = arr[p1] = nil
				p1 += 1
				p2 += 1
			else
				p2 += 1
			end
		else
			p1 += 1
		end
	else
		p2 += 1
	end
end
puts answer


n, t= gets.split(" ").map(&:to_i)
day = 1
a = gets.split(" ").map(&:to_i)
a.each do |work_time|
  t = t-86400+work_time
  day+=1 if t>0
end
print(day)

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

SECONDS = 86400
num = 0
result = 0

while t > 0
	t -= (SECONDS - arr[result])
	result += 1
end

puts result

n, t = gets.strip.split.map(&:to_i)
a = gets.strip.split.map(&:to_i)
ans = 0
n.times do |i|
    free = 86400 - a[i]
    t -= free
    ans += 1
    break if t <= 0
end
puts ans

n,t=gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
cur = 0
n.times do |i|
    cur += 86400 - a[i]
    if cur >= t
        puts i + 1
        break
    end
end

in_str = gets
n, allt = in_str.split(' ')
n, allt = n.to_i, allt.to_i
in_str = gets
time_arr = in_str.split(' ').map { |element| element.to_i }
t = 0
time_arr.each_with_index do |elem, i|
  t += 86400 - elem
  if t >= allt
    puts i + 1
    break
  end
end

n,t=gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
cur = 0
n.times do |i|
    cur += 86400 - a[i]
    if cur >= t
        puts i + 1
    end
end


t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == a)
		puts "1 1 1"
		next
	end
	aa = [a, [b, c].max + 1].max - a
	bb = [b, [a, c].max + 1].max - b
	cc = [c, [a, b].max + 1].max - c
	puts "#{aa} #{bb} #{cc}"
}

gets.to_i.times do
    arr = gets.split.each_with_index.map{ |c, i| c.to_i  }
    mx = arr.max
    c = arr.count(mx)
    puts arr.map{|e| e != mx ? mx - e + 1 : c > 1 ? 1 : 0}.join ' '

end

class Pair
    attr_accessor :first, :second
    include Comparable

    def initialize(first, second)
        @first = first
        @second = second
    end

    def <=>(an_other)
        @first <=> an_other.first
    end
end
gets.to_i.times do
    arr = gets.split.each_with_index.map{ |c, i| Pair.new(c.to_i, i)  }
    arr = arr.sort()
    ans = Array.new;
    arr.length.times do |idx|
        if idx == arr.length - 1
            if arr.length >= 2 and arr[arr.length - 1].first == arr[arr.length - 2].first
                ans[ arr[arr.length - 1].second ] = 1;
            else
                ans[ arr[arr.length - 1].second ] = 0;
            end
        else
            ans[ arr[idx].second ] = arr[arr.length-1].first - arr[idx].first + 1;
        end
    end

    arr.length.times do |idx|
        if idx == arr.length - 1
            puts(ans[idx])
        else
            print(ans[idx])
            print(" ")
        end
    end

end

gets.to_i.times do
    votes = gets.split.map(&:to_i)
    m = votes.max
    c = votes.count(m)
    puts votes.map{|v| v != m ? m - v + 1 : c > 1 ? 1 : 0}.join ' '
end

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each do |inp|
  cans = inp.split(' ').map(&:to_i)

  r = []
  cans.each_with_index do |n, i|
    other = cans.reject.with_index{|_, j| j == i }
    max = other.max

    if n > max
      r << 0
    else
      r << max - n + 1
    end
  end

  puts r.join(' ')
end

t = gets().to_i()

def bar(arr)

	solve = []

	max = arr.max
	max_counter = 0

	for i in 0..arr.length - 1
		if (arr[i] == max)
			max_counter += 1
		end
	end

	for i in 0..arr.length - 1
		tmp = -1
		if (arr[i] == max)
			if (max_counter == 1)
				tmp = 0
			else
				tmp = 1
			end
		else
			tmp = max - arr[i] + 1
		end

		solve << tmp
	end
	puts solve.join(" ")
end

for i in 1..t
	arr = gets.chomp.split(" ").map{|item| item.to_i}

	bar(arr)

end

n = gets.chomp.to_i

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

    a = [[votes[1], votes[2]].max - votes[0] + 1, 0].max
    b = [[votes[0], votes[2]].max - votes[1] + 1, 0].max
    c = [[votes[0], votes[1]].max - votes[2] + 1, 0].max

    puts "#{a} #{b} #{c}"
end

def f(x, y, z)
  w = [y, z].max - x + 1
  w > 0 ? w : 0
end

gets.to_i.times do

a, b, c = gets.split.map!(&:to_i)
puts "#{f(a,b,c)} #{f(b,c,a)} #{f(c,a,b)}"

end

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == a)
		puts "#{a+1} #{b+1} #{c+1}"
		next
	end
	aa = [a, [b, c].max + 1].max - a
	bb = [b, [a, c].max + 1].max - b
	cc = [c, [a, b].max + 1].max - c
	puts "#{aa} #{bb} #{cc}"
}

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == b) && (c == a)
		puts "#{a+1} #{b+1} #{c+1}"
		next
	end
	ma = [a, b, c].max
	if ma - a == 0
		a = 0
	else
		a = ma - a + 1
	end
	if ma - b == 0
		b = 0
	else
		b = ma - b + 1
	end
	if ma - c == 0
		c = 0
	else
		c = ma - c + 1
	end
	puts "#{a} #{b} #{c}"
	# p [a, b, c].join' '
}

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == b) && (c == a)
		puts "#{a+1} #{b+1} #{c+1}"
		next
	end
	ma = [a, b, c].max
	if ma - a == 0
		a = 0
	else
		a = ma - a + 1
	end
	if ma - b == 0
		b = 0
	else
		b = ma - b + 1
	end
	if ma - c == 0
		c = 0
	else
		c = ma - c + 1
	end
	# puts "#{a} #{b} #{c}"
	p [a, b, c].join' '
}

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == b) && (c == a)
		puts a+1, a+1, a+1
		next
	end
	ma = [a, b, c].max
	if ma - a == 0
		a = 0
	else
		a = ma - a + 1
	end
	if ma - b == 0
		b = 0
	else
		b = ma - b + 1
	end
	if ma - c == 0
		c = 0
	else
		c = ma - c + 1
	end
	puts "#{a} #{b} #{c}"
}

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each do |inp|
  cans = inp.split(' ').map(&:to_i)

  r = []
  max = cans.max
  min = cans.min
  cans.each do |n|
    r << max - n + (max != n || max == min ? 1 : 0)
  end

  puts r.join(' ')
end

t = gets().to_i()

def bar(arr)

	max = arr.max

	solve = []

	for i in 0..arr.length - 1
		tmp = -1
		if (arr[i] == max)
			tmp = 0
		else
			tmp = max - arr[i] + 1
		end

		solve << tmp
	end
	puts solve.join(" ")
end

for i in 1..t
	arr = gets.chomp.split(" ").map{|item| item.to_i}

	bar(arr)

end


def c(n, m)
  c = 1
  m.times do |i|
    c = c * (n - i) / (i + 1)
  end
  c
end

# c(n, 3) - c(n - 1, 3) = c(n - 1, 2) !!
# c(n, 2) - c(n - 1, 2) = c(n - 1, 1) = n - 1 !!

m = gets.to_i
k = 3
k += 1 while c(k + 1, 3) <= m
m -= c(k, 3)

a = []
while m > 0
  b = 2
  b += 1 while c(b + 1, 2) <= m
  m -= c(b, 2)
  a << b
end

n = k + a.size
e = Array.new(n){'0' * n}
k.times do |i|
  k.times do |j|
    e[i][j] = '1' if i != j
  end
end
a.each_with_index do |c, j|
  c.times do |i|
    e[i][k + j] = '1'
    e[k + j][i] = '1'
  end
end
p n
puts e * "\n"

n=gets.to_i
a=Array.new(101){|el| el=Array.new(101){|elem| elem=0}}
c,y=0,0
for i in 0..101 do
y=0
	while y<i do
		break if y>n
		a[i][y],a[y][i]=1,1
		n-=y
		y+=1
	end
c+=1
break if n==0
end
puts c
for i in 0..c-1 do
	for j in 0..c-1 do
		print "#{a[i][j]}"
	end
	printf("\n")
end

x = gets.to_i
d = Array.new(105) { Array.new(105) {0} }
cnt = Math.log((x * 2 * 3), 3).floor
while (cnt + 1) * (cnt + 2) * (cnt + 3) <= x * 6
  cnt += 1
end
while (cnt) * (cnt + 1) * (cnt + 2) > x * 6
  cnt -= 1
end
cnt += 2
0.upto(cnt - 2) do |i|
  (i + 1).upto(cnt - 1) do |j|
    d[i][j] = d[j][i] = 1
  end
end
p = cnt - 1
x -= cnt * (cnt - 1) * (cnt - 2) / 6
while x > 0
  while p * (p - 1) / 2 > x
    p -= 1
  end
  0.upto(p - 1) do |i|
    d[cnt][i] = d[i][cnt] = 1
  end
  cnt += 1
  x -= p * (p - 1) / 2
end
puts cnt
0.upto(cnt - 1) do |i|
  0.upto(cnt - 1) do |j|
    print "#{d[i][j]}"
  end
  puts ''
end

def c(n, m)
  c = 1
  m.times do |i|
    c = c * (n - i) / (i + 1)
  end
  c
end

# c(n, 3) - c(n - 1, 3) = c(n - 1, 2) !!
# c(n, 2) - c(n - 1, 2) = c(n - 1, 1) = n - 1 !!

m = gets.to_i
k = 3
k += 1 while c(k + 1, 3) <= m
m -= c(k, 3)

a = []
while m > 0
  b = 2
  b += 1 while c(b + 1, 2) <= m
  m -= c(k, 2)
  a << b
end

n = k + a.size
e = Array.new(n){'0' * n}
k.times do |i|
  k.times do |j|
    e[i][j] = '1' if i != j
  end
end
a.each_with_index do |c, j|
  c.times do |i|
    e[i][k + j] = '1'
    e[k + j][i] = '1'
  end
end
p n
puts e * "\n"

n=gets.to_i
a=Array.new(101){|el| el=Array.new(101){|elem| elem=0}}
c,y=0,0
for i in 0..101 do
y=0
	while y<i do
		break if y>n
		a[i][y],a[y][i]=1,1
		n-=y
		y+=1
	end
c+=1
break if n==0
end
puts c
for i in 0..c-1 do
	for j in 0..c-1 do
		print "#{a[i][j]} "
	end
	printf("\n")
end

x = gets.to_i
d = Array.new(105) { Array.new(105) {0} }
cnt = Math.log((x * 2 * 3), 3).floor
while (cnt + 1) * (cnt + 2) * (cnt + 3) <= x * 6
  cnt += 1
end
while (cnt) * (cnt + 1) * (cnt + 2) > x * 6
  cnt -= 1
end
cnt += 2
0.upto(cnt - 2) do |i|
  (i + 1).upto(cnt - 1) do |j|
    d[i][j] = d[j][i] = 1
  end
end
x -= cnt * (cnt - 1) * (cnt - 2) / 6
while x > 0
  p = (x >= cnt / 2) ? (cnt / 2) : x
  x -= p
  0.upto(p - 1) do |i|
    d[2 * i][cnt] = d[2 * i + 1][cnt] = d[cnt][2 * i] = d[cnt][2 * i + 1] = 1
  end
  cnt += 1
end
puts cnt
0.upto(cnt - 1) do |i|
  0.upto(cnt - 1) do |j|
    print "#{d[i][j]}"
  end
  puts ''
end


n = gets.to_i
pa,pb = 0,0
ans = 1
n.times do
  a,b = gets.split.map(&:to_i)
  if a == b
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a > b && pa <= b
    ans += b-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a < b && pb <= a
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  end
  pa,pb = a,b
end
p ans

DBG = !true
n = gets.to_i
sum = 0
pends = -1
pende = -1
for i in 0...n
  a,b = gets.split.map{|z| z.to_i}
  mn = [a,b].min
  mx = [a,b].max
  if pende != -1 && mn >= pends
      sum += [mn, pende].min - pends + 1
  end
  if mx <= mn + 1
    pende = -1
    pends = -1
  else
    pends = mn + 1
    pende = mx - 1
  end
  puts "a #{a} b #{b} sum #{sum} pends #{pende}" if DBG
end
puts "#{[a,b].min - sum + 1}"

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0

maxtie = -1

arr.each do |z|
	x = z[0]
	y = z[1]

#	if(prevx==x && prevy==y)
#		next
#	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		prevx+=1 if prevx==maxtie
		prevy+=1 if prevy==maxtie
		next
	elsif(x==y && prevx==prevy)
		ans+=x-prevx+1
		prevx=x+1
		prevy=y+1
		maxtie=x
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
				maxtie = y
			else
				ans+=x-prevy+1
				maxtie = x
			end
		elsif(inrange(prevx,x,y))
			ans+=y-prevx+1
			maxtie = y
		else#(inrange(prevy,y,prevx)&&inrange(prevy,y,x))
			ans+=x-prevx+1
			maxtie = x
		end

	end
=begin
	p x
	p y
	p prevx
	p prevy
#	p toincx
#	p toincy
	p maxtie
	p ans
	puts
=end
	prevx=x
	prevy=y

	prevx+=1 if prevx==maxtie
	prevy+=1 if prevy==maxtie

end

#if arr[n-1][0]==0 and arr[n-1][1]==0
#	ans+=1
#end
puts ans

n = gets.to_i
pa,pb = 0,0
ans = 1
n.times do
  a,b = gets.split.map(&:to_i)
  if a == b
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a > b && pa <= b
    ans += b-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a < b && pb <= a
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  end
  pa,pb = a,b
  p ans
end
p ans

DBG = true
n = gets.to_i
sum = 0
prev = -1
for i in 0...n
  a,b = gets.split.map{|z| z.to_i}
  d = [[a,b].min, prev].max
  sum += [0, [a,b].max - d - 1].max
  prev = (a==b ? a : ([a,b].max - 1))
end
puts "#{[a,b].min + 1 - sum}"

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0
start=1

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0
arr.each do |z|
	x = z[0]
	y = z[1]

	if(prevx==x && prevy==y)
		next
	end
	start=0

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
			else
				ans+=x-prevy+1
			end
		end
		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x))
			ans+=x-prevx+1
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
	p ans
	puts
=end
	prevx=x
	prevy=y
end
if arr[n-1][0]==0 and arr[n-1][1]==0
	ans+=1
end
puts ans

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 1
arr.each do |z|
	x = z[0]
	y = z[1]

	if(prevx==x && prevy==y)
		next
	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy
			else
				ans+=x-prevy
			end
		end
		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x) && !(prevx==prevy && x==y))
			ans+=x-prevx+1
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
	p ans
	puts
=end
	prevx=x
	prevy=y
end

#if arr[n-1][0]==0 and arr[n-1][1]==0
#	ans+=1
#end
puts ans

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0

maxtie = -1

arr.each do |z|
	x = z[0]
	y = z[1]

	if(prevx==x && prevy==y)
		next
	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
				maxtie = y
			else
				ans+=x-prevy+1
				maxtie = x
			end
		elsif(inrange(prevx,x,y))
			ans+=y-prevx+1
			maxtie = y
		end

		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x) && !(prevx==prevy && x==y))
			ans+=x-prevx+1
			maxtie = x
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
	p toincx
	p toincy
	p ans
	puts
=end
	prevx=x
	prevy=y

	prevx+=1 if prevx==maxtie
	prevy+=1 if prevy==maxtie

end

if arr[n-1][0]==0 and arr[n-1][1]==0
	ans+=1
end
puts ans

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0

maxtie = -1

arr.each do |z|
	x = z[0]
	y = z[1]

#	if(prevx==x && prevy==y)
#		next
#	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		prevx+=1 if prevx==maxtie
		prevy+=1 if prevy==maxtie
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
				maxtie = y
			else
				ans+=x-prevy+1
				maxtie = x
			end
		elsif(inrange(prevx,x,y))
			ans+=y-prevx+1
			maxtie = y
		end

		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x) && !(prevx==prevy && x==y))
			ans+=x-prevx+1
			maxtie = x
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
#	p toincx
#	p toincy
	p maxtie
	p ans
	puts
=end
	prevx=x
	prevy=y

	prevx+=1 if prevx==maxtie
	prevy+=1 if prevy==maxtie

end

#if arr[n-1][0]==0 and arr[n-1][1]==0
#	ans+=1
#end
puts ans


q = gets.to_i
q.times{
  n = gets.to_i
  data = []
  data << gets.chomp.chars.map(&:to_i).map{|i| i<3}
  data << gets.chomp.chars.map(&:to_i).map{|i| i<3}

  state = true
  ans = true
  pos_r = 0 # 0..1
  pos_c = 0 # 0..n-1
  until pos_c==n
    if state && data[pos_r][pos_c]
      pos_c += 1
    elsif (state && (!data[pos_r][pos_c]))
      state = false
      pos_r = pos_r==0 ? 1 : 0
    elsif (!state) && data[pos_r][pos_c]
      ans = false
      break
    else
      state = true
      pos_c += 1
    end
  end
  puts (ans && state && pos_r==1) ? :YES : :NO
}

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end
def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
def row(i)
  i==0 ? 1 : 0
end

q = gi
ans=[]
q.times do
  n = gi
  p1 = gs.chars.map(&:to_i)
  p2 = gs.chars.map(&:to_i)

  pi=[p1,p2]

  ri=0
  i=0
  fix=false
  while i<n do
    if pi[ri][i]<=2
    else
      ri=row(ri)
      if pi[ri][i]<=2
        ans<<'NO'
        fix=true
        break
      end
    end
    i+=1
  end

  next if fix

  if ri==0
    ans<<'NO'
  else
    ans<<'YES'
  end
end

puts ans


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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split("")
end

ans = 0
(1..n-2).each do |a|
	(1..n-2).each do |b|
		if arr[a][b]=="X" and arr[a-1][b-1]=="X" and arr[a+1][b-1]=="X" and arr[a-1][b+1]=="X" and arr[a+1][b+1]=="X"
			ans += 1
		end
	end
end

puts ans

# Input
n = gets.to_i
m = Array.new(n)
n.times { |i| m[i] = gets.split("") }

# Processing
count = 0
1.upto(n - 2) do |i|
  1.upto(n - 2) do |j|
    condition = (m[i][j] == "X") &&
                (m[i - 1][j - 1] == "X") &&
                (m[i - 1][j + 1] == "X") &&
                (m[i + 1][j - 1] == "X") &&
                (m[i + 1][j + 1] == "X")
    count += 1 if condition
  end
end

# Output
puts count

n = gets.strip.to_i
board = Array.new n
n.times do |i|
  board[i] = gets.strip
end
counter = 0
if n > 0
  (1...(n - 1)).each do |i|
    (1...(n - 1)).each do |j|
      counter += 1 if board[i][j] == 'X' \
        && board[i][j] == board[i - 1][j - 1] \
        && board[i][j] == board[i - 1][j + 1] \
        && board[i][j] == board[i + 1][j - 1] \
        && board[i][j] == board[i + 1][j + 1]
    end
  end
end
puts counter

DBG = !true
n = gets.to_i
s = []
for i in 0...n
  s << gets.chomp
  #a,b = gets.split
  #a = a.to_i
  #b = b.to_i
end
print n if DBG
print s if DBG
sum = 0
for i in 1..(n-2)
 for j in 1..(n-2)
   if s[i][j] == 'X' &&
      s[i][j] == s[i-1][j-1] &&
      s[i][j] == s[i-1][j+1] &&
      s[i][j] == s[i+1][j-1] &&
      s[i][j] == s[i+1][j+1]
       sum += 1
    end
  end
end
puts "#{sum}"


current_color = 'blue'
count = gets.chomp.to_i
colors = []

count.times do
  colors << gets.chomp
end

locked = false
colors.each do |color_item|
  if locked
    locked = false if color_item == 'unlock'
  else
    if color_item == 'lock'
      locked = true
    elsif color_item != 'unlock'
      current_color = color_item
    end
  end
end

puts current_color

n = Integer(gets)
is_locked = false
ans = "blue"
Lk = 'lock'
ULk = 'unlock'
while n > 0 do
    str1 = gets
    if str1[0] == 'l' and str1[1] == 'o' then
        is_locked = true
    else
        if str1[0] == 'u' and str1[1] == 'n' then
            is_locked = false
        else
            if is_locked == false then
                ans = str1
            end
        end
    end
    n = n - 1
end
print ans


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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (recleft..recright).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    if xa<xc
      (xa..xc-1).each do |k|
        puts "#{k} #{ya}"
      end
    else
      (xc+1..xa).each do |k|
        puts "#{k} #{ya}"
      end
    end

    if ya<yc
      (ya..yc).each do |k|
        puts "#{xc} #{k}"
      end
    else
      (yc..ya).each do |k|
        puts "#{xc} #{k}"
      end
    end

    if xb<xc
      (xb..xc-1).each do |k|
        puts "#{k} #{yc}"
      end
    else
      (xc+1..xb).each do |k|
        puts "#{k} #{yc}"
      end
    end

    if yb<yc
      (yb..yc-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (yc+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (ya..yb).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    if xa<xc
      (xa..xc-1).each do |k|
        puts "#{k} #{ya}"
      end
    else
      (xc+1..xa).each do |k|
        puts "#{k} #{ya}"
      end
    end

    if ya<yc
      (ya..yc).each do |k|
        puts "#{xc} #{k}"
      end
    else
      (yc..ya).each do |k|
        puts "#{xc} #{k}"
      end
    end

    if xb<xc
      (xb..xc-1).each do |k|
        puts "#{k} #{yc}"
      end
    else
      (xc+1..xb).each do |k|
        puts "#{k} #{yc}"
      end
    end

    if yb<yc
      (yb..yc-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (yc+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (ya..yb).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    (acxmin..acxmax-1).each do |k|
      puts "#{k} #{ya}"
    end

    (acymin..acymax).each do |k|
      puts "#{xc} #{k}"
    end

    (bcxmin+1..bcxmax-1).each do |k|
      puts "#{k} #{yc}"
    end

    (bcymin+1..bcymax).each do |k|
      puts "#{xb} #{k}"
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (ya..yb).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    (acxmin..acxmax-1).each do |k|
      puts "#{k} #{ya}"
    end

    (acymin..acymax).each do |k|
      puts "#{xc} #{k}"
    end

    (bcxmin+1..bcxmax-1).each do |k|
      puts "#{k} #{yc}"
    end

    (bcymin..bcymax).each do |k|
      puts "#{xb} #{k}"
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (recleft..recright).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    if xa<xc
      (xa..xc-1).each do |k|
        puts "#{k} #{ya}"
      end
    else
      (xc+1..xa).each do |k|
        puts "#{k} #{ya}"
      end
    end

    if ya<yc
      (ya..yc).each do |k|
        puts "#{xc} #{k}"
      end
    else
      (yc..ya).each do |k|
        puts "#{xc} #{k}"
      end
    end

    if xb<xc
      (xb..xc-1).each do |k|
        puts "#{k} #{yc}"
      end
    else
      (xc+1..xb).each do |k|
        puts "#{k} #{yc}"
      end
    end

    if yb<yc
      (yb..yc-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (yc+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    end

  end
end


T = gets.to_i

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

  ans = 0

  a.each_with_index do |cnt, dist|
    break if d <= 0
    next if cnt == 0

    if dist == 0
      ans += cnt
    else
      c = [cnt, d / dist].min
      cost = c * dist
      d -= cost
      ans += c
    end
  end

  puts ans
end

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

t = next_int
a = Array.new(101)
t.times do
  n = next_int
  d = next_int

  n.times do |i|
    a[i] = next_int
  end

  (1...n).each do |i|
    break if d == 0

    k = d / i
    k = a[i] if k > a[i]
    a[0] += k
    d -= k * i
  end
  puts a[0]
end


s = gets.chomp!
mods = 1000000007
dp = [[0,0,0],[0,0,0]]

len =s.size
cur =0;nxt=1;

case s[0]
when '0'
	dp[cur][0]=1
when '1'
	dp[cur][1]=1
when '2'
	p 0
	exit
when '*'
	dp[cur][2]=1
when '?'
	dp[cur][0]=dp[cur][1]=dp[cur][2]=1
end

for i in 1...len
	case s[i]
	when '0'
		dp[nxt][0]=dp[cur][0]
	when '1'
		dp[nxt][1]= dp[cur][0]
		dp[nxt][0]= dp[cur][2]
	when '2'
		dp[nxt][1]=dp[cur][2]
	when '*'
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	when '?'
		dp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods
		dp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	end
	cur = nxt
	nxt = cur==0?1:0
	dp[nxt] = [0,0,0]
end

puts "#{(dp[cur][0]+dp[cur][2])%mods}"

s = gets.chomp!
mods = 1000000007
dp = [[0,0,0],[0,0,0]]

len =s.size
cur =0;nxt=1;

case s[0]
when '0'
	dp[cur][0]=1
when '1'
	dp[cur][1]=1
when '2'
	p 0
	exit
when '*'
	dp[cur][2]=1
when '?'
	dp[cur][0]=dp[cur][1]=dp[cur][2]=1
end

for i in 1...len
	case s[i]
	when '0'
		dp[nxt][0]=dp[cur][0]
	when '1'
		dp[nxt][1]= dp[cur][0]
		dp[nxt][0]= dp[cur][2]
	when '2'
		dp[nxt][1]=dp[cur][2]
	when '*'
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	when '?'
		dp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods
		dp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	end
	a = cur
	cur = nxt
	nxt = a
	dp[nxt] = [0,0,0]
end

puts "#{(dp[cur][0]+dp[cur][2])%mods}"

s = gets.chomp!
mods = 1000000007
dp = [[0,0,0],[0,0,0]]

len =s.size
cur =0;nxt=1;

case s[0]
when '0'
	dp[cur][0]=1
when '1'
	dp[cur][1]=1
when '2'
	p 0
	exit
when '*'
	dp[cur][2]=1
when '?'
	dp[cur][0]=dp[cur][1]=dp[cur][2]=1
end

for i in 1...len
	case s[i]
	when '0'
		dp[nxt][0]=dp[cur][0]
	when '1'
		dp[nxt][1]= dp[cur][0]
		dp[nxt][0]= dp[cur][2]
	when '2'
		dp[nxt][1]=dp[cur][2]
	when '*'
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	when '?'
		dp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods
		dp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	end
	a = cur
	cur = nxt
	nxt = a
	dp[nxt] = [0,0,0]
end

p (dp[cur][0]+dp[cur][2])%mods


s = gets.strip
d = s.reverse
ans = s+d
puts ans

# coding: utf-8

# if __FILE__ == $0
# end

order = gets.chomp

puts "#{order}#{order.reverse}"

pal = gets.strip
ans = ""
ans << pal << pal.reverse
puts ans

a = gets.chomp

a = a + a.reverse

puts a

#!/usr/bin/ruby
s=gets.chomp
puts s+s.reverse

str = gets.chomp

print str
print str.reverse

#! /usr/bin/env ruby
gets.chomp.tap{|s|print s; puts s.reverse}

a=gets.chomp
b=a.split("").reverse.join("")
print a
print b

#!/usr/bin/ruby
n=gets.to_i
i=1
t=9
while n>t
	n-=t
	i+=1
	t=t*10+9
end
s='%0*d'%[i,n]
puts s.reverse+s

a=gets.chomp.to_i
(a*2).times do
print "1"
end

s=gets
print s
print s.reverse
print "\n"


main = -> {
  N = int
  N.times do
    digits = gets.chomp.chars.map(&:to_i)
    first = digits[0, 3].sum
    last = digits[3, 3].sum
    puts first == last ? "YES" : "NO"
  end
}

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

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]

 gets.to_i.times {
 	v = gets.chars.map {|x| x.to_i }
 	puts ({true=>"YES", false=>"NO"}[v[..2].sum==v[3..].sum])
 }

gets.to_i.times do
  s = gets.chomp.chars.map(&:to_i)
  puts s[..2].sum == s[3..].sum ? "YES" : "NO"
end

gets.to_i.times do
  s = gets.strip.chars
  fail unless s.length == 6
  a = s[..2].map(&:to_i).sum
  b = s[3..].map(&:to_i).sum
  puts(if a == b then 'YES' else 'NO' end)
end

def main
  gets.to_i.times do
    x = gets.chomp.chars.map(&:to_i)
    puts uyesno(x[..2].sum == x[3..].sum)
  end
end

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

main


l = gets.to_i
n = gets.chomp
c = l
idx = 0
(1...l).each do |i|
    if n[i] != "0"
        m = (l-i > i ? l-i : i)
        if c > m
            c = m
            idx = i
        end
    end
end
ans = n[0,idx].to_i+n[idx..-1].to_i
s = ans.to_s.size
(1...l).each do |i|
    if n[i] != "0"
        next if s < (l-i > i ? l-i : i)
        ret = n[0,i].to_i+n[i..-1].to_i
        if ret < ans
            ans = ret
            s = ans.to_s.size
        end
    end
end
puts ans

n = gets.to_i
s = gets.chomp
ans = []
if n%2 == 0
  3.times do |i|
    next if s[n/2-1+i] == "0"
    ans << s[0,n/2-1+i].to_i + s[n/2-1+i,n/2+1-i].to_i
  end
else
  4.times do |i|
    k = n/2-1+i
    next if k <= 0 || k >= n || s[k] == "0"
    ans << s[0,k].to_i + s[k,n-k].to_i
  end
end
if ans.empty?
  l,r = n/2,n/2
  l -=1 while l >= 0 && s[l] == "0"
  r += 1 while r < n && s[r] == "0"
  ans << s[0,l].to_i + s[l,n-l].to_i if l > 0
  ans << s[0,r].to_i + s[r,n-r].to_i if r < n
  #p [l,r]
end
#p ans
puts ans.min

L = gets.to_i
N = gets.chomp

ans = Float::INFINITY

pt = L/2
pt -= 1 while N[pt] == '0'
if pt > 0
    res = N[0, pt].to_i + N[pt..-1].to_i
    ans = [ans, res].min
end
pt = (L+1)/2
pt += 1 while pt < L && N[pt] == '0'
if pt < L
    res = N[0, pt].to_i + N[pt..-1].to_i
    ans = [ans, res].min
end

puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  if l[i+1] == '0' && i != n-2
    next
  end
  ans = [ans, l[0..i].to_i + l[(i+1)..n-1].to_i].min
end

puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  if l[i+1] == '0'
    next
  end
  ans = [ans, l[0..i].to_i + l[(i+1)..n-1].to_i].min
end

puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  a = l[0..i]
  b = l[(i+1)..n-1]
  if b.size >= 2 && b[0] == '0'
    next
  end
  ans = [ans, a.to_i + b.to_i].min
end

puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  a = l[0..i]
  b = l[(i+1)..n-1]
  if b[0] == '0'
    next
  end
  ans = [ans, a.to_i + b.to_i].min
end

puts ans

l = gets.to_i
n = gets.chomp
c = l
idx = 0
(l-1).downto(1) do |i|
    if n[i] != "0"
        m = (l-i > i ? l-i : i)
        if c > m
            c = m
            idx = i
        end
    end
end
ans = n[0,idx].to_i+n[idx..-1].to_i
(1...l).each do |i|
    if n[i] != "0"
        break if ans.to_s.size < (l-i > i ? l-i : i)
        ret = n[0,i].to_i+n[i..-1].to_i
        if ret < ans
            ans = ret
        end
    end
end
puts ans


a,b,c=gets.split.map(&:to_f)
if a<0
a=-a
b=-b
c=-c
end
p (-b+Math.sqrt(b*b-4*a*c))/2/a
p (-b-Math.sqrt(b*b-4*a*c))/2/a

a, b, c = gets.chomp.split

a = a.to_f
b = b.to_f
c = c.to_f

d=(b**2-4*a*c)**(0.5)
x=(-b+d)/(2*a)
y=(-b-d)/(2*a)

if y>x
x, y= y, x
end

puts x
print y

 a,b,c=gets.split.map(&:to_f)
(a=-a;b=-b;c=-c)if a<0
puts [1,-1].map{|s|(-b+s*Math.sqrt(b*b-4*a*c))/2/a}

a,b,c=gets.split.map(&:to_f)
p (-b+Math.sqrt(b*b-4*a*c))/2
p (-b-Math.sqrt(b*b-4*a*c))/2

a,b,c=gets.split.map(&:to_f)
p (-b+Math.sqrt(b*b-4*a*c))/2/a
p (-b-Math.sqrt(b*b-4*a*c))/2/a


n = gets.to_i
a = gets.split.map{|i|i.to_i%2}.count(0)
puts [a,n-a].min

n = gets.to_i
nums = gets.split.map(&:to_i)
even = nums.select(&:even?).length
v = [even, n - even]
puts v.min

nn = gets.to_i
xx = gets.split.map(&:to_i)

min0=0
min1=0

0.upto(nn-1) do |n|
  t = xx[n]
  min0+=1 if t%2==0
  min1+=1 if (t+1)%2==0
end

puts min0<min1 ? min0 : min1

n = gets.to_i
x = gets.split.map &:to_i
puts [x.select{|x| x%2 == 0}.size, x.select{|x| x%2 == 1}.size].min

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

puts [a.count(&:even?), a.count(&:odd?)].min

n=gets.to_i
a=gets.split(' ').map(&:to_i)
e=[0,0]
a.each {|x| e[x&1]+=1}
puts e.min

n = gets.to_i
a = gets.split.map(&:to_i)
puts [a.count(&:odd?), a.count(&:even?)].min

n = gets.to_i
arr = gets.split.map(&:to_i)
even = 0
odd = 0

arr.map{ |x| x % 2 == 0 ? even += 1 : odd += 1 }

even < odd ? puts(even) : puts(odd)

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

odd_c = xs.count { |x| x.odd? }
even_c = n - odd_c

puts [odd_c, even_c].min

n = gets.strip.to_i
chips = gets.strip.split(' ').map(&:to_i)
cnt = chips.group_by { |x| x % 2 }
puts [(cnt[0] || []).size, (cnt[1] || []).size].min

n = gets.to_i
a = gets.split.map(&:to_i)
cnt_odd = 0
a.each do |x|
  cnt_odd += 1 if x % 2 == 1
end
puts [n - cnt_odd, cnt_odd].min

N = gets.to_i
arr = gets.split.map(&:to_i)
arr.map! { |x| x % 2 }
zero = arr.count 0
one = arr.count 1
if zero < one
	puts zero
else
	puts one
end

n = gets.chomp.to_i
xx = gets.split.map(&:to_i)
puts n/2

n = gets.to_i
#xx = gets.split.map(&:to_i)
puts n/2

n = gets.to_i
x = gets.split.map(&:to_i).sort
puts x[1..-1].inject(0) { |s, e| s + (e - x[0])%2 }


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

nm = gets.chomp.split(" ").map(&:to_i)
n = nm[0]
m = nm[1]
amount = gets.chomp.split(" ").map(&:to_i)
cost = gets.chomp.split(" ").map(&:to_i)
orders = []
m.times do
	orders.push gets.chomp.split(" ").map(&:to_i)
end

costin = []

(0..n-1).each do |i|
	costin.push [cost[i],i]
end

costin.sort_by!{|x| [x[0],x[1]]}

cheapestindex = 0

orders.each do |ord|
	type = ord[0]-1
	num = ord[1]

	totalcost = 0

	if cheapestindex==n
		puts totalcost
		next
	end

	left = amount[type]

	if(num<=left)
		amount[type]-=num
		totalcost+=num*cost[type]
		num=0
	else
		totalcost+=left*cost[type]
		amount[type]=0
		num-=left
		while(num>0)
			nextdishind = costin[cheapestindex][1]
			left = amount[nextdishind]

			if(left<1)
				cheapestindex+=1
				if cheapestindex==n
					break
				end
				nextdishind = costin[cheapestindex][1]
				left = amount[nextdishind]
			end

			if(num<=left)
				amount[nextdishind]-=num
				totalcost+=num*cost[nextdishind]
				num=0
			else
				totalcost+=left*cost[nextdishind]
				amount[nextdishind]=0
				num-=left
			end
		end
	end

#	p amount

	if num>0
		puts 0
	else
		puts totalcost
	end
end

DBG = !true
n,m = gets.split.map{|z| z.to_i}
a = gets.split.map{|z| z.to_i}
c = gets.split.map{|z| z.to_i}
td = []
for i in 0...m
  t,d = gets.split.map{|z| z.to_i}
  td << [t-1,d]
  #a,b = gets.split
  #a = a.to_i
  #b = b.to_i
end

if DBG
  puts "n #{n} m #{m}"
  print a
  puts ""
  print c
  puts ""
  print td
  puts ""
end

sortedc = []
for i in 0...n
  sortedc << [i,c[i]]
end
sortedc.sort_by!{|z| 200000*z[1]+z[0]}
print sortedc if DBG
cheapest = 0

#totalcost = 0
for j in 0...m
  if cheapest == n
    puts "0"
    next
  end
  cost = 0
  t = td[j][0]
  d = td[j][1]
  if a[t] > 0
    amt = [d, a[t]].min
    d -= amt
    a[t] -= amt
    cost += c[t] * amt
  end
  while d > 0
    while  cheapest < n && a[sortedc[cheapest][0]] == 0
      cheapest += 1
    end
    if cheapest == n
      cost = 0
      break
    end
    t = sortedc[cheapest][0]
    amt = [d, a[t]].min
    d -= amt
    a[t] -= amt
    cost += c[t] * amt
  end
  if DBG
    print a
    puts ""
  end
  puts "#{cost}"
  #totalcost += cost
end

n,m=gets.split.map(&:to_i)
$stdout.sync = true
a=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
M=[]
n.times{|i|M<<i}
M.sort_by!{|e|c[e]}
s=0
m.times{
	t,d=gets.split.map(&:to_i)
	q=[a[t-1], d].min
	r=c[t-1]*q
	a[t-1]-=q
	d-=q
	i=s
	while d>0&&i<n do
		k=M[i]
		q=[a[k],d].min
		d-=q
		a[k]-=q
		r+=c[k]*q
		s+=1 if a[k]==0
		i+=1
	end
	print "#{r}\n" if d==0
	print "0\n" if d>0
}

s = (gets.chomp).split(' ').map(&:to_i)
n = s[0]
m = s[1]
t = Array.new(n)
ost = (gets.chomp).split(' ').map(&:to_i)
cost = (gets.chomp).split(' ').map(&:to_i)
(0..n-1).each do |i|
    t[i] = [cost[i], i]
end
t.sort_by!{ |x| x[0] }
pos = 0
m.times do |p|
    s = (gets.chomp).split(' ').map(&:to_i)
    a = s[0]
    b = s[1]
    ans = 0
    ans += cost[a-1] * [b, ost[a-1]].min()
    ost[a-1] -= b
    if ost[a-1] <= 0
        temp = ost[a-1].abs()
        ost[a-1] = 0

        while temp > 0 do
            if pos == n && temp > 0
                ans = 0
                break
            end
            tt = temp
            temp -= [ost[t[pos][1]], tt].min()
            ans += t[pos][0] * [ost[t[pos][1]], tt].min()
            ost[t[pos][1]] -= [ost[t[pos][1]], tt].min()
            if ost[t[pos][1]] == 0
                pos += 1
            end
        end
    end
    puts ans
end

n,m=gets.split.map(&:to_i)
$stdout.sync = true
a=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
M=[]
n.times{|i|M<<i}
M.sort_by!{|e|c[e]}
s=0
m.times{
	t,d=gets.split.map(&:to_i)
	q=[a[t-1], d].min
	r=c[t-1]*q
	a[t-1]-=q
	d-=q
	i=s
	while d>0&&i<n do
		k=M[i]
		q=[a[k],d].min
		d-=q
		a[k]-=q
		r+=c[k]*q
		s+=1 if a[k]==0
		i+=1
	end
	print "#{d}\n" if d==0
	print "0\n" if d>0
}

n,m=gets.split.map(&:to_i)
$stdout.sync = true
a=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
M=[]
n.times{|i|M<<i}
M.sort_by!{|e|c[e]}
s=0
m.times{
	t,d=gets.split.map(&:to_i)
	q=[a[t-1], d].min
	r=c[t-1]*q
	a[t-1]-=q
	d-=q
	i=s
	while d>0&&i<n do
		k=M[i]
		q=[a[k],d].min
		d-=q
		a[k]-=q
		r+=c[k]*q
		s+=1 if a[k]==0
		i+=1
	end
	print "#{d}\n" if d>0
	print "0\n" if d==0
}


gets.chomp.to_i.times do

    str = gets.chomp
    i=1
    a=str[0]
    while i < str.length-1
        a+= str[i] if(i%2!=1)
        i+=1
    end

    puts a+str[-1]
end

gets.to_i.times do
	s = gets.chomp
	n = s.size
	puts (0...n / 2).map { s[_1 * 2] }.join + s[-1]
end

n = gets.to_i
n.times do
	b = gets.chomp
	m = b.size
	a = b[0]
	(b.size/2-1).times do |i|
		a += b[1+i*2]
	end
	a += b[-1]
	puts a
end

t = gets.to_i
t.times do
	b = gets.chomp
	a = b[0]
	for i in 1...b.length
		a += b[i] if i%2 == 1
	end
	puts a
end

n = gets.chomp.to_i
(0...n).each do |i|
    arr = gets.chomp.split("")
    arsize = arr.size
    print arr[0]
    (1...arsize).each do |j|
        if j % 2 == 0 && arr[j] == arr[j-1]
            next
        else
            print arr[j]
        end
    end
    print "\n"
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	b = gets.chomp
	if b.length < 4
		result.push b
		next
	end
	a = b[0]
	(1..(b.length-2)).step(2).each do |index|
		a += b[index]
	end
	a += b[b.length-1]
	result.push a
end
result.each do |res|
	puts res
end

def solve(str)
	return str if str.size < 3

	compact_mid = str[1..-2].chars.each_slice(2).map(&:first).join

	"%s%s%s" % [str[0], compact_mid,str[-1]]
end

inputs = ARGF.each_line.drop(1).map(&:strip).to_a

inputs.each do |i|
	puts solve(i)
end

n = gets.chomp.to_i
n.times do
	s = gets.chomp
	if s.length == 2
		puts s
		next
	end
	print s[0]
	i = 1
	while i < s.length - 1
		print s[i]
		i += 2
	end
	puts s[-1]
end

n=gets.chomp.to_i
n.times do
  inpstr = gets.chomp.split('')
  result = inpstr[0]
  (1..inpstr.size).step(2).each do |index|
    result << inpstr[index]
  end
  puts result
end

gets.to_i.times do
  s = gets.chomp
  puts (0.step(s.length, 2).to_a + [s.length - 1]).map { |i| s[i] }.join
end

n=gets.chomp.to_i
n.times do
  inpstr = gets.chomp.split('')
  result = inpstr[0]
  (1..inpstr.size).step(2).each do |index|
    result << inpstr[index]
  end
  p result
end


N, Q = gets.split.map(&:to_i)
Q.times do
    x, y = gets.split.map(&:to_i)
    ans = 0
    if (x+y)%2 == 0
        ans += (x-1)/2 * N
        ans += (N+1)/2 if x%2==0
        ans += (y+1)/2
    else
        ans += (N*N+1)/2
        ans += (x-1)/2 * N
        ans += N/2 if x%2==0
        ans += (y+1)/2
    end
    puts ans
end


def inv(arr)
  i1 = i2 = 0
  res = 0
  while i1 < arr.size - 1
    while i2 < arr.size - 1
      i2 += 1
      res += 1 if arr[i2] < arr[i1]
    end
    i1 += 1
    i2 = i1
  end
  res
end

t = gets.chomp.to_i
t.times do
  n, k = gets.chomp.split(' ').map(&:to_i)
  max_n = k * 2 - 1
  # puts "max_n = #{max_n.to_s}"
  diff = max_n - n
  # puts "DIFF = #{diff.to_s}"
  arr = (1..(diff)).to_a
  # puts "ARR = #{arr.to_s}"
  arr << k
  # puts "ARR = #{arr.to_s}"
  while arr.size < k
    arr << arr[-1] - 1
  end
  # puts "ARR = #{arr.to_s}"
  puts arr.join(' ')
end

gets.to_i.times do
  n, k = gets.split.map &:to_i
  (1...2 * k - n).each do |i|
    print i, " "
  end
  (2 * k - n..k).reverse_each do |i|
    print i, " "
  end
  puts
end

gets.to_i.times do
  n,k=gets.split.map(&:to_i)
  l=[*1...2*k-n]+[*2*k-n..k].reverse
  puts l.join(" ")
end


require "set"

n = gets.to_i

n.times do
  s = gets.chomp

  set = Set.new
  days = 1
  i = 0

  while (i < s.size)
    char = s[i]
    if !set.include?(char) && set.size == 3
      set = Set.new
      days += 1
    else
      set.add(char)
      i += 1
    end
  end

  p days
end

#input the number of test cases
t = gets.chomp.to_i

#list of results
results = []

#solve
for i in 1..t
    str = gets.chomp

    #solving code
	result = 0
    letterAlreadyToday = []
    for j in str.split ""
		if letterAlreadyToday.include? j
			next
		else
			if letterAlreadyToday.length == 3
				letterAlreadyToday = []
			end
		end
		if letterAlreadyToday.length < 1
			result += 1
		end
		letterAlreadyToday.push j
    end
	results.push result
end

puts results

t = gets.to_i
t.times {
  s = gets.chomp
  d = 1
  chrs = []
  while !s.empty?
    c = s[0]
    s = s[1..-1]
    if !(chrs.index c)
      if chrs.size == 3
        d += 1
        chrs = []
      end
      chrs += [c]
    end
  end
  puts d
}


def binary_search(x, y, a, b)
  first = 0
  last = (y - x)/[a,b].max

  while first <= last
    i = (first + last) / 2

    xx = x + i*a
    yy = y - i*b

    if xx == yy
      return i
    elsif xx > yy
      last = i - 1
    elsif yy > xx
      first = i + 1
    else
      return -1
    end
  end
  -1
end

n = gets.strip.to_i
results = []

n.times do
  arr = gets.strip.split.map(&:to_i)
  x, y, a, b = *arr
  results << binary_search(x, y, a, b)
end

n.times do |x|
  puts results[x]
end

num = gets.to_i
while num > 0
    myArray = gets.chomp.split().map { |e| e.to_i  }
    x = myArray[0]
    y = myArray[1]
    a = myArray[2]
    b = myArray[3]
    result = (y-x)/(a+b)
    if (y-x) % (a+b) == 0
        puts result
    else
        puts '-1'
    end
    num = num -1
end

class Rabbit

    def initialize(pos, jump_size)
        @pos = pos
        @jump_size = jump_size
    end

    def jump_size()
       @jump_size
    end

    def position()
       @pos
    end

end

def read_input()

   input = STDIN.gets()

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

   raise "Invalid input" if input.size != 4

   return Rabbit.new(input[0], input[2]), Rabbit.new(input[1], input[3])

end

def calc_rabbits_jumps()

    rabbit1, rabbit2 = read_input()

    distance = rabbit2.position - rabbit1.position
    speed = rabbit1.jump_size + rabbit2.jump_size
    point = distance / speed.to_f

    if point % 1 == 0
        puts point.to_i
    else
        puts -1
    end

end

cnt = Integer(STDIN.gets())

cnt.times {
    calc_rabbits_jumps()
}

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

t = next_int

t.times do |_i|
  x = next_int
  y = next_int
  a = next_int
  b = next_int

  if (y - x) % (a + b) == 0
    t = (y - x) / (a + b)
    puts t
  else
    puts -1
  end
end

t = gets.to_i
z = []
(0..t - 1).each { |i|
  str = gets
  z[i] = str
  counter = 0
  bloc = z[i].split(' ')
  a = bloc[0].to_i; b = bloc[1].to_i; c = bloc[2].to_i; d = bloc[3].to_i
  if (b - a) % (c + d) != 0
    puts '-1'
  else
    puts (b - a) / (c + d)
  end
}

n = gets.to_i
n.times {
  x, y, a, b = gets.split.map(&:to_i)
  if (y-x) % (a + b) > 0
    puts "-1"
  else
    puts (y-x)/(a+b)
  end
}

1.upto(gets.to_i) do
  x, y, a, b = gets.split(' ').map(&:to_i)

  delta = y-x

  puts delta.modulo(a+b).zero? ? delta.div(a+b) : -1
end


gets.to_i.times do
    n, z = gets.split.map(&:to_i)
    ans = 0
    gets.split.map(&:to_i).each do |x|
        ans = [ans, x | z].max
    end
    puts ans
end

gets.to_i.times do
  n, z = gets.split.map(&:to_i)
  as = gets.split.map(&:to_i)
  p as.map { |a| a | z }.max
end

gets.to_i.times do
  n, z = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)
  p xs.map { |x| x | z }.max
end

gets.to_i.times do
    n, z = gets.split.map(&:to_i)
    p gets.split.map(&:to_i).map{|x| x | z}.max
end

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

gets.to_i.times do
  n, z = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i).sort.reverse

  num = 0
  ai = -1
  zi = 0
  n.times do |i|
    if (a[i] & z) > ai
      ai = a[i] | z
      zi = a[i] & z
      num = i
    end
  end

  ans = ai
  n.times do |i|
    next if i == num
    ai = a[i] | z
    zi &= a[i]
    ans = [ans, ai].max
  end
  puts ans
end


islands = gets.chomp.to_i
array = gets.chomp.split(' ').map {|x| x.to_i }
result = gets.chomp.split(' ').map {|x| x.to_i }

a = array.dup
b = result.dup
a.delete(0)
b.delete(0)

a << a.dup
a.flatten!

for i in 0..a.length-1
  break if a.length - i< b.length
  yay = true
  for j in 0..b.length-1
    if a[i+j] != b[j]
      yay = false
      break
    end
  end
  if yay
    puts "YES"
    exit 0
  end
end

puts "NO"


nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  #puts e-m+1
  #puts s
  flag = false
  if s == 0
    x = 0
    flag = true
  else
    x = s-1
  end

  if e-m+1<0 or e-m+1<(s-1)
    puts 0
    next
  end
  if flag
    puts newarr[e-m+1]
  else
    puts newarr[e-m+1]-newarr[x]
  end
  #puts
#  puts arr.count{|x| x>=s and x<=e-len+1}
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    num[i] = S[i, M] == T ? 1 : 0
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    puts r-l+1 >= M ? num[r-M+1] - num[l-1] : 0
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    if S[i, M] == T
        num[i] = 1
    else
        num[i] = 0
    end
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    ans = r >= M-1 ? num[r-M+1] - num[l-1] : 0
    puts ans > 0 ? ans : 0
end

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n+m, 0)

point = 0
(0+m-1).upto(n-1) do |i|
	flag = true
	m.times do |j|
		if s[i-m+1+j] != t[j]
			flag = false
		end
	end

	if flag
		ac[i] += 1
	end
	# p "#{s[i]} #{t[point]}, #{point}"

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x+m-3 > y-1
		puts 0
		# puts "CASE1"
	elsif x+m-3 >= 0 && x+m-3 < n
		puts ac[y-1] - ac[x+m-3]
		# puts "CASE2 #{y-1} #{x+m-3} #{ac[y-1]} #{ac[x+m-3]}"
	else
		puts ac[y-1]
		# puts "CASE3"
	end
end

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n, 0)

point = 0
n.times do |i|

	# p "#{s[i]} #{t[point]}, #{point}"
	if s[i] == t[point]
		point += 1
	else
		point = 0
	end

	if point == m
		ac[i] += 1
		point = 0
	end

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x+m-3 >= 0 && x+m-3 < n
		puts ac[y-1] - ac[x+m-3]
	else
		puts ac[y-1]
	end
end

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n, 0)

point = 0
n.times do |i|

	# p "#{s[i]} #{t[point]}, #{point}"
	if s[i] == t[point]
		point += 1
	else
		point = 0
	end

	if point == m
		ac[i] += 1
		point = 0
	end

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x+m-3 > y-1
		puts 0
		# puts "CASE1"
	elsif x+m-3 >= 0 && x+m-3 < n
		puts ac[y-1] - ac[x+m-3]
		# puts "CASE2 #{y-1} #{x+m-3} #{ac[y-1]} #{ac[x+m-3]}"
	else
		puts ac[y-1]
		# puts "CASE3"
	end
end

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n, 0)

point = 0
n.times do |i|

	# p "#{s[i]} #{t[point]}, #{point}"
	if s[i] == t[point]
		point += 1
	else
		point = 0
	end

	if point == m
		ac[i] += 1
		point = 0
	end

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x-1 >= 0
		puts ac[y-1] - ac[x-1]
	else
		puts ac[y-1]
	end
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    if S[i, M] == T
        num[i] = 1
    else
        num[i] = 0
    end
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    puts r >= M-1 ? num[r-M+1] - num[l-1] : 0
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    num[i] = S[i, M] == T ? 1 : 0
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    ans = r-l+1 >= M ? num[r-M+1] - num[l-1] : 0
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    if S[i, M] == T
        num[i] = 1
    else
        num[i] = 0
    end
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    puts num[r-M+1] - num[l-1]
end

nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i-m+1)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  puts newarr[e]-newarr[s]
#  puts arr.count{|x| x>=s and x<=e-len+1}
end

nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  #puts e-m+1
  #puts s
  flag = false
  if s == 0
    x = 0
    flag = true
  else
    x = s-1
  end

  if e-m+1<0
    puts 0
    next
  end
  if flag
    puts newarr[e-m+1]
  else
    puts newarr[e-m+1]-newarr[x]
  end
  #puts
#  puts arr.count{|x| x>=s and x<=e-len+1}
end

nmq = gets.chomp.split(" ").map(&:to_i)
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  (0..biglen-smallen+1).each do |i|
    next if i<index

    if string[i] == lookfor
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if char==lookfor
          index = i+j
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length
#p arr
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  puts arr.count{|x| x>=s and x<=e-len+1}
end

nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  #puts e-m+1
  #puts s
  if s == 0
    x = 0
  else
    x = s-1
  end

  if e-m+1<0
    puts 0
    next
  end

  puts newarr[e-m+1]-newarr[x]
  #puts
#  puts arr.count{|x| x>=s and x<=e-len+1}
end


gets.to_i.times do
  n, l, r = gets.split.map(&:to_i)
  array = (1..n).map do |i|
    ((l - 1) / i) * i + i
  end
  if array.max <= r
    puts "YES"
    puts array.join(" ")
  else
    puts "NO"
  end
end

class Solution
  def initialize(n, l, r)
    @n = n
    @l = l
    @r = r
    @arr = []
    solve
    print
  end

  def solve
    # unique gcd(i, a_i) means that a_i must be a multiple of i
    # -> try to find a_i s.t. a|a_i && l <= a_i <= r, i.e. smallest multiple of i > l
    # -> [(l - 1)/i + 1] * i
    (1..@n).each do |i|
      a_i = ((@l - 1)/i + 1) * i
      if a_i <= @r
        @arr << a_i
      else
        break
      end
    end
  end

  def print
    if @arr.length == @n
      puts 'YES'
      puts @arr.join(' ')
    else
      puts 'NO'
    end
  end
end

t = gets.chomp.to_i
t.times do
  n, l, r = gets.chomp.split(' ').map(&:to_i)
  Solution.new(n, l, r)
end

gets.to_i.times do
  n, l, r = gets.split.map(&:to_i)
  array = (1..n).map do |i|
    ((l - 1) / i + 1) * i
  end
  if array.max <= r
    puts "YES"
    puts array.join(" ")
  else
    puts "NO"
  end
end


def solve m
  ans = []
  cur = 1
  m.times do
    l, r = gets.chomp.split.map(&:to_i)
    if cur > r
      ans << 0
    elsif cur <= l
      ans << l
      cur = l + 1
    else
      ans << cur
      cur += 1
    end
  end
  puts ans.join(' ')
end

q = gets.to_i
q.times do
  m = gets.to_i
  solve m
end

inp = gets.chomp.to_i
otv = []
gg = []
inp.times{ |w|
ppl = gets.chomp.to_i

map = []

ppl.times{ |line|

user = gets.chomp.split(" ")
map.push([user[0].to_i,user[1].to_i])

}

#todo

ctime = 0
curentuser = 0

loop do
if curentuser>=map.size then break; end
ctime+=1

if ctime <= map[curentuser][1]

if ctime >= map[curentuser][0]

otv.push(ctime)
curentuser+=1
else
#wait
end

else
otv.push(0)
curentuser+=1
ctime-=1
end

end

gg.push(otv.join(" "))
otv = []

}

gg.size.times{|w|
puts gg[w]
}

def solve m
  ans = []
  cur = 1
  m.times do
    l, r = gets.chomp.split.map(&:to_i)
    if cur >= r
      ans << 0
    elsif cur <= l
      ans << l
      cur = l + 1
    else
      ans << cur
      cur += 1
    end
  end
  puts ans.join(' ')
end

q = gets.to_i
q.times do
  m = gets.to_i
  solve m
end


n = gets.to_i
g = Array.new(1000) { Array.new(1000, -1) }
n.times do |v|
  a, b = gets.split.map(&:to_i)
  g[a-1][b-1] = v
end
ans = 0
m1 = 999
for s in 0..2*m1 do
  a = [0, s-m1].max
  b = [m1, s].min
  i = a
  j = s - a
  tmp = []
  while i <= b
    tmp.push(g[i][j]) if g[i][j] != -1
    i += 1
    j -= 1
  end
  ans += tmp.combination(2).size
end

for s in (-1*m1)..m1 do
  a = [0, (-1*s)].max
  b = [m1, (m1-s)].min
  i = a
  j = s + a
  tmp = []
  while i <= b
    tmp.push(g[i][j]) if g[i][j] != -1
    i += 1
    j += 1
  end
  ans += tmp.combination(2).size
end
puts ans

r=0
h1=Hash.new(0)
h2=Hash.new(0)
gets.to_i.times{
	x,y=gets.split.map(&:to_i)
	r+=h1[x-y]
	h1[x-y]+=1
	r+=h2[x+y]
	h2[x+y]+=1
}
p r

n = gets.chomp.to_i
bishops1 = Hash.new(0)
bishops2 = Hash.new(0)

n.times do
  x, y = gets.chomp.split(" ").map(&:to_i)
  bishops1[x+y] += 1
  bishops2[x-y] += 1
end

def choose(n, k)
  # n!/(n-k)!
  pTop = (n-k+1 .. n).inject(1, &:*)
  # k!
  pBottom = (2 .. k).inject(1, &:*)
  pTop / pBottom
end

sum1 = bishops1.reduce(0) do |acc, (k, v)|
  acc+choose(v, 2)
end

sum2 = bishops2.reduce(0) do |acc, (k, v)|
  acc+choose(v, 2)
end

puts sum1+sum2

a=gets.chomp.to_i
b=Array.new(1000){Array.new(1000,0)}
a.times do
temp=gets.chomp.split(" ")
b[temp[0].to_i-1][temp[1].to_i-1]=1
end
col=[]
0.upto(998) do |i|
j=0
temp=0
p=0
p+=i
loop do
temp+=b[i][j]
i+=1
j+=1
break if i==1000
end
i=p
col << temp
temp=0
j=999
loop do
temp+=b[i][j]
i+=1
j-=1
break if i==1000
end
i=p
col << temp
end

1.upto(998) do |i|
j=0
p=0
p+=i
temp=0
loop do
temp+=b[j][i]
i+=1
j+=1
break if i==1000
end
i=p
col << temp
temp=0
j=0
loop do
temp+=b[j][i]
i-=1
j+=1
break if i<0
end
i=p
col << temp
end
col.delete(0)

ans=0
col.each {|i|  ans+=(i*(i-1))/2 }
puts "#{ans}"

n = gets.chomp.to_i
xs = []
ys = []
hs0 = Hash.new(0)
hs1 = Hash.new(0)
n.times do
  x, y = gets.chomp.split.map(&:to_i)
  hs0[x + y] += 1
  hs1[x - y] += 1
end

cnt = 0
hs0.each do |_, v|
  cnt += v * (v - 1) / 2
end
hs1.each do |_, v|
  cnt += v * (v - 1) / 2
end
puts cnt

r=0
h1=Hash.new(0)
h2=Hash.new(0)
gets.to_i.times{
	x,y=gets.split.map(&:to_i)
	r+=h1[x-y]
	h1[x-y]+=1
	r+=h2[x+y]
	h2[x+y]+=1
}
puts r

# def solve_stupid(a)
#   cnt = 0
#   a.each_with_index do |(xx,yy), i|
#     dd = xx - yy
#     ss = xx + yy
#     cnt += (0...i).count do |j|
#       x, y = a[j]
#       x + y == ss || x - y == dd
#     end
#   end
#   cnt
# end

def solve(a)
  diag1 = Hash.new(0)
  diag2 = Hash.new(0)
  a.each do |x, y|
    diag1[x+y] += 1
    diag2[x-y] += 1
  end
  (diag1.values + diag2.values).map do |v|
    v*(v-1)
  end.reduce(0, :+) / 2
end

n = gets.to_i
a = Array.new(n) do
  gets.split.map(&:to_i)
end

puts solve(a)

# def rnd
#   require 'set'
#   s = Set.new
#   Array.new(rand(1..100)) do
#     c = [rand(100), rand(100)]
#     until s.add?(c)
#       c = [rand(100), rand(100)]
#     end
#     c
#   end
# end

# p((0..1000).all?{ t = rnd; solve(t) == solve_stupid(t)})

n = gets.to_i
b = []
d = Array.new(2001, 0)
nd = Array.new(2001, 0)
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  pos = y-x
  neg = y+x
  d[999+ pos]+=1
  nd[neg]+=1
end

def bin(n, k)
  u = l = 1
  for v in 1..k
    u*=n+1-v
    l*=v
  end
  u/l
end
p = 0
for i in 0...2001
  p += bin(d[i].to_i, 2) + bin(nd[i].to_i, 2)
end

puts p

n = gets.chomp.to_i
coords1 = Hash.new(0)
coords2 = Hash.new(0)
n.times do
  coord = gets.chomp.split(" ").map(&:to_i)
  coords1[coord[1] - coord[0]] += 1
  coords2[coord[1] + coord[0]] += 1
end

def comb(n, k)
  return ((n - k + 1)..n).inject(:*) / (1..k).inject(:*)
end

res = 0
coords1.values.each do |v|
  res += comb(v, 2)
end
coords2.values.each do |v|
  res += comb(v, 2)
end

puts res

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0...(b.size-1)
  for i in (j+1)...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dx!=0 && (dy/dx).abs == 1
      p += 1
    end
  end
end

puts p

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0..b.size
  for i in (j+1)...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dy>0 && (dx/dy).abs == 1
      p += 2
    end
  end
end

puts p

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0...b.size
  for i in 0...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dy>0 && (dx/dy).abs == 1
      p += 1
    end
  end
end

puts p

n = gets.to_i
b = []
d = Array.new(2002, 0)
nd = Array.new(2002, 0)
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  d[1001 + x - y]+=1
  nd[1001 + y - x]+=1
end

def bin(n, k)
  u = l = 1
  for v in 1..k
    u*=n+1-v
    l*=v
  end
  u/l
end
p = 0
for i in 0...2002
  p += bin(d[i].to_i, 2) + bin(nd[i].to_i, 2)
end

puts p

# b = b.sort {|a,b| a[0] - b[0] }

# p=0
# for j in 0...(b.size-1)
#   for i in (j+1)...b.size
#     dx = b[j][0] - b[i][0]
#     dy = b[j][1] - b[i][1]
#     if dx!=0 && (dy/dx).abs == 1
#       p += 1
#     end
#   end
# end

# puts p

n = gets.to_i
b = []
d = Array.new(2000, 0)
nd = Array.new(2000, 0)
for _ in 0...n
  x, y = gets.split(' ').map &:to_i

  d[1000 + x - y]+=1
  nd[1000 + y - x]+=1
end

def bin(n, k)
  u = l = 1
  for v in 1..k
    u*=n+1-v
    l*=v
  end
  u/l
end
p = 0
for i in 0...2000
  if d[i] > 1
    p += bin(d[i], 2)
  end

  if nd[i] > 1
    p += bin(nd[i], 2)
  end
end

puts p

# b = b.sort {|a,b| a[0] - b[0] }

# p=0
# for j in 0...(b.size-1)
#   for i in (j+1)...b.size
#     dx = b[j][0] - b[i][0]
#     dy = b[j][1] - b[i][1]
#     if dx!=0 && (dy/dx).abs == 1
#       p += 1
#     end
#   end
# end

# puts p

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0..b.size
  for i in (j+1)...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dy>0 && (dx/dy).abs == 1
      p += 1
    end
  end
end

puts p

n = gets.chomp.to_i
coords = Hash.new(0)
n.times do
  coord = gets.chomp.split(" ").map(&:to_i)
  k = coord[1] % coord[0]
  coords[k] += 1
end

def comb(n, k)
  return ((n - k + 1)..n).inject(:*) / (1..k).inject(:*)
end

res = 0
coords.values.each do |v|
  res += comb(v, 2)
end

puts res


t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = gets.split.map(&:to_i)
  plus = false
  minus = false
  ok = true
  for i in 0...n do
    if b[i] > a[i]
      if !plus
        ok = false
        break
      end
    elsif b[i] < a[i]
      if !minus
        ok = false
        break
      end
    end
    plus = true if a[i] == 1
    minus = true if a[i] == -1
  end
  puts ok ? "YES" : "NO"
end

# https://codeforces.com/problemset/problem/1333/B

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

 first_plus_index = nil
 first_minus_index = nil

 possible = true
 n.times do |i|
  first_plus_index = i if first_plus_index.nil? && a[i] == 1
  first_minus_index = i if first_minus_index.nil? && a[i] == -1

  if a[i] != b[i] &&
   (i == 0 ||
   (b[i] < a[i] && (first_minus_index.nil? || first_minus_index == i)) ||
   (b[i] > a[i] && (first_plus_index.nil? || first_plus_index == i)))

   possible = false
   break
  end
 end

 puts possible ? "YES" : "NO"
end

# coding: utf-8

t = gets.to_i
Unit = Struct.new(:val, :n, :z, :p)
t.times {
  n = gets.to_i
  neg, zero, pos = 0, 0, 0
  ar = []
  gets.split.each { |i|
    i = i.to_i
    ar << Unit.new(i, neg, zero, pos)
    neg += 1 if i < 0
    zero += 1 if i == 0
    pos += 1 if i > 0
  }

  failed = false
  br = gets.split.map(&:to_i)
  # p ar
  until br.empty?
    b = br.pop # future state
    a = ar.pop # past state
    # from past to future
    if b == a.val
      next
    else
      if a.val < b
        if a.p < 1
          failed = true
          break
        end
      else
        if a.n < 1
          failed = true
          break
        end
      end
    end
  end

  # initial_state  ->next_state -> next_state -> goal

  if failed == true
    puts "NO"
  else
    puts "YES"
  end
}


require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  a = gets.chomp.split(//).map(&:to_i)
  index = a.index(0)
  if index.nil?
    puts 'cyan'
    next
  end
  a.delete_at(index)
  is_2 = a.any?{|e| e.even?}
  is_3 = a.inject(&:+) % 3 == 0
  puts (is_3 && is_2) ? 'red' : 'cyan'
end

gets.strip.to_i.times {
    x = gets.strip.split('')
    even = false
    zero1 = false
    zero2 = false
    odd = false
    sum = 0
    x.each { |i|
        w = i.to_i
        odd = true if w.odd?
        even = true if w.even? && w > 0
        zero2 = true if w == 0 && zero1
        zero1 = true if w == 0
        sum += w
    }
    if sum % 3 == 0 && ((!odd && !even && zero1) || (even && odd && zero1) || (even && zero1) || (odd && zero1 && zero2))
        puts "red"
    else
        puts "cyan"
    end
}

gets.strip.to_i.times {
    x = gets.strip.split('')
    even = false
    zero = false
    odd = false
    sum = 0
    x.each { |i|
        w = i.to_i
        odd = true if w.odd?
        even = true if w.even? && w > 0
        zero = true if w.even? && w == 0
        sum += w
    }
    if sum % 3 == 0 && ((!odd && !even && zero) || (even && odd && zero) || (even && zero))
        puts "red"
    else
        puts "cyan"
    end
}

gets.strip.to_i.times {
    x = gets.strip.split('')
    even = false
    zero = false
    odd = false
    sum = 0
    x.each { |i|
        w = i.to_i
        odd = true if w.odd?
        even = true if w.even? && w > 0
        zero = true if w.even? && w == 0
        sum += w
    }
    if sum % 3 == 0 && ((!odd && !even && zero) || (even && odd && zero) || (even && zero))
        puts "red"
    else
        puts "clan"
    end
}