#include <bits/stdc++.h>
using namespace std;
pair<int, int> E[100010];
int A[100010];
int pr[100010];
int n, m, k;
vector<pair<int, int> > R[100010];
int ata(int x) {
  if (x == pr[x]) return x;
  return pr[x] = ata(pr[x]);
}
void uni(int a, int b) {
  a = ata(a);
  b = ata(b);
  if (a == b) return;
  pr[b] = a;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) pr[i] = i;
  for (int i = 1; i <= m; i++) scanf("%d%d", &E[i].first, &E[i].second);
  scanf("%d", &k);
  int a, b;
  for (int i = 1; i <= k; i++) scanf("%d%d", &a, &b), R[a].push_back({b, i});
  for (int i = 1; i <= m; i++) {
    sort(R[i].begin(), R[i].end());
    reverse(R[i].begin(), R[i].end());
  }
  for (int i = 1; i <= m; i++) {
    int v[100010];
    for (int h = 1; h <= n; h++) v[h] = pr[h];
    int now = m;
    for (auto h : R[i]) {
      while (now > h.first) uni(E[now].first, E[now].second), now--;
      int ans = 0;
      for (int j = 1; j <= n; j++)
        if (j == pr[j]) ans++;
      A[h.second] = ans;
    }
    for (int h = 1; h <= n; h++) pr[h] = v[h];
    uni(E[i].first, E[i].second);
  }
  for (int i = 1; i <= k; i++) printf("%d\n", A[i]);
}
