#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
  int tt, n, i, k, s, r, e;
  cin >> tt;
  while (tt--) {
    cin >> n;
    for (i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for (i = n; i >= 1; i--) {
      r = a[i];
      s = a[i - 1];
      k = s - 1;
      e = i - 2;
      if (e >= k) {
        cout << k << endl;
        break;
      } else {
        cout << e << endl;
        break;
      }
    }
  }
}
