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