Problem:
Cross Point

For given two segments s1 and s2, print the coordinate of the cross point of them.

s1 is formed by end points p0 and p1, and
s2 is formed by end points p2 and p3.

Input

The entire input looks like:

q (the number of queries)
1st query
2nd query
...
qth query

Each query consists of integer coordinates of end points of s1 and s2 in the following format:

xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3

Output

For each query, print the coordinate of the cross point. The output values should be in a decimal fraction with an error less than 0.00000001.

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 ≠ p1 and
p2 ≠ p3.

The given segments have a cross point and are not in parallel.

Sample Input

3
0 0 2 0 1 1 1 -1
0 0 1 1 0 1 1 0
0 0 1 1 1 0 0 1

Sample Output

1.0000000000 0.0000000000
0.5000000000 0.5000000000
0.5000000000 0.5000000000

Solution in Ruby (judge verdict: Accepted)

class Point
  attr_reader :x, :y
  def initialize(x, y)
    @x = x
    @y = y
  end
end
class Line
  attr_reader :p1, :p2, :maxx, :minx, :maxy, :miny, :x, :y
  def initialize(x1, y1, x2, y2)
    @p1 = Point.new(x1, y1)
    @p2 = Point.new(x2, y2)
    @maxx = (x1 > x2)? x1 : x2
    @minx = (x1 > x2)? x2 : x1
    @maxy = (y1 > y2)? y1 : y2
    @miny = (y1 > y2)? y2 : y1
    @x = x1 - x2
    @y = y1 - y2
  end
  def parallel?(other)
    return (x * other.y) == (y * other.x)
  end
  def over_x?(other)
    return (x ** 2 + other.x ** 2 >= (maxx - other.maxx) ** 2 + (minx - other.minx) ** 2)
  end
  def over_y?(other)
    return (y ** 2 + other.y ** 2 >= (maxy - other.maxy) ** 2 + (miny - other.miny) ** 2)
  end
  def get_xy(other)
    return (y * other + (x * p2.y - y * p2.x))
  end
  def get_yx(other)
    return (x * other + (y * p2.x - x * p2.y))
  end
  def diff_xx(point)
    return (x * point.y - get_xy(point.x))
  end
  def diff_yy(point)
    return (y * point.x - get_yx(point.y))
  end
  def cross_x?(other)
    return (other.x * p1.y - other.get_xy(p1.x)) * (other.x * p2.y - other.get_xy(p2.x)) <= 0
  end
  def cross_y?(other)
    return (other.y * p1.x - other.get_yx(p1.y)) * (other.y * p2.x - other.get_yx(p2.y)) <= 0
  end
  def get_cross_x(other)
    #p ["x" , other.p1.x, other.p2.x, diff_xx(other.p2), diff_xx(other.p1)]
    return ((other.p1.x * diff_xx(other.p2).abs + other.p2.x * diff_xx(other.p1).abs) / (diff_xx(other.p1).abs + diff_xx(other.p2).abs))
  end
  def get_cross_y(other)
    #p ["y", other.p1.y, other.p2.y, diff_yy(other.p2), diff_yy(other.p1)]
    return ((other.p1.y * diff_yy(other.p2).abs + other.p2.y * diff_yy(other.p1).abs) / (diff_yy(other.p1).abs + diff_yy(other.p2).abs))
  end
  def get_cross_point(other)
    if (y == 0)
      return get_cross_x(other), p1.y
    elsif (x == 0)
      return p1.x, get_cross_y(other)
    else
      return get_cross_x(other), get_cross_y(other)
    end
  end
end
def main(input = STDIN)
  q = input.gets.to_i
  q.times do
    arg = input.gets.split(" ").map(&:to_f)
    l1 = Line.new(*arg[0...4])
    l2 = Line.new(*arg[4..-1])
    puts l1.get_cross_point(l2).join(" ")
  end
end
main