Problem:
Polygon-Point-Containment

For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.

g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.

Note that the polygon is not necessarily convex.

Input

The entire input looks like:

g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query

g is given by coordinates of the points p1,..., pn in the following format:

n
x1 y1
x2 y2
:
xn yn

The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.

Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.

Output

For each query, print "2", "1" or "0".

Constraints

3 ≤ n ≤ 100

1 ≤ q ≤ 1000

-10000 ≤ xi, yi ≤ 10000

No point of the polygon will occur more than once.

Two sides of the polygon can intersect only at a common endpoint.

Sample Input

4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2

Sample Output

2
1
0

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static readonly double EPS = 1E-10;

        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            Point[] G = new Point[n];

            for (int i = 0; i < n; i++)
            {
                double[] input = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse);
                G[i] = new Point(input[0], input[1]);
            }

            StringBuilder sb = new StringBuilder();
            int q = int.Parse(Console.ReadLine());

            for (int i = 0; i < q; i++)
            {
                double[] input = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse);
                Point p = new Point(input[0], input[1]);

                sb.AppendLine(Contains(G, p).ToString());
            }
            Console.Write(sb);
        }

        static int Contains(Point[] G, Point p)
        {
            int n = G.Length;
            bool x = false;

            for (int i = 0; i < n; i++)
            {
                Point a = G[i] - p;
                Point b = G[(i + 1) % n] - p;

                if (Math.Abs(a.Cross(b)) < EPS && a.Dot(b) < EPS) return 1;

                if (a.y > b.y)
                {
                    Point temp = a;
                    a = b;
                    b = temp;
                }

                if (a.y < EPS && EPS < b.y && a.Cross(b) > EPS) x = !x;
            }
            return x ? 2 : 0;
        }
    }

    class Point : IComparable<Point>
    {
        static readonly double EPS = 1E-10;

        internal double x, y;

        public Point(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public double Abs { get { return Math.Sqrt(Norm); } }
        public double Norm { get { return x * x + y * y; } }

        public double Dot(Point p) { return x * p.x + y * p.y; }
        public double Cross(Point p) { return x * p.y - y * p.x; }

        public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); }
        public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); }
        public static Point operator *(Point p, double d) { return new Point(p.x * d, p.y * d); }
        public static Point operator /(Point p, double d) { return new Point(p.x / d, p.y / d); }
        public static bool operator <(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; }
        public static bool operator >(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; }
        public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; }
        public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; }

        public int CompareTo(Point other)
        {
            return x != other.x ? (int)(x - other.x) : (int)(y - other.y);
        }

        public override bool Equals(object obj)
        {
            Point other = obj as Point;
            return Math.Abs(x - other.x) < EPS && Math.Abs(y - other.y) < EPS;
        }

        public override int GetHashCode()
        {
            return x.GetHashCode() ^ y.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("{0:f10} {1:f10}", x, y);
        }
    }

    class Segment
    {
        internal Point p1, p2;

        public Segment(Point p1, Point p2)
        {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

    class Circle
    {
        internal Point p;
        internal double r;

        public Circle(Point p, double r)
        {
            this.p = p;
            this.r = r;
        }
    }
}