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

fun dot(A : Pair<Double,Double>,B : Pair<Double,Double>) = A.first * B.first + A.second * B.second
fun cross(A : Pair<Double,Double>,B : Pair<Double,Double>) = A.first * B.second - A.second * B.first
fun norm(P : Pair<Double,Double>) = (P.first * P.first + P.second * P.second)
fun arg(P : Pair<Double,Double>) = Math.atan2(P.first,P.second)
fun angle(p1 : Pair<Double,Double>,p2 : Pair<Double,Double>) : Double{
    val tmp = Math.abs(arg(p1) - arg(p2))
    return Math.min(tmp,2*Math.acos(-1.0)-tmp)
}

//高々３頂点なので全部見てしまう
fun Convex_contain(Polygon : MutableList<Pair<Double,Double>>,p : Pair<Double,Double>) : Boolean{
    var flag = true
    for(i in 0 until 3){
        val tmp = CCW(Polygon[i],Polygon[(i+1)%3],p)
        if(tmp != -1)flag = false
    }
    return flag
}


/**
 * a->bで時計方向に折れてb->c : 1
 * a->bで反時計方向に折れてb->c : -1
 * a->bで逆を向いてaを通り越してb->c : 2
 * a->bでそのままb->c: -2
 * a->bで逆を向いてb->c(or b == c) : 0
 */
fun CCW(a : Pair<Double,Double>,b : Pair<Double,Double>,c : Pair<Double,Double>) : Int{
    val B : Pair<Double,Double> = Pair(b.first - a.first,b.second - a.second)
    val C : Pair<Double,Double> = Pair(c.first - a.first,c.second - a.second)
    if(cross(B,C) > 0) return +1
    if(cross(B,C) < 0) return -1
    if(dot(B,C) < 0) return +2
    if(norm(B)< norm(C)) return -2
    return 0
}


fun crossPoint(l1 :  Pair<Double,Double>,
               l2 :  Pair<Double,Double>,
               m1 :  Pair<Double,Double>,
               m2 :  Pair<Double,Double>) : Pair<Double,Double>{
    val ll = Pair(l2.first - l1.first,l2.second - l1.second)
    val mm = Pair(m2.first - m1.first,m2.second - m1.second)
    val lm = Pair(l2.first - m1.first,l2.second - m1.second)
    val A = cross(ll,mm)
    val B = cross(ll,lm)
    if(Math.abs(A) < 1e-9 && Math.abs(B) < 1e-9)return m1
    return Pair((m1.first + (B / A) * mm.first), (m1.second + (B / A) * mm.second))
}

fun getsRate(Polygon : MutableList<Pair<Double,Double>>,p : Pair<Double,Double>) : DoubleArray{
    var ret = DoubleArray(3,{0.0})
    for(i in 0 until 3){
        if(CCW(Polygon[i] ,Polygon[(i+1)%3],p) == 0){
            if(!(Math.abs(Polygon[i].first - Polygon[(i+1)%3].first) < 1e-9)){
                val r = (p.first - Polygon[(i+1)%3].first) / (Polygon[i].first - Polygon[(i+1)%3].first)
                ret[i] = r
                ret[(i+1)%3] = 1 - r
                return ret
            }
            if(!(Math.abs(Polygon[i].second - Polygon[(i+1)%3].second) < 1e-9)){
                val r = (p.second - Polygon[(i+1)%3].second) / (Polygon[i].second - Polygon[(i+1)%3].second)
                ret[i] = r
                ret[(i+1)%3] = 1 - r
                return ret
            }
        }
    }
    val D = crossPoint(Polygon[0],p,Polygon[1],Polygon[2])
    if(!(Math.abs(Polygon[1].first - Polygon[2].first) < 1e-9 || Math.abs(Polygon[0].first - D.first) < 1e-9)){
        val r1 = (D.first - Polygon[2].first) / (Polygon[1].first - Polygon[2].first)
        val r2 = (p.first - D.first) / (Polygon[0].first - D.first)
        ret = doubleArrayOf(r2,r1*(1-r2),((1-r1)*(1-r2)))
        return ret
    }
    if(!(Math.abs(Polygon[1].second - Polygon[2].second) < 1e-9 || Math.abs(Polygon[0].second - D.second) < 1e-9)){
        val r1 = (D.second - Polygon[2].second) / (Polygon[1].second - Polygon[2].second)
        val r2 = (p.second - D.second) / (Polygon[0].second - D.second)
        ret = doubleArrayOf(r2,r1*(1-r2),((1-r1)*(1-r2)))
        return ret
    }
    return doubleArrayOf(0.0,0.0,0.0)
}


fun main(args : Array<String>){

    val n = readLine()!!.toInt()

    for(i in 0 until n){
        val A = readLine()!!.split(' ').map{ it.toDouble() }
        val ans = crossPoint(Pair(A[0],A[1]),Pair(A[2],A[3]),Pair(A[4],A[5]),Pair(A[6],A[7]))
        println("${ans.first} ${ans.second}")
    }

}