#include <bits/stdc++.h>
using namespace std;
struct cable {
  int u, v;
} a[10005];
int n, m, par[505], prepar[10005][505], sufpar[10005][505];
int rt(long k) { return (par[k] < 0) ? k : (par[k] = rt(par[k])); }
bool mrg(int u, int v) {
  if ((u = rt(u)) == (v = rt(v))) return false;
  if (u > v) swap(u, v);
  par[u] += par[v];
  par[v] = u;
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) cin >> a[i].u >> a[i].v;
  memset(prepar[0], -1, sizeof(prepar[0]));
  memset(par, -1, sizeof(par));
  for (int i = 1; i <= m; ++i) {
    mrg(a[i].u, a[i].v);
    for (int j = 1; j <= n; ++j) prepar[i][j] = par[j];
  }
  memset(sufpar[m + 1], -1, sizeof(sufpar[m + 1]));
  memset(par, -1, sizeof(par));
  for (int i = m; i; --i) {
    mrg(a[i].u, a[i].v);
    for (int j = 1; j <= n; ++j) sufpar[i][j] = par[j];
  }
  int q, l, r, ans;
  cin >> q;
  while (q--) {
    cin >> l >> r;
    --l;
    ++r;
    ans = n;
    memset(par, -1, sizeof(par));
    for (int i = 1; i <= n; ++i) {
      if (prepar[l][i] > 0) ans -= mrg(i, prepar[l][i]);
      if (sufpar[r][i] > 0) ans -= mrg(i, sufpar[r][i]);
    }
    cout << ans << '\n';
  }
  return 0;
}
