#include <bits/stdc++.h>
using namespace std;
const int MAXN = 501;
const int MAXM = 10001;
int n, m, k;
vector<pair<int, int> > edge;
vector<pair<int, int> > lt, rt;
vector<int> lpos, rpos;
int p[MAXN + 1];
int cnt[MAXN + 1];
int dsu_get(int a) {
  if (a != p[a]) p[a] = dsu_get(p[a]);
  return p[a];
}
void dsu_unit(int a, int b) {
  a = dsu_get(a);
  b = dsu_get(b);
  if (rand() & 1)
    p[a] = b;
  else
    p[b] = a;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    edge.push_back(make_pair(u, v));
  }
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = 0; i < m; i++) {
    int a = dsu_get(edge[i].first);
    int b = dsu_get(edge[i].second);
    if (a != b) {
      dsu_unit(a, b);
      lt.push_back(make_pair(a, b));
      lpos.push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = m - 1; i >= 0; i--) {
    int a = dsu_get(edge[i].first);
    int b = dsu_get(edge[i].second);
    if (a != b) {
      dsu_unit(a, b);
      rt.push_back(make_pair(a, b));
      rpos.push_back(i);
    }
  }
  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    for (int j = 1; j <= n; j++) p[j] = j;
    for (int j = 0; j < (int)lt.size(); j++)
      if (lpos[j] < l) dsu_unit(lt[j].first, lt[j].second);
    for (int j = 0; j < (int)rt.size(); j++)
      if (rpos[j] > r) dsu_unit(rt[j].first, rt[j].second);
    for (int j = 1; j <= n; j++) cnt[j] = 0;
    int ans = 0;
    for (int j = 1; j <= n; j++) ans += (cnt[dsu_get(p[j])]++ == 0);
    printf("%d\n", ans);
  }
  return 0;
}
