using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
class Myon
{
public Myon() { }
public static int Main()
{
new Myon().calc();
return 0;
}
Scanner cin;
long mod = (int)1e9 + 7;
void calc()
{
cin = new Scanner();
int N = cin.nextInt();
int M = cin.nextInt();
int[] A = new int[M];
int[] B = new int[M];
int[] C = new int[M];
for (int i = 0; i < M; i++)
{
A[i] = cin.nextInt() - 1;
B[i] = cin.nextInt() - 1;
C[i] = cin.nextInt();
}
int K = cin.nextInt();
int[] X = new int[K];
int[] Y = new int[K];
int[] Z = new int[K];
for (int i = 0; i < K; i++)
{
X[i] = cin.nextInt() - 1;
Y[i] = cin.nextInt() - 1;
Z[i] = cin.nextInt();
}
int MAX = int.MaxValue / 3;
int[,] dist = new int[N, N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
dist[i, j] = MAX;
}
dist[i, i] = 0;
}
for (int i = 0; i < M; i++)
{
dist[A[i], B[i]] = Math.Min(dist[A[i], B[i]], C[i]);
dist[B[i], A[i]] = dist[A[i], B[i]];
}
for (int k = 0; k < N; k++)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
dist[i, j] = Math.Min(dist[i, j],
dist[i, k] + dist[k, j]);
}
}
}
for (int t = 0; t < K; t++)
{
int x = X[t];
int y = Y[t];
int z = Z[t];
dist[x, y] = Math.Min(dist[x, y], z);
dist[y, x] = dist[x, y];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < i; j++)
{
dist[i, j] = Math.Min(dist[i, j]
,Math.Min(dist[i, x] + dist[y, j]+ z
, dist[i, y] + dist[x, j] + z));
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < i; j++)
{
dist[j, i] = dist[i, j];
}
}
long ret = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < i; j++)
{
ret += dist[i, j];
}
}
Console.WriteLine(ret);
}
}
}
class Scanner
{
string[] s;
int i;
char[] cs = new char[] { ' ' };
public Scanner()
{
s = new string[0];
i = 0;
}
public string next()
{
if (i < s.Length) return s[i++];
string st = Console.ReadLine();
while (st == "") st = Console.ReadLine();
s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
i = 0;
return s[i++];
}
public int nextInt()
{
return int.Parse(next());
}
public long nextLong()
{
return long.Parse(next());
}
public double nextDouble()
{
return double.Parse(next());
}
}
class XRand
{
uint x, y, z, w;
public XRand()
{
init();
}
public XRand(uint s)
{
init();
init_xor128(s);
}
void init()
{
x = 314159265; y = 358979323; z = 846264338; w = 327950288;
}
public void init_xor128(uint s)
{
z ^= s;
z ^= z >> 21; z ^= z << 35; z ^= z >> 4;
z *= 736338717;
}
uint next()
{
uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}
public long nextLong(long m)
{
return (long)((((ulong)next() << 32) + next()) % (ulong)m);
}
public int nextInt(int m)
{
return (int)(next() % m);
}
public long nextLong(long min, long max)
{
return min + nextLong(max - min + 1);
}
public int nextInt(int min, int max)
{
return min + nextInt(max - min + 1);
}
public int nextIntP(int a)
{
return (int)Math.Pow(a, nextDouble());
}
public int nextIntP(int min, int max)
{
int diff = max - min;
return min + nextIntP(diff + 2) - 1;
}
public long nextLongP(long a)
{
return (long)Math.Pow(a, nextDouble());
}
public long nextLongP(long min, long max)
{
long diff = max - min;
return min + nextLongP(diff + 2) - 1;
}
public double nextDouble()
{
return (double)next() / uint.MaxValue;
}
public double nextDoubleP(double a)
{
return Math.Pow(a, nextDouble());
}
}