#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i32 = int32_t;
using ui32 = uint32_t;
using i64 = int64_t;
using ui64 = uint64_t;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
template <typename T>
T gcd(T a, T b) {
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
static int constexpr dbg = 0;
int constexpr N = 512, M = 1.1e4;
int n, m, k;
int xi[M], yi[M];
struct T {
  int16_t fa[N];
  void init() {
    for (int i = 1; i <= (int)(n); ++i) fa[i] = i;
  }
  int Fa(int i) {
    if (fa[i] == i) return i;
    return fa[i] = Fa(fa[i]);
  }
  void add(int i, int j) {
    int ii = Fa(i), jj = Fa(j);
    if (ii != jj) fa[ii] = jj;
  }
  int size() {
    int ans = 0;
    for (int i = 1; i <= (int)(n); ++i)
      if (fa[i] == i) ++ans;
    return ans;
  }
} fwd[M], bkd[M];
int slv(T l, T const &r) {
  for (int i = 1; i <= (int)(n); ++i)
    if (i != r.fa[i]) l.add(i, r.fa[i]);
  return l.size();
}
int main(int argc, char *argv[]) {
  if (!dbg) {
    std::ios::sync_with_stdio(false);
  }
  cin >> n >> m;
  for (int i = 1; i <= (int)(m); ++i) cin >> xi[i] >> yi[i];
  fwd[0].init();
  bkd[m + 1].init();
  for (int i = 1; i <= (int)(m); ++i) {
    fwd[i] = fwd[i - 1];
    fwd[i].add(xi[i], yi[i]);
  }
  for (int i = m; i >= 1; --i) {
    bkd[i] = bkd[i + 1];
    bkd[i].add(xi[i], yi[i]);
  }
  cin >> k;
  for (int i = 1; i <= (int)(k); ++i) {
    int l, r;
    cin >> l >> r;
    cout << slv(fwd[l - 1], bkd[r + 1]) << '\n';
  }
  return 0;
}
