#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = 2 * acos(0);
struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
  Point(const Point& p) : x(p.x), y(p.y) {}
  bool operator<(const Point& rhs) const {
    return make_pair(y, x) < make_pair(rhs.y, rhs.x);
  }
  bool operator==(const Point& rhs) const {
    return make_pair(y, x) == make_pair(rhs.y, rhs.x);
  }
  Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }
  Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }
  Point operator*(double c) const { return Point(x * c, y * c); }
  Point operator/(double c) const { return Point(x / c, y / c); }
};
double cross(Point p, Point q) { return p.x * q.y - p.y * q.x; }
double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; }
double dist(Point p, Point q) { return sqrt(dot(p - q, p - q)); }
Point Intersection(Point a, Point b, Point c, Point d) {
  b = b - a;
  d = c - d;
  c = c - a;
  return a + b * cross(c, d) / cross(b, d);
}
const int maxn = 1010;
int n, m;
void solve() {
  cin >> n >> m;
  vector<Point> poly;
  for (int i = (0); i < (n); i++) {
    double x, y;
    cin >> x >> y;
    poly.push_back(Point(x, y));
  }
  for (int i = (0); i < (m); i++) {
    Point a, b;
    cin >> a.x >> a.y >> b.x >> b.y;
    vector<pair<Point, int> > vp;
    for (int j = (0); j < (n); j++) {
      Point u = poly[j], v = poly[(j + 1) % n], w = poly[(j + n - 1) % n];
      if (cross(u - a, b - a) * cross(v - a, b - a) < 0) {
        Point p = Intersection(a, b, u, v);
        if (cross(u - a, b - a) > 0)
          vp.push_back(make_pair(p, 2));
        else
          vp.push_back(make_pair(p, -2));
      }
      if (fabs(cross(u - a, b - a)) < 1e-9) {
        int r = 0;
        if (cross(w - a, b - a) > 1e-9)
          r++;
        else if (cross(w - a, b - a) < -1e-9)
          r--;
        if (cross(v - a, b - a) > 1e-9)
          r--;
        else if (cross(v - a, b - a) < -1e-9)
          r++;
        vp.push_back(make_pair(u, r));
      }
    }
    sort((vp).begin(), (vp).end());
    double ans = 0;
    int tot = 0;
    for (int j = (0); j < (int((vp).size()) - 1); j++) {
      tot += vp[j].second;
      if (tot) ans += dist(vp[j].first, vp[j + 1].first);
    }
    cout << fixed << setprecision(9) << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  solve();
  return 0;
}
