#include <bits/stdc++.h>
using namespace std;
int main() {
  int T, n, a[100005];
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    if (n <= 2) {
      printf("0\n");
      continue;
    }
    sort(a, a + n, greater<int>());
    int base = min(a[0], a[1]);
    printf("%d\n", min(base - 1, n - 2));
  }
  return 0;
}
