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;
int n, m, ch[1005], tt;
long long x[1005], y[1005], xa, ya, xb, yb, xc[1005], yc[1005], cc[1005], xcc,
    ycc, ccc, tx, ty, ca, cb, tc;
double ta, tb, ans, taa, tbb;
pair<double, double> pt;
vector<pair<pair<pair<double, double>, pair<double, double> >, int> > xx;
bool ins;
double dist(pair<double, double>& a, pair<double, double>& b) {
  ta = a.first - b.first;
  tb = a.second - b.second;
  return sqrt(ta * ta + tb * tb);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int a = 0; a < n; a++) {
    scanf("%lf%lf", &ta, &tb);
    x[a] = (long long)round(ta * 200);
    y[a] = (long long)round(tb * 200);
  }
  for (int a = 0; a < m; a++) {
    ans = 0;
    xx.clear();
    scanf("%lf%lf", &ta, &tb);
    xa = (long long)round(ta * 200);
    ya = (long long)round(tb * 200);
    scanf("%lf%lf", &ta, &tb);
    xb = (long long)round(ta * 200);
    yb = (long long)round(tb * 200);
    xcc = ya - yb;
    ycc = xb - xa;
    ccc = xa * xcc + ya * ycc;
    for (int b = 0; b < n; b++) {
      xc[b] = y[b] - y[(b + 1) % n];
      yc[b] = x[(b + 1) % n] - x[b];
      cc[b] = x[b] * xc[b] + y[b] * yc[b];
      tx = (x[b] + x[(b + 1) % n]) / 2;
      ty = (y[b] + y[(b + 1) % n]) / 2;
      if (tx * xcc + ty * ycc == ccc)
        ch[b] = 0;
      else if (tx * xcc + ty * ycc > ccc)
        ch[b] = 1;
      else
        ch[b] = -1;
    }
    for (int b = 0; b < n; b++) {
      if (ch[b] == 0 && xc[b] * ycc == yc[b] * xcc)
        ans += sqrt((double)(xc[b] * xc[b] + yc[b] * yc[b]));
      else if (xcc * x[(b + 1) % n] + ycc * y[(b + 1) % n] != ccc) {
        if (xcc * x[b] + ycc * y[b] == ccc) {
          if (-ch[b] == ch[(b + n - 1) % n])
            xx.push_back(make_pair(
                make_pair(make_pair(x[b], y[b]), make_pair(0, 0)), 0));
          else if (ch[(b + n - 1) % n] == 0) {
            tt = (b + n - 1) % n;
            while (ch[tt] == 0) tt = (tt + n - 1) % n;
            if (ch[tt] == ch[b])
              xx.push_back(make_pair(
                  make_pair(make_pair(x[b], y[b]),
                            make_pair(x[(tt + 1) % n], y[(tt + 1) % n])),
                  1));
            else
              xx.push_back(make_pair(
                  make_pair(make_pair(x[b], y[b]),
                            make_pair(x[(tt + 1) % n], y[(tt + 1) % n])),
                  2));
            if (xx.back().first.first > xx.back().first.second)
              swap(xx.back().first.first, xx.back().first.second);
          }
        } else if ((xcc * x[b] + ycc * y[b] > ccc) ^
                   (xcc * x[(b + 1) % n] + ycc * y[(b + 1) % n] > ccc)) {
          ca = cc[b] / (1LL << 30);
          cb = ccc / (1LL << 30);
          ta = ca * ycc - yc[b] * cb;
          tb = xc[b] * cb - ca * xcc;
          ca = cc[b] - ca * (1LL << 30);
          cb = ccc - cb * (1LL << 30);
          taa = ca * ycc - yc[b] * cb;
          tbb = xc[b] * cb - ca * xcc;
          tc = xc[b] * ycc - yc[b] * xcc;
          xx.push_back(
              make_pair(make_pair(make_pair(ta / tc * (1LL << 30) + taa / tc,
                                            tb / tc * (1LL << 30) + tbb / tc),
                                  make_pair(0, 0)),
                        0));
        }
      }
    }
    sort(xx.begin(), xx.end());
    for (int b = 0; b < xx.size(); b++) {
      if (xx[b].second == 1) {
        if (ins) {
          ans += dist(pt, xx[b].first.first);
          pt = xx[b].first.second;
        }
      } else if (xx[b].second == 2) {
        if (ins)
          ans += dist(pt, xx[b].first.first);
        else
          pt = xx[b].first.second;
        ins = !ins;
      } else {
        if (ins) ans += dist(pt, xx[b].first.first);
        ins = !ins;
        pt = xx[b].first.first;
      }
    }
    printf("%.30lf\n", ans / 200);
  }
  return 0;
}