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 JAVA (judge verdict: Accepted)

//CGL_2_C
public class Main{
  public void run(java.io.InputStream in, java.io.PrintStream out){
    java.util.Scanner sc = new java.util.Scanner(in);
/*answer*/
    double xp0, xp1, yp0, yp1, xp2, yp2, xp3, yp3;
    int q;
    int i;
    double s0x, s0y, s1x, s1y, s, t, x, y;

    q = sc.nextInt();
    for(i = 0;i < q;i++){
      xp0 = sc.nextDouble();
      yp0 = sc.nextDouble();
      xp1 = sc.nextDouble();
      yp1 = sc.nextDouble();
      xp2 = sc.nextDouble();
      yp2 = sc.nextDouble();
      xp3 = sc.nextDouble();
      yp3 = sc.nextDouble();

      s0x = xp1 - xp0;
      s0y = yp1 - yp0;
      s1x = xp3 - xp2;
      s1y = yp3 - yp2;

      s = s0x * s1y - s1x * s0y;

      if(xp0 == xp2 && yp0 == yp2){
        x = xp0;
        y = yp0;
      }else if(xp0 == xp3 && yp0 == yp3){
        x = xp0;
        y = yp0;
      }else if(xp1 == xp2 && yp1 == yp2){
        x = xp1;
        y = yp1;
      }else if(xp1 == xp3 && yp1 == yp3){
        x = xp1;
        y = yp1;
      }else{
        t = (s1y * (xp2 - xp0) - s1x * (yp2 - yp0)) / s;
        x = xp0 + t * s0x;
        y = yp0 + t * s0y;
      }

      System.out.println(x + " " + y);
    }

/*fin*/
    sc.close();
  }
  public static void main(String[] args){
    (new Main()).run(System.in, System.out);
  }
}