#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-12;
const int maxn = 1000 + 5;
const int maxm = 100 + 5;
struct Point {
  long double x, y;
  Point(long double x_ = 0, long double y_ = 0) : x(x_), y(y_) {}
  void read() {
    double x_, y_;
    scanf("%lf%lf", &x_, &y_);
    x = x_;
    y = y_;
  }
};
Point operator+(const Point& a, const Point& b) {
  return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point& a, const Point& b) {
  return Point(a.x - b.x, a.y - b.y);
}
Point operator*(const Point& a, const double b) {
  return Point(a.x * b, a.y * b);
}
double operator*(const Point& a, const Point& b) {
  return a.x * b.y - a.y * b.x;
}
double operator%(const Point& a, const Point& b) {
  return a.x * b.x + a.y * b.y;
}
bool operator<(const Point& a, const Point& b) {
  return a.x + eps < b.x || (abs(a.x - b.x) < eps && a.y + eps < b.y);
}
Point po[maxn], poly[maxn];
Point e[maxn];
int N, M, E;
int main() {
  int i, j, k;
  long double t1, t2, ans;
  Point a1, a2, a3, b1, b2;
  scanf("%d%d", &N, &M);
  for (i = 1; i <= N; i++) po[i].read();
  t1 = 0;
  for (i = 1; i <= N; i++) t1 += po[i] * po[i < N ? i + 1 : 1];
  if (t1 < 0) {
    for (i = 1, j = N; i < j; i++, j--) swap(po[i], po[j]);
  }
  for (i = 1, j = 0; i <= N; i++) {
    t1 = (po[i > 1 ? i - 1 : N] - po[i]) * (po[i < N ? i + 1 : 1] - po[i]);
    if (abs(t1) < eps) continue;
    poly[++j] = po[i];
  }
  N = j;
  for (k = 1; k <= M; k++) {
    b1.read();
    b2.read();
    if (b2 < b1) swap(b1, b2);
    E = 0;
    for (i = 1; i <= N; i++) {
      a1 = poly[i];
      a2 = poly[i < N ? i + 1 : 1];
      t1 = (b1 - a1) * (b2 - a1);
      t1 = abs(t1) < eps ? 0 : t1;
      t2 = (b2 - a2) * (b1 - a2);
      t2 = abs(t2) < eps ? 0 : t2;
      if (t1 * t2 < -eps) continue;
      if (t1 == 0 || t2 == 0) continue;
      e[++E] = (a1 * t2 + a2 * t1) * (1.0 / (t1 + t2));
    }
    for (i = 1; i <= N; i++) {
      a1 = poly[i > 1 ? i - 1 : N];
      a2 = poly[i];
      a3 = poly[i < N ? i + 1 : 1];
      t1 = (a2 - b1) * (b2 - b1);
      if (abs(t1) > eps) continue;
      t1 = (a1 - a2) * (b2 - b1);
      if (abs(t1) > eps) {
        t2 = (a3 - a2) * (b2 - b1);
        if (abs(t2) > eps && t1 * t2 < 0)
          e[++E] = a2;
        else if (abs(t2) < eps) {
          t1 = (a2 - a1) * (a3 - a2);
          if (t1 > eps) e[++E] = a2;
        }
      } else {
        t1 = (a2 - a1) * (a3 - a2);
        if (t1 > eps) e[++E] = a2;
      }
    }
    sort(e + 1, e + E + 1);
    ans = 0;
    for (i = 1; i < E; i += 2)
      ans += sqrt((e[i + 1] - e[i]) % (e[i + 1] - e[i]));
    printf("%.20lf\n", (double)(ans));
  }
  return 0;
}
