#include <bits/stdc++.h>
using namespace std;
const int MaxN = 510, MaxM = 11000;
int n, m, k, lf[MaxM][MaxN * 2], rf[MaxM][MaxN * 2], f[MaxN * 2];
pair<int, int> e[MaxM];
int Get(int *f, const int &t) {
  if (f[t] != t) f[t] = Get(f, f[t]);
  return f[t];
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) cin >> e[i].first >> e[i].second;
  for (int i = 1; (lf[0][i] = rf[m + 1][i] = i) <= n * 2; ++i)
    ;
  lf[0][0] = rf[m + 1][0] = n;
  for (int i = 1, x, y; i <= m; ++i) {
    memcpy(lf[i], lf[i - 1], sizeof(lf[i]));
    if ((x = Get(lf[i], e[i].first)) != (y = Get(lf[i], e[i].second))) {
      lf[i][y] = x;
      --lf[i][0];
    }
  }
  for (int i = m, x, y; i; --i) {
    memcpy(rf[i], rf[i + 1], sizeof(rf[i]));
    if ((x = Get(rf[i], e[i].first + n)) != (y = Get(rf[i], e[i].second + n))) {
      rf[i][y] = x;
      --rf[i][0];
    }
  }
  cin >> k;
  for (int i = 1, l, r; i <= k; ++i) {
    cin >> l >> r;
    memcpy(f + 1, lf[l - 1] + 1, sizeof(f[0]) * n);
    memcpy(f + n + 1, rf[r + 1] + n + 1, sizeof(f[0]) * n);
    f[0] = lf[l - 1][0] + rf[r + 1][0];
    for (int i = 1, x, y; i <= n; ++i)
      if ((x = Get(f, i)) != (y = Get(f, n + i))) {
        f[y] = x;
        --f[0];
      }
    cout << f[0] << endl;
  }
  return 0;
}
