#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
const int N = 111, INF = (int)1e9;
vector <ii> G[N], S;
int V, E, dist[N], used[N][N], vis[N], all, cnt;
int lev[N];
int p[N];
void clear(void) {
for (int i = 0; i <= V; ++i) {
dist[i] = 0, vis[i] = 0;
p[i] = -1;
lev[i] = 0;
}
}
int bfs(ii cur) {
int sum = 0;
int s = cur.first, t = cur.second;
int level = 0x7FFFFFFF;
queue <int> q; q.push(s);
dist[s] = 0;
p[s] = 0;
vis[s] = 1;
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < (int)G[u].size(); ++i) {
ii v = G[u][i];
if (v.first == t) {
if (sum < dist[u] + v.second) {
sum = dist[u] + v.second;
dist[t] = sum;
p[v.first] = u;
level = lev[v.first] + 1;
}
} else if (!vis[v.first]) {
dist[v.first] = dist[u] + v.second;
p[v.first] = u;
++lev[v.first];
q.push(v.first);
}
}
}
for (int i = t; i > 0; i = p[i]) vis[i] = 1;
return sum;
}
int main(void) {
int tc; scanf("%d", &tc);
while (tc--) {
scanf("%d %d", &V, &E);
S.clear();
int s[4], t[4];
for (int i = 0; i <= V; ++i) G[i].clear();
for (int i = 0; i < 3; ++i) scanf("%d", &s[i]);
for (int i = 0; i < 3; ++i) scanf("%d", &t[i]);
for (int i = 0; i < 3; ++i) S.push_back(ii(s[i], t[i]));
for (int i = 0; i < E; ++i) {
int u, v, w; scanf("%d %d %d", &u, &v, &w);
G[u].push_back(ii(v, w));
}
sort(S.begin(), S.end());
int res = 0;
bool flag = false;
int x = 0;
do {
bool f = false;
all = cnt = 0;
for (int i = 0; i < 3; ++i) {
x = bfs(S[i]);
if (!x) {
f = true; break;
}
all += x;
}
clear();
if (f) continue;
res = max(all, res);
} while (next_permutation(S.begin(), S.end()));
printf("%d\n", res);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIHBhaXI8aW50LCBp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