#include <bits/stdc++.h>
using namespace std;
pair<int, int> edges[10007];
struct query {
  int l, r, index;
  bool operator<(const query &other) const {
    if (l == other.l) return r > other.r;
    return l < other.l;
  }
} queries[20007];
class dsu {
 private:
  int p[505], r[505], n;
  stack<int> previous;

 public:
  int components;
  dsu(int n) : n(n), components(n) {
    for (int i = 0; i < n; ++i) {
      p[i] = i;
      r[i] = 0;
    }
  }
  int find(int x) {
    for (; p[x] != x; x = p[x])
      ;
    return x;
  }
  bool merge(int x, int y, bool save) {
    int px = find(x), py = find(y);
    if (px == py) return false;
    if (r[x] < r[y]) swap(x, y);
    if (save) {
      previous.push(py);
      previous.push(px);
    }
    p[py] = px;
    if (r[px] == r[py]) ++r[px];
    --components;
    return true;
  }
  void undo() {
    int px = previous.top();
    previous.pop();
    int py = previous.top();
    previous.pop();
    p[py] = py;
    if (r[px] - 1 == r[py]) --r[px];
    ++components;
  }
  void clean() {
    while (!previous.empty()) undo();
  }
};
int ans[20007];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) cin >> edges[i].first >> edges[i].second;
  dsu from_right(n + 1);
  vector<int> necessary_edges;
  for (int i = m; i > 0; --i) {
    if (from_right.merge(edges[i].first, edges[i].second, false))
      necessary_edges.push_back(i);
  }
  cin >> k;
  for (int i = 1; i <= k; ++i) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].index = i;
  }
  sort(queries + 1, queries + k + 1);
  dsu sets(n + 1);
  int left = 1, right, idx = 0;
  for (int i = 1; i <= k; ++i) {
    if (left != queries[i].l) {
      sets.clean();
      idx = 0;
    }
    for (; left < queries[i].l; ++left)
      sets.merge(edges[left].first, edges[left].second, false);
    for (; idx < necessary_edges.size();) {
      right = necessary_edges[idx];
      if (right <= queries[i].r) break;
      sets.merge(edges[right].first, edges[right].second, true);
      ++idx;
    }
    ans[queries[i].index] = sets.components - 1;
  }
  for (int i = 1; i <= k; ++i) cout << ans[i] << '\n';
}
