#include <bits/stdc++.h>
using namespace std;
const int MAX = 10000 + 10;
int n, m;
int a[MAX], b[MAX], fa[MAX];
vector<int> ch1[MAX], ch2[MAX];
int findfather(int u) { return u == fa[u] ? u : (fa[u] = findfather(fa[u])); }
int main() {
  int i;
  scanf("%d%d", &n, &m);
  for ((i) = (1); (i) <= (n); ++(i)) fa[i] = i;
  for ((i) = (1); (i) <= (m); ++(i)) {
    scanf("%d%d", &a[i], &b[i]);
    ch1[i] = ch1[i - 1];
    if (findfather(a[i]) != findfather(b[i])) {
      fa[findfather(a[i])] = b[i];
      ch1[i].push_back(i);
    }
  }
  for ((i) = (1); (i) <= (n); ++(i)) fa[i] = i;
  for (i = m; i >= 1; --i) {
    ch2[i] = ch2[i + 1];
    if (findfather(a[i]) != findfather(b[i])) {
      fa[findfather(a[i])] = b[i];
      ch2[i].push_back(i);
    }
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int l, r, i;
    scanf("%d%d", &l, &r);
    for ((i) = (1); (i) <= (n); ++(i)) fa[i] = i;
    for ((i) = (0); (i) != (ch1[l - 1].size()); ++(i))
      fa[findfather(a[ch1[l - 1][i]])] = findfather(b[ch1[l - 1][i]]);
    for ((i) = (0); (i) != (ch2[r + 1].size()); ++(i))
      fa[findfather(a[ch2[r + 1][i]])] = findfather(b[ch2[r + 1][i]]);
    int tot = 0;
    for ((i) = (1); (i) <= (n); ++(i))
      if (findfather(i) == i) ++tot;
    printf("%d\n", tot);
  }
  return 0;
}
