#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:167772160")
const int INF = 1e9;
const double EPS = 1e-9;
const double PI = acos(-1.);
pair<int, int> E[20500];
pair<int, int> C[20500];
int V[20500];
int name[1000];
bool cmp(int a, int b) {
  if (E[a].second != E[b].second) return E[a].second < E[b].second;
  return E[a].first < E[b].first;
}
int curAns;
int prvF = -1;
int prvT = -1;
int ans[20500];
int N, M;
int getName(int x) {
  if (name[x] == x) return x;
  name[x] = getName(name[x]);
  return name[x];
}
void un(pair<int, int> &x) {
  int a = getName(x.first);
  int b = getName(x.second);
  if (a == b) return;
  name[a] = b;
  --curAns;
}
void solve(int nm) {
  int frm = E[nm].first;
  int to = E[nm].second;
  if (prvT != to) {
    curAns = N;
    prvF = 0;
    for (int i = 0; i < N; ++i) {
      name[i] = i;
    }
    for (int i = to + 1; i < M; ++i) {
      un(C[i]);
    }
    prvT = to;
  }
  while (prvF < frm) {
    un(C[prvF]);
    ++prvF;
  }
  ans[nm] = curAns;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    C[i] = pair<int, int>(a, b);
  }
  int K;
  scanf("%d", &K);
  for (int i = 0; i < K; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    E[i] = pair<int, int>(a, b);
    V[i] = i;
  }
  sort(V, V + K, cmp);
  for (int i = 0; i < K; ++i) {
    solve(V[i]);
  }
  for (int i = 0; i < K; ++i) printf("%d\n", ans[i]);
  return 0;
}
