#include <bits/stdc++.h>
using namespace std;
const int N = 400100;
int T, n, a[N];
int main() {
  scanf("%d", &T);
  for (int t = 1; t <= T; t++) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    sort(a, a + n);
    for (int i = n - 2; i >= 0; i--) {
      if (a[n - 1] >= i + 1 && a[n - 2] >= i + 1) {
        printf("%d\n", i);
        break;
      }
    }
  }
  return 0;
}
