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;
bool pcmp(complex<long double> a, complex<long double> b) {
  return (fabs(a.real() - b.real()) > 1e-7L && a.real() < b.real()) ||
         (fabs(a.real() - b.real()) < 1e-7L &&
          fabs(a.imag() - b.imag()) > 1e-7L && a.imag() < b.imag());
}
inline bool pointOnSegment(complex<long double> a, complex<long double> b,
                           complex<long double> p) {
  return (((((b) - (a)).real()) * (((p) - (a)).real())) +
          ((((b) - (a)).imag()) * (((p) - (a)).imag()))) > 1e-4L &&
         (((((a) - (b)).real()) * (((p) - (b)).real())) +
          ((((a) - (b)).imag()) * (((p) - (b)).imag()))) > 1e-7L;
}
complex<long double> lineIntersection(complex<long double> a1,
                                      complex<long double> b1,
                                      complex<long double> a2,
                                      complex<long double> b2) {
  long double A1 = a1.imag() - b1.imag(), B1 = b1.real() - a1.real(),
              A2 = a2.imag() - b2.imag(), B2 = b2.real() - a2.real();
  long double C1 = A1 * a1.real() + B1 * a1.imag(),
              C2 = A2 * a2.real() + B2 * a2.imag(), det = A1 * B2 - B1 * A2;
  if (fabs(det) < 1e-7L) return complex<long double>(1e+9, 1e+9);
  return complex<long double>(C1 * B2 - B1 * C2, A1 * C2 - C1 * A2) / det;
}
void order(vector<complex<long double> > &v) {
  long double area = 0;
  int sz = v.size();
  for (int i = 1; i + 1 < sz; i++)
    area += ((conj(((v[i]) - (v[0]))) * (((v[i + 1]) - (v[0])))).imag());
  if (area < -1e-7L) reverse(v.begin(), v.end());
}
int main() {
  int n, m;
  long double x1, y1, x2, y2;
  cout << setprecision(11) << fixed;
  cin >> n >> m;
  vector<complex<long double> > vec(n);
  map<complex<long double>, int,
      bool (*)(complex<long double>, complex<long double>)>
      vertex(pcmp);
  for (int i = 0; i < n; i++) {
    cin >> x1 >> y1;
    vec[i] = complex<long double>(x1, y1);
  }
  order(vec);
  for (int i = 0; i < n; i++) vertex[vec[i]] = i;
  while (m--) {
    long double com = 0;
    cin >> x1 >> y1 >> x2 >> y2;
    complex<long double> a = complex<long double>(x1, y1),
                         b = complex<long double>(x2, y2);
    vector<complex<long double> > inter;
    for (int i = 0; i < n; i++)
      if (fabs(((conj(((b) - (a))) * (((vec[i]) - (a)))).imag())) < 1e-7L) {
        int j = (i - 1 + n) % n;
        int k = (i + 1) % n;
        if (((conj(((b) - (a))) * (((vec[j]) - (a)))).imag()) *
                ((conj(((b) - (a))) * (((vec[k]) - (a)))).imag()) <
            1e-7L)
          inter.push_back(vec[i]);
      }
    for (int i = 0; i < n; i++) {
      int j = (i + 1) % n;
      complex<long double> d = lineIntersection(vec[i], vec[j], a, b);
      if (pcmp(d, complex<long double>(1e+9, 1e+9)) &&
          pointOnSegment(vec[i], vec[j], d))
        inter.push_back(d);
    }
    sort(inter.begin(), inter.end(), pcmp);
    int sz = inter.size();
    bool f = 1;
    for (int i = 0; i < sz - 1; i++) {
      bool flag = 1;
      if (f)
        flag = 0, com += (hypotl((((inter[i + 1]) - (inter[i]))).imag(),
                                 (((inter[i + 1]) - (inter[i]))).real()));
      f = !f;
      if (vertex.count(inter[i]) && vertex.count(inter[i + 1])) {
        int a = vertex[inter[i]], b = vertex[inter[i + 1]], c = b + 1;
        int d = abs(a - b);
        if (d != 1 && d != n - 1) continue;
        if (a > b && b) {
          c = a;
          a = b - 1;
        }
        if (c < n && a >= 0) {
          if (flag)
            com += (hypotl((((inter[i + 1]) - (inter[i]))).imag(),
                           (((inter[i + 1]) - (inter[i]))).real()));
          f = (((conj(((vec[b]) - (vec[a]))) * (((vec[c]) - (vec[a]))))
                    .imag()) < 1e-7L);
        }
      }
    }
    cout << com << endl;
  }
  return 0;
}