#include <bits/stdc++.h>
using namespace std;
int n, m;
int g[510];
void init() {
  for (int i = 1; i <= n; i++) g[i] = i;
}
int find(int x) {
  if (g[x] == x) return x;
  return g[x] = find(g[x]);
}
bool to(int a, int b) {
  int x = find(a), y = find(b);
  g[x] = y;
  return x != y;
}
int a[10010], b[10010];
int pre[510], suf[510];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
  }
  init();
  int cnt1 = 0;
  for (int i = 1; i <= m; i++) {
    if (to(a[i], b[i])) {
      pre[cnt1++] = i;
    }
  }
  init();
  int cnt2 = 0;
  for (int i = m; i >= 1; i--) {
    if (to(b[i], a[i])) {
      suf[cnt2++] = i;
    }
  }
  int T;
  cin >> T;
  while (T--) {
    int l, r;
    cin >> l >> r;
    init();
    int num = n;
    for (int i = 0; i < cnt1; i++) {
      int k = pre[i];
      if (k < l) {
        if (to(a[k], b[k])) num--;
      } else
        break;
    }
    for (int i = 0; i < cnt2; i++) {
      int k = suf[i];
      if (k > r) {
        if (to(a[k], b[k])) num--;
      } else
        break;
    }
    cout << num << endl;
  }
  return 0;
}
