Problem:
Intersection

For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.

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 "1" or "0".

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 ≠ p1 and
p2 ≠ p3.

Sample Input 1

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

Sample Output 1

1
1
0

Solution in Ruby (judge verdict: Accepted)

gets.to_i.times do
  p0, p1, p2, p3 = gets.split.map(&:to_i).each_slice(2).map {|a, b| a + b * 1i }
  u, v, w, z = p1 - p0, p3 - p2, p2 - p0, p3 - p0
  d, c = (u.conj * v).rect
  if c.zero?
    if (u.conj * w).imag.zero?
      ud, wd, zd = [u, w, z].map {|x| (u.conj * x).real }
      a, b = 0, ud
      a, b = b, a if b < a
      p wd < a && zd < a || wd > b && zd > b ? 0 : 1
    else
      p 0
    end
  else
    # t, s are such that p0 + t * u = p2 + s * v
    t = (Complex(*v.rect.reverse!) * w).real.to_f / c
    s = (Complex(*u.rect.reverse!) * w).real.to_f / c
    p 0 <= t && t <= 1 && 0 <= s && s <= 1 ? 1 : 0
  end
end