Problem: 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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-12;
const int maxn = 1000 + 5;
const int maxm = 100 + 5;
struct Point {
  long double x, y;
  Point(long double x_ = 0, long double y_ = 0) : x(x_), y(y_) {}
  void read() {
    double x_, y_;
    scanf("%lf%lf", &x_, &y_);
    x = x_;
    y = y_;
  }
};
Point operator+(const Point& a, const Point& b) {
  return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point& a, const Point& b) {
  return Point(a.x - b.x, a.y - b.y);
}
Point operator*(const Point& a, const double b) {
  return Point(a.x * b, a.y * b);
}
double operator*(const Point& a, const Point& b) {
  return a.x * b.y - a.y * b.x;
}
double operator%(const Point& a, const Point& b) {
  return a.x * b.x + a.y * b.y;
}
bool operator<(const Point& a, const Point& b) {
  return a.x + eps < b.x || (abs(a.x - b.x) < eps && a.y + eps < b.y);
}
Point po[maxn], poly[maxn];
Point e[maxn];
int N, M, E;
int main() {
  int i, j, k;
  long double t1, t2, ans;
  Point a1, a2, a3, b1, b2;
  scanf("%d%d", &N, &M);
  for (i = 1; i <= N; i++) po[i].read();
  t1 = 0;
  for (i = 1; i <= N; i++) t1 += po[i] * po[i < N ? i + 1 : 1];
  if (t1 < 0) {
    for (i = 1, j = N; i < j; i++, j--) swap(po[i], po[j]);
  }
  for (i = 1, j = 0; i <= N; i++) {
    t1 = (po[i > 1 ? i - 1 : N] - po[i]) * (po[i < N ? i + 1 : 1] - po[i]);
    if (abs(t1) < eps) continue;
    poly[++j] = po[i];
  }
  N = j;
  for (k = 1; k <= M; k++) {
    b1.read();
    b2.read();
    if (b2 < b1) swap(b1, b2);
    E = 0;
    for (i = 1; i <= N; i++) {
      a1 = poly[i];
      a2 = poly[i < N ? i + 1 : 1];
      t1 = (b1 - a1) * (b2 - a1);
      t1 = abs(t1) < eps ? 0 : t1;
      t2 = (b2 - a2) * (b1 - a2);
      t2 = abs(t2) < eps ? 0 : t2;
      if (t1 * t2 < -eps) continue;
      if (t1 == 0 || t2 == 0) continue;
      e[++E] = (a1 * t2 + a2 * t1) * (1.0 / (t1 + t2));
    }
    for (i = 1; i <= N; i++) {
      a1 = poly[i > 1 ? i - 1 : N];
      a2 = poly[i];
      a3 = poly[i < N ? i + 1 : 1];
      t1 = (a2 - b1) * (b2 - b1);
      if (abs(t1) > eps) continue;
      t1 = (a1 - a2) * (b2 - b1);
      if (abs(t1) > eps) {
        t2 = (a3 - a2) * (b2 - b1);
        if (abs(t2) > eps && t1 * t2 < 0)
          e[++E] = a2;
        else if (abs(t2) < eps) {
          t1 = (a2 - a1) * (a3 - a2);
          if (t1 > eps) e[++E] = a2;
        }
      } else {
        t1 = (a2 - a1) * (a3 - a2);
        if (t1 > eps) e[++E] = a2;
      }
    }
    sort(e + 1, e + E + 1);
    ans = 0;
    for (i = 1; i < E; i += 2)
      ans += sqrt((e[i + 1] - e[i]) % (e[i + 1] - e[i]));
    printf("%.20lf\n", (double)(ans));
  }
  return 0;
}