#include <bits/stdc++.h>
using namespace std;
struct DSU {
  vector<int> pv;
  vector<int> h;
  DSU(int n) : pv(n, 0), h(n, 1) {
    for (int i = 0; i < n; ++i) pv[i] = i;
  }
  int get(int v) { return pv[v] == v ? v : pv[v] = get(pv[v]); }
  void unite(int u, int v) {
    u = get(u), v = get(v);
    if (h[v] < h[u]) swap(u, v);
    pv[u] = v;
    if (h[u] == h[v]) ++h[u];
  }
  int components() {
    unordered_set<int> st;
    for (int i = 0; i < pv.size(); ++i) st.insert(get(i));
    return st.size();
  }
};
vector<DSU> ds[2];
vector<pair<int, int>> inp;
int unite(DSU d1, DSU d2) {
  for (int i = 0; i < d1.pv.size(); ++i) d1.unite(i, d2.get(i));
  return d1.components();
}
int main() {
  int n, m;
  cin >> n >> m;
  inp.resize(m);
  for (pair<int, int>& p : inp) cin >> p.first >> p.second;
  DSU d(n);
  int i;
  for (i = 0; i < m; ++i) {
    d.unite(inp[i].first - 1, inp[i].second - 1);
    ds[0].push_back(d);
  }
  d = DSU(n);
  for (i = m - 1; i >= 0; --i) {
    d.unite(inp[i].first - 1, inp[i].second - 1);
    ds[1].push_back(d);
  }
  reverse(ds[1].begin(), ds[1].end());
  int k;
  cin >> k;
  while (k--) {
    int l, r;
    cin >> l >> r;
    if (l == 1 && r == m)
      cout << n;
    else if (l == 1)
      cout << ds[1][r].components();
    else if (r == m)
      cout << ds[0][l - 2].components();
    else
      cout << unite(ds[0][l - 2], ds[1][r]);
    cout << endl;
  }
  return 0;
}
