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>
typedef struct point {
  long double x, y;
} point;
typedef struct line {
  long double k, b;
} line;
point createPoint(long double x, long double y) {
  point A;
  A.x = x;
  A.y = y;
  return A;
}
line createLine(point A, point B) {
  long double a = A.x, b = A.y, c = B.x, d = B.y;
  line l;
  if (fabsl(c - a) < 1e-11L) {
    l.k = 1e+11L;
    l.b = c;
  } else {
    l.k = (d - b) / (c - a);
    l.b = d - l.k * c;
  }
  return l;
}
int sign(line l, point A) {
  long double k = l.k, b = l.b, x = A.x, y = A.y;
  if (fabsl(k - 1e+11L) < 1e-11L) {
    if (fabsl(b - x) < 1e-11L)
      return 0;
    else if (x > b)
      return 1;
    else
      return -1;
  } else {
    if (fabsl(y - (k * x + b)) < 1e-11L)
      return 0;
    else if (y > k * x + b)
      return 1;
    else
      return -1;
  }
}
void swap(line *l, line *ll) {
  long double temp = l->k;
  l->k = ll->k;
  ll->k = temp;
  temp = l->b;
  l->b = ll->b;
  ll->b = temp;
}
point intersect(line l, line ll) {
  point p;
  if (fabsl(ll.k - 1e+11L) < 1e-11L) swap(&l, &ll);
  if (fabsl(l.k - 1e+11L) < 1e-11L) {
    if (fabsl(ll.k - 1e+11L) < 1e-11L) {
      if (fabsl(l.b - ll.b) < 1e-11L) {
        p.x = 0;
        p.y = 1e+11L;
      } else {
        p.x = 1e+11L;
        p.y = 1e+11L;
      }
    } else {
      p.x = l.b;
      p.y = ll.k * p.x + ll.b;
    }
  } else if (fabsl(ll.k - l.k) < 1e-11L) {
    if (fabsl(ll.b - l.b) < 1e-11L) {
      p.x = 0;
      p.y = 1e+11L;
    } else {
      p.x = 1e+11L;
      p.y = 1e+11L;
    }
  } else {
    p.x = (l.b - ll.b) / (ll.k - l.k);
    p.y = l.k * p.x + l.b;
  }
  return p;
}
long double dist(point A, point B) {
  return sqrtl((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
}
void swapP(point *a, point *b) {
  point temp = *a;
  *a = *b;
  *b = temp;
}
void swapInt(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}
long double len(int n, point p[1002], line l) {
  point onLinePoint[1002];
  int onLineCount = 0;
  int onLineNum[1002], pSign[1002];
  long double length = 0;
  pSign[n - 1] = sign(l, p[n - 1]);
  for (int i = 0; i < n; ++i) {
    pSign[i] = sign(l, p[i]);
    if (pSign[i] == 0) {
      onLinePoint[onLineCount] = p[i];
      onLineNum[onLineCount++] = i;
    } else if (pSign[i] * pSign[(n + i - 1) % n] < 0) {
      line ll = createLine(p[(n + i - 1) % n], p[i]);
      onLinePoint[onLineCount] = intersect(l, ll);
      onLineNum[onLineCount++] = i;
    }
    int j = onLineCount - 1;
    while (j > 0 && (onLinePoint[j - 1].x > onLinePoint[j].x ||
                     (fabsl(onLinePoint[j - 1].x - onLinePoint[j].x) < 1e-11L &&
                      onLinePoint[j - 1].y > onLinePoint[j].y))) {
      swapP(&onLinePoint[j - 1], &onLinePoint[j]);
      swapInt(&onLineNum[j - 1], &onLineNum[j]);
      --j;
    }
  }
  for (int i = 0; i < onLineCount - 1; ++i) {
    int intersectNum = 0, prevSign = 0, firstSign = 0, lastSign = 0;
    for (int j = i + 1; j < onLineCount; ++j) {
      if (pSign[onLineNum[j]] != 0 ||
          (pSign[(onLineNum[j] + n - 1) % n] * pSign[(onLineNum[j] + 1) % n] <
           0))
        intersectNum += 1;
      else if (pSign[(onLineNum[j] + n - 1) % n] !=
               pSign[(onLineNum[j] + 1) % n]) {
        if (prevSign * (pSign[(onLineNum[j] + n - 1) % n] +
                        pSign[(onLineNum[j] + 1) % n]) <
            0) {
          intersectNum += 1;
          prevSign = 0;
        } else
          lastSign = prevSign =
              pSign[(onLineNum[j] + n - 1) % n] + pSign[(onLineNum[j] + 1) % n];
        if (firstSign == 0) firstSign = prevSign;
      }
    }
    if (firstSign != lastSign) intersectNum++;
    if ((pSign[onLineNum[i]] == 0 && pSign[onLineNum[i + 1]] == 0 &&
             (dist(onLinePoint[i + 1], p[(onLineNum[i] + 1) % n]) < 1e-11L ||
              dist(onLinePoint[i + 1], p[(onLineNum[i] + n - 1) % n]) <
                  1e-11L) ||
         (intersectNum > 0 && intersectNum % 2 == 1)))
      length += dist(onLinePoint[i], onLinePoint[i + 1]);
  }
  return length;
}
int main() {
  int n, m;
  double x, y, a, b;
  point p[1002], p1, p2;
  line l[101];
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%lf%lf", &x, &y);
    p[i] = createPoint(x, y);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%lf%lf%lf%lf", &x, &y, &a, &b);
    p1 = createPoint(x, y);
    p2 = createPoint(a, b);
    l[i] = createLine(p1, p2);
    printf("%20.20f\n", (double)len(n, p, l[i]));
  }
}