#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=3010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1000000000;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
ll sum,ans;
bool q[N][N],bo[N];
int n,m,tot,u[N],v[2*N],pre[2*N];
int in[N],out[N],fa[N],DI[N],dis[N][N],dp[N][N],DP[N];
void add(int a,int b) {v[tot]=b;pre[tot]=u[a];u[a]=tot++;v[tot]=a;pre[tot]=u[b];u[b]=tot++;
}
void dfs(int a,int b) {in[a]=++tot;fa[a]=b;for (int i=u[a];i!=-1;i=pre[i])if (v[i]!=b) dfs(v[i],a);out[a]=tot;for (int i=1;i<=n;i++)if (!(in[i]>=in[a]&&out[i]<=out[a])) {if (!q[i][a]) dp[i][a]=dis[i][a];for (int j=u[a];j!=-1;j=pre[j])if (!q[i][v[j]]&&v[j]!=b) dp[i][a]=min(dp[i][a],dp[i][v[j]]);}
}
void init() {int i,j,x,y,z;for (i=1;i<=n;i++)for (j=1;j<=n;j++) dis[i][j]=1e8;for (i=1;i<=n;i++) dis[i][i]=0;for (i=1;i<=m;i++) {scanf("%d%d%d", &x, &y, &z);x++;y++;dis[x][y]=dis[y][x]=z;}
}
void prim() {int i,j,w,mi;memset(q,0,sizeof(q));for (i=1;i<=n;i++) q[i][i]=1;tot=0;sum=0;memset(u,-1,sizeof(u));memset(bo,0,sizeof(bo));for (i=1;i<=n;i++) fa[i]=1,DI[i]=dis[1][i];for (i=1;i<=n;i++) {w=0;mi=1e8;for (j=1;j<=n;j++)if (!bo[j]&&DI[j]<mi) w=j,mi=DI[j];sum+=mi;bo[w]=1;add(w,fa[w]);q[w][fa[w]]=q[fa[w]][w]=1;for (j=1;j<=n;j++)if (dis[w][j]<DI[j]) DI[j]=dis[w][j],fa[j]=w;}
}
void deal() {int i,j;for (i=1;i<=n;i++)for (j=1;j<=n;j++) dp[i][j]=1e8;for (i=1;i<=n;i++) in[i]=out[i]=-1;tot=0;dfs(1,1);for (i=1;i<=n;i++) DP[i]=1e8;for (i=1;i<=n;i++)for (j=1;j<=n;j++)if (!(in[j]>=in[i]&&out[j]<=out[i])) DP[i]=min(DP[i],dp[j][i]);
}
void query() {int i,x,y,z;scanf("%d", &m);ans=0;for (i=1;i<=m;i++) {scanf("%d%d%d", &x, &y, &z);x++;y++;if (!q[x][y]) ans+=sum;else if (fa[x]==y) ans+=sum-dis[x][y]+min(DP[x],z);else ans+=sum-dis[x][y]+min(DP[y],z);}printf("%.4f\n", 1.0*ans/m);
}
int main()
{while (scanf("%d%d", &n, &m)&&(n||m)) {init();prim();deal();query();}return 0;
}