#include <bits/stdc++.h>
using namespace std;
const int N = 520;
struct Dsu {
  int n;
  vector<int> dsu, size;
  int cc;
  Dsu() : n(N) {
    dsu.resize(n);
    size.resize(n);
    cc = 0;
    for (int i = 0; i < n; ++i) {
      dsu[i] = i;
      size[i] = 1;
    }
  }
  int Find(int a) {
    if (dsu[a] == a) return a;
    return dsu[a] = Find(dsu[a]);
  }
  bool Union(int a, int b) {
    int x = Find(a), y = Find(b);
    if (x == y) return false;
    if (size[x] > size[y]) {
      dsu[x] = y;
      size[y] += size[x];
      size[x] = 0;
    } else {
      dsu[y] = x;
      size[x] += size[y];
      size[y] = 0;
    }
    cc++;
    return true;
  }
};
int main() {
  int n, m, u, v;
  scanf("%d %d", &n, &m);
  vector<pair<int, int> > edge(m);
  for (int i = (0); i < (m); i++) {
    scanf("%d %d", &u, &v);
    u--;
    v--;
    edge[i] = {u, v};
  }
  Dsu l[m + 1], r[m + 1];
  for (int i = (1); i < (m + 1); i++) {
    l[i] = l[i - 1];
    l[i].Union(edge[i - 1].first, edge[i - 1].second);
  }
  for (int i = (m)-1; i >= (0); --i) {
    r[i] = r[i + 1];
    r[i].Union(edge[i].first, edge[i].second);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d", &u, &v);
    Dsu aa = l[u - 1];
    for (int i = (0); i < (n); i++) aa.Union(i, r[v].dsu[i]);
    cout << n - aa.cc << endl;
  }
}
