Given simple (without self-intersections) n-gon. It is not necessary convex. Also you are given m lines. For each line find the length of common part of the line and the n-gon.

The boundary of n-gon belongs to polygon. It is possible that n-gon contains 180-degree angles.

Input

The first line contains integers n and m (3 ≤ n ≤ 1000;1 ≤ m ≤ 100). The following n lines contain coordinates of polygon vertices (in clockwise or counterclockwise direction). All vertices are distinct.

The following m lines contain line descriptions. Each of them contains two distict points of a line by their coordinates.

All given in the input coordinates are real numbers, given with at most two digits after decimal point. They do not exceed 105 by absolute values.

Output

Print m lines, the i-th line should contain the length of common part of the given n-gon and the i-th line. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

Examples

Input

4 3
0 0
1 0
1 1
0 1
0 0 1 1
0 0 0 1
0 0 1 -1


Output

1.41421356237309514547
1.00000000000000000000
0.00000000000000000000
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        static final double eps = 1e-8;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            Point[] p = new Point[n];
            for (int i = 0; i < n; ++i) {
                p[i] = new Point(in.nextDouble(), in.nextDouble());
            }
            double tmp = 0;
            for (int i = 0; i < n; ++i) {
                tmp += p[i].cross(p[(i + 1) % n]);
            }
            if (tmp < 0) {
                Point[] q = new Point[n];
                for (int i = 0; i < n; ++i) {
                    q[i] = new Point(p[n - 1 - i]);
                }
                p = q;
            }
            for (; m > 0; --m) {
                Point a = new Point(in.nextDouble(), in.nextDouble());
                Point b = new Point(in.nextDouble(), in.nextDouble());
                out.printf("%.15f\n", gao(n, p, a, b));
            }
        }

        double gao(int n, Point[] p, Point a, Point b) {
            int m = 0;
            Point[] q = new Point[n];
            int[] id = new int[n];
            for (int i = 0; i < n; ++i) {
                if (Math.abs(p[i].substract(a).cross(b.substract(a))) < eps) {
                    q[m] = p[i];
                    id[m++] = i;
                }
            }
            for (int i = 0; i < n; ++i) {
                q[m] = intersect(p[i], p[(i + 1) % n], a, b);
                if (q[m] != null) {
                    id[m++] = -1;
                }
            }
            if (m == 0) {
                return 0;
            }
            Node[] node = new Node[m];
            for (int i = 0; i < m; ++i) {
                node[i] = new Node(q[i], id[i]);
            }
            Point dir = b.substract(a).unit();
            Arrays.sort(node, new Comparator<Node>() {
                public int compare(Node o1, Node o2) {
                    double d1 = o1.p.substract(a).dot(dir), d2 = o2.p.substract(a).dot(dir);
                    if (Math.abs(d1 - d2) < eps) return 0;
                    return d1 < d2 - eps ? -1 : 1;
                }
            });
            double ret = 0;
            boolean flag = false;
            Point last = node[0].p;
            for (int i = 0; i < m; ++i) {
                if (flag) {
                    ret += node[i].p.substract(last).norm();
                }
                if (node[i].id == -1) {
                    flag = !flag;
                } else {
                    int u = node[i].id;
                    if (check(a, b, p[(u + n - 1) % n], p[u], p[(u + 1) % n])) {
                        flag = !flag;
                    }
                }
                last = node[i].p;
            }
            return ret;
        }

        boolean check(Point a, Point b, Point p, Point mid, Point q) {
            double s1 = b.substract(a).cross(p.substract(a));
            double s2 = b.substract(a).cross(q.substract(a));
            if (s1 > eps && s2 > eps || s1 < -eps && s2 < -eps) {
                return false;
            }
            if (s1 < -eps && s2 > eps || s1 > eps && s2 < -eps) {
                return true;
            }
            if (Math.abs(s1) < eps && Math.abs(s2) < eps) {
                return false;
            }
            return mid.substract(p).cross(q.substract(p)) > eps;
        }

        Point intersect(Point sa, Point sb, Point la, Point lb) {
            double s1 = lb.substract(la).cross(sa.substract(la));
            double s2 = lb.substract(la).cross(sb.substract(la));
            if (s1 < -eps && s2 > eps || s1 > eps && s2 < -eps) {
                return sa.multiply(s2).substract(sb.multiply(s1)).divide(s2 - s1);
            }
            return null;
        }

        class Node {
            Point p;
            int id;

            public Node(Point p, int id) {
                this.p = new Point(p);
                this.id = id;
            }

        }

    }

    static class Point implements Comparable<Point> {
        static final double eps = 1e-12;
        double x;
        double y;

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

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


        public int compareTo(Point o) {
            if (Math.abs(x - o.x) < eps && Math.abs(y - o.y) < eps) return 0;
            return x < o.x - eps || x < o.x - eps && y < o.y ? -1 : 1;
        }

        public Point substract(Point o) {
            return new Point(x - o.x, y - o.y);
        }

        public Point multiply(double k) {
            return new Point(x * k, y * k);
        }

        public Point divide(double k) {
            return new Point(x / k, y / k);
        }

        public double dot(Point o) {
            return x * o.x + y * o.y;
        }

        public double cross(Point o) {
            return x * o.y - y * o.x;
        }

        public double norm() {
            return Math.sqrt(x * x + y * y);
        }

        public Point unit() {
            return this.divide(norm());
        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

    }
}
