#include <bits/stdc++.h>
using namespace std;
int n, m, x, k, t, y, w, z, p[600], a[600], f[600], c[10050], b[10050],
    pre[10020][502], suf[10020][502];
int findset(int i) {
  if (p[i] == i) return i;
  return p[i] = findset(p[i]);
}
void unionset(int i, int j) {
  i = findset(i);
  j = findset(j);
  if (i > j) swap(i, j);
  p[i] = j;
  a[j] += a[i];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = pre[0][i] = suf[m + 1][i] = i;
  for (int i = 1; i <= m; ++i) {
    cin >> b[i] >> c[i];
    if (findset(b[i]) != findset(c[i])) unionset(b[i], c[i]);
    for (int j = 1; j <= n; ++j) pre[i][j] = findset(j);
  }
  for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = i;
  for (int i = m; i > 0; --i) {
    if (findset(b[i]) != findset(c[i])) unionset(b[i], c[i]);
    for (int j = 1; j <= n; ++j) suf[i][j] = findset(j);
  }
  cin >> k;
  while (k--) {
    z = 0;
    cin >> x >> y;
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= n; ++i) p[i] = pre[x - 1][i];
    for (int i = 1; i <= n; ++i)
      if (findset(i) != suf[y + 1][i]) unionset(i, suf[y + 1][i]);
    for (int i = 1; i <= n; ++i)
      if (!f[findset(i)]) f[findset(i)] = 1, z++;
    cout << z << endl;
  }
}
