#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m, q, x[10010], y[10010], t = 100, par[510], sv[510], ans[20010];
vector<pair<pair<int, int>, int> > v[110];
int FIND(int pos) {
  if (par[pos] != pos) par[pos] = FIND(par[pos]);
  return par[pos];
}
int UNION(int pos, int pos2) {
  int xx = FIND(pos), yy = FIND(pos2);
  if (xx == yy) return 0;
  par[xx] = yy;
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) scanf("%d%d", &x[i], &y[i]);
  cin >> q;
  for (int qn = 0; qn < q; qn++) {
    int xx, yy;
    scanf("%d%d", &xx, &yy);
    v[yy / t].push_back(make_pair(make_pair(xx - 1, yy - 1), qn));
  }
  for (int i = 0; i < 105; i++) sort(v[i].begin(), v[i].end());
  for (int i = 0; i < 104; i++) {
    if (v[i].size() == 0) continue;
    int mxr = 0, res = n, svres;
    for (int j = 0; j < v[i].size(); j++) mxr = max(mxr, v[i][j].first.second);
    for (int j = 0; j < 505; j++) par[j] = j;
    for (int j = 0; j < v[i][0].first.first; j++) res -= UNION(x[j], y[j]);
    for (int j = m - 1; j > mxr; j--) res -= UNION(x[j], y[j]);
    int lb = v[i][0].first.first;
    for (int j = 0; j < v[i].size(); j++) {
      while (lb < v[i][j].first.first) res -= UNION(x[lb], y[lb]), lb++;
      memcpy(sv, par, sizeof(sv));
      svres = res;
      for (int k = v[i][j].first.second + 1; k <= mxr; k++)
        res -= UNION(x[k], y[k]);
      ans[v[i][j].second] = res;
      memcpy(par, sv, sizeof(par));
      res = svres;
    }
  }
  for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
  return 0;
}
