#include <bits/stdc++.h>
int from[10000], to[10000], parent[500], el[10001], er[10001];
int find(int i) {
  if (parent[i] == i) return i;
  return parent[i] = find(parent[i]);
}
int main() {
  int n, m, k, l, r, c, r1, r2, lc = 0, rc = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", from + i, to + i);
    --from[i];
    --to[i];
  }
  for (int i = 0; i < n; ++i) parent[i] = i;
  for (int i = 0; i < m; ++i) {
    r1 = find(from[i]);
    r2 = find(to[i]);
    if (r1 != r2) {
      parent[r1] = r2;
      ;
      el[lc++] = i;
    }
  }
  for (int i = 0; i < n; ++i) parent[i] = i;
  for (int i = m - 1; i >= 0; --i) {
    r1 = find(from[i]);
    r2 = find(to[i]);
    if (r1 != r2) {
      parent[r1] = r2;
      ;
      er[rc++] = i;
    }
  }
  el[lc] = m;
  er[rc] = -1;
  scanf("%d", &k);
  ++k;
  while (--k) {
    c = 0;
    for (int i = 0; i < n; ++i) parent[i] = i;
    scanf("%d%d", &l, &r);
    --l;
    --r;
    for (int i = 0; l > el[i]; ++i) {
      r1 = find(from[el[i]]);
      r2 = find(to[el[i]]);
      parent[r1] = r2;
      ;
    }
    for (int i = 0; r < er[i]; ++i) {
      r1 = find(from[er[i]]);
      r2 = find(to[er[i]]);
      parent[r1] = r2;
      ;
    }
    for (int i = 0; i < n; ++i) c += parent[i] == i;
    printf("%d\n", c);
  }
  return 0;
}
