#include <bits/stdc++.h>
using namespace std;
long long n, m, T;
long long check(long long x) {
  long long i, C = 1, used, rest, sum = 1;
  for (rest = x * m, i = 1; i <= rest && i <= x; rest -= i * used, ++i) {
    C = C * (x - i + 1) / i;
    used = min(rest / i, C);
    sum += used;
    if (sum >= n) return true;
  }
  return false;
}
int main() {
  long long l, r, mid;
  scanf("%I64d", &T);
  while (T--) {
    scanf("%I64d %I64d\n", &n, &m);
    for (l = 0, r = n; l < r;) {
      mid = (l + r) >> 1;
      if (check(mid))
        r = mid;
      else
        l = mid + 1;
    }
    if (n == 1)
      printf("0\n");
    else
      printf("%I64d\n", l);
  }
  return 0;
}
