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: Wrong Answer)

def projecter(x, y, xp, yp)
  return (xp * x + yp * y) / (x ** 2 + y ** 2), (yp * x - xp * y) / (x ** 2 + y ** 2)
end
def check(a1, b1, a2, b2)
  return ((((b2 - b1) * (1 - a1) + b1 * (a2 - a1)) * (b1 * (a2 - a1) - (b2 - b1) * a1) <= 0) and (b1 * b2 <= 0) and subcheck(a1, b1, a2, b2))
end
def subcheck(a1, b1, a2, b2)
  return (b1 != b2 or a1.between?(0,1) or a2.between?(0, 1))
end
          

def main(input = STDIN)
  input.gets.to_i.times do
    points = input.gets.split(" ").map(&:to_r)
    a1, b1, a2, b2 = *projecter(points[2] - points[0], points[3] - points[1], points[4] - points[0], points[5] - points[1]), *projecter(points[2] - points[0], points[3] - points[1], points[6] - points[0], points[7] - points[1])
    if  check(a1, b1, a2, b2)
      puts "1"
    else
      puts "0"
    end
  end
end
main