Problem:
Integral Rectangles

Let us consider rectangles whose height, h, and
width, w, are both integers. We call such rectangles
integral rectangles. In this problem, we consider only wide
integral rectangles, i.e., those with w > h.

We define the following ordering of wide integral rectangles. Given
two wide integral rectangles,

The one shorter in its diagonal line is smaller, and

If the two have diagonal lines with the same length, the one shorter
in its height is smaller.

Given a wide integral rectangle, find the smallest wide integral
rectangle bigger than the given one.

Input

The entire input consists of multiple datasets. The number of
datasets is no more than 100. Each dataset describes a wide integral
rectangle by specifying its height and width, namely h
and w, separated by a space in a line, as follows.

h w

For each dataset, h and w (>h) are both
integers greater than 0 and no more than 100.

The end of the input is indicated by a line of two zeros separated by a space.

Output

For each dataset, print in a line two numbers describing the
height and width, namely h and w (> h), of the
smallest wide integral rectangle bigger than the one described in the
dataset. Put a space between the numbers. No other
characters are allowed in the output.

For any wide integral rectangle given in the input, the width and
height of the smallest wide integral rectangle bigger than the given
one are both known to be not greater than 150.

In addition, although
the ordering of wide integral rectangles uses the comparison of
lengths of diagonal lines, this comparison can be replaced with that
of squares (self-products) of lengths of diagonal lines, which can
avoid unnecessary troubles possibly caused by the use of floating-point
numbers.

Sample Input

1 2
1 3
2 3
1 4
2 4
5 6
1 8
4 7
98 100
99 100
0 0

Output for the Sample Input

1 3
2 3
1 4
2 4
3 4
1 8
4 7
2 8
3 140
89 109

Solution in Ruby (judge verdict: Accepted)

HWs = readlines.map(&:chomp).map{|s|s.split.map(&:to_i)}

def f(a,b)
  ra=a[1]**2+a[2]**2
  rb=b[1]**2+b[2]**2
  if ra>rb then return true end
  if a[1] > b[1]
    return true
    else
    return false
    end
end

HWs.each{|hh,ww|
  if hh==0 && ww==0 then break end
  rr=hh**2+ww**2
  cands = []
  1.upto(150){|h|
    (h+1).step{|w|
      if h==hh and w==ww then next end
      r = h**2 + w**2
      if r >= rr
        cands.push([r,h,w])
        break
      end
    }
  }
  #rr_min = cands.min_by{|rr,h,w| rr}[0]
  #min = cands.select{|rr,h,w|
  #  if rr == rr_min
  #    h>=hh
  #  else rr_min > rr
  #  end}.min_by{|rr,h,w| h}
  #puts min[1..2]*' '
  puts cands.select{|x| f(x, [rr, hh, ww])}.min_by{|rr,h,w| if w>h then rr*1000+h else 1/0.0 end}[1..2]*' '
}