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