#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const long long INF = (long long)1e18;
bool get(int m, int k, int n) {
  if (k <= 40 && (1LL << k) < n) return false;
  long long res = 0;
  long long c = 1;
  long long t = m * 1LL * k;
  for (int i = 0; i <= k; i++) {
    if (t < i) return false;
    long long mi = c;
    if (i != 0) mi = min(mi, t / i);
    res += mi;
    if (res >= n) return true;
    t -= mi * i;
    if (t == 0) break;
    if (i + 1 <= k) {
      if (c >= INF / (k - i)) {
        c = INF;
      } else {
        c = c * (k - i);
        c = c / (i + 1);
      }
    }
  }
  return false;
}
void solve() {
  int n, m;
  cin >> n >> m;
  if (n == 1) {
    puts("0");
    return;
  }
  if (m > n / 2) m = n / 2;
  int l = -1;
  int r = n;
  while (l < r - 1) {
    int mid = (l + r) >> 1;
    if (get(m, mid, n)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
