#include <bits/stdc++.h>
using namespace std;
int m, n, x[10005], y[10005], k, l, r;
struct dsu {
  int f[505];
  int find(int x) {
    if (f[x] == x) return f[x];
    return f[x] = find(f[x]);
  }
  void unionn(int x, int y) {
    int fa = find(x);
    int fb = find(y);
    if (fa != fb) f[fa] = fb;
  }
} pre[10005], suf[10005], t;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) pre[0].f[i] = suf[m + 1].f[i] = i;
  for (int i = 1; i <= m; i++) cin >> x[i] >> y[i];
  for (int i = 1; i <= m; i++) pre[i] = pre[i - 1], pre[i].unionn(x[i], y[i]);
  for (int i = m; i >= 1; i--) suf[i] = suf[i + 1], suf[i].unionn(x[i], y[i]);
  cin >> k;
  for (int i = 1; i <= k; i++) {
    cin >> l >> r;
    t = pre[l - 1];
    for (int j = 1; j <= n; j++) {
      t.unionn(suf[r + 1].f[j], j);
    }
    int ans = 0;
    for (int j = 1; j <= n; j++)
      if (t.f[j] == j) ans++;
    cout << ans << endl;
  }
}
