#include <bits/stdc++.h>
using namespace std;
int a[100010];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", a + i);
    sort(a + 1, a + 1 + n);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      int s = lower_bound(a + 1, a + 1 + n, i + 1) - a;
      if (n - s + 1 >= 2 && i + 2 <= n) ans = i;
    }
    printf("%d%c", ans, '\n');
  }
}
