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;
struct point {
  double x, y;
  point operator+(const point& p) const { return {x + p.x, y + p.y}; }
  point operator-(const point& p) const { return {x - p.x, y - p.y}; }
  point operator*(const double d) const { return {x * d, y * d}; }
  double operator*(const point& p) const { return x * p.x + y * p.y; }
};
struct line {
  point p1, p2;
};
point getPoint(point& p1, point& p2, point& p3, point& p4) {
  double a = p1.y - p2.y, b = p2.x - p1.x, e = p1.x * a + p1.y * b;
  double c = p3.y - p4.y, d = p4.x - p3.x, f = p3.x * c + p3.y * d;
  return {(d * e - b * f) / (a * d - b * c),
          (-c * e + a * f) / (a * d - b * c)};
}
double ccw(point& p1, point& p2, point& p3) {
  double ret = (p1.x * p2.y + p2.x * p3.y + p3.x * p1.y) -
               (p2.x * p1.y + p3.x * p2.y + p1.x * p3.y);
  if (-0.00000001 <= ret && ret <= 0.00000001) return 0;
  return ret;
}
double solve(line& l, vector<point>& p) {
  int n = p.size(), s = -1, nm1 = -1, nm2 = 0;
  for (int i = 0; i < n; ++i)
    if (ccw(l.p1, l.p2, p[i]) != 0) s = i;
  if (s == -1) return 0;
  vector<point> ip;
  vector<int> via;
  vector<pair<double, int> > d;
  for (int i = 0; i < n; ++i) {
    int x = i;
    while (x < n && ccw(l.p1, l.p2, p[(s + x + 1) % n]) == 0) x++;
    if (ccw(l.p1, l.p2, p[(s + i) % n]) * ccw(l.p1, l.p2, p[(s + x + 1) % n]) <
        0) {
      if (x == i) {
        ip.push_back(getPoint(l.p1, l.p2, p[(s + i) % n], p[(s + i + 1) % n]));
        via.push_back(-1);
      } else {
        if (i + 1 == x) {
          ip.push_back(p[(s + i + 1) % n]);
          via.push_back(-1);
        } else {
          ip.push_back(p[(s + i + 1) % n]);
          via.push_back(++nm2);
          ip.push_back(p[(s + x) % n]);
          via.push_back(nm2);
        }
      }
    } else {
      if (i + 1 < x) {
        ip.push_back(p[(s + i + 1) % n]);
        via.push_back(--nm1);
        ip.push_back(p[(s + x) % n]);
        via.push_back(nm1);
      }
    }
    i = x;
  }
  for (int i = 0, ipn = ip.size(); i < ipn; ++i)
    d.push_back({(ip[i] - l.p1) * (l.p2 - l.p1), via[i]});
  for (int i = 0, ipn = ip.size(); i < ipn; ++i)
    d[i].first /= sqrt((l.p2 - l.p1) * (l.p2 - l.p1));
  sort(d.begin(), d.end());
  int inside = 0;
  double ret = 0;
  for (int i = 1, dn = d.size(); i < dn; ++i) {
    double d1 = d[i - 1].first, d2 = d[i].first;
    if (d[i - 1].second >= 0 && d[i - 1].second == d[i].second) {
      ret += d2 - d1;
      continue;
    }
    if (d[i - 1].second < -1 && d[i - 1].second == d[i].second) {
      ret += d2 - d1;
      inside = 1 - inside;
      continue;
    }
    inside = 1 - inside;
    if (inside) ret += d2 - d1;
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<point> p(n);
  vector<line> l(m);
  for (int i = 0; i < n; ++i) {
    scanf("%lf%lf", &p[i].x, &p[i].y);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%lf%lf%lf%lf", &l[i].p1.x, &l[i].p1.y, &l[i].p2.x, &l[i].p2.y);
    printf("%.10lf\n", solve(l[i], p));
  }
  return 0;
}