#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e4 + 5;
const int mxK = 26;
struct qry {
  int l, r, i;
  bool operator<(const qry& q) const { return r > q.r; }
};
vector<qry> qs[mxN];
struct DSU {
  vector<int> size;
  vector<int> p;
  int comps;
  void copy(DSU& dsu) {
    dsu.size = size;
    dsu.p = p;
    dsu.comps = comps;
  }
  void build(int n) {
    size = vector<int>(n, 1);
    p = vector<int>(n);
    iota(p.begin(), p.end(), 0);
    comps = n;
  }
  int get(int x) {
    if (p[x] != x) {
      p[x] = get(p[x]);
    }
    return p[x];
  }
  bool unite(int a, int b) {
    a = get(a);
    b = get(b);
    if (a == b) return false;
    if (size[a] > size[b]) swap(a, b);
    p[a] = b;
    size[b] += size[a];
    comps--;
    return true;
  }
};
int ans[mxN];
struct edge {
  int u, v;
};
edge edges[mxN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    edges[i] = {u, v};
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    qs[l].push_back({l, r, i});
  }
  DSU save;
  save.build(n);
  for (int l = 1; l <= m; l++) {
    sort(qs[l].begin(), qs[l].end());
    DSU dsu;
    save.copy(dsu);
    int r = m;
    for (auto qr : qs[l]) {
      while (r > qr.r) {
        dsu.unite(edges[r].u, edges[r].v);
        r--;
      }
      ans[qr.i] = dsu.comps;
    }
    save.unite(edges[l].u, edges[l].v);
  }
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
