#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& a, const vector<int>& b) {
  for (auto k : b) cout << k << " ";
  return a;
}
const int INF = 1e18;
const int MAXN = 1e4;
int n, m;
pair<int, int> edges[MAXN];
int p[501], d[501];
int cnt;
int get_par(int v) {
  if (p[v] == v) return v;
  return p[v] = get_par(p[v]);
}
void join(int u, int v) {
  u = get_par(u);
  v = get_par(v);
  if (u != v) {
    cnt--;
    if (d[u] > d[v]) swap(u, v);
    p[u] = v;
    if (d[u] == d[v]) d[v]++;
  }
}
struct query {
  int l, r, ind;
  query() {}
  query(int l_, int r_, int ind_) {
    l = l_;
    r = r_;
    ind = ind_;
  }
};
inline bool comp(const query& a, const query& b) {
  if (a.l == b.l) return a.r > b.r;
  return a.l < b.l;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> edges[i].first >> edges[i].second;
    edges[i].first--;
    edges[i].second--;
  }
  int q;
  cin >> q;
  vector<query> kek(q);
  for (int i = 0; i < q; i++) {
    cin >> kek[i].l >> kek[i].r;
    kek[i].l--;
    kek[i].r--;
    kek[i].ind = i;
  }
  sort((kek).begin(), (kek).end(), comp);
  int curind = 0;
  vector<int> ans(q, -1);
  for (int l = 0; l < m; l++) {
    cnt = n;
    for (int i = 0; i < n; i++) {
      p[i] = i;
      d[i] = 0;
    }
    for (int i = 0; i < l; i++) join(edges[i].first, edges[i].second);
    for (int r = m - 1; r >= l; r--) {
      while (curind < q && kek[curind].l == l && kek[curind].r == r) {
        ans[kek[curind].ind] = cnt;
        curind++;
      }
      join(edges[r].first, edges[r].second);
    }
  }
  for (auto k : ans) cout << k << "\n";
}
