#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, K, zuo[1000], you[1000], father[1000], a[200000], b[200000];
int find(int i) {
  if (father[i] == 0) return i;
  father[i] = find(father[i]);
  return father[i];
}
void insert(int i) {
  if (find(a[i]) != find(b[i])) father[find(a[i])] = find(b[i]);
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) cin >> a[i] >> b[i];
  memset(father, 0, sizeof(father));
  for (i = 1; i <= m; i++) {
    if (find(a[i]) != find(b[i])) {
      father[find(a[i])] = find(b[i]);
      zuo[0]++;
      zuo[zuo[0]] = i;
    }
  }
  memset(father, 0, sizeof(father));
  for (i = m; i >= 1; i--) {
    if (find(a[i]) != find(b[i])) {
      father[find(a[i])] = find(b[i]);
      you[0]++;
      you[you[0]] = i;
    }
  }
  cin >> K;
  for (i = 1; i <= K; i++) {
    memset(father, 0, sizeof(father));
    int l, r;
    cin >> l >> r;
    for (j = 1; j <= zuo[0]; j++) {
      if (zuo[j] < l)
        insert(zuo[j]);
      else
        break;
    }
    for (j = 1; j <= you[0]; j++) {
      if (you[j] > r)
        insert(you[j]);
      else
        break;
    }
    int cnt = 0;
    for (j = 1; j <= n; j++)
      if (father[j] == 0) cnt++;
    cout << cnt << endl;
  }
  return 0;
}
