Problem:
Projection

For given three points p1, p2, p, find the projection point x of p onto p1p2.

Input

xp1 yp1 xp2 yp2
q
xp0 yp0
xp1 yp1
...
xpq−1 ypq−1

In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p.

Output

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

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xi, yi ≤ 10000

p1 and p2 are not identical.

Sample Input 1

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

Sample Output 1

-1.0000000000 0.0000000000
0.0000000000 0.0000000000
1.0000000000 0.0000000000

Sample Input 2

0 0 3 4
1
2 5

Sample Output 2

3.1200000000 4.1600000000

Solution in Ruby (judge verdict: Runtime Error)

# encoding: utf-8

class Point
  attr_reader :x, :y
  def initialize(x = 0, y = 0)
    @x, @y = x, y
  end

  def + o
    Point.new(@x + o.x, @y + o.y)
  end

  def - o
    Point.new(@x - o.x, @y - o.y)
  end

end

class Vector
  attr_reader :a, :b

  def initialize(a = 0, b = 0)
    @a, @b = a, b
    @a = 0.0 if sign(@a) == 0
    @b = 0.0 if sign(@b) == 0
  end

  def + o
    Vector.new(@a + o.a, @b + o.b)
  end

  def - o
    Vector.new(@a - o.a, @b - o.b)
  end

  def cross o
    @a * o.b - @b * o.a
  end

  def len
    Math.sqrt(a ** 2 + b ** 2)
  end

  def output
    puts ("%.10f" % a) + " " + ("%.10f" % b)
  end
end

def sign(x)
  x < 1e-6 ? -1 : (x > 1e-6 ? 1: 0)
end

def cos_theory(a, b, c)
  t = (a ** 2 + b ** 2 - c ** 2) / (2 * a * b)
  t = 1.0 if sign(t - 1.0) == 0
  t = -1.0 if sign(t + 1.0) == 0
  Math.acos(t)
end

def main
  xp0, yp0, xp1, yp1 = gets.chomp.split(/\s/).collect { |x| x.to_f}
  q = gets.chomp.to_i
  (1..q).each do |ca|
    xp2, yp2 = gets.chomp.split(/\s/).collect { |x| x.to_f}
    v1 = Vector.new(xp1 - xp0, yp1 - yp0)
    v2 = Vector.new(xp2 - xp0, yp2 - yp0)
    v3 = Vector.new(xp2 - xp1, yp2 - yp1)
    a, b, c = v1.len, v2.len, v3.len
    theta = cos_theory(a, b, c)
    rate = b * Math.cos(theta) / a
    t = Vector.new(xp0 + rate * (xp1 - xp0),
                   yp0 + rate * (yp1 - yp0))
    t.output
  end
end

main