#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
void maximize(T &x, U y) {
  if (x < y) x = y;
}
template <class T, class U>
void minimize(T &x, U y) {
  if (x > y) x = y;
}
template <class T>
T Abs(T x) {
  return (x < 0 ? -x : x);
}
template <class T>
T safe_sqrt(T x) {
  return sqrt(max(x, (T)0));
}
template <class T, class U, class V>
T addmod(T x, U k, V __base) {
  return ((x + k) % __base + __base) % __base;
}
template <class T, class U, class V>
T submod(T x, U k, V __base) {
  return ((x - k) % __base + __base) % __base;
}
template <class T, class U, class V>
T mul(T x, U y, V __base) {
  return (long long)x * y % __base;
}
namespace taskD {
const int N = 5e2 + 55;
struct query {
  int l, r, pos;
  query() {}
  query(int _l, int _r, int _pos) : l(_l), r(_r), pos(_pos) {}
};
vector<query> left, right;
vector<pair<int, int> > edges;
int n, m, q;
int par[N];
int findp(int x) { return (par[x] < 0 ? x : findp(par[x])); }
bool join(int u, int v) {
  u = findp(u), v = findp(v);
  if (u == v) return false;
  if (par[u] > par[v]) swap(u, v);
  par[u] += par[v], par[v] = u;
  return true;
}
void solve() {
  scanf("%d%d", &n, &m);
  for (int u, v, i = 1; i <= m; i++)
    scanf("%d%d", &u, &v), edges.push_back(make_pair(u, v));
  for (int i = 1; i <= n; i++) par[i] = -1;
  for (int i = 0; i < ((int)edges.size()); i++) {
    int u = edges[i].first;
    int v = edges[i].second;
    if (!join(u, v)) continue;
    left.push_back(query(u, v, i + 1));
  }
  for (int i = 1; i <= n; i++) par[i] = -1;
  for (int i = ((int)edges.size()) - 1; i >= 0; i--) {
    int u = edges[i].first;
    int v = edges[i].second;
    if (!join(u, v)) continue;
    right.push_back(query(u, v, i + 1));
  }
  scanf("%d", &q);
  while (q--) {
    int lef, rig;
    scanf("%d%d", &lef, &rig);
    int ans = n;
    for (int i = 1; i <= n; i++) par[i] = -1;
    for (auto p : left) {
      int pos = p.pos, u = p.l, v = p.r;
      if (pos >= lef) break;
      if (!join(u, v)) continue;
      ans--;
    }
    for (auto p : right) {
      int pos = p.pos, u = p.l, v = p.r;
      if (pos <= rig) break;
      if (!join(u, v)) continue;
      ans--;
    }
    printf("%d\n", ans);
  }
}
}  // namespace taskD
int main(void) { taskD::solve(); }
