#include <bits/stdc++.h>
using namespace std;
const int maxn = 509;
const int maxm = 2e4 + 3;
const int mod = 1e9 + 7;
const int oo = 1e9 + 5;
void input() {
  if (fopen("abc"
            ".inp",
            "r")) {
    freopen(
        "abc"
        ".inp",
        "r", stdin);
    freopen(
        "abc"
        ".out",
        "w", stdout);
  }
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int n, m, q, ntime = 0;
vector<int> g[maxn];
int root[maxn], ed[maxm];
bool used[maxm];
pair<int, int> e[maxm];
int get_root(int i) { return root[i] == i ? i : root[i] = get_root(root[i]); }
int main() {
  input();
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; ++i) {
    cin >> u >> v;
    e[i] = pair<int, int>(u, v);
  }
  memset(used, false, sizeof(used));
  int cnt = 0;
  for (int i = 1; i <= n; ++i) root[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = get_root(e[i].first), v = get_root(e[i].second);
    if (u == v) continue;
    if (!used[i]) {
      used[i] = true;
      ed[cnt++] = i;
    }
    root[v] = u;
  }
  for (int i = 1; i <= n; ++i) root[i] = i;
  for (int i = m; i > 0; --i) {
    int u = get_root(e[i].first), v = get_root(e[i].second);
    if (u == v) continue;
    if (!used[i]) {
      used[i] = true;
      ed[cnt++] = i;
    }
    root[v] = u;
  }
  cin >> q;
  int l, r;
  while (q--) {
    cin >> l >> r;
    for (int i = 1; i <= n; ++i) root[i] = i;
    for (int i = 0; i < cnt; ++i) {
      if (ed[i] < l || ed[i] > r) {
        int u = get_root(e[ed[i]].first);
        int v = get_root(e[ed[i]].second);
        if (u != v) root[v] = u;
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) ans += (root[i] == i);
    cout << ans << "\n";
  }
  return 0;
}
