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 int N = 1000 + 9;
struct Point {
  double x, y;
  Point operator-(const Point& b) const { return (Point){x - b.x, y - b.y}; }
  Point operator*(const double& b) const { return (Point){x * b, y * b}; }
  Point operator/(const double& b) const { return (Point){x / b, y / b}; }
  Point turn(const double& k) {
    return (Point){x * cos(k) - y * sin(k), x * sin(k) + y * cos(k)};
  }
  double cross(Point a, Point b) {
    return (a.x - x) * (b.y - y) - (b.x - x) * (a.y - y);
  }
  bool operator<(const Point& b) const {
    if (fabs(x - b.x) < 1e-8) return y < b.y;
    return x < b.x;
  }
} p[N], tp[N];
double X[N * 3], Xadd[N * 3];
int sgn(double x) {
  if (fabs(x) < 1e-8) return 0;
  if (x < 0) return -1;
  return 1;
}
double dis(Point a, Point b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool insert(Point a, Point b, Point c, Point d) {
  return sgn(a.cross(b, c)) * sgn(a.cross(b, d)) < 0;
}
Point LineCross(Point a, Point b, Point c, Point d) {
  double u = a.cross(b, c), v = b.cross(a, d);
  return (Point){(c.x * v + d.x * u) / (u + v), (c.y * v + d.y * u) / (u + v)};
}
int n;
void init() {
  int cnt = 0;
  for (int i = 1; i <= n + 1; ++i) {
    if (cnt >= 2 && sgn(p[i].cross(tp[cnt], tp[cnt - 1])) == 0) --cnt;
    tp[++cnt] = p[i];
  }
  for (int i = 1; i <= cnt; ++i) p[i] = tp[i];
  n = cnt - 1;
}
void solve(Point s, Point t) {
  double deta = atan2(t.y - s.y, t.x - s.x);
  for (int i = 1; i <= n + 1; ++i) {
    tp[i] = (p[i] - s).turn(-deta);
  }
  t = (Point){-1000000, 0};
  s = (Point){1000000, 0};
  int cnt = 0;
  vector<pair<double, double> > add;
  vector<double> tem;
  for (int i = 1; i <= n; ++i) {
    Point a = tp[i], b = tp[i + 1];
    if (fabs(a.y) < 1e-8 && fabs(b.y) < 1e-8) {
      add.push_back({min(a.x, b.x), max(a.x, b.x)});
      X[++cnt] = a.x;
      X[++cnt] = b.x;
      continue;
    }
    if (a.y > b.y + 1e-8) swap(a, b);
    if (fabs(b.y) < 1e-8 || (fabs(a.y) > 1e-8 && insert(s, t, a, b))) {
      Point pt = LineCross(s, t, a, b);
      tem.push_back(pt.x);
      X[++cnt] = pt.x;
    }
  }
  double ans = 0;
  sort(X + 1, X + 1 + cnt);
  cnt = unique(X + 1, X + 1 + cnt) - X - 1;
  sort(tem.begin(), tem.end());
  for (int i = 0; i < tem.size(); i += 2) add.push_back({tem[i], tem[i + 1]});
  for (int i = 1; i <= cnt; ++i) Xadd[i] = 0;
  for (auto it : add) {
    int pl = lower_bound(X + 1, X + 1 + cnt, it.first) - X;
    int pr = lower_bound(X + 1, X + 1 + cnt, it.second) - X;
    Xadd[pl] += 1;
    Xadd[pr] -= 1;
  }
  int num = 0;
  for (int i = 1; i < cnt; ++i) {
    num += Xadd[i];
    if (num) ans += X[i + 1] - X[i];
  }
  printf("%.15f\n", ans);
}
int main() {
  int m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; ++i) scanf("%lf %lf", &p[i].x, &p[i].y);
  p[n + 1] = p[1];
  init();
  for (int i = 1; i <= m; ++i) {
    Point a, b;
    scanf("%lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y);
    solve(a, b);
  }
  return 0;
}