#include <bits/stdc++.h>
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int a;
    scanf("%d", &a);
    int arr[a];
    for (int i = 0; i < a; i++) scanf("%d", &arr[i]);
    int m, n;
    int p;
    m = -1;
    n = -1;
    for (int i = 0; i < a; i++) {
      if (arr[i] > m) {
        m = arr[i];
        p = i;
      }
    }
    for (int i = 0; i < a; i++) {
      if (arr[i] > n && i != p) {
        n = arr[i];
      }
    }
    int ans = n - 1;
    if ((a - 2) < ans) ans = (a - 2);
    printf("%d\n", ans);
  }
  return 0;
}
