#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9;
const long double PI = acos(-1);
struct pnt {
  long double x, y;
  pnt(long double x = 0, long double y = 0) : x(x), y(y) {}
  pnt operator-(const pnt &p) const { return pnt(x - p.x, y - p.y); }
  pnt operator+(const pnt &p) const { return pnt(x + p.x, y + p.y); }
  pnt operator*(long double p) const { return pnt(x * p, y * p); }
  pnt operator/(long double p) const { return pnt(x / p, y / p); }
  long double operator&(const pnt &p) const { return x * p.x + y * p.y; }
  long double operator^(const pnt &p) const { return x * p.y - y * p.x; }
  inline long double dist() { return sqrt(x * x + y * y); }
  void input() {
    double xx, yy;
    scanf("%lf %lf", &xx, &yy);
    x = (long double)xx, y = (long double)yy;
  }
  void print() { printf("%lf %lf\n", (double)x, (double)y); }
} p[1005];
int n;
bool inside(pnt a, pnt b, pnt c) {
  long double A = a - b & a - c;
  long double B = a - b ^ a - c;
  if (fabs(B) > EPS) return 0;
  return A <= 0;
}
int intersect(pnt a, pnt b, pnt c, pnt d, pnt &res) {
  if (fabs(b - a ^ d - c) < EPS) {
    if (fabs(b - c ^ a - c) < EPS) return -1;
    return 0;
  }
  long double chk1 = b - a ^ c - a;
  long double chk2 = b - a ^ d - a;
  if (chk1 > EPS && chk2 > EPS) return 0;
  if (chk1 < EPS && chk2 < EPS) return 0;
  long double s1 = b - a ^ d - c, s2 = b - c ^ a - c;
  long double s = s2 / s1;
  res = c + ((d - c) * s);
  return 1;
}
long double sto[1005];
pair<long double, long double> V[1005];
int main() {
  int m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) p[i].input();
  while (m--) {
    pnt a, b;
    a.input(), b.input();
    int cnt = 0, cv = 0;
    long double len = (b - a & b - a);
    for (int i = 0; i < n; i++) {
      pnt tp;
      int chk = intersect(a, b, p[i], p[(i + 1) % n], tp);
      if (chk > 0) {
        sto[cnt++] = (tp - a & b - a) / len;
      } else if (chk < 0) {
        long double x = (p[i] - a & b - a) / len;
        long double y = (p[(i + 1) % n] - a & b - a) / len;
        if (x > y) swap(x, y);
        V[cv++] = pair<long double, long double>(x, y);
      }
    }
    sort(sto, sto + cnt);
    for (int i = 0; i + 1 < cnt; i += 2)
      V[cv++] = pair<long double, long double>(sto[i], sto[i + 1]);
    sort(V, V + cv);
    long double rlt = 0, cur = -(1e18);
    len = sqrt(len);
    for (int i = 0; i < cv; i++) {
      if (cur < V[i].first) cur = V[i].first;
      if (cur < V[i].second) rlt += V[i].second - cur, cur = V[i].second;
    }
    printf("%.15lf\n", (double)(rlt * len));
  }
}
