#include <bits/stdc++.h>
using namespace std;
int n, comp = 0;
vector<pair<int, int> > edg;
int pa[100005];
int rnk[100005];
int ans[100005];
class query {
 public:
  int l, r, idx;
  query() {}
  query(int l, int r, int idx) {
    this->l = l;
    this->r = r;
    this->idx = idx;
  }
} Q[100005];
int cmp(query a, query b) {
  if (a.l < b.l) return true;
  if (a.l == b.l && a.r > b.r) return true;
  return false;
}
int find(int x) {
  if (pa[x] != x) pa[x] = find(pa[x]);
  return pa[x];
}
int uni(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return 0;
  if (rnk[x] >= rnk[y]) {
    pa[y] = x;
    if (rnk[x] == rnk[y]) rnk[x] += 1;
  } else
    pa[x] = y;
  return 1;
}
void init() {
  for (int i = 0; i <= n; i++) {
    pa[i] = i;
    rnk[i] = 1;
  }
  comp = n;
  return;
}
void add(int idx) {
  if (uni(edg[idx].first, edg[idx].second)) comp -= 1;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int m, x, y, q;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    edg.push_back({x, y});
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    x -= 1;
    y -= 1;
    Q[i] = query(x, y, i);
  }
  sort(Q, Q + q, cmp);
  int lft = -1, rgt = m, l, r;
  for (int i = 0; i < q; i++) {
    l = Q[i].l;
    r = Q[i].r;
    if (l != lft) {
      init();
      lft = 0;
      rgt = m - 1;
      while (lft < l) {
        add(lft);
        lft += 1;
      }
    }
    while (r < rgt) {
      add(rgt);
      rgt -= 1;
    }
    ans[Q[i].idx] = comp;
  }
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
  return 0;
}
