#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:255000000")
bool firstout = 1;
template <class T>
T &minn(T &a, T b) {
  if (b < a) a = b;
  return a;
}
template <class T>
T &maxx(T &a, T b) {
  if (a < b) a = b;
  return a;
}
int &madd(int &a, int b) {
  a += b;
  if (a >= 1000000009) a -= 1000000009;
  return a;
}
int &msub(int &a, int b) {
  a -= b;
  if (a < 0) a += 1000000009;
  return a;
}
int &mmult(int &a, int b) { return a = (long long)a * b % 1000000009; }
int mdiv(long long a, long long b, long long m) {
  a = (a % m + m) % m;
  b = (b % m + m) % m;
  if (a % b == 0) return a / b;
  return (a + m * mdiv(-a, m, b)) / b;
}
int n, m;
int A[1012];
int cnt(int x) {
  long long c = (long long)x * m;
  long long res = 1;
  int i;
  long long d = 1;
  for (i = 1; c > 0 && x - i + 1 > 0; ++i) {
    d = d * (x - i + 1) / i;
    if (d * i > c) {
      res += c / i;
      break;
    }
    c -= d * i;
    res += d;
    if (res > 1012345678) break;
  }
  return min(res, (long long)1012345678);
}
int main() {
  int i, j, k;
  char c;
  int a, d;
  int ts;
  int tss;
  scanf(
      "%"
      "d",
      &(tss));
  for (ts = (1); ts < (tss + 1); ++ts) {
    scanf(
        "%"
        "d",
        &(n));
    scanf(
        "%"
        "d",
        &(m));
    int l = -1, r = n, mid;
    for (; l + 1 < r;) {
      mid = (l + r) / 2;
      if (cnt(mid) >= n)
        r = mid;
      else
        l = mid;
    }
    int res = r;
    printf(
        "%"
        "d",
        (res));
    printf("\n"), firstout = 1;
  }
  return 0;
}
