#include <bits/stdc++.h>
using namespace std;
int n, q;
int k, cnt, pcnt;
int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); }
struct node {
  int x, y, id;
} vec[300000 + 5];
struct Query {
  int l, r, id;
} Q[300000 + 5];
int pabs(int x) { return (x < 0) ? -x : x; }
int L[300000 + 5], R[300000 + 5], col[300000 + 5], C[300000 + 5],
    ans[300000 + 5];
bool cmp(node s1, node s2) {
  if (s1.x == s2.x) return s1.y < s2.y;
  return s1.x < s2.x;
}
bool cmp2(Query s1, Query s2) {
  if ((s1.l - 1) / 548 != (s2.l - 1) / 548) return s1.l < s2.l;
  return s1.r < s2.r;
}
void Insert(int x) {
  if (!C[x]) pcnt++;
  C[x]++;
}
void popout(int x) {
  C[x]--;
  if (!C[x]) pcnt--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k);
    L[i] = cnt + 1, R[i] = cnt + k;
    int x1, y1, x, y, px, py;
    scanf("%d%d", &x1, &y1);
    px = x1, py = y1;
    for (int j = 2; j <= k; j++) {
      scanf("%d%d", &x, &y);
      int G = gcd(pabs(x - px), pabs(y - py));
      vec[++cnt] = (node){(x - px) / G, (y - py) / G, cnt};
      px = x, py = y;
    }
    int G = gcd(pabs(x1 - x), pabs(y1 - y));
    vec[++cnt] = (node){(x1 - x) / G, (y1 - y) / G, cnt};
  }
  sort(vec + 1, vec + cnt + 1, cmp);
  int num = 0;
  for (int i = 1; i <= cnt; i++) {
    if (vec[i].x != vec[i - 1].x || vec[i].y != vec[i - 1].y) num++;
    col[vec[i].id] = num;
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int idl, idr;
    scanf("%d%d", &idl, &idr);
    Q[i] = (Query){L[idl], R[idr], i};
  }
  sort(Q + 1, Q + q + 1, cmp2);
  int L = 1, R = 0;
  for (int i = 1; i <= q; i++) {
    while (L < Q[i].l) popout(col[L++]);
    while (L > Q[i].l) Insert(col[--L]);
    while (R < Q[i].r) Insert(col[++R]);
    while (R > Q[i].r) popout(col[R--]);
    ans[Q[i].id] = pcnt;
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
