#include <bits/stdc++.h>
using namespace std;
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
const int M = 1e9 + 7;
const int FFTM = 998244353;
const int N = 6e5 + 7;
struct fenwik {
  vector<int> t;
  fenwik() { t.resize(N); }
  void upd(int p, int x) {
    p += 2;
    for (; p < N; p += p & -p) t[p] += x;
  }
  int q(int p) {
    p += 2;
    int res = 0;
    for (; p > 0; p -= p & -p) res += t[p];
    return res;
  }
  int q(int l, int r) { return q(r) - q(l - 1); }
};
int n, k, x[N], y[N], q, l, r;
vector<int> All[N];
vector<pair<int, int> > Q[N];
map<pair<long double, int>, int> all;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int K = 0;
  for (int i = 0; i < n; i++) {
    cin >> k;
    for (int j = 0; j < k; j++) cin >> x[j] >> y[j];
    x[k] = x[0];
    y[k] = y[0];
    int X = -1e9 - 7;
    for (int j = 0; j < k; j++) X = max(X, x[j]);
    for (int j = 0; j < k; j++) {
      pair<long double, int> now;
      if (x[j] == x[j + 1])
        now.first = -1e12 - 13;
      else
        now.first = (y[j] - y[j + 1]) / (long double)(x[j] - x[j + 1]);
      now.second = 0;
      if (x[j] < x[j + 1] || (x[j] == x[j + 1] && x[j] == X)) now.second = 1;
      if (!all.count(now)) all[now] = ++K;
      All[i].push_back(all[now]);
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) cin >> l >> r, l--, r--, Q[r].push_back({l, i});
  vector<int> res(q);
  vector<int> p(N, -1);
  fenwik t;
  for (int i = 0; i < n; i++) {
    for (auto it : All[i]) t.upd(p[it], -1), p[it] = i, t.upd(p[it], 1);
    for (auto it : Q[i]) res[it.second] = t.q(it.first, i);
  }
  for (int i = 0; i < q; i++) cout << res[i] << '\n';
  return 0;
}
