#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> fa(1);
vector<vector<int> > pre;
vector<vector<int> > suf;
vector<pair<int, int> > g;
int getfa(vector<int> &par, int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = getfa(par, par[x]);
}
void Merge(vector<int> &par, int a, int b) {
  int x = getfa(par, a), y = getfa(par, b);
  if (x == y) return;
  par[x] = y;
  return;
}
inline int solve(int l, int r) {
  vector<int> a = pre[l - 1], b = suf[m - r];
  for (int i = 1; i <= n; i++) {
    Merge(a, i, b[i]);
  }
  int ret = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == i) ret++;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    fa.push_back(i);
  }
  for (int j = 1; j <= m; j++) {
    int a, b;
    cin >> a >> b;
    g.push_back(make_pair(a, b));
  }
  pre.push_back(fa);
  for (int i = 1; i <= m; i++) {
    Merge(fa, g[i - 1].first, g[i - 1].second);
    pre.push_back(fa);
  }
  for (int i = 1; i <= n; i++) fa[i] = i;
  reverse(g.begin(), g.end());
  suf.push_back(fa);
  for (int i = 1; i <= m; i++) {
    Merge(fa, g[i - 1].first, g[i - 1].second);
    suf.push_back(fa);
  }
  int k;
  cin >> k;
  while (k--) {
    int l, r;
    cin >> l >> r;
    int ans = solve(l, r);
    cout << ans << endl;
  }
  return 0;
}
