#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
using namespace std;

typedef long long LL;
typedef pair<int,LL> PII;
const int MAXN = 10005<<1;
vector<PII> e[MAXN];
map< int, int > mp;
int M, Q, n, num;
LL p[MAXN], sign[MAXN], a[MAXN], comp[MAXN];
PII vroot[MAXN];
bool vst[MAXN];

void dfs(int v, int A) {
	vst[v] = true;
	comp[v] = num;
	int u = p[v];
	if (u > 0) {
		if (p[u] == -1) sign[v] = 1, a[v] = A;
		else {
			if (sign[u] == 1) sign[v] = -1, a[v] = a[u] - A;
			else sign[v] = 1, a[v] = a[u] + A;
		}
	}
	for (int i = 0; i < e[v].size(); ++i) {
		PII newv = e[v][i];
		LL z = newv.first, sum = newv.second;
		if (vst[z]) {
			if (z == v) {
				if (p[z] < 0) vroot[num] = PII(1, sum);
				else {
					if (sign[v] == 1) vroot[num] = PII(1,a[v] - sum);
					else vroot[num] = PII(1, a[v]+sum);
				}
				continue;
			}
			if (p[z] < 0) {
				if (sign[v] < 0) vroot[num] = PII(1, (sum+a[v])/2);
				continue;
			}
			if (sign[z] == sign[v]) {
				if (sign[z] == 1) vroot[num] = PII(1, a[z]+a[v]-sum);
				else vroot[num] = PII(1, a[z]+a[v]+sum);
			}
		} else {
			p[newv.first] = v;
			dfs(newv.first, newv.second);
		}
	}
}

LL getvalue(int x, LL vr) { // the value of x when the value of root is vr
	if (p[x] < 0) return vr;
	if (sign[x] == 1) return a[x]-vr;
	else return vr-a[x];
}

int main() {
	while (scanf("%d%d", &M, &Q) == 2) {
		if (!M && !Q) break;
		for (int i = 0; i < MAXN; ++i)
			e[i].clear();
		mp.clear();
		n = 0;
		for (int i = 0; i < M; ++i) {
			int x, y, A;
			scanf("%d%d%d", &x, &y, &A);
			if (!mp.count(x)) mp[x] = ++n;
			if (!mp.count(y)) mp[y] = ++n;
			x = mp[x], y = mp[y];
			e[x].push_back(PII(y, A));
			e[y].push_back(PII(x, A));
		}
		memset(p, 0xff, sizeof(p));
		memset(vst, 0, sizeof(vst));
		memset(sign, 0, sizeof(sign));
		memset(a, 0, sizeof(a));
		memset(comp, 0, sizeof(comp));
		memset(vroot, 0, sizeof(vroot));
		num = 0;
		for (int i = 1; i <= n; ++i)
			if (!vst[i]) {
				++num;
				dfs(i, 0);
			}
		for (int qq = 0; qq < Q; ++qq) {
			int x, y;
			scanf("%d%d", &x, &y);
			if (!mp.count(x) || !mp.count(y)) { printf("*\n"); continue; }
			x = mp[x], y = mp[y];
			if (comp[x] != comp[y]) {
				if (!vroot[comp[x]].first || !vroot[comp[y]].first)
					printf("*\n");
				else {
					LL vx = getvalue(x, vroot[comp[x]].second);
					LL vy = getvalue(y, vroot[comp[y]].second);
					printf("%lld\n", vx+vy);
				}
				continue;
			}
			PII root = vroot[comp[x]];
			if (root.first) {
				LL vx = getvalue(x, root.second);
				LL vy = getvalue(y, root.second);
				if (x == y) printf("%lld\n", vx);
				else printf("%lld\n", vx+vy);
			} else {
				if (p[y] < 0) swap(x, y);
				if (p[x] < 0) {
					if (sign[y] == 1) printf("%lld\n", a[y]);
					else printf("*\n");
					continue;
				}
				if (sign[x] == sign[y]) { printf("*\n"); continue; }
				if (sign[x] > 0) printf("%lld\n", a[x]-a[y]);
				else printf("%lld\n", a[y]-a[x]);
			}
		}
		printf("-\n");
	}
	return 0;
}
