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

q = gets.to_i
q.times do
  lrd = gets.chomp.split(" ").map(&:to_i)
  l = lrd[0]
  r = lrd[1]
  d = lrd[2]
  if d<l
    puts d
  else
    a = (r/d)*d
    while a<=r
      a += d
    end
    puts a
  end
end

gets
$<.map{|e|l,r,d=e.split.map &:to_i;p d+(d<l ? 0 : r/d*d)}

def solve(l, r, x)
  return x unless x.between?(l, r)
  return r - ((r + x) % x) + x
end

gets.strip.to_i.times do
l, r, x = gets.strip.split.map(&:to_i)
puts solve(l, r, x)
end

# Utility
def process(l, r, d)
  re = d
  while re >= l && re <= r
    re += d * (1 + (r - re) / d)
  end
  re
end

# Input
q = gets.to_i
result = Array.new(q)
q.times { |i| result[i] = process(*gets.split(" ").map { |e| e.to_i }) }

# Output
puts result

n = gets.to_i
n.times do
  l, r, d = gets.split(' ').map(&:to_i)
  if d < l
    puts d
  else
    t = (r + 1) * 1.0 / d
    puts t.ceil * d
  end
end

DBG = true
n = gets.to_i
for i in 0...n
  l,r,d = gets.split.map{|x| x.to_i}
  if d < l
    puts "#{d}"
    next
  end
  puts "#{((r+d)/d)*d}"
end

# You are given 𝑞 queries in the following form:

# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum
# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and
# it does not belong to the segment [𝑙𝑖,𝑟𝑖].

# Can you answer all the queries?

# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.

# Input
# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.

# Then 𝑞 lines follow, each containing a query given in
# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.

# Output
# For each query print one integer: the answer to this query.

# Example
# Input
# 5
# 2 4 2
# 5 10 4
# 3 10 1
# 1 2 3
# 4 6 5

# Output
# 6
# 4
# 1
# 3
# 10

def minimum_integer(queries)
  queries.each do |query|
    query = query.split(' ')

    return 0 unless query.length == 3

    low  = query.first.to_i
    high = query[1].to_i
    div  = query.last.to_i

    puts div < low ? div : high + (div - (high % div))
  end

  return nil
end

# Codeforces required setup
number_of_queries = gets.rstrip.to_i

queries = []

number_of_queries.times do |query|
  queries << gets.rstrip
end

result = minimum_integer queries
puts result

# You are given 𝑞 queries in the following form:

# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum
# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and
# it does not belong to the segment [𝑙𝑖,𝑟𝑖].

# Can you answer all the queries?

# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.

# Input
# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.

# Then 𝑞 lines follow, each containing a query given in
# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.

# Output
# For each query print one integer: the answer to this query.

# Example
# Input
# 5
# 2 4 2
# 5 10 4
# 3 10 1
# 1 2 3
# 4 6 5

# Output
# 6
# 4
# 1
# 3
# 10

MAX_NUMBER = 1_000_000_000

def minimum_integer(string)
  numbers = string.split(' ')

  return 0 unless numbers.length == 3

  low  = numbers.first.to_i
  high = numbers[1].to_i
  div  = numbers.last.to_i

  min_int = 0

  if low > 1
    for i in (1..(low - 1)) do
      if i % div == 0
        min_int = i
        break
      end
    end
  end

  return min_int if min_int > 0

  for i in ((high + 1)..MAX_NUMBER) do
    if i % div == 0
      min_int = i
      break
    end
  end

  return min_int
end

# Codeforces required setup
s = gets.rstrip

result = minimum_integer s
puts result

# You are given 𝑞 queries in the following form:

# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum
# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and
# it does not belong to the segment [𝑙𝑖,𝑟𝑖].

# Can you answer all the queries?

# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.

# Input
# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.

# Then 𝑞 lines follow, each containing a query given in
# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.

# Output
# For each query print one integer: the answer to this query.

# Example
# Input
# 5
# 2 4 2
# 5 10 4
# 3 10 1
# 1 2 3
# 4 6 5

# Output
# 6
# 4
# 1
# 3
# 10

MAX_NUMBER = 1_000_000_000

def minimum_integer(queries)
  queries.each do |query|
    query = query.split(' ')

    return 0 unless query.length == 3

    low  = query.first.to_i
    high = query[1].to_i
    div  = query.last.to_i

    min_int = 0

    if low > 1
      for i in (1..(low - 1)) do
        if i % div == 0
          min_int = i
          break
        end
      end
    end

    return min_int if min_int > 0

    for i in ((high + 1)..MAX_NUMBER) do
      if i % div == 0
        min_int = i
        break
      end
    end

    puts min_int.to_i
  end
end

# Codeforces required setup
number_of_queries = gets.rstrip.to_i

queries = []

number_of_queries.times do |query|
  queries << gets.rstrip
end

result = minimum_integer queries
puts result

# You are given 𝑞 queries in the following form:

# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum
# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and
# it does not belong to the segment [𝑙𝑖,𝑟𝑖].

# Can you answer all the queries?

# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.

# Input
# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.

# Then 𝑞 lines follow, each containing a query given in
# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.

# Output
# For each query print one integer: the answer to this query.

# Example
# Input
# 5
# 2 4 2
# 5 10 4
# 3 10 1
# 1 2 3
# 4 6 5

# Output
# 6
# 4
# 1
# 3
# 10

MAX_NUMBER = 1_000_000_000

def minimum_integer(queries)
  queries.each do |query|
    query = query.split(' ')

    return 0 unless query.length == 3

    low  = query.first.to_i
    high = query[1].to_i
    div  = query.last.to_i

    min_int = 0

    if low > 1
      for i in (1..(low - 1)) do
        if i % div == 0
          min_int = i
          break
        end
      end
    end

    if min_int == 0
      for i in ((high + 1)..MAX_NUMBER) do
        if i % div == 0
          min_int = i
          break
        end
      end
    end

    puts min_int.to_i
  end

  return nil
end

# Codeforces required setup
number_of_queries = gets.rstrip.to_i

queries = []

number_of_queries.times do |query|
  queries << gets.rstrip
end

result = minimum_integer queries
puts result

# You are given 𝑞 queries in the following form:

# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum
# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and
# it does not belong to the segment [𝑙𝑖,𝑟𝑖].

# Can you answer all the queries?

# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.

# Input
# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.

# Then 𝑞 lines follow, each containing a query given in
# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.

# Output
# For each query print one integer: the answer to this query.

# Example
# Input
# 5
# 2 4 2
# 5 10 4
# 3 10 1
# 1 2 3
# 4 6 5

# Output
# 6
# 4
# 1
# 3
# 10

MAX_NUMBER = 1_000_000_000

def minimum_integer(queries)
  queries.each do |query|
    query = query.split(' ')

    return 0 unless query.length == 3

    low  = query.first.to_i
    high = query[1].to_i
    div  = query.last.to_i

    min_int = 0

    if low > 1
      for i in (1..(low - 1)) do
        if i % div == 0
          min_int = i
          break
        end
      end
    end

    if min_int == 0
      for i in ((high + 1)..Float::INFINITY) do
        if i % div == 0
          min_int = i
          break
        end
      end
    end

    puts min_int.to_i
  end
end

# Codeforces required setup
number_of_queries = gets.rstrip.to_i

queries = []

number_of_queries.times do |query|
  queries << gets.rstrip
end

result = minimum_integer queries
puts result


t = gets.chomp.to_i
t.times do |testcase|
    a,b = gets.split.map(&:to_i)
    c = (a-b).abs
    puts ( c/5 + (c%5)/2 + (c%5)%2 )
end

t = gets.to_i
op = [-5, 2, -1, 1, 2, 5]
t.times do
  a, b = gets.split.map(&:to_i)
  ans = (a-b).abs/5
  a += (a-b) < 0 ? ans*5 : -ans*5
  if (a-b).abs == 0
  elsif (a-b).abs <= 2
    ans += 1
  else
    ans += 2
  end
  puts ans
end

def lol
	b, a = gets.chomp.split(" ").map { |e| e.to_i}
	if a > b
		a -= b
	else
		a = b - a
	end
	k = a/5
	if a%5 == 0
		p k
	elsif a%5 == 1
		p k + 1
	elsif a%5 == 2
		p k + 1
	elsif a%5 == 3
		p k + 2
	elsif a%5 == 4
		p k + 2
	end

end

n = gets.chomp.to_i

n.times do
	lol
end


t = gets.chomp.to_i

for i in 1..t
    n = gets.chomp.to_i
    s = gets.chomp

    p = 0
    c = 0
    for j in 0..n-1
        ch = s[j]
        if ch == '('
            c+=1
        else
            c-=1
        end

        if c < 0
            p+=1
            c = 0
        end
    end
    puts p
end

gets.to_i.times do
	n = gets.to_i
	s = gets.chomp
	while s.gsub!('()', ''); end
	puts s.size / 2
end

t = (gets || "").to_i
t.times do
	cnt = 0
	neg = 0
	n = (gets || "").to_i
	s = (gets || "").chomp

	s.each_char do |c|
		case c
		when '('
			cnt += 1
		when ')'
			cnt -= 1
		end
		if cnt < 0
			neg -= cnt
			cnt = 0
		end
	end

	p [neg, cnt].max
end

def run
  n = gets.chomp.to_i
  s = gets.chomp
  st = 0
  min_st = 0

  s.each_char do |ch|
    if ch == "("
      st += 1
    else
      st -= 1
      if st < min_st
        min_st = st
      end
    end
  end

  puts min_st.abs
end

def main
  t = gets.chomp.to_i

  t.times do
    run
  end
end

main

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
    mezirusi = 1
    count = 0
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            kakko = kakko.split("")
            ookisa = kakko.size
            if kakko == []
                kakko_size = 0
                break
            end
             mezirusi = 1
        else
            mezirusi += 1
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    kakko_size = kakko.size
    if  kakko.count("(") != kakko.count(")")
        puts "-1"
        next
    elsif kakko_size % 2 == 0
        puts kakko_size /2
        next
    end
end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
#p kakko
    mezirusi = 1
    #while
    count = 0
    #2.times do
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            #p kakko
            kakko = kakko.split("")
           # p kakko
            ookisa = kakko.size
           #kakko = kakko.delte(" ")
            #p kakko
            if kakko == []
                kakko_size = 0
                break
            end
            #mezirusi -= 1
            # if kakko[mezirusi] == nil && mezirusi >= 1
            #     mezirusi -= 1
            # end
             mezirusi = 1
        else
            mezirusi += 1
            #p mezirusi
            #puts "UU"
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    #p kakko.size
    #puts "UUUUUUUUU"
    kakko_size = kakko.size
    # p kakko
    # p kakko.count("(")
    if  kakko.count("(") != kakko.count(")")
        puts "-1"
        next
    elsif kakko_size % 2 == 0
        puts kakko_size /2
        next
    end

end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    kakko = gets.chomp.split("")
    mezirusi = 1
        while true
            if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
                kakko[mezirusi-1] = " "
                kakko[mezirusi] = " "
                kakko = kakko.join("").gsub(" ", "")
                kakko = kakko.split("")
                ookisa = kakko.size
                if kakko == []
                    kakko_size = 0
                    break
                end
                mezirusi = 1
            else
                mezirusi += 1
                if kakko[mezirusi] == nil
                    break
                end
            end
        end
    kakko_size = kakko.size
        if  kakko.count("(") != kakko.count(")")
            puts "-1"
            next
        else
            puts kakko_size /2
            next
        end
end

gets.to_i.times do
  ans = 0
  n = gets.to_i
  s = gets.chomp
  s.chars.inject(0) do |state, x|
    state += 1 if x == '('
    state -= 1 if x == ')'
    if state < 0
      state += 1
      ans += 1
    end
    state
  end
  puts ans
end

gets.to_i.times do
  gets
  a = gets.chomp.split('')
  result = 0

  while !a.size.zero?
    x = a.shift

    case x
    when '('
      a.delete_at(a.index(')')) if a.index(')')
    when ')'
      result += 1
    end
  end

  puts result
end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
p kakko
    mezirusi = 1
    #while
    count = 0
    #2.times do
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            #p kakko
            kakko = kakko.split("")
           # p kakko
            ookisa = kakko.size
           #kakko = kakko.delte(" ")
            #p kakko
            if kakko == []
                kakko_size = 0
                break
            end
            #mezirusi -= 1
            if kakko[mezirusi] == nil && mezirusi >= 1
                mezirusi -= 1
            end
            #p mezirusi
        else
            mezirusi += 1
            #p mezirusi
            #puts "UU"
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    #p kakko.size
    #puts "UUUUUUUUU"
    kakko_size = kakko.size
    if kakko_size % 2 == 1
        puts "-1"
    elsif kakko_size % 2 == 0
        puts kakko_size /2
    end

end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
#p kakko
    mezirusi = 1
    #while
    #count = 0
    #2.times do
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            #p kakko
            kakko = kakko.split("")
           # p kakko
            ookisa = kakko.size
           #kakko = kakko.delte(" ")
            #p kakko
            if kakko == []
                kakko_size = 0
                break
            end
            #mezirusi -= 1
            if kakko[mezirusi] == nil && mezirusi >= 1
                mezirusi -= 1
            end
            #p mezirusi
        else
            mezirusi += 1
            #p mezirusi
            #puts "UU"
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    #p kakko.size
    #puts "UUUUUUUUU"
    kakko_size = kakko.size
    # p kakko
    # p kakko.count("(")
    if  kakko.count("(") != kakko.count(")")
        puts "-1"
        next
    elsif kakko_size % 2 == 0
        puts kakko_size /2
        next
    end

end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
#p kakko
    mezirusi = 1
    #while
    count = 0
    #2.times do
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            #p kakko
            kakko = kakko.split("")
           # p kakko
            ookisa = kakko.size
           #kakko = kakko.delte(" ")
            #p kakko
            if kakko == []
                kakko_size = 0
                break
            end
            #mezirusi -= 1
            if kakko[mezirusi] == nil && mezirusi >= 1
                mezirusi -= 1
            end
            #p mezirusi
        else
            mezirusi += 1
            #p mezirusi
            #puts "UU"
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    #p kakko.size
    #puts "UUUUUUUUU"
    kakko_size = kakko.size
    # p kakko
    # p kakko.count("(")
    if  kakko.count("(") != kakko.count(")")
        puts "-1"
        next
    elsif kakko_size % 2 == 0
        puts kakko_size /2
        next
    end

end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
#p kakko
    mezirusi = 1
    #while
    count = 0
    #2.times do
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            #p kakko
            kakko = kakko.split("")
           # p kakko
            ookisa = kakko.size
           #kakko = kakko.delte(" ")
            #p kakko
            if kakko == []
                kakko_size = 0
                break
            end
            #mezirusi -= 1
            if kakko[mezirusi] == nil && mezirusi >= 1
                mezirusi -= 1
            end
            #p mezirusi
        else
            mezirusi += 1
            #p mezirusi
            #puts "UU"
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    #p kakko.size
    #puts "UUUUUUUUU"
    kakko_size = kakko.size
    # p kakko
    # p kakko.count("(")
    if  kakko.count("(") != kakko.count(")")
        puts "-1"
    elsif kakko_size % 2 == 0
        puts kakko_size /2
    end

end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
#p kakko
    mezirusi = 1
    #while
    count = 0
    #2.times do
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            #p kakko
            kakko = kakko.split("")
           # p kakko
            ookisa = kakko.size
           #kakko = kakko.delte(" ")
            p kakko
            if kakko == []
                kakko_size = 0
                break
            end
            #mezirusi -= 1
            # if kakko[mezirusi] == nil && mezirusi >= 1
            #     mezirusi -= 1
            # end
             mezirusi = 1
        else
            mezirusi += 1
            #p mezirusi
            #puts "UU"
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    #p kakko.size
    #puts "UUUUUUUUU"
    kakko_size = kakko.size
    # p kakko
    # p kakko.count("(")
    if  kakko.count("(") != kakko.count(")")
        puts "-1"
        next
    elsif kakko_size % 2 == 0
        puts kakko_size /2
        next
    end

end

n = gets.chomp.to_i
n.times do
    nn = gets.chomp.to_i
    arr = Array.new(nn,0)
    kakko = gets.chomp.split("")
#p kakko
    mezirusi = 1
    #while
    count = 0
    #2.times do
        while true
        if kakko[mezirusi-1] == "(" && kakko[mezirusi] == ")"
            kakko[mezirusi-1] = " "
            kakko[mezirusi] = " "
            kakko = kakko.join("").gsub(" ", "")
            #p kakko
            kakko = kakko.split("")
           # p kakko
            ookisa = kakko.size
           #kakko = kakko.delte(" ")
            #p kakko
            if kakko == []
                kakko_size = 0
                break
            end
            #mezirusi -= 1
            if kakko[mezirusi] == nil && mezirusi >= 1
                mezirusi -= 1
            end
            #p mezirusi
        else
            mezirusi += 1
            #p mezirusi
            #puts "UU"
            if kakko[mezirusi] == nil
                break
            end
        end

    end
    #p kakko.size
    #puts "UUUUUUUUU"
    kakko_size = kakko.size
    if kakko_size % 2 == 1
        puts "-1"
    elsif kakko_size % 2 == 0
        puts kakko_size /2
    end

end

class String
  def regular_bracket_sequence?
    if self.chars.inject(0) do |state, c|
      state += 1 if c == '('
      state -= 1 if c == ')'
      break false if state < 0
      state
    end then true else false end
  end
end
gets.to_i.times do
  ans = 0
  n = gets.to_i
  s = gets.chomp
  while not s.regular_bracket_sequence? do
    ans += 1
    idx = s.index(')')
    s[idx, idx + 1] = ''
    s << ')'
  end
  puts ans
end


m,n=gets.chomp.split.map(&:to_i)
t=[0,0,0,0,0]
m.times do
	a=gets.chomp.split.map(&:to_i)
	a.length.times {|j| (j!=0 && t[j]<t[j-1]) ? (t[j]=t[j-1]+a[j]) : (t[j]+=a[j]) }
	print "#{t[n-1]} "
end
puts

def run
  m, n = $stdin.gets.split.map(&:to_i)
  t = $stdin.readlines.map do |line|
    line.chomp.split.map(&:to_i)
  end

  a = []
  m.times do |i|
    a << []
  end

  count = 0
  m.times do |j|
    count += t[j][0]
    a[j][0] = count
  end

  1.upto(n - 1) do |i|
    m.times do |j|
      x = a[j][i - 1] || 0
      y = a[j - 1][i] || 0
      max = [x, y].max
      a[j][i] = max + t[j][i]
    end
  end

  puts a.map { |x| x.last}.join(" ")
end

run if $0 == __FILE__

m,n = gets.split(" ").map(&:to_i)
arr = []
m.times do
	arr << gets.split(" ").map(&:to_i)
end
dp = []

arr.each_with_index do |a,i|
	dp << []
	a.each_with_index do |ele,j|
		calc = [0]
		calc << dp[i][j-1] if j > 0
		calc << dp[i-1][j] if i > 0

		dp[i][j] = calc.max + arr[i][j]
	end
end

ans = []
dp.each do |a|
	ans << a[-1]
end
puts ans.join(" ")


t = gets.chomp.to_i
t.times do
  n, m = gets.chomp.split(' ').map(&:to_i)
  rows = []
  n.times do
    rows << gets.chomp.split(' ').map(&:to_i)
  end
  cols = []
  m.times do
    cols << gets.chomp.split(' ').map(&:to_i)
  end
  res = Array.new(n){[]}
  col_1 = cols.find {|el| el.include? rows[0][0]}
  hsh = {}
  col_1.each_with_index do |el, ind|
    hsh[el] = ind
  end
  rows.each do |row|
    res[hsh[row[0]]] = row
  end
  res.each do |el|
    puts el.join ' '
  end
end

t = gets.to_i
t.times do
  n, m = gets.split.map(&:to_i)
  rows = n.times.map { gets.split.map(&:to_i) }
  cols = m.times.map { gets.split.map(&:to_i) }
  x = rows[0][0]
  k = 0
  while cols[k].index(x).nil? do
    k += 1
  end
  n.times do |i|
    v = rows[i][0]
    pos = cols[k].index(v) + 1
    rows[i].unshift(pos)
  end
  rows.sort! do |e, f|
    e[0] <=> f[0]
  end
  n.times do |i|
    rows[i].shift
    puts rows[i].join(" ")
  end
end


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

diff = 0

turn = 1

(2*n).times do

  if a[-1].nil?
    amax = 0
  else
    amax = a[-1]
  end

  if b[-1].nil?
    bmax = 0
  else
    bmax = b[-1]
  end
=begin
  puts turn
  puts amax
  p a
  puts bmax
  p b
  puts
=end
  if turn==1
    if amax>bmax
      diff += amax
      a.pop
    else
      b.pop
    end
  else
    if amax>bmax
      a.pop
    else
      diff -= bmax
      b.pop
    end
  end

  turn = 3-turn

end

puts diff

n = gets.strip.to_i
a1 = gets.strip.split.map(&:to_i)
a2 = gets.strip.split.map(&:to_i)

a1.sort!
a2.sort!

s1=s2=0
i1=i2=n-1
t=0

while i1>=0 || i2>=0 do
  if i1>=0 && i2>=0
    if a1[i1] > a2[i2]
      if t.even?
        s1 += a1[i1]
      end
      i1 -= 1
    else
      if t.odd?
        s2 += a2[i2]
      end
      i2 -= 1
    end
  else
    if i1<0
      s2 += a2[i2] if t.odd?
      i2 -= 1
    end
    if i2<0
      s1 += a1[i1] if t.even?
      i1 -= 1
    end
  end
  t += 1
end

puts s1-s2


def main
  n = gets.chomp.to_i

  left = {}
  gets.chomp.chars.each_with_index {|c, i| if left[c].nil? then left[c] = [i + 1] else left[c] << i + 1 end}

  right = {}
  gets.chomp.chars.each_with_index {|c, i| if right[c].nil? then right[c] = [i + 1] else right[c] << i + 1 end}

  pairs = []

  for l in left.keys
    if l != '?'
      while left.key?(l) and right.key?(l)
        pairs << [left[l].last, right[l].last]
        left[l].pop
        right[l].pop
        left.delete(l) if left[l].empty?
        right.delete(l) if right[l].empty?
      end
    end
  end

  for l in left.keys
    while left.key?(l) and right.key?('?')
      pairs << [left[l].last, right['?'].last]
      left[l].pop
      right['?'].pop
      left.delete(l) if left[l].empty?
      right.delete('?') if right['?'].empty?
    end
  end

  for r in right.keys
    while right.key?(r) and left.key?('?')
      pairs << [left['?'].last, right[r].last]
      left['?'].pop
      right[r].pop
      left.delete('?') if left['?'].empty?
      right.delete(r) if right[r].empty?
    end
  end

  if left.key('?')
    while left.key('?') and right.key?('?')
      pairs << [left['?'].last, right['?'].last]
      left['?'].pop
      right['?'].pop
      left.delete('?') if left['?'].empty?
      right.delete('?') if right['?'].empty?
    end
  end

  puts pairs.size
  for p in pairs
    puts p.join(" ")
  end
end

main

N = gets.to_i
L = gets.chomp
R = gets.chomp
lh,rh = (0..1).map{
  h = {}
  ("a".."z").each{ |c| h[c] = [] }
  h["?"] = []
  h
}
(1..N).each{ |i|
  cl = L[i-1]
  lh[cl] << i
  cr = R[i-1]
  rh[cr] << i
}
ans = []
("a".."z").each{ |c|
  while lh[c].size > 0 && rh[c].size > 0 do
    ans << [lh[c].pop, rh[c].pop]
  end
}
("a".."z").each{ |c|
  while lh[c].size > 0 && rh["?"].size > 0 do
    ans << [lh[c].pop, rh["?"].pop]
  end
  while lh["?"].size > 0 && rh[c].size > 0 do
    ans << [lh["?"].pop, rh[c].pop]
  end
}
while lh["?"].size > 0 && rh["?"].size > 0 do
  ans << [lh["?"].pop, rh["?"].pop]
end
p ans.size
ans.each{ |l,r| puts "#{l} #{r}" }

n = gets.to_i
a = gets.chomp
b = gets.chomp
l = Hash.new{|h,k|h[k] = []}
r = Hash.new{|h,k|h[k] = []}
n.times do |i|
    l[a[i]].push(i+1)
    r[b[i]].push(i+1)
end
ans = []
s = "abcdefghijklmnopqrstuvwxyz?"
26.times do |i|
    t = [l[s[i]].size,r[s[i]].size].min
    t.times do |j|
        ans << [l[s[i]][-1],r[s[i]][-1]]
        l[s[i]].pop
        r[s[i]].pop
    end
end
26.times do |i|
    t = [l[s[i]].size,r[s[-1]].size].min
    t.times do |j|
        ans << [l[s[i]][-1],r[s[-1]][-1]]
        l[s[i]].pop
        r[s[-1]].pop
    end
end
26.times do |i|
    t = [l[s[-1]].size,r[s[i]].size].min
    t.times do |j|
        ans << [l[s[-1]][-1],r[s[i]][-1]]
        l[s[-1]].pop
        r[s[i]].pop
    end
end
t = [l[s[-1]].size,r[s[-1]].size].min
t.times do |i|
    ans << [l[s[-1]][i],r[s[-1]][i]]
end
puts ans.size
ans.each do |arr|
    puts arr.join(" ")
end


n = gets.to_i
a = gets.split(" ").map(&:to_i)
r = (n - a[0]) % n
a.each_with_index{|x, i|
    if (x + r * (i.even? ? 1 : -1)) % n != i
        puts 'No'
        exit
    end
}
puts 'Yes'

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

x = a[0]
dir = 1

for i in (0...n)
	a[i] += dir * x

	if a[i] < 0
		a[i] += n
	elsif a[i] >= n
		a[i] -= n
	end

	dir *= -1
end

ok = true

for i in (0...n)
	if a[i] != i
		ok = false
		break
	end
end

if ok == true
	puts "Yes"
else
	dir = -1
	for i in (0...n)
		a[i] += dir * 2 * x

		while a[i] < 0
			a[i] += n
		end

		while a[i] >= n
			a[i] -= n
		end

		dir *= -1
	end

	ok = true

	for i in (0...n)
		if a[i] != i
			ok = false
			break
		end
	end

	puts ok == true ? "Yes" : "No"
end

gearNum = gets.chomp.to_i
active = gets.chomp.split(' ').map { |item| item.to_i}
flag = true
loopCount = 0

def proceedSwitch(active = nil, gearNum)
=begin
	active.each do |item|
		print "#{item} "
	end

	puts ""
=end
	for i in 0...active.length
		active[i] += 1 if i % 2 == 0
		active[i] -= 1 if i % 2 == 1

		active[i] = 0 if active[i] == gearNum
		active[i] = gearNum - 1 if active[i] < 0
	end
end

loop do
	proceedSwitch(active, gearNum)

	for i in 0...active.length
		flag = false if active[i] != i
		break if flag == false
	end

	break if loopCount == gearNum
	break if flag == true

	loopCount += 1
	flag = true
end

puts "YES" if flag
puts "NO" if !flag

#!/usr/bin/ruby
N=gets.to_i
D,*A=gets.split.map(&:to_i)
puts (1...N).all?{|i|i==(A[i-1]+[-D,D][i&1])%N} ? :Yes : :No

def function
arr=[]
a=gets.chomp.to_i
b=gets.chomp
arr=b.split.map(&:to_i)
l=arr.length
m=true
l.times do |i|
if arr==Array(0..arr.length-1)
m=false
break
end
0.upto l-1 do |i|
arr[i]=(arr[i]+1)%l if i%2==0
arr[i]=(arr[i]-1)%l if i%2!=0
end
end
if m==true
print "No"
else
print "Yes"
end
end
function

n = gets.to_i
gears = gets.split(' ')
gears = gears.map{|i| i.to_i}
shifts = n - gears.first
stack = []
real = nil
gears.each_with_index do |gear, index|
    if index%2==0
        new_num = (gear+shifts)%n
    else
        new_num = (gear-shifts)%n
    end
    if stack.empty?
        stack.push(new_num)
    else
        if stack.last == new_num - 1
            stack.push(new_num)
        else
            real = 0
            break
        end
    end
end
if real == 0
    puts 'No'
else
    puts 'Yes'
end

n = Integer(gets)
nums = gets.chomp.split
a = []
for i in 0...n
    a[i] = Integer(nums[i])
end

gear = []
gear[0] = a
for i in 1...n
    gear[i] = []
    for j in 0...n
        if j % 2 == 0
            if gear[i-1][j] + 1 == n
                gear[i].push(0)
            else
                gear[i].push(gear[i-1][j] + 1)
            end
        else
            if gear[i-1][j] == 0
                gear[i].push(n-1)
            else
                gear[i].push(gear[i-1][j] - 1)
            end
        end
    end
end
pointer = 0
for i in 0...n
    for j in 0...n-1
        if gear[i][j + 1] > gear[i][j]
            pointer = 1
        else
            pointer = 0
            break
        end
    end
    if pointer == 1
        break
    end
end
if n == 1
    puts "YES"
else
    if pointer == 0
        puts "NO"
    else
        puts "YES"
    end
end

n = gets.to_i
a = gets.split.map(&:to_i)
v = a[0]
n.times do |i|
  a[i] += i.odd? ? v : -v
  a = a.map{|x| x % n}
end
puts a == (0...n).to_a ? 'Yes' : 'No'

n = gets.to_i
m, *a = gets.split.map(&:to_i)
(n - 1).times{|i| a[i] += i.odd? ? -m : m}
a = a.map{|x| x % n}
puts a == (1...n).to_a ? 'Yes' : 'No'

def function
arr=[]
a=gets.chomp.to_i
b=gets.chomp
arr=b.split.map(&:to_i)
l=arr.length
m=true
l.times do |i|
if arr==Array(0..arr.length-1)
m=false
break
end
0.upto l-1 do |i|
arr[i]=(arr[i]+1)%l if i%2==0
arr[i]=(arr[i]-1)%l if i%2!=0
end
end
if m==true
p "No"
else
p "Yes"
end
end
function

n = gets.to_i
gears = gets.split(' ')
gears = gears.map{|i| i.to_i}
shifts = n - gears.first
stack = []
real = nil
gears.each_with_index do |gear, index|
	if index%2==0
		new_num = (gear+shifts)%n
	else
		new_num = (gear-shifts)%n
	end
	if new_num == 0
		stack.push(new_num)
	else
		if stack.last == new_num - 1
			stack.push(new_num)
		else
			real = 0
			break
		end
	end
end
if real == 0
	puts 'No'
else
	puts 'Yes'
end

n = Integer(gets)
nums = gets.chomp.split
a = []
for i in 0...n
    a[i] = Integer(nums[i])
end

gear = []
gear[0] = a
for i in 1...n
    gear[i] = []
    for j in 0...n
        if j % 2 == 0
            if gear[i-1][j] + 1 == n
                gear[i].push(0)
            else
                gear[i].push(gear[i-1][j] + 1)
            end
        else
            if gear[i-1][j] == 0
                gear[i].push(n-1)
            else
                gear[i].push(gear[i-1][j] - 1)
            end
        end
    end
end
pointer = 0
for i in 0...n
    for j in 0...n-1
        if gear[i][j + 1] > gear[i][j]
            pointer = 1
        else
            pointer = 0
            break
        end
    end
    if pointer == 1
        break
    end
end
if pointer == 0
    puts "NO"
else
    puts "YES"
end

n = gets.to_i
a = gets.split.map(&:to_i)
ans = false
v = a[0]
n.times do |i|
  a[i] += i.odd? ? v : -v
  a = a.map{|x| x % n}
  ans = true if a == (0...n).to_a
end
puts ans ? 'Yes' : 'No'


def debug(var)
	p "debug #{var}" if ARGV[0]=="LOCAL_DEV"
end

Q=STDIN.gets.to_i
Q.times do
	n=STDIN.gets.to_i
	n.times do
		print "1 "
	end
	print "\n"
end

n = gets.chomp.to_i
n.times do
    arr = []
    count = 0
    t = gets.chomp.to_i
    1.upto(t-1) do |i|
        arr << 1
        count += 1
    end
    arr << t - count
    puts arr.join(" ")
end

def debug(var)
	p "debug #{var}" if ARGV[0]=="LOCAL_DEV"
end

Q=STDIN.gets.to_i
Q.times do
	n=STDIN.gets.to_i
	n.times do
		print "1 "
	end
	print '\n'
end


class Summand
  attr_accessor :coeff, :incr
end

class Solution
  def initialize()
    @summands = []
    @a = 0
  end

  def parse_summand(s)
    match_data = /^(\+|-)?([0-9]*)\*?(a\+\+|\+\+a)/.match(s)
    sign  = match_data[1].nil?? '+' : match_data[1]
    coeff = match_data[2].empty?? 1 : match_data[2].to_i
    coeff = -coeff if sign == '-'
    incr = match_data[3] == 'a++' ? 0 : 1 # 0 - post, 1 - pre

    summand = Summand.new
    summand.coeff = coeff
    summand.incr  = incr
    @summands << summand

    s[match_data[0].size..-1]
  end

  def solve()
    @a = gets.to_i
    s = gets.chomp
    while not s.empty?
      s = parse_summand(s)
    end
    @summands.sort_by!{|s| s.coeff}

    result = 0
    a = @a
    @summands.each do |s|
      a += s.incr
      result += s.coeff * a
      a += (1 - s.incr)
    end
    puts result
  end
end

solution = Solution.new
solution.solve()


n = gets.to_i
ary = gets.strip.split.map(&:to_i)

if n==1
  puts 1
  exit
end

hsh = {}
0.upto(n-1).each do |i|
  x = ary[i]
  if hsh[x].nil?
    hsh[x]=i
  else
    hsh[x]=-1
  end
end
possible = {}
to_delete = 0
maxai = ((10**9) + 0)

0.upto(n-1).each do |i|
  x = ary[i]
  if possible[x].nil?
    tp = 2
    while true do
      if x < tp
        rem = tp - x
        if rem > maxai
          possible[x] = false
          break
        end
        hsh_rem = hsh[rem]
        if hsh_rem!=nil && (hsh_rem==-1 || hsh_rem!=i)
          possible[x] = true
          break
        end
      end
      tp = (tp << 1)
    end
  end
  to_delete += 1 if possible[x] == false
end

puts to_delete

require 'set'

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

x = 1
z = []
while (x<= 1e10)
	z << x
	x *= 2
end

map_a = {}
a.each do |x|
	map_a[x] ||= 0
	map_a[x] += 1
end

ac = 0
a.each do |i|
	flag = 1
	z.each do |j|
		if ((j-i == i) && map_a[i] && (map_a[i] >= 2)) || ((j-i != i) && map_a[j-i] && (map_a[j-i] >= 1))
			flag = 0
		end
	end
	ac += flag
end

print ac

POWERS = (0..31).map { |i| 2**i }

def is_good(arr)
  elem_indices = Hash.new { |h, k| h[k] = [] }

  arr.each_with_index do |x, i|
    elem_indices[x] << i
  end

  count_to_remove = 0

  arr.each_with_index do |x, i|

    good = POWERS.any? { |pow|
      complement = pow - x
      complement > 0 && elem_indices[complement].any? { |j| j != i }
    }

    count_to_remove += 1 unless good

  end

  count_to_remove

end

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

puts(is_good(arr))

require 'set'

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

x = 1
z = []
while (x<= 1e9)
	z << x
	x *= 2
end

map_a = {}
a.each do |x|
	map_a[x] ||= 0
	map_a[x] += 1
end

ac = 0
a.each do |i|
	flag = 1
	z.each do |j|
		if ((j-i == i) && map_a[i] && (map_a[i] >= 2)) || ((j-i != i) && map_a[j-i] && (map_a[j-i] >= 1))
			flag = 0
		end
	end
	ac += flag
end

print ac


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

min = data.min
if data.all? { |i| i % k == min % k }
    puts data.map { |i| (i - min) / k }.reduce(0, :+)
else
    puts -1
end

#!/usr/bin/ruby
n,k,*a=$<.read.split.map(&:to_i)
m=a.min
a=a.map{|e|e-m}
if a.all?{|e|e%k==0}
	p a.reduce(0){|s,e|s+e/k}
else
	p -1
end

# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

minval = a.min

cnt = 0

a.each do |e|
    d = e - minval
    if d % k != 0
        puts -1
        exit
    end
    cnt += d / k
end

puts cnt

n, k, *aa = $<.read.split.map(&:to_i)
b = aa.min
aa = aa.map {|a| a -= b}
p (aa.all? {|a| a % k == 0}) ? aa.inject(:+) / k : -1


ss = gets.chomp
if ss.slice(ss.length-1) == "."
  puts "NO"
  exit
end
s = ss.split(".")
ret = []
pp = ""
for i in 0..s.length-1
   if s[i].length <= 0
      ret = nil
      break
   end
   if i == 0
    if s[i].length > 8
      ret = nil
      break
    end
    pp = s[i]
  elsif i == s.length-1
    if s[i].length > 3
      ret = nil
      break
    end
    ret << pp + "." + s[i]
  else
    if s[i].length > 11 || s[i].length <= 1
      ret = nil
      break
    end
    if s[i].length == 2
      ret << pp + "." + s[i].slice(0,1)
      pp = s[i].slice(1,1)
    elsif s[i].length == 3
      ret << pp + "." + s[i].slice(0,2)
      pp = s[i].slice(2,1)
    else
      ret << pp + "." + s[i].slice(0,3)
      pp = s[i].slice(3,s[i].length-3)
    end
   end
end

if ret == nil || ret.length == 0
    puts "NO"
else
    puts "YES"
    for i in 0..ret.length-1
      puts ret[i]
    end
end

s = gets.chomp.split(".")
ret = []
pp = ""
for i in 0..s.length-1
   if s[i].length <= 0
      ret = nil
      break
   end
   if i == 0
    if s[i].length > 8
      ret = nil
      break
    end
    pp = s[i]
  elsif i == s.length-1
    if s[i].length > 3
      ret = nil
      break
    end
    ret << pp + "." + s[i]
  else
    if s[i].length > 11 || s[i].length <= 1
      ret = nil
      break
    end
    if s[i].length == 2
      ret << pp + "." + s[i].slice(0,1)
      pp = s[i].slice(1,1)
    elsif s[i].length == 3
      ret << pp + "." + s[i].slice(0,2)
      pp = s[i].slice(2,1)
    else
      ret << pp + "." + s[i].slice(0,3)
      pp = s[i].slice(3,s[i].length-3)
    end
   end
end

if ret == nil
    puts "NO"
else
    puts "YES"
    for i in 0..ret.length-1
      puts ret[i]
    end
end

ss = gets.chomp
if ss.slice(ss.length-1) == "."
  puts "NO"
  exit
end
s = ss.split(".")
ret = []
pp = ""
for i in 0..s.length-1
   if s[i].length <= 0
      ret = nil
      break
   end
   if i == 0
    if s[i].length > 8
      ret = nil
      break
    end
    pp = s[i]
  elsif i == s.length-1
    if s[i].length > 3
      ret = nil
      break
    end
    ret << pp + "." + s[i]
  else
    if s[i].length > 11 || s[i].length <= 1
      ret = nil
      break
    end
    if s[i].length == 2
      ret << pp + "." + s[i].slice(0,1)
      pp = s[i].slice(1,1)
    elsif s[i].length == 3
      ret << pp + "." + s[i].slice(0,2)
      pp = s[i].slice(2,1)
    else
      ret << pp + "." + s[i].slice(0,3)
      pp = s[i].slice(3,s[i].length-3)
    end
   end
end

if ret == nil
    puts "NO"
else
    puts "YES"
    for i in 0..ret.length-1
      puts ret[i]
    end
end


def doubler(a)
  k = 0
  while a % 2 == 0
    k += 1
    a /= 2
  end
  return k
end

n = gets.chomp.to_i
arr = gets.chomp.split(" ")

arr.each_index do |i|
  arr[i] = arr[i].to_i
end

maxD = 0
arr.each_index do |i|
  maxD = doubler(arr[i]) if i == 0
  maxD = doubler(arr[i]) if arr[i] % (2 ** maxD) == 0

end
k = 0
arr.each{ |iM|
  k += 1 if iM % (2 ** maxD) == 0
}

puts "#{2 ** maxD} #{k}"

#!/usr/bin/env ruby
n = gets.to_i
a = gets.split.map(&:to_i)
pow2 = [1]
i = 0
ret = 1
ret2 = 0
while pow2.max < a.max do
  pow2[i] = 2**(i+1)
  i += 1
end
for i in 0..pow2.length-1
  for j in 0..n-1
    if a[j] % pow2[i] == 0 and pow2[i] > ret then ret = pow2[i] end
  end
end
for i in 0..n-1
  if a[i] % ret == 0 then ret2 += 1 end
end
printf "%d %d", ret, ret2
puts


n,x=gets.split.map(&:to_i); x-=1
a=gets.split.map(&:to_i)

min=a.min
a.map! {|i| i-min}

count=min*n
while a[x]>0
  a[x]-=1
  x-=1
  count+=1
end

a[x]=count

puts a*' '

infile = STDIN
n, end_index = infile.readline.split.map(&:to_i); end_index -= 1
a = infile.readline.split.map(&:to_i)
t = a.min
a.map! { |i| i-t }
c = 0
while a[end_index] > 0 do
  a[end_index] -= 1
  end_index -= 1
  end_index = n-1 if end_index < 0
  c += 1
end # c changes
a[end_index] = c + n*t
puts a*' '

infile = STDIN
n, end_index = infile.readline.split.map(&:to_i); end_index -= 1
a = infile.readline.split.map(&:to_i)
t = a.min
a.map! { |i| i-t }
c = 0
while a[end_index] > 0 do
  a[end_index] -= 1
  end_index -= 1
  end_index = n-1 if end_index < 0
  c += 1
end
a[end_index] = c + n*t
puts a*' '

infile = STDIN
n, end_index = infile.readline.split.map(&:to_i); end_index -= 1
a = infile.readline.split.map(&:to_i)

temp = end_index.downto(end_index-n+1).map { |i| a[i] }
begin_index = end_index - temp.index(temp.min)
begin_index = n + begin_index if begin_index < 0
t = a[begin_index]
a.map! { |i| i-t } # n changes
c = 0
while end_index != begin_index do
  a[end_index] -= 1
  end_index -= 1
  end_index = n-1 if end_index < 0
  c += 1
end # c changes
a[begin_index] = c + n*t
puts a*' '

n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=2000000000
k=-1
x-=1
for i in 0..n-1 do
    t=(x-i+n)%n
    if m > a[t] then
        k=t
        m=a[t]
    end
end

for i in 0..n-1 do
    a[i] = i==k ? m*n : a[i]-m
end
t=(n+x-k)%n
for i in 1..t do
    a[(k+i)%n]-=1
    a[k]+=1
end

puts a*' '

n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=2000000000
k=-1
x-=1
for i in 0..n-1 do
    t=(x-i+n)%n
    if m > a[t] then
        k=t
        m=a[t]
    end
end

for i in 0..n-1 do
    a[i] = i==k ? m*n : a[i]-m
end
t=(n+x-k)%n
for i in 1..t do
    a[(k+i)%n]-=1
    a[k]+=1
end

a.each do |v|
    print "#{v} "
end
puts

n,x=gets.split.map(&:to_i); x-=1
a=gets.split.map(&:to_i)

min=a.min
a.map! {|i| i-min}

count=min*n
while a[x]>0
  a[x]-=1
  x-=1
  count+=1
end

a[x]=count

a.each {|v| print v,' '}

n,x=gets.split.map(&:to_i)
x-=1

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

min=a.min
a.length.times {|i| a[i]-=min}

count=min*n
while a[x]>0
  a[x]-=1
  x-=1
  count+=1
end

a[x]=count

a.each {|v| print "#{v} "}

BEGIN{
def main()
	n, x = gets.split.map(&:to_i)
	a = gets.split.map(&:to_i)
	x -= 1
	m = a.min
	a.each_with_index do |v,i|
		a[i] -= m
	end
	sum = 0
	rx = x
	while a[x] != 0 do
		a[x] -= 1
		sum += 1
		x -= 1
	end
	a[x] = m * n + sum
	a.each do |p|
		print p,' '
	end
	puts
end
1.times do
	main()
end
}

n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=a.min
k=a.index{|v| v==m}
k=x-1 if a[x-1]==a[k]

if n==48 && x==34 then
    for i in 20..n-1 do
        print "#{a[i]} "
    end
end

for i in 0..n-1 do
    a[i] = i==k ? m*n : a[i]-m
end
t=(n+x-1-k)%n;
a[k]+=t
for i in 1..t do
    a[(i+k)%n]-=1
end
a.each do |v|
    print "#{v} "
end
puts

n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=a.min
k=a.index{|v| v==m}
if m > 0 then
    for i in 0..n-1 do
        if i==k then
            print "#{m*n} "
        else
            print "#{a[i]-m} "
        end
    end
else
    a[(k+1)%n]-=1
    a[k]=1;
    a.each do |v|
        print "#{v} "
    end
end
puts

n,x=gets.split.map(&:to_i)
x-=1

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

min=a.min
start=a.index(min)

if a[start]==a[x]
  if start<x
    start=x
  end
end

a.length.times do |i|
  a[i]-=min
  if x>start && (start+1..x)===i
    a[i]-=1
  end

  if x<start && (i<=x || i>start)
    a[i]-=1
  end
end

a[start]=min*n

if(x>start)
  a[start]+=x-start
elsif(x<start)
  a[start]+=n-(start-x)
end

a.each {|v| print "#{v} "}

n,x=gets.split.map(&:to_i)
x-=1

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

min=a.min
start=a.index(min)

a.length.times do |i|
  a[i]-=min
  if x>start && (start+1..x)===i
    a[i]-=1
  end

  if x<start && (i<=x || i>start)
    a[i]-=1
  end
end

a[start]=min*n

if(x>start)
  a[start]+=x-start
elsif(x<start)
  a[start]+=n-(start-x)
end

a.each {|v| print "#{v} "}

n,x=gets.split.map(&:to_i)
x-=1

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

min=a.min
start=a.index(min)
if a[start]==a[x]
  start=x
end

a.length.times do |i|
  a[i]-=min
  if x>start && (start+1..x)===i
    a[i]-=1
  end

  if x<start && (i<=x || i>start)
    a[i]-=1
  end
end

a[start]=min*n

if(x>start)
  a[start]+=x-start
elsif(x<start)
  a[start]+=n-(start-x)
end

a.each {|v| print "#{v} "}

n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=a.min
k=a.index{|v| v==m}
for i in 0..n-1 do
    a[i] = i==k ? m*n : a[i]-m
end
t=(n+x-1-k)%n;
a[k]+=t
for i in 1..t do
    a[(i+k)%n]-=1
end
a.each do |v|
    print "#{v} "
end
puts

n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=a.min
k=a.index{|v| v==m}
k=x-1 if a[x-1]==a[k]

for i in 0..n-1 do
    a[i] = i==k ? m*n : a[i]-m
end
t=(n+x-1-k)%n;
a[k]+=t
for i in 1..t do
    a[(i+k)%n]-=1
end
a.each do |v|
    print "#{v} "
end
puts


gets.chomp.to_i
a = gets.chomp.split(/ /).map(&:to_i)
tests = Array.new(3001, 0)
a.each do |i|
    tests[i - 1] += 1
end
puts tests.index(0) + 1

n = -1
indexes = []
chars = []

ARGF.each_with_index do |line, i|

	#read number of indexes provided
	n = line if i == 0

	#read indexes
	chars = line.split(' ') if i == 1

end

#transfer characters to array of integers

chars.each do |c|
	indexes.push(c.to_i)
end

#sort indexes

indexes = indexes.sort

result = -1

if indexes.count > 1

	for i in 0..(indexes.count - 1)

		if indexes[(i+1)].to_i - indexes[i].to_i > 1

			#there is space for a new index
			result = indexes[i].to_i + 1
			break;

		end
	end

else

	if indexes[0].to_i == 1

		#first index is not available, next availble will be first index + 1
		result = indexes[(indexes.count - 1)].to_i + 1;

	else

		#first index is available
		result = 1
	end

end

if result == -1

	#no available space was found, check for minimum value

	if indexes[0].to_i == 1

		#first index is one, if no space was found in the chain, then next available is max value + 1
		result = indexes[(indexes.count - 1)].to_i + 1;

	else

		#first index is not equal to 1 so 1 is available
		result = 1
	end

end

puts result

n = -1
indexes = []
chars = []

ARGF.each_with_index do |line, i|

	#read number of indexes provided
	n = line if i == 0

	#read indexes
	chars = line.split(' ') if i == 1

end

#transfer characters to array of integers

chars.each do |c|
	indexes.push(c.to_i)
end

#sort indexes

indexes = indexes.sort

result = -1

if indexes.count > 1

	for i in 0..(indexes.count - 1)

		if indexes[( i+ 1)].to_i - indexes[i].to_i > 1

			#there is space for a new index
			result = indexes[i].to_i + 1
			break;

		end
	end

else

	if indexes[0].to_i == 1

		#first index is not available, next availble will be first index + 1
		result = indexes[(indexes.count - 1)].to_i + 1;

	else

		#first index is available
		result = 1
	end

end

if result == -1

	#no available space was found, check for minimum value

	if indexes[0].to_i == 1

		#first index is one, if no space was found in the chain, then next available is max value + 1
		result = indexes[(indexes.count - 1)].to_i + 1;

	else

		#first index is not equal to 1 so 1 is available
		result = 1
	end

end

puts result

n = gets.to_i

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

t = true
i = 1
while t && i <= n do
    if !nums.delete(i) then
        result = i
        t = false
    end
    i += 1
end

if t then
    result = n + 1
end

puts result

n=gets.to_i
a=Array.new(n+1,0)
gets.chomp.split.map(&:to_i).each {|x| a[x]=1}
for i in 1..n
	if a[i]==0
		puts i
		exit
	end
end
puts n+1

s = (1..3001).to_a
n = gets.to_i
a = gets.split.map{|r| r.to_i}
s = s - a
puts s[0]

def main()
	n=gets.chomp.to_i
	stin=gets.chomp.split(" ",n)
	stin=stin.map{|i| i.to_i}
	stin=stin.sort()
	j=1;f=0;
	for i in 0...(stin.length)
		if stin[i]==j
			j+=1;
		else
			f=1;
			puts j
			break;
		end
	end
	if f==0
		puts j
	end
end
main

n = gets.to_i
a = gets.split(" ").map(&:to_i)
a.sort!
ans = nil

a.each_with_index do |x,i|
	if x != i + 1
		ans = i + 1
		break
	end
end

ans = a[-1] + 1 unless ans
puts ans

gets
puts ((1..3001).to_a - gets.chomp.split.map(&:to_i))[0]

gets
p ((1..3001).to_a - gets.split.map(&:to_i)).min

n = -1
indexes = []

ARGF.each_with_index do |line, i|

	#read number of indexes provided
	n = line if i == 0

	#read indexes
	indexes = line.split(' ') if i == 1

end

#sort indexes
indexes = indexes.sort

result = -1

if indexes.count > 1

	0.upto(indexes.count-1) do |i|

		if indexes[i+1].to_i - indexes[i].to_i > 1

			#there is space for a new index
			result = indexes[i].to_i + 1
			break;

		end
	end

else

	if indexes[0].to_i > 1

		#first index is available
		result = 1

	else

		#first element is equal to 1, assign next to 2
		result = 2
	end

end

if result == -1

	#no available space was found, check for minimum value

	if indexes[0].to_i == 1

		#first index is one, if no space was found in the chain, then next available is max value + 1
		result = indexes[indexes.count -1].to_i + 1;

	else

		#first index is not equal to 1 so 1 is available
		result = 1
	end

end

puts result

n = -1
indexes = []

ARGF.each_with_index do |line, i|

	#read number of indexes provided
	n = line if i == 0

	#read indexes
	indexes = line.split(' ') if i == 1

end

#sort indexes
indexes = indexes.sort

result = -1

if indexes.count > 1

	0.upto(indexes.count-1) do |i|

		if indexes[i+1].to_i - indexes[i].to_i > 1

			#there is space for a new index
			result = indexes[i].to_i + 1
			break;

		end
	end

else

	if indexes[0].to_i == 1

		#first index is not available, next availble will be first index + 1
		result = indexes[indexes.count -1].to_i + 1;

	else

		#first index is available
		result = 1
	end

end

if result == -1

	#no available space was found, check for minimum value

	if indexes[0].to_i == 1

		#first index is one, if no space was found in the chain, then next available is max value + 1
		result = indexes[indexes.count -1].to_i + 1;

	else

		#first index is not equal to 1 so 1 is available
		result = 1
	end

end

puts result

n = -1
indexes = []

ARGF.each_with_index do |line, i|

	#read number of indexes provided
	n = line if i == 0

	#read indexes
	indexes = line.split(' ') if i == 1

end

#sort indexes
indexes = indexes.sort

result = -1

if indexes.count > 1

	0.upto(indexes.count) do |i|

		if indexes[i+1].to_i - indexes[i].to_i > 1

			#there is space for a new index
			result = indexes[i].to_i + 1
			break;

		end
	end

else

	if indexes[0].to_i == 1

		#first index is not available, next availble will be first index + 1
		result = indexes[indexes.count -1].to_i + 1;

	else

		#first index is available
		result = 1
	end

end

if result == -1

	#no available space was found, check for minimum value

	if indexes[0].to_i == 1

		#first index is one, if no space was found in the chain, then next available is max value + 1
		result = indexes[indexes.count -1].to_i + 1;

	else

		#first index is not equal to 1 so 1 is available
		result = 1
	end

end

puts result

n = -1
indexes = []

ARGF.each_with_index do |line, i|

	#read number of indexes provided
	n = line if i == 0

	#read indexes
	indexes = line.split(' ') if i == 1

end

#sort indexes
indexes = indexes.sort

result = -1

if indexes.count > 1

	0.upto((indexes.count-1)) do |i|

		if indexes[(i+1)].to_i - indexes[i].to_i > 1

			#there is space for a new index
			result = indexes[i].to_i + 1
			break;

		end
	end

else

	if indexes[0].to_i == 1

		#first index is not available, next availble will be first index + 1
		result = indexes[(indexes.count - 1)].to_i + 1;

	else

		#first index is available
		result = 1
	end

end

if result == -1

	#no available space was found, check for minimum value

	if indexes[0].to_i == 1

		#first index is one, if no space was found in the chain, then next available is max value + 1
		result = indexes[(indexes.count - 1)].to_i + 1;

	else

		#first index is not equal to 1 so 1 is available
		result = 1
	end

end

puts result

n=gets.to_i
a=Array.new(n+1,0)
gets.chomp.split.map(&:to_i).each {|x| a[x]=1}
print a
puts
for i in 1..n
	if a[i]==0
		puts i
		exit
	end
end
puts n+1

n=gets.to_i
b=Array.new(n,1)
n.times { |i| b[i]=i+1}
a=gets.chomp.split.map(&:to_i).sort
n.times do |i|
	if a[i]!=b[i]
		puts b[i]
		break
	end
end

s = (1..3000).to_a
n = gets.to_i
a = gets.split.map{|r| r.to_i}
s = s - a
puts s[0]

def main()
	n=gets.chomp.to_i
	stin=gets.chomp.split(" ",n)
	stin=stin.map{|i| i.to_i}
	stin=stin.sort()
	j=1;
	for i in 0...(stin.length)
		if stin[i]==j
			j+=1;
		else
			puts j
			break;
		end
	end
end
main

gets
nums = gets.chomp.split.map(&:to_i)

puts ((1..3000).to_a - nums)[0]

gets
nums = gets.chomp.split.map(&:to_i)[0..2999]

puts ((1..3000).to_a - nums)[0]


def solve(n, k1, k2, w, b)
  w_o = (k1 + 1) / 2 + k2 / 2
  w_e = k1 / 2 + (k2 + 1) / 2

  k3, k4 = n - k1, n - k2
  b_o = (k3 + 1) / 2 + k4 / 2
  b_e = k3 / 2 + (k4 + 1) / 2

  w <= min(w_o, w_e) && b <= min(b_o, b_e)
  #(w_o == w_e and b_o == b_e and w <= w_o and b <= b_o)
end

def main
  t = int
  t.times do
    n, k1, k2 = ints
    w, b = ints
    yesno solve(n, k1, k2, w, b)
  end
end

DEBUG = true
MOD = 10**9+7
YESNO = %w(NO YES)
INF = 10**9

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 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 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 * mod_inv(y, mod) % 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
end

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

main

gets.to_i.times do
  n, k1, k2 = gets.split(' ').map(&:to_i)
  w, b = gets.split(' ').map(&:to_i)

  white_count = [k1, k2].min + ((k1-k2).abs / 2)
  left_w_c = (k1-k2).abs % 2 # this may be 1 or 0

  black_count = [n-k1, n-k2].min + (((n-k1) - (n-k2)).abs / 2)
  left_b_c = ((n-k1) - (n-k2)).abs % 2 # this may be 1 or 0

  puts white_count >= w && black_count >= b ? 'YES' : 'NO'
end


t = gets.to_i
t.times do
  played = 0
  cleared = 0
  ok = true
  n = gets.to_i
  n.times do
    p, c = gets.split.map(&:to_i)
    ok = false if p < c
    ok = false if p < played
    ok = false if c < cleared
    ok = false if (p - played) < (c - cleared)
    played = p
    cleared = c
  end
  puts ok ? "YES" : "NO"
end

def run
  n = gets.chomp.to_i
  pp, pc = *[0, 0]

  a = []

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

  a.each do |p, c|
    if p >= pp && c >= pc && (p - pp) >= (c - pc)
      pp = p
      pc = c
    else
      puts 'no'
      return
    end
  end

  puts 'yes'
end

def main
  t = gets.chomp.to_i

  t.times do
    run
  end
end

main

n = STDIN.gets.to_i
n.times do |i|
  j = STDIN.gets.to_i
  sets = []
  j.times do
    match = STDIN.gets.strip.match(/(\d+) (\d+)/)
    sets << [match[1].to_i, match[2].to_i]
  end
  r = 0
  c = 0
  skip = false
  for a, b in sets do
    if (a < r) or (b < c) or (b > a) or ((a == r) and (b > c)) or ((a - r) < (b - c))
      puts 'NO'
      skip = true
      break
    else
      r = a
      c = b
    end
  end
  puts 'YES' unless skip
end

gets.to_i.times do
  prev = [0, 0]
  ok = true

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

    ok &&= (a.last  - prev.last)  >= 0
    ok &&= (a.first - prev.first) >= (a.last - prev.last)

    prev = a
  end

  puts ok ? 'YES' : 'NO'
end

t = gets.chomp.to_i
while t>0 do
    n = gets.chomp.to_i
    las_a = 0
    las_b = 0
    ok = true
    for i in 1..n do
        tmp = gets.chomp.split(" ")
        a = tmp[0].to_i
        b = tmp[1].to_i
        if b-las_b>a-las_a or b<las_b or a<las_a then
            ok = false
        end
        las_a = a
        las_b = b
    end
    if ok then
        puts "YES"
    else
        puts "NO"
    end
    t -= 1
end

n = STDIN.gets.to_i
n.times do |i|
  j = STDIN.gets.to_i
  sets = []
  j.times do
    match = STDIN.gets.strip.match(/(\d+) (\d+)/)
    sets << [match[1].to_i, match[2].to_i]
  end
  p = 0
  c = 0
  skip = false
  for a, b in sets do
    if (a < p) or (b < c) or (b > a)
      puts 'NO'
      skip = true
      break
    else
      p = a
      c = b
    end
  end
  puts 'YES' unless skip
end

t = gets.to_i
t.times do
  played = 0
  cleared = 0
  ok = true
  n = gets.to_i
  n.times do
    p, c = gets.split.map(&:to_i)
    ok = false if p < c
    ok = false if p < played
    ok = false if c < cleared
    played = p
    cleared = c
  end
  puts ok ? "YES" : "NO"
end


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

  x,y = y,x if x > y
  puts [a*x + a*y, b*x + a*(y-x)].min
end

gets.to_i.times {
    x, y=gets.split.map &:to_i
    a, b=gets.split.map &:to_i

    s=x.abs
    t=y.abs
    puts [s*a+t*a, s*b+(y-x).abs*a, t*b+(x-y).abs*a].min
}

g = gets.chomp.to_i
g.times{|w|
x,y = gets.split(" ").map{|x|x.to_i}
a,b = gets.split(" ").map{|x|x.to_i}
all = []

x1=0
y1=0

x1+=x
y1+=y

z=0
#method 1
loop do
if (x==0 && y==0)
break
else

if (x > 0 && y > 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.min*b)
x-=hh.min
y-=hh.min

if (x!=0)
z+=x*a
x-=x
end
if (y!=0)
z+=y*a
y-=y
end
end

if (x < 0 && y < 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.max.abs*b)
x+=hh.max.abs
y+=hh.max.abs

if (x!=0)
z+=x.abs*a
x+=x.abs
end
if (y!=0)
z+=y.abs*a
y+=y.abs
end
end

if (x<0)
z+=x.abs*a
x+=x.abs
end
if (x>0)
z+=x*a
x-=x
end
if (y<0)
z+=y.abs*a
y+=y.abs
end
if (y>0)
z+=y*a
y-=y
end

end
end
all.push(z)

z=0
x=x1
y=y1
loop do
if (x==0 && y==0)
break
else

if (x<0)
z+=x.abs*a
x+=x.abs
end
if (x>0)
z+=x*a
x-=x
end
if (y<0)
z+=y.abs*a
y+=y.abs
end
if (y>0)
z+=y*a
y-=y
end

end
end
all.push(z)

puts all.min
}

t = gets.chomp.to_i
t.times do
	x,y  = gets.chomp.split(" ").map(&:to_i)
	a, b = gets.chomp.split(" ").map(&:to_i)
	if 2 * a < b then
		puts a * (x + y)
	else
		diff = (x - y).abs
		puts ([x,y].max - diff) * b + a * diff
	end
end

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

  puts [
    [x, y].min * b + (x - y).abs * a,
    x * a + y * a
  ].min
end

def solution
  x, y = gets.split.map(&:to_i)
  a, b = gets.split.map(&:to_i)

  return 0 if x == 0 && y == 0

  if b > 2 * a
    return (x + y) * a
  end

  min = [x, y].min
  max = [x, y].max
  min * b + (max - min) * a
end

t = gets.to_i
t.times do
  p solution
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.chomp.to_i
t.times do
  x, y =  file.gets.chomp.split.map(&:to_i)
  a, b =  file.gets.chomp.split.map(&:to_i)

  if a == 0 || b == 0
    puts 0
    next
  end

  if x==y
    puts [x*b, a*2*x].min
  elsif x > y
    c=a*(x-y)+b*y
    d=a*(x-y)+2*a*y
    puts [c,d].min
  else
    c=a*(y-x)+b*x;
    d=a*(y-x)+2*a*x;
    puts [c,d].min
  end
end

def run
  x, y = gets.chomp.split(" ").map(&:to_i)
  a, b = gets.chomp.split(" ").map(&:to_i)

  r = if a * 2 < b || x == 0 ||  y == 0
    (x + y) * a
  else
    (x - y).abs * a + [x, y].min * b
  end
  puts r
end

def main
  t = gets.chomp.to_i

  t.times do
    run
  end
end

main

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.chomp.to_i
t.times do
  x, y =  file.gets.chomp.split.map(&:to_i)
  a, b =  file.gets.chomp.split.map(&:to_i)
  res = 0

  if x==y
    res+= [x*b, a*2*x].min
  else
    d = (x-y).abs
    res += d * a
    res += ([x,y].max-d)*b
  end
  puts res
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.chomp.to_i
t.times do
  x, y =  file.gets.chomp.split.map(&:to_i)
  a, b =  file.gets.chomp.split.map(&:to_i)

  if x==y
    puts [x*b, a*2*x].min
  else
    res = 0
    d = (x-y).abs
    res += d * a
    res += ([x,y].max-d)*b
    res2 = (d*a) +(b*[x,y].max)
    puts [res, res2].min
  end
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.chomp.to_i
t.times do
  x, y =  file.gets.chomp.split.map(&:to_i)
  a, b =  file.gets.chomp.split.map(&:to_i)

  if a == 0 || b == 0
    puts 0
    next
  end

  if x==y
    puts [x*b, a*2*x].min
  else
    res = 0
    d = (x-y).abs
    res += d * a
    res += ([x,y].max-d)*b
    res2 = (d*a) +(b*[x,y].max)
    # puts "#{[res, res2]}"
    puts [res, res2].min
  end
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

t = file.gets.chomp.to_i
t.times do
  x, y =  file.gets.chomp.split.map(&:to_i)
  a, b =  file.gets.chomp.split.map(&:to_i)

  if a == 0 || b == 0
    puts 0
    next
  end

  if x==y
    puts [x*b, a*2*x].min
  else
    d = (x-y).abs

    res = (d*a) + ([x,y].max-d)*b

    # res2 = (d*a) +(b*[x,y].max)
    # puts "#{[res, res2]}"
    # puts [res, res2].min
    puts res
  end
end

g = gets.chomp.to_i
g.times{|w|
x,y = gets.split(" ").map{|x|x.to_i}
a,b = gets.split(" ").map{|x|x.to_i}
all = []

z=0
#method 1
loop do
if (x==0 && y==0)
break
else

if (x > 0 && y > 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.min*b)
x-=hh.min
y-=hh.min

if (x!=0)
z+=x*a
x-=x
end
if (y!=0)
z+=y*a
y-=y
end
end

if (x < 0 && y < 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.max*b)
x+=hh.max.abs
y+=hh.max.abs

if (x!=0)
z+=x.abs*a
x+=x.abs
end
if (y!=0)
z+=y.abs*a
y+=y.abs
end
end

if (x<0)
z+=x.abs*a
x+=x.abs
end
if (x>0)
z+=x*a
x-=x
end
if (y<0)
z+=y.abs*a
y+=y.abs
end
if (y>0)
z+=y*a
y-=y
end

end
end

puts z
}

g = gets.chomp.to_i
g.times{|w|
x,y = gets.split(" ").map{|x|x.to_i}
a,b = gets.split(" ").map{|x|x.to_i}
all = []

z=0
#method 1
loop do
if (x==0 && y==0)
break
else

if (x > 0 && y > 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.min*b)
x-=hh.min
y-=hh.min

if (x!=0)
z+=x*a
x-=x
end
if (y!=0)
z+=y*a
y-=y
end
end

if (x < 0 && y < 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.max.abs*b)
x+=hh.max.abs
y+=hh.max.abs

if (x!=0)
z+=x.abs*a
x+=x.abs
end
if (y!=0)
z+=y.abs*a
y+=y.abs
end
end

if (x<0)
z+=x.abs*a
x+=x.abs
end
if (x>0)
z+=x*a
x-=x
end
if (y<0)
z+=y.abs*a
y+=y.abs
end
if (y>0)
z+=y*a
y-=y
end

end
end

puts z
}

g = gets.chomp.to_i
g.times{|w|
x,y = gets.split(" ").map{|x|x.to_i}
a,b = gets.split(" ").map{|x|x.to_i}
all = []

z=0
#method 1
if (x==0 && y==0)

else

if (x > 0 && y > 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.min*b)
x-=hh.min
y-=hh.min

if (x!=0)
z+=x*a
x-=x
end
if (y!=0)
z+=y*a
y-=y
end
end

if (x < 0 && y < 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.max*b)
x+=hh.max
y+=hh.max

if (x!=0)
z+=x.abs*a
x+=x
end
if (y!=0)
z+=y.abs*a
y+=y
end
end

end
puts z
}

g = gets.chomp.to_i
g.times{|w|
x,y = gets.split(" ").map{|x|x.to_i}
a,b = gets.split(" ").map{|x|x.to_i}
all = []

z=0
#method 1
loop do
if (x==0 && y==0)
break
else

if (x > 0 && y > 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.min*b)
x-=hh.min
y-=hh.min

if (x!=0)
z+=x*a
x-=x
end
if (y!=0)
z+=y*a
y-=y
end
end

if (x < 0 && y < 0)
hh=[]
hh.push(x)
hh.push(y)
z+=(hh.max*b)
x+=hh.max
y+=hh.max

if (x!=0)
z+=x.abs*a
x+=x
end
if (y!=0)
z+=y.abs*a
y+=y
end
end

if (x<0)
z+=x.abs*a
x+=x.abs
end
if (x>0)
z+=x*a
x-=x
end
if (y<0)
z+=y.abs*a
y+=y.abs
end
if (y>0)
z+=y*a
y-=y
end

end
end

puts z
}


def sum(arr)
  ret = 0
  arr.each do |i|
    ret += i
  end
  return ret
end

n = gets.to_i
a = gets.split.map {|i| i.to_i}
b = gets.split.map {|i| i.to_i}.sort
if sum(a) <= sum(b.last(2)) then
	puts "YES"
else
	puts "NO"
end

gets
a=gets.split.map{|e| e.to_i}
b=gets.split.map{|e| e.to_i}.sort.reverse
if b[0]+b[1]-a.inject(0){|sum,e| sum+e}<0 then
puts "NO"
else
puts "YES"
end

n = Integer(gets.chomp)

volume_line = gets.chomp.split(" ").to_a.map{ |i| Integer(i) }
value = volume_line.inject(0){ |v, i| v + i }

cups_line = gets.chomp.split(" ").to_a.map{ |i| Integer(i) }.sort.reverse

print (value <= cups_line[0] + cups_line[1]) ? "YES" : "NO"

n = gets
n = n.to_i

arr = Array.new(n){Array.new(2){0}}

s = gets
s.split(" ").map(&:to_i).each.with_index do |v, index|
  arr[index][0] = v
end

s = gets
s.split(" ").map(&:to_i).each.with_index do |v, index|
  arr[index][1] = v
end

first_max = -Float::INFINITY
second_max = -Float::INFINITY

sum_volumes = 0

arr.each do |v,c|
  sum_volumes += v
  if c >= first_max
    second_max = first_max
    first_max = c
  elsif c > second_max
    second_max = c
  end
end

if sum_volumes <= (first_max + second_max )
  puts "YES"
else
  puts "NO"
end

n = gets.to_i
a = gets.split.map(&:to_i).inject(:+)
b = gets.split.map(&:to_i).sort
puts a <= b[-1] + b[-2] ? "YES" : "NO"

n = gets.to_i
a = gets.split.map &:to_i
b = gets.split.map &:to_i
puts a.inject(0){|s, x| s + x} <= b.sort[-2] +  b.sort[-1] ? 'YES' : 'NO'

n= gets.to_i

remaining = gets.split(" ").map(&:to_i).inject(0, :+)
capacities = gets.split(" ").map(&:to_i)
max1 = capacities.max
capacities.delete_at capacities.index(max1)
max2 = capacities.max

if max1+max2 >= remaining
	print('YES')
else
	print('NO')
end

gets
a=gets.split.map(&:to_i).reduce :+
b=gets.split.map(&:to_i).sort[-2..-1].reduce :+
puts a<=b ? :YES : :NO

def solve(n, cans, sizes)
  total = cans.inject(:+)
  free = sizes.sort.reverse.first(2).inject(:+)
  free >= total ? "YES" : "NO"
end

puts solve(gets.strip.to_i, gets.strip.split.map(&:to_i), gets.strip.split.map(&:to_i))

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

def solve
  $b.sort_by! { |x| x }
  if ($a.inject(0, :+) <= $b[-2..-1].inject(0, :+))
    return "YES"
  end
  return "NO"
end

print solve


# coding: utf-8

# bogosort 정렬이 아니란 거죠.
# i < j 인 인덱스 페어에 대하여 i - ai != j - aj
# 예를 들어 a = [1, 1, 3, 5] 에 대해서
# (1, 2) 는 1 - 1 != 2 - 1 로 만족이지만 (1, 3)은 1 - 1 == 3 - 3이므로
# 성립하지 않음.
# 배열의 최소 크기는 1

def bogosort(a)
    ar = a.sort.reverse
    return ar
end

t = gets.to_i
t.times {
    n = gets.to_i
    a = gets.split.map(&:to_i)
    puts bogosort(a).join(' ')
}

 gets.to_i.times do
     gets
     puts gets.split.map(&:to_i).sort.reverse.join(' ')
 end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  l = gets.chomp.split.map(&:to_i)
  #r = []
  r = l.sort.reverse
  print r.join(' ')
  puts
end

T = gets.to_i

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

  puts a.sort.reverse.join(' ')
end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  print gets.chomp.split.map(&:to_i).sort.reverse.join(' ')
  puts
end

 gets.to_i.times do
     gets
     puts gets.split.sort.reverse.join(' ')
 end

def check(a)
  (0...a.size).each do |i|
    state = true
    (i+1...a.size).each do |j|
      (state = false; break;) if j - a[j] == i - a[i]
    end
    return true if state
  end
  false
end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  l = gets.chomp.split.map(&:to_i)
  r = []
  l.sort.permutation.each do |a|
    (r = a; break;) if check(a)
  end
  print r.join(' ')
  puts
end

def check(a)
  (0...a.size).each do |i|
    state = true
    (i+1...a.size).each do |j|
      (state = false; break;) if j - a[j] == i - a[i]
    end
    return true if state
  end
  false
end

t = gets.chomp.to_i
(0...t).each do |_|
  n = gets.chomp.to_i
  l = gets.chomp.split.map(&:to_i)
  r = []
  l.permutation.each do |a|
    r = a if check(a)
  end
  print r.join(' ')
  puts
end


M, N = gets.split.map(&:to_i)
wrong = Array.new(N)

N.times do |i|
    puts 1
    $stdout.flush
    result = gets.to_i
    exit if result == 0
    wrong[i] = (result != 1)
end

ub = M
lb = 2
60.times do |i|
    mid = (ub+lb)/2
    puts mid
    $stdout.flush
    result = gets.to_i
    exit if result == 0
    if (result == 1) ^ wrong[i%N]
        lb = mid + 1
    else
        ub = mid - 1
    end
end

q = 60
m, n = gets.strip.split.map(&:to_i)
seq=Array.new(n)

q-=n

n.times do |i|
  puts 1
  STDOUT.flush
  seq[i] = gets.to_i
  exit(0) if seq[i]==0
end

lo=1
hi=m
si=0
while lo<=hi
  mid=((lo+hi)/2)
  puts mid
  STDOUT.flush
  res = gets.to_i
  res *= seq[si]
  si+=1
  si%=n
  if res==0 || res.abs==2
    exit 0
  elsif res==1
    lo=mid+1
  else
    hi=mid
  end
end

N, M = gets.split.map(&:to_i)
wrong = Array.new(N)

N.times do |i|
    puts 1
    $stdout.flush
    result = gets.to_i
    exit if result == 0
    wrong[i] = (result == 1)
end

ub = M
lb = 2
N.times do |i|
    mid = (ub+lb)/2
    puts mid
    $stdout.flush
    result = gets.to_i
    exit if result == 0
    if (result == 1) ^ wrong[i]
        lb = mid + 1
    else
        ub = mid - 1
    end
end

M, N = gets.split.map(&:to_i)
wrong = Array.new(N)

N.times do |i|
    puts 1
    $stdout.flush
    result = gets.to_i
    exit if result == 0
    wrong[i] = (result != 1)
end

ub = M
lb = 2
N.times do |i|
    mid = (ub+lb)/2
    puts mid
    $stdout.flush
    result = gets.to_i
    exit if result == 0
    if (result == 1) ^ wrong[i]
        lb = mid + 1
    else
        ub = mid - 1
    end
end

q = 60
m, n = gets.strip.split.map(&:to_i)
seq=Array.new(n)

q-=n

n.times do |i|
  puts 0
  STDOUT.flush
  seq[i] = gets.to_i
end

lo=1
hi=m
si=0
while lo<=hi
  mid=((lo+hi)/2)
  puts mid
  STDOUT.flush
  res = gets.to_i
  res *= seq[si]
  si+=1
  si%=n
  if res==0 || res.abs==2
    exit 0
  elsif res==1
    lo=mid+1
  else
    hi=mid
  end
end

q = 60
m, n = gets.strip.split.map(&:to_i)
seq=Array.new(n)

q-=n

n.times do |i|
  puts -1
  STDOUT.flush
  seq[i] = gets.to_i
end

lo=1
hi=m
si=0
while lo<=hi
  mid=((lo+hi)/2)
  puts mid
  STDOUT.flush
  res = gets.to_i
  res *= seq[si]
  si+=1
  si%=n
  if res==0 || res.abs==2
    exit 0
  elsif res==1
    lo=mid+1
  else
    hi=mid
  end
end


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

gets.to_i.times do
    n, k = gets.split.map &:to_i
    k *= (n + k - 1) / k
    puts (k + n - 1) / n
end

_t = gets.to_s.to_i
_t.times do
  n, k = gets.to_s.split.map{ |e| e.to_i }
  k *= (n + k - 1) / k if n > k
  puts (k + n - 1) / n
end


q = gets.to_i
(1..q).each do
  n = gets.to_i
  count = Array.new(n + 10, 0)
  candies = gets.split(" ").map{|c| c.to_i}
  candies.each {|candy| count[candy] += 1}
  hash = {}
  res = []
  count.each do |cnt|
    tmp = cnt
    while tmp > 0 && hash[tmp] == 1
      tmp -= 1
    end
    res << tmp
    hash[tmp] = 1
  end
  sum = 0
  res.each {|rs| sum += rs}
  puts sum
end

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 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
########### ( ˘ω˘ )ｽﾔｧ… ###########
q=gi
q.times do
  n=gi
  a=gsmi
  h=Hash.new(0)
  a.each do |i|
    h[i]+=1
  end
  #puts h.to_s
  ar = desc(h.to_a.map{|j|j[1]})
  sum=0
  prev=2*10**5+1
  ar.size.times do |i|
    c=min(prev-1,ar[i])
    break if c<=0
    sum+=c
    prev=c
  end
  puts sum
end

q = gets.chomp.to_i

for ii in 1..q
  gets.chomp.to_i
  tab = gets.chomp.split(" ").map {|el| el.to_i}
  better = {}
  tab.each do |el|
    better[el] = 0 unless better.key?(el)
    better[el] += 1
  end

  better = better.sort_by {|k,v| -v}
  ans = 0
  last = 2000000

  better.each do |k, val|
    last = val >= last ? last-1 : val
    last = last < 0 ? 0 : last
    ans += last
  end

  print "#{ans}\n"
end

q = gets.chomp.to_i

for ii in 1..q
  gets.chomp.to_i
  tab = gets.chomp.split(" ").map {|el| el.to_i}
  better = {}
  tab.each do |el|
    better[el] = 0 unless better.key?(el)
    better[el] += 1
  end

  # better = better.sort_by(&:last).reverse
  ans = 0
  last = 2000000

  better.each do |k, val|
    last = val >= last ? last-1 : val
    last = last < 0 ? 0 : last
    ans += last
  end

  print "#{ans}\n"
end

q = gets.to_i
(1..q).each do
  n = gets.to_i
  count = Array.new(1000000, 0)
  candies = gets.split(" ").map{|c| c.to_i}
  candies.each {|candy| count[candy] += 1}
  hash = Array.new(1000000, 0)
  res = Array.new(1, 0)
  count.each do |cnt|
    tmp = cnt
    while tmp > 0 && hash[tmp] == 1
      tmp -= 1
    end
    #res.append(tmp)
    hash[tmp] = 1
  end
  sum = 0
  res.each {|rs| sum += rs}
  puts sum
end


n_and_m = gets.chomp
squares = gets.chomp

n_and_m = n_and_m.split(" ")

n = n_and_m[0].to_i
m = n_and_m[1].to_i

squares = squares.split(" ")
squares.map! {|s| s.to_i}

platform = Array.new(n, 0)

squares.each do |square|
  platform[square - 1] += 1
end

score = 0
total = 0
length = platform.length

while true
  platform.map {|s| total += 1 if !s.equal?(0) }
  break if !total.equal?(length)
  score += 1
  platform.map! {|s| s -= 1}
  total = 0
end

puts score

n, m = gets.split.map(&:to_i)
ar = gets.split.map(&:to_i)
counts = Array.new(n)
n.times{|i| counts[i] = ar.count{|a| a == i+1}}
puts counts.include?(nil) ? 0 : counts.min

#bismillahir rahmanir rahim

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

num = gets.split.map{ |x| x.to_i }

x=num.count(1)

for i in 2..n
    y = num.count(i)
    x=y if x>y
    #puts i,x,y
end

puts x

=begin
for i in 0...m
    puts num[i]
end
=end

n,m,*a=$<.read.split.map &:to_i
x=[0]*n
a.each{|e|x[e-1]+=1}
p x.min

n_and_m = gets.chomp
squares = gets.chomp

n_and_m = n_and_m.split(" ")
puts n_and_m.inspect
n = n_and_m[0].to_i
m = n_and_m[1].to_i

squares = squares.split(" ")
squares.map! {|s| s.to_i}

platform = Array.new(n, 0)

#puts squares.inspect
#puts "el tamaño del platform es : #{platform.length}"
squares.each do |square|
  platform[square - 1] += 1
end

score = 0
total = 0
length = platform.length

while true
  platform.map {|s| total += 1 if !s.equal?(0) }
  break if !total.equal?(length)
  score += 1
  platform.map! {|s| s -= 1}
  total = 0
end

puts score


n,m,k=gets.split.map(&:to_i)
a=Array.new(m)
n.times{r,c=gets.split.map(&:to_i)
r-=1
a[r]=a[r] ? [a[r],c].min : c}
b=a.inject:+
p b>k ? k : b

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

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

hs=Hash.new(10**9)
n.times{
	r, c=gets.split.map(&:to_i)
	hs[r]=[c, hs[r]].min
}

puts [hs.values.inject(:+), k].min

s = gets.split(" ")
n = s[0].to_i
m = s[1].to_i
k = s[2].to_i

row = Array.new()
for i in 1..m
  row[i] = 9999999
end

for i in 1..n
  s = gets.split(" ")
  key = s[0].to_i
  value = s[1].to_i
  if (value < row[key])
    row[key] = value
  end
end

sum = 0
for i in 1..m
  sum += row[i]
end

puts sum < k ? sum : k

s = gets.chomp
ns,ms,ks = s.split(/ /)
n = ns.to_i
m = ms.to_i
k = ks.to_i
h = Hash.new
for i in 1..n
	s2 = gets.chomp
	rs,cs = s2.split(/ /)
	r = rs.to_i
	c = cs.to_i

	if h.has_key?(r)
		h[r] = c if h[r] > c
	else
		h[r] = c
	end
end
sum = 0
h.each {|key,val|
	sum += val
}
ret = [sum,k].min
puts ret

n, m, k = gets.split.map(&:to_i)
a = [nil] * n
n.times do
	r, c = gets.split.map(&:to_i)
	r -= 1
	a[r] = a[r] ? [a[r], c].min : c
end
p [a.compact.inject(:+), k].min


gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  as = gets.split.map(&:to_i)
  gets
  bs = gets.split.map(&:to_i)

  powers = (0..30).map { |i| [m ** i, i] }.to_h

  split = []
  as.each do |a|
    aa = a
    aa /= m while aa % m == 0
    x, y = aa, a / aa
    if split.last && split.last[0] == x
      split.last[1] += y
    else
      split << [x, y]
    end
  end

  ok = true
  bs.each do |b|
    if split.empty?
      ok = false
      break
    end
    x, y = split[0]
    if b % x == 0 && p = powers[b / x]
      if y >= b / x
        split[0][1] -= b / x
      else
        ok = false
      end
    else
      ok = false
    end
    split.shift if split[0][1] == 0
  end

  ok &&= split.empty?

  puts ok ? "YES" : "NO"
end

split = ->xs, m {
  chunks = []
  xs.each do |x|
    xx = x
    xx /= m while xx % m == 0
    n, count = xx, x / xx
    if chunks.last && chunks.last[0] == n
      chunks.last[1] += count
    else
      chunks << [n, count]
    end
  end
  chunks
}

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  as = gets.split.map(&:to_i)
  gets
  bs = gets.split.map(&:to_i)

  ok = split[as, m] == split[bs, m]

  puts ok ? "YES" : "NO"
end

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

def f(x, m)
  cnt = 1
  while x % m == 0
    x /= m
    cnt *= m
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    num, cnt = f(a[i], m)
    if ac.empty?
      ac << [num, cnt]
    elsif ac[-1][0] == num
      ac[-1][1] += cnt
    else
      ac << [num, cnt]
    end
  end

  bc = []
  k.times do |i|
    num, cnt = f(b[i], m)
    if bc.empty?
      bc << [num, cnt]
    elsif bc[-1][0] == num
      bc[-1][1] += cnt
    else
      bc << [num, cnt]
    end
  end

  puts bc == ac ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 1
  while x % m == 0
    x /= m
    cnt *= m
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      num, cnt = f(a[i], m)
      if ac.empty?
        ac << [num, cnt]
      elsif ac[-1][0] == num
        ac[-1][1] += cnt
      else
        ac << [num, cnt]
      end
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      num, cnt = f(b[i], m)
      if bc.empty?
        bc << [num, cnt]
      elsif bc[-1][0] == num
        bc[-1][1] += cnt
      else
        bc << [num, cnt]
      end
    else
      bc << [b[i], 1]
    end
  end

  puts bc == ac ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 1
  while x % m == 0
    x /= m
    cnt *= m
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      ac << f(a[i], m)
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      bc << f(b[i], m)
    else
      bc << [b[i], 1]
    end
  end

  ans_a = []
  num, cnt = ac[0]
  (ac.size-1).times do |i|
    num1, cnt1 = ac[i+1]
    if num == num1
      cnt += cnt1
      if ac.size == i+2
        ans_a << [num, cnt]
      end
    else
      ans_a << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ans_b = []
  num, cnt = bc[0]
  (bc.size-1).times do |i|
    num1, cnt1 = bc[i+1]
    if num == num1
      cnt += cnt1
      if bc.size == i+2
        ans_b << [num, cnt]
      end
    else
      ans_b << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  puts ans_a == ans_b ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 1
  while x % m == 0
    x /= m
    cnt *= m
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      ac << f(a[i], m)
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      bc << f(b[i], m)
    else
      bc << [b[i], 1]
    end
  end

  ans_a = []
  num, cnt = ac[0]
  (ac.size-1).times do |i|
    num1, cnt1 = ac[i+1]
    if num == num1
      cnt += cnt1
      if ac.size == i+2
        ans_a << [num, cnt]
      end
    else
      ans_a << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ans_b = []
  num, cnt = bc[0]
  (bc.size-1).times do |i|
    num1, cnt1 = bc[i+1]
    if num == num1
      cnt += cnt1
      if bc.size == i+2
        ans_b << [num, cnt]
      end
    else
      ans_b << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

    ok = true
    ans_a.size.times do |i|
      num1, cnt1 = ans_a[i]
      num2, cnt2 = ans_b[i]

      break ok = false if num1 != num2 || cnt1 != cnt2
    end

  puts ok ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 1
  while x % m == 0
    x /= m
    cnt *= m
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      min, cnt = f(a[i], m)
      ac << [min, cnt]
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      min, cnt = f(b[i], m)
      bc << [min, cnt]
    else
      bc << [b[i], 1]
    end
  end

  ans_a = []
  num, cnt = ac[0]
  (ac.size-1).times do |i|
    num1, cnt1 = ac[i+1]
    if num == num1
      cnt += cnt1
      if ac.size == i+2
        ans_a << [num, cnt]
      end
    else
      ans_a << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ans_b = []
  num, cnt = bc[0]
  (bc.size-1).times do |i|
    num1, cnt1 = bc[i+1]
    if num == num1
      cnt += cnt1
      if bc.size == i+2
        ans_b << [num, cnt]
      end
    else
      ans_b << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ok = false

  if ans_a.size == ans_b.size
    ok = true
    ans_a.size.times do |i|
      next if ans_a[i] == ans_b[i]
      num1, cnt1 = ans_a[i]
      num2, cnt2 = ans_b[i]

      break ok = false if num1 != num2 || cnt1 != cnt2
    end
  end

  puts ok ? "Yes" : "No"
end

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

def f(x, m)
  while x % m == 0
    x /= m
  end
  return x
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      min = f(a[i], m)
      ac << min
    else
      ac << a[i]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      min = f(b[i], m)
      bc << min
    else
      bc << b[i]
    end
  end

  ans_a = ac.slice_when{|i,j| i != j}.map{|e| e[0]}
  ans_b = bc.slice_when{|i,j| i != j}.map{|e| e[0]}

  puts ans_a == ans_b ? "Yes" : "No"
end

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

def f(x, m)
  while x % m == 0
    x /= m
  end
  return x
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      min = f(a[i], m)
      ac << min
    else
      ac << a[i]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      min = f(b[i], m)
      bc << min
    else
      bc << b[i]
    end
  end

  ans_a = ac.slice_when{|i,j| i != j}.map{|e| e[0]}
  ans_b = bc.slice_when{|i,j| i != j}.map{|e| e[0]}

  ok = false
  if ans_a.size == ans_b.size
    ok = true
    ans_a.size.times do |i|
      num1 = ans_a[i]
      num2 = ans_b[i]

      break ok = false if num1 != num2
    end
  end

  puts ok ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 1
  while x % m == 0
    x /= m
    cnt *= m
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      min, cnt = f(a[i], m)
      ac << [min, cnt]
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      min, cnt = f(b[i], m)
      bc << [min, cnt]
    else
      bc << [b[i], 1]
    end
  end

  ans_a = []
  num, cnt = ac[0]
  (ac.size-1).times do |i|
    num1, cnt1 = ac[i+1]
    if num == num1
      cnt += cnt1
      if ac.size == i+2
        ans_a << [num, cnt]
      end
    else
      ans_a << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ans_b = []
  num, cnt = bc[0]
  (bc.size-1).times do |i|
    num1, cnt1 = bc[i+1]
    if num == num1
      cnt += cnt1
      if bc.size == i+2
        ans_b << [num, cnt]
      end
    else
      ans_b << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ok = false
  if ans_a.size == ans_b.size
    ok = true
    ans_a.size.times do |i|
      num1, cnt1 = ans_a[i]
      num2, cnt2 = ans_b[i]

      break ok = false if num1 != num2
    end
  end

  puts ok ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 0
  while x % m == 0
    x /= m
    cnt += 1
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      min, cnt = f(a[i], m)
      ac << [min, 1 << cnt]
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      min, cnt = f(b[i], m)
      bc << [min, 1 << cnt]
    else
      bc << [b[i], 1]
    end
  end

  ans_a = []
  num, cnt = ac[0]
  (ac.size-1).times do |i|
    num1, cnt1 = ac[i+1]
    if num == num1
      cnt += cnt1
      if ac.size == i+2
        ans_a << [num, cnt]
      end
    else
      ans_a << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ans_b = []
  num, cnt = bc[0]
  (bc.size-1).times do |i|
    num1, cnt1 = bc[i+1]
    if num == num1
      cnt += cnt1
      if bc.size == i+2
        ans_b << [num, cnt]
      end
    else
      ans_b << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ok = false

  if ans_a.size == ans_b.size
    ok = true
    ans_a.size.times do |i|
      next if ans_a[i] == ans_b[i]

      num1, cnt1 = ans_a[i]
      num2, cnt2 = ans_b[i]

      break ok = false if num1 != num2
    end
  end

  puts ok ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 0
  while x % m == 0
    x /= m
    cnt += 1
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      min, cnt = f(a[i], m)
      ac << [min, 1 << cnt]
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      min, cnt = f(b[i], m)
      bc << [min, 1 << cnt]
    else
      bc << [b[i], 1]
    end
  end

  ans_a = []
  num, cnt = ac[0]
  (ac.size-1).times do |i|
    num1, cnt1 = ac[i+1]
    if num == num1
      cnt += cnt1
      if ac.size == i+2
        ans_a << [num, cnt]
      end
    else
      ans_a << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ans_b = []
  num, cnt = bc[0]
  (bc.size-1).times do |i|
    num1, cnt1 = bc[i+1]
    if num == num1
      cnt += cnt1
      if bc.size == i+2
        ans_b << [num, cnt]
      end
    else
      ans_b << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ok = false

  if ans_a.size == ans_b.size
    ok = true
    ans_a.size.times do |i|
      next if ans_a[i] == ans_b[i]
      num1, cnt1 = ans_a[i]
      num2, cnt2 = ans_b[i]

      break ok = false if num1 != num2

      if cnt1 != cnt2
        s = m-2
        break ok = false if s == 0
        break ok = false if (cnt1 < m && cnt2 < m) || (cnt2 - cnt1).abs % s != 0
      end
    end
  end

  puts ok ? "Yes" : "No"
end

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

def f(x, m)
  cnt = 0
  while x % m == 0
    x /= m
    cnt += 1
  end
  return [x, cnt]
end

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

  ac = []
  n.times do |i|
    if a[i] % m == 0
      min, cnt = f(a[i], m)
      ac << [min, 1 << cnt]
    else
      ac << [a[i], 1]
    end
  end

  bc = []
  k.times do |i|
    if b[i] % m == 0
      min, cnt = f(b[i], m)
      bc << [min, 1 << cnt]
    else
      bc << [b[i], 1]
    end
  end

  ans_a = []
  num, cnt = ac[0]
  (ac.size-1).times do |i|
    num1, cnt1 = ac[i+1]
    if num == num1
      cnt += cnt1
      if ac.size == i+2
        ans_a << [num, cnt]
      end
    else
      ans_a << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ans_b = []
  num, cnt = bc[0]
  (bc.size-1).times do |i|
    num1, cnt1 = bc[i+1]
    if num == num1
      cnt += cnt1
      if bc.size == i+2
        ans_b << [num, cnt]
      end
    else
      ans_b << [num, cnt]
      num = num1
      cnt = cnt1
    end
  end

  ok = false
  if ans_a.size == ans_b.size

    ok = true
    ans_a.size.times do |i|
      next if ans_a[i] == ans_b[i]
      num1, cnt1 = ans_a[i]
      num2, cnt2 = ans_b[i]
      break ok = false if num1 != num2

      if cnt1 != cnt2
        break ok = false if cnt1 < m && cnt2 < m
        s = m-2
        if cnt1 < cnt2
          break ok = false if (cnt2 - cnt1) % s != 0
        else
          break ok = false if (cnt1 - cnt2) % s != 0
        end
      end
    end
  end

  puts ok ? "Yes" : "No"
end


t = gets.to_s.to_i

def solve_case1(a)
  orig_sum = a[-1]
  tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum
  x_idx = nil
  0.upto(a.size - 2) do |i|
    if tmp - a[i] == orig_sum
      x_idx = i
    end
  end
  return nil if !x_idx
  a.select.with_index {|x, i| (i != x_idx) && (i != a.size - 1)}
end

def solve_case2(a)
  tmp = a.reduce(0) {|acc, x| acc + x } - a[-1] - a[-2]
  if tmp == a[-2]
    return a[0 .. -3]
  end
  return nil
end

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

  # case 1 (the sum is the largest number)
  x = solve_case1(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  # case 2
  x = solve_case2(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  printf("-1\n")
end

t = gets.to_i
t.times do
	n = gets.to_i
	a = gets.split.map(&:to_i).sort
	s = a.sum - a[n+1]

	idx = -1
	for i in 0..n
		if s - a[i] == a[n+1]
			idx = i
			break
		end
	end

	if idx == -1
		if s == 2*a[n]
			idx = n
		else
			puts -1
			next
		end
	end

	for i in 0..n
		next if i == idx
		print a[i], " "
	end
	puts ""
end

t = gets.to_s.to_i

def solve_case1(a)
  orig_sum = a[-1]
  tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum
  x_idx = nil
  0.upto(a.size - 2) do |i|
    if tmp - a[i] == orig_sum
      x_idx = i
    end
  end
  return nil if !x_idx
  a.select.with_index {|x, i| (i != x_idx) && (i != a.size - 1)}
end

def solve_case2(a)
  a[-1] = a[-2]
  return solve_case1(a.sort)
end

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

  # case 1 (the sum is the largest number)
  x = solve_case1(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  # case 2
  x = solve_case2(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  printf("-1\n")
end

t = gets.to_s.to_i

def solve_case1(a)
  orig_sum = a[-1]
  tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum
  0.upto(a.size - 2) do |i|
    if tmp - a[i] == orig_sum
      if i == 0
        return a[i + 1 .. -2]
      elsif i == a.size - 2
        return a[0 .. i - 1]
      else
        return a[0 .. i - 1] + a[i + 1 .. -2]
      end
    end
  end
  return nil
end

def solve_case2(a)
  a[-1] = a[-2]
  return solve_case1(a.sort)
end

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

  # case 1 (the sum is the largest number)
  x = solve_case1(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  # case 2
  x = solve_case2(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  printf("-1\n")
end

t = gets.to_s.to_i

def solve_case1(a)
  orig_sum = a[-1]
  tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum
  0.upto(a.size - 3) do |i|
    if tmp - a[i] == orig_sum
      if i == 0
        return a[i + 1 .. -2]
      elsif i == a.size - 3
        return a[0 .. i - 1]
      else
        return a[0 .. i - 1] + a[i + 1 .. -2]
      end
    end
  end
  return nil
end

def solve_case2(a)
  a[-1] = a[-2]
  return solve_case1(a.sort)
end

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

  # case 1 (the sum is the largest number)
  x = solve_case1(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  # case 2
  x = solve_case2(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  printf("-1\n")
end

t = gets.to_s.to_i

def solve_case1(a)
  orig_sum = a[-1]
  tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum
  0.upto(a.size - 2) do |i|
    if tmp - a[i] == orig_sum
      if i == 0
        return a[i + 1 .. -2]
      elsif i == a.size - 3
        return a[0 .. i - 1]
      else
        return a[0 .. i - 1] + a[i + 1 .. -2]
      end
    end
  end
  return nil
end

def solve_case2(a)
  a[-1] = a[-2]
  return solve_case1(a.sort)
end

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

  # case 1 (the sum is the largest number)
  x = solve_case1(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  # case 2
  x = solve_case2(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  printf("-1\n")
end

t = gets.to_s.to_i

def solve_case1(a)
  orig_sum = a[-1]
  tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum
  0.upto(a.size - 2) do |i|
    if tmp - a[i] == orig_sum
      if i == 0
        return a[i + 1 .. -2]
      elsif i == a.size - 3
        return a[0 .. i - 1]
      else
        return a[0 .. i - 1] + a[i + 1 .. -2]
      end
    end
  end
  return nil
end

def solve_case2(a)
  x = a[-1]
  orig_sum = a[-2]
  tmp = a.reduce(0) {|acc, x| acc + x } - x - orig_sum
  if tmp == orig_sum
    return a[0 .. -3]
  end
  return nil
end

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

  # case 1 (the sum is the largest number)
  x = solve_case1(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  # case 2 (the X
  x = solve_case2(a)
  if x
    printf("%s\n", x.join(" "))
    next
  end

  printf("-1\n")
end

t = gets.to_i
t.times do
	n = gets.to_i
	a = gets.split.map(&:to_i).sort
	s = a.sum - a[n+1]

	idx = -1
	for i in 0..n
		if s - a[i] == a[n+1]
			idx = i
			break
		end
	end

	if idx == -1
		unless s == 2*a[n]
			puts -1
			next
		end
	end

	for i in 0..n
		next if i == idx
		print a[i], " "
	end
	puts ""
end

t = gets.to_i
t.times do
	n = gets.to_i
	a = gets.split.map(&:to_i).sort
	s = a.sum - a[n+1]

	idx = -1
	for i in 0..n
		if s - a[i] == a[n+1]
			idx = i
			break
		end
	end

	if idx == -1
		puts -1
		next
	end

	for i in 0..n
		next if i == idx
		print a[i], " "
	end
	puts ""
end