Problem: <image>


As shown in the figure above, cut a convex polygon g by a line p1p2 and print the area of the cut polygon which is on the left-hand side of the line.

g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n−1) are sides of the convex polygon. The line segment connecting pn and p1 is also a side of the polygon.

Constraints

* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 100
* -10000 ≤ xi, yi ≤ 10000
* -10000 ≤ p1x,p1y,p2x,p2y ≤ 10000
* No point in g will occur more than once.
* p1 ≠ p2

Input

The input is given in the following format:


g (the sequence of the points of the polygon)
q (the number of queries = the number of target lines)
1st query
2nd query
:
qth query


g is given as a sequence of points p1,..., pn in the following format:


n
x1 y1
x2 y2
:
xn yn


The first integer n is the number of points. The coordinate of the i-th point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them. Note that all interior angles of given convex polygons are less than or equal to 180.

For each query, a line represented by two points p1 and p2 is given. The coordinates of the points are given by four integers p1x, p1y, p2x and p2y.

Output

For each query, print the area of the cut polygon. The output values should be in a decimal fraction with an error less than 0.00001.

Example

Input

4
1 1
4 1
4 3
1 3
2
2 0 2 4
2 4 2 0


Output

2.00000000
4.00000000

Solution (in CPP):

Solution:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

class Vec;
Vec operator/(const Vec& v, const double& divisor);
Vec operator*(const double& scale, const Vec& v);

class Line;
template <typename G, typename H>
pair<std::vector<Vec>, bool> intersection(const G& lh, const H& rh, double eps);

class Vec {
  using F = double;
  vector<F> container;

 public:
  Vec(size_t size) : container(size) {}
  Vec(initializer_list<F> elements)
      : container(elements.begin(), elements.end()) {}
  size_t dim() const { return container.size(); }
  F& operator[](const size_t size) { return container.at(size); }
  const F& operator[](const size_t size) const { return container.at(size); }
  Vec operator+(const Vec& other) const {
    Vec ret(*this);
    for (size_t i = 0; i < dim(); i++) ret[i] += other[i];
    return ret;
  }
  Vec operator-(const Vec& other) const { return (*this) + (-other); }
  Vec operator-() const { return -1.0 * (*this); }
  F inner(const Vec& other) const {
    F acc = 0;
    for (size_t i = 0; i < dim(); i++) {
      acc += (*this)[i] * other[i];
    }
    return acc;
  }
  F norm() const { return sqrt(inner(*this)); }
  Vec unit() const { return (*this) / this->norm(); }
  F ccw(const Vec& other) const {
    return (*this)[0] * other[1] - other[0] * (*this)[1];
  }
  Vec normal() const { return Vec({-(*this)[1], (*this)[0]}); }
};
Vec operator/(const Vec& v, const double& divisor) { return 1.0 / divisor * v; }
Vec operator*(const double& scale, const Vec& v) {
  Vec ret(v);
  for (size_t i = 0; i < ret.dim(); i++) ret[i] *= scale;
  return ret;
}

class Line {
  Vec m_grad;
  double m_bias;

 public:
  Line(const Vec& grad, double bias) : m_grad(grad), m_bias(bias) {}
  static Line fromPoints(const Vec& p0, const Vec& p1) {
    Vec d = p0 - p1;
    Vec g({-d[1], d[0]});
    return Line(g, g.inner(p0));
  }
  const Vec& grad() const { return m_grad; }
  double bias() const { return m_bias; }
  double dist(const Vec& p) const {
    return abs(m_grad.inner(p) - m_bias) / m_grad.norm();
  }
  Vec projection(const Vec& p) const {
    const Vec g = m_grad.unit();
    const Vec h({-g[1], g[0]});
    return m_bias / m_grad.norm() * g + h.inner(p) * h;
  }
};

template <>
pair<vector<Vec>, bool> intersection(const Line& lh, const Line& rh,
                                     double eps) {
  const Vec &g1 = lh.grad(), g2 = rh.grad();
  const double c1 = lh.bias(), c2 = rh.bias();
  const Vec g = g1.unit();
  const Vec h = g.normal();

  const double det = h.inner(g2);

  if (abs(det) / g2.norm() < eps) {
    return make_pair(vector<Vec>(), abs(c1 * g2.norm() - c2 * g1.norm()) < eps);
  } else {
    const double k = (c2 - g.inner(g2) * c1 / g1.norm()) / det;
    vector<Vec> result = vector<Vec>(1, c1 / g1.norm() * g + k * h);
    return make_pair(result, false);
  }
}

class Convex {
  const vector<Vec>& m_points;
  vector<int> m_hull;

 public:
  Convex(const vector<Vec>& points) : m_points(points), m_hull() {
    const size_t n = m_points.size();

    vector<int> idx(n);
    for (size_t i = 0; i < n; i++) idx[i] = i;
    sort(idx.begin(), idx.end(), [&](const int& lh, const int& rh) {
      if (m_points[lh][0] == m_points[rh][0]) {
        return m_points[lh][1] < m_points[rh][1];
      }
      return m_points[lh][0] < m_points[rh][0];
    });

    vector<int> conv[2];
    double si = 1;
    for (int j = 0; j < 2; j++) {
      vector<int>& cv = conv[j];
      for (size_t i = 0; i < n; i++) {
        const int k = idx[i];
        const Vec& c = m_points[k];
        while (cv.size() >= 2 &&
               si * (m_points[cv[cv.size() - 1]] - m_points[cv[cv.size() - 2]])
                           .ccw(c - m_points[cv[cv.size() - 2]]) <
                   0) {
          cv.pop_back();
        }
        cv.push_back(k);
      }
      si *= -1;
    }

    copy(conv[0].begin(), conv[0].end(), back_inserter(m_hull));
    copy(next(conv[1].rbegin()), prev(conv[1].rend()), back_inserter(m_hull));
  }
  double diameter() const {
    double result = 0;
    const size_t n = m_hull.size();
    int j = 0;
    for (size_t i = 0; i < n; i++) {
      double d = ((*this)[i] - (*this)[j % n]).norm();
      double e = ((*this)[i] - (*this)[(j + 1) % n]).norm();
      while (d < e) {
        j++;
        d = e;
        e = ((*this)[i] - (*this)[(j + 1) % n]).norm();
      }
      result = max(result, d);
    }
    return result;
  }
  size_t size() const { return m_hull.size(); }
  const vector<int>& ids() const { return m_hull; }
  const Vec& operator[](size_t index) const {
    return m_points.at(m_hull[index % size()]);
  }
};

int main() {
  int n;
  cin >> n;

  vector<Vec> p;
  rep(i, n) {
    double x, y;
    cin >> x >> y;
    p.push_back(Vec({x, y}));
  }

  auto hull = Convex(p);

  int q;
  cin >> q;
  rep(i, q) {
    double x0, y0, x1, y1;
    cin >> x0 >> y0 >> x1 >> y1;
    Vec p0({x0, y0}), p1({x1, y1});
    Vec v = p1 - p0;

    int s = -1, t = -1;
    bool left_empty = true;
    rep(i, n) {
      const double a = v.ccw(hull[i] - p0);
      const double b = v.ccw(hull[i + 1] - p0);
      if (!(a * b > 0)) {
        if (a > b) {
          t = i;
        } else {
          s = i;
        }
      }

      left_empty = (left_empty && !(a > 0) && !(b > 0));
    }

    double result = 0;
    if (!left_empty) {
      if (s < 0 || t < 0) {
        rep(i, n) { result += hull[i].ccw(hull[i + 1]); }
      } else {
        const double EPS = 1e-9;
        vector<Vec> c0, c1;
        bool eq1, eq2;
        tie(c0, eq1) =
            intersection(Line::fromPoints(p0, p1),
                         Line::fromPoints(hull[s], hull[s + 1]), EPS);
        tie(c1, eq2) =
            intersection(Line::fromPoints(p0, p1),
                         Line::fromPoints(hull[t], hull[t + 1]), EPS);

        if (c0.empty() || c1.empty()) {
          rep(i, n) { result += hull[i].ccw(hull[i + 1]); }
        } else {
          result +=
              hull[t].ccw(c1[0]) + c1[0].ccw(c0[0]) + c0[0].ccw(hull[s + 1]);

          for (int i = s + 1; i % n != t; i++) {
            result += hull[i].ccw(hull[i + 1]);
          }
        }
      }
    }

    // cerr << s << " " << t << "\t";
    cout << fixed << setprecision(7) << result / 2.0 << "\n";
  }
  return 0;
}