#include <bits/stdc++.h>
using namespace std;
unsigned long long a, b, c, d, e[5555555], f, g, h, i, j, k, l, m, n, o, p, q,
    r, s, t, u, v, w, x, y, z, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll,
    mm, nn, oo, pp, qq, rr, ss, tt, uu, vv, ww, xx, yy, zz;
int main() {
  cin >> a;
  for (b = 0; b < a; b++) {
    cin >> c;
    for (d = 0; d < c; d++) {
      cin >> e[d];
    }
    sort(e, e + c);
    cout << min((e[c - 2] - 1), c - 2) << endl;
  }
}
