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 inf = 0x3f3f3f3f;
const long long infLL = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-10;
int sgn(double x) { return x < -eps ? -1 : x > eps; }
double dot(complex<double> u, complex<double> v) {
  return u.real() * v.real() + u.imag() * v.imag();
}
double cross(complex<double> u, complex<double> v) {
  return u.real() * v.imag() - u.imag() * v.real();
}
const int maxn = 1000 + 5;
int n, m;
complex<double> g[maxn];
int sz;
pair<complex<double>, int> vec[maxn];
struct cmp {
  complex<double> s, t;
  cmp(complex<double> src, complex<double> trg) : s(src), t(trg) {}
  bool operator()(pair<complex<double>, int> u,
                  pair<complex<double>, int> v) const {
    complex<double> a = u.first, b = v.first;
    return sgn(a.real() - b.real())
               ? sgn(a.real() - b.real()) == sgn(s.real() - t.real())
               : sgn(a.imag() - b.imag()) == sgn(s.imag() - t.imag());
  }
};
bool check(complex<double> v, complex<double> v1, complex<double> v2) {
  double c1 = cross(v1, v), c2 = cross(v, v2);
  return sgn(cross(v1, v2)) <= 0 ? sgn(c1) <= 0 && sgn(c2) <= 0
                                 : !(sgn(c1) > 0 && sgn(c2) > 0);
}
double inter(complex<double> s, complex<double> t) {
  sz = 0;
  for (int i = 0; i < (n); ++i) {
    complex<double> a = g[i], b = g[(i + 1) % n];
    double c1 = cross(a - s, t - s), c2 = cross(b - s, t - s);
    if (!sgn(c1))
      vec[sz++] = make_pair(a, n + i);
    else if ((sgn(c1) ^ sgn(c2)) == -2)
      vec[sz++] = make_pair((c1 * b - c2 * a) / (c1 - c2), i);
  }
  sort(vec, vec + sz, cmp(s, t));
  double ret = 0;
  for (int j = 0; j < (sz - 1); ++j) {
    complex<double> v = vec[j + 1].first - vec[j].first;
    if (vec[j].second < n) {
      int i = vec[j].second;
      if (sgn(cross(g[(i + 1) % n] - g[i], v)) >= 0) ret += abs(v);
    } else {
      int i = vec[j].second - n;
      if (check(v, g[(i - 1 + n) % n] - g[i], g[(i + 1) % n] - g[i]))
        ret += abs(v);
    }
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  double x, y;
  for (int i = 0; i < (n); ++i) {
    scanf("%lf%lf", &x, &y);
    g[i] = complex<double>(x, y);
  }
  double area = 0;
  for (int i = 1; i < n - 1; ++i) area += cross(g[i] - g[0], g[i + 1] - g[0]);
  if (sgn(area) < 0) reverse(g, g + n);
  while (m--) {
    scanf("%lf%lf", &x, &y);
    complex<double> s = complex<double>(x, y);
    scanf("%lf%lf", &x, &y);
    complex<double> t = complex<double>(x, y);
    printf("%.10f\n", inter(s, t));
  }
  return 0;
}