#include <bits/stdc++.h>
int calc() {
  int n;
  scanf("%d", &n);
  int max = 0;
  int prevmax = 0;
  for (int a = 0; a < n; a++) {
    int tmp;
    scanf("%d", &tmp);
    if (tmp >= max) {
      prevmax = max;
      max = tmp;
    } else if (tmp > prevmax) {
      prevmax = tmp;
    }
  }
  int output = std::min(n - 2, prevmax - 1);
  if (output < 1) return 0;
  return output;
}
int main() {
  int t;
  scanf("%d", &t);
  for (int a = 0; a < t; a++) {
    printf("%d\n", calc());
  }
}
