#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <climits>
#define int long long
using namespace std;
#define all(c) c.begin(), c.end()
#define line "\n"
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
vvi graph, dsu;
vector<bool> vis;
vi travel;
void dfs(int node) {
if (vis[node]) return;
vis[node] = true;
travel.push_back(node);
for (int adj : graph[node]) dfs(adj);
}
void solve() {
int n, m; cin >> n >> m;
graph.assign(n + 1, vector<int>());
vis.assign(n + 1, false);
dsu.clear();
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
travel.clear();
dfs(1);
sort(all(travel));
vi dsu1 = travel;
travel.clear();
dfs(n);
sort(all(travel));
vi dsun = travel;
for (int i = 2; i < n; i++) {
if (!vis[i]) {
travel.clear();
dfs(i);
dsu.push_back(travel);
}
}
int ans = LLONG_MAX;
for (int x : dsu1) {
auto it = upper_bound(all(dsun), x);
if (it != dsun.end()) ans = min(ans, (*it) * (*it));
if (it != dsun.begin()) {
--it;
ans = min(ans, (*it) * (*it));
}
}
for (auto &v : dsu) {
for (int x : v) {
int val1 = LLONG_MAX, val2 = LLONG_MAX;
auto it = upper_bound(all(dsun), x);
if (it != dsun.end()) val1 = min(val1, (*it - x) * (*it - x));
if (it != dsun.begin()) {
--it;
val1 = min(val1, (*it - x) * (*it - x));
}
it = upper_bound(all(dsu1), x);
if (it != dsu1.end()) val2 = min(val2, (*it - x) * (*it - x));
if (it != dsu1.begin()) {
--it;
val2 = min(val2, (*it - x) * (*it - x));
}
ans = min(ans, val1 + val2);
}
}
if (dsu1 == dsun) cout << 0 << line;
else cout << ans << line;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8ZnVuY3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sgaSsrKSB7CiAgICAgICAgaWYgKCF2aXNbaV0pIHsKICAgICAgICAgICAgdHJhdmVsLmNsZWFyKCk7CiAgICAgICAgICAgIGRmcyhpKTsKICAgICAgICAgICAgZHN1LnB1c2hfYmFjayh0cmF2ZWwpOwogICAgICAgIH0KICAgIH0KCiAgICBpbnQgYW5zID0gTExPTkdfTUFYOwogICAgZm9yIChpbnQgeCA6IGRzdTEpIHsKICAgICAgICBhdXRvIGl0ID0gdXBwZXJfYm91bmQoYWxsKGRzdW4pLCB4KTsKICAgICAgICBpZiAoaXQgIT0gZHN1bi5lbmQoKSkgYW5zID0gbWluKGFucywgKCppdCkgKiAoKml0KSk7CiAgICAgICAgaWYgKGl0ICE9IGRzdW4uYmVnaW4oKSkgewogICAgICAgICAgICAtLWl0OwogICAgICAgICAgICBhbnMgPSBtaW4oYW5zLCAoKml0KSAqICgqaXQpKTsKICAgICAgICB9CiAgICB9CgogICAgZm9yIChhdXRvICZ2IDogZHN1KSB7CiAgICAgICAgZm9yIChpbnQgeCA6IHYpIHsKICAgICAgICAgICAgaW50IHZhbDEgPSBMTE9OR19NQVgsIHZhbDIgPSBMTE9OR19NQVg7CiAgICAgICAgICAgIGF1dG8gaXQgPSB1cHBlcl9ib3VuZChhbGwoZHN1biksIHgpOwogICAgICAgICAgICBpZiAoaXQgIT0gZHN1bi5lbmQoKSkgdmFsMSA9IG1pbih2YWwxLCAoKml0IC0geCkgKiAoKml0IC0geCkpOwogICAgICAgICAgICBpZiAoaXQgIT0gZHN1bi5iZWdpbigpKSB7CiAgICAgICAgICAgICAgICAtLWl0OwogICAgICAgICAgICAgICAgdmFsMSA9IG1pbih2YWwxLCAoKml0IC0geCkgKiAoKml0IC0geCkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGl0ID0gdXBwZXJfYm91bmQoYWxsKGRzdTEpLCB4KTsKICAgICAgICAgICAgaWYgKGl0ICE9IGRzdTEuZW5kKCkpIHZhbDIgPSBtaW4odmFsMiwgKCppdCAtIHgpICogKCppdCAtIHgpKTsKICAgICAgICAgICAgaWYgKGl0ICE9IGRzdTEuYmVnaW4oKSkgewogICAgICAgICAgICAgICAgLS1pdDsKICAgICAgICAgICAgICAgIHZhbDIgPSBtaW4odmFsMiwgKCppdCAtIHgpICogKCppdCAtIHgpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBhbnMgPSBtaW4oYW5zLCB2YWwxICsgdmFsMik7CiAgICAgICAgfQogICAgfQoKICAgIGlmIChkc3UxID09IGRzdW4pIGNvdXQgPDwgMCA8PCBsaW5lOwogICAgZWxzZSBjb3V0IDw8IGFucyA8PCBsaW5lOwp9CgppbnQzMl90IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7CiAgICBjb3V0LnRpZShOVUxMKTsKCiAgICBpbnQgdDsgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgc29sdmUoKTsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==