#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int T;
cin >> T;
int n;
while (T--)
{
cin >> n;
ll arr[n];
ll copg[n], cops[n];
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
arr[i] %= mod;
}
copy(arr, arr + n, copg);
copy(arr, arr + n, cops);
sort(copg, copg + n, greater<ll>());
sort(cops, cops + n);
ll copmin = (copg[0] - copg[2]) % mod;
ll compsmin = (cops[2] - cops[0]) % mod;
ll mi = min(compsmin, copmin) % mod;
cout << mi << endl;
}
}
ICNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgogI2RlZmluZSBsbCBsb25nIGxvbmcgaW50CiAjZGVmaW5lIG1vZCAxMDAwMDAwMDA3CiAKIHVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBtYWluKCkKewoJc3RkOjppb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiNpZm5kZWYgT05MSU5FX0pVREdFCglmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKCWZyZW9wZW4oIm91dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CglmcmVvcGVuKCJlcnJvci50eHQiLCAidyIsIHN0ZGVycik7CiNlbmRpZgoJaW50IFQ7CgljaW4gPj4gVDsKCWludCBuOwoJd2hpbGUgKFQtLSkKCXsKCQljaW4gPj4gbjsKCQlsbCBhcnJbbl07CgkJbGwgY29wZ1tuXSwgY29wc1tuXTsKCQlmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkKCQl7CgkJCWNpbiA+PiBhcnJbaV07CgkJCWFycltpXSAlPSBtb2Q7CgkJfQoJCWNvcHkoYXJyLCBhcnIgKyBuLCBjb3BnKTsKCQljb3B5KGFyciwgYXJyICsgbiwgY29wcyk7CgkJc29ydChjb3BnLCBjb3BnICsgbiwgZ3JlYXRlcjxsbD4oKSk7CgkJc29ydChjb3BzLCBjb3BzICsgbik7CgkJbGwgY29wbWluID0gKGNvcGdbMF0gLSBjb3BnWzJdKSAlIG1vZDsKCQlsbCBjb21wc21pbiA9IChjb3BzWzJdIC0gY29wc1swXSkgJSBtb2Q7CgkJbGwgbWkgPSBtaW4oY29tcHNtaW4sCWNvcG1pbikgJSBtb2Q7CgkJY291dCA8PCBtaSAgPDwgZW5kbDsKIAoJfQp9Cgk=