#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int len[maxn];
int main() {
  int t, n;
  scanf("%d", &t);
  while (t--) {
    memset(len, 0, sizeof len);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &len[i]);
    }
    if (n == 2) {
      printf("0\n");
      continue;
    }
    sort(len, len + n);
    int a = len[n - 1];
    int b = len[n - 2];
    int minn = min(a, b);
    minn--;
    minn = min(n - 2, minn);
    cout << minn << endl;
  }
}
