#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[505], a[10005], b[10005], q[505], h[505];
void init() {
  int i;
  for (i = 1; i <= n; i = i + 1) {
    f[i] = i;
  }
}
int gf(int t) {
  if (f[t] == t) return t;
  return f[t] = gf(f[t]);
}
int join(int a, int b) {
  a = gf(a);
  b = gf(b);
  f[a] = b;
  return a != b;
}
int main() {
  int i, j, cnt1, cnt2, qu;
  cin >> n >> m;
  for (i = 1; i <= m; i = i + 1) {
    cin >> a[i] >> b[i];
  }
  init();
  cnt1 = 0;
  for (i = 1; i <= m; i = i + 1) {
    if (join(a[i], b[i])) {
      q[cnt1] = i;
      ++cnt1;
    }
  }
  init();
  cnt2 = 0;
  for (i = m; i >= 1; i = i - 1) {
    if (join(a[i], b[i])) {
      h[cnt2] = i;
      ++cnt2;
    }
  }
  cin >> qu;
  for (i = 1; i <= qu; i = i + 1) {
    int l, r;
    cin >> l >> r;
    init();
    int num = n;
    for (j = 0; j < cnt1; j = j + 1) {
      int ed = q[j];
      if (ed < l) {
        if (join(a[ed], b[ed])) num = num - 1;
      } else
        break;
    }
    for (j = 0; j < cnt2; j = j + 1) {
      int ed = h[j];
      if (ed > r) {
        if (join(a[ed], b[ed])) num = num - 1;
      } else
        break;
    }
    cout << num << endl;
  }
  return 0;
}
