#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
map<pair<int, int>, int> mp;
vector<pair<int, int> > g[N];
int n, m, a[N], b[N], l[N], r[N], cnt, ans[N], tr[N], lst[N], Q, d;
inline void gcd(const int a, const int b) {
  return !b ? d = a, void() : gcd(b, a % b);
}
inline void add(register int x, const int num) {
  while (x) tr[x] += num, x -= (x & -x);
}
inline int que(register int x) {
  register int ans = 0;
  while (x <= cnt) ans += tr[x], x += (x & -x);
  return ans;
}
int main() {
  scanf("%d", &n);
  register int i, j, x, y;
  for (i = 1; i <= n; ++i) {
    scanf("%d", &m), l[i] = cnt + 1;
    for (j = 1; j <= m; ++j) scanf("%d%d", &a[j], &b[j]);
    for (j = 1; j <= m; ++j)
      ++cnt, x = a[j % m + 1] - a[j], y = b[j % m + 1] - b[j], gcd(x, y),
             d < 0 ? d = -d : 0, (!d ? d = 1 : 0), x /= d, y /= d,
             mp.count(make_pair(x, y)) ? lst[cnt] = mp[make_pair(x, y)] : 0,
             mp[make_pair(x, y)] = cnt;
    r[i] = cnt;
  }
  scanf("%d", &Q);
  for (i = 1; i <= Q; ++i)
    scanf("%d%d", &x, &y), g[r[y]].push_back(make_pair(l[x], i));
  for (i = 1; i <= cnt; ++i) {
    add(lst[i], -1), add(i, 1);
    for (const auto &j : g[i]) ans[j.second] = que(j.first);
  }
  for (i = 1; i <= Q; ++i) printf("%d\n", ans[i]);
  return 0;
}
