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

using System;
using System.Text;

namespace CGL_1_A_Projection
{
	class Program
	{
		static void Main ( string[] args )
		{
			int[] inputNum = Array.ConvertAll (Console.ReadLine ().Split (' '), int.Parse);
			int inputCount = int.Parse (Console.ReadLine ());

			StringBuilder output = new StringBuilder (inputCount * 10 * 2);

			Vector2 beginPoint = new Vector2 (inputNum[0], inputNum[1]);
			Vector2 endPoint = new Vector2 (inputNum[2], inputNum[3]);

			for (int lp = 0; lp < inputCount; lp++)
			{
				inputNum = Array.ConvertAll (Console.ReadLine ().Split (' '), int.Parse);

				Vector2 result = Vector2.Projection (beginPoint, endPoint, new Vector2 (inputNum[0], inputNum[1]));

				output.Append (result.X);
				output.Append (" ");
				output.Append (result.Y);
				output.AppendLine ();
			}

			Console.Write (output);
		}
	}

	class Vector2
	{
		public Vector2 ( decimal x, decimal y )
		{
			this.X = x;
			this.Y = y;
		}

		public decimal X { private set; get; }
		public decimal Y { private set; get; }

		public static Vector2 operator + ( Vector2 ob1, Vector2 ob2 )
		{
			return new Vector2 (ob1.X + ob2.X, ob1.Y + ob2.Y);
		}
		public static Vector2 operator - ( Vector2 ob1, Vector2 ob2 )
		{
			return new Vector2 (ob1.X - ob2.X, ob1.Y - ob2.Y);
		}
		public static Vector2 operator * ( Vector2 ob, decimal scalar )
		{
			return new Vector2 (ob.X * scalar, ob.Y * scalar);
		}

		public decimal Length ()
		{
			return X * X + Y * Y;
		}
		public static decimal DotProduct ( Vector2 ob1, Vector2 ob2 )
		{
			return ob1.X * ob2.X + ob1.Y * ob2.Y;
		}

		public static Vector2 Projection ( Vector2 beginPoint, Vector2 endPoint, Vector2 point )
		{
			Vector2 segment = endPoint - beginPoint;
			decimal ratio = Vector2.DotProduct (point - beginPoint, segment) / segment.Length ();
			return beginPoint + segment * ratio;
		}
	}
}