#include <bits/stdc++.h>
using namespace std;
int q[100010], t, n;
bool cmp(int a, int b) { return a < b; }
int main() {
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) {
    scanf("%d", &n);
    for (int j = 1; j <= n; j++) {
      scanf("%d", &q[j]);
    }
    int flag = 1;
    sort(q + 1, q + n + 1, cmp);
    for (int k = n - 2; k >= 1; k--) {
      if (q[n - 1] >= k + 1) {
        printf("%d\n", k);
        flag = 0;
        break;
      }
    }
    if (flag) printf("0\n");
  }
}
