#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 305;
ll g[MAX_N][MAX_N]; // 算法中的 G 矩阵
int n,m;
void init() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j) {
g[i][j] = 0;
} else {
g[i][j] = 1e9+10;
}
}
}
}
void insert(int u, int v, ll w) {
g[u][v] = w;
}
void floyd() {
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (g[i][k] + g[k][j] < g[i][j]) {
g[i][j] = g[i][k] + g[k][j];
}
}
}
}
}
int main() {
int _,u,v;
ll w;
for(scanf("%d",&_);_;_--){
scanf("%d %d",&n,&m);
init();
for(int i=1;i<=m;i++){
scanf("%d %d %lld",&u,&v,&w);
//insert(v,u,-w);
insert(u,v,w);
}
floyd();
for(int l=1;l<=6;l++){
scanf("%d %d",&u,&v);
printf("%lld\n",-g[v][u]);
insert(u,v,-g[v][u]);
floyd();
}
}
return 0;
}