#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int LIM = 300;
const int MAXN = 100000;
const int MAXM = 100000;
const int MAXQ = 100000;
typedef struct E {
  int a, b, c;
} E;
bool operator<(const E &p, const E &q) { return p.c < q.c; }
typedef struct Q {
  int a, b, idx;
  long long id;
} Q;
bool operator<(const Q &p, const Q &q) { return p.id < q.id; }
int n, m, nq;
E e[MAXM];
Q q[MAXQ];
int curcol;
int ufcol[MAXN];
int ufpar[MAXN];
int ufrnk[MAXN];
int ncomp;
int comp[2 * LIM];
int nextra;
long long extra[((MAXM + LIM - 1) / LIM) * (LIM * (LIM + 1) / 2)];
int ret[MAXQ];
int uffind(int x) {
  if (ufcol[x] != curcol) {
    ufcol[x] = curcol;
    comp[ncomp++] = x;
    ufpar[x] = -1;
    ufrnk[x] = 0;
  }
  return ufpar[x] == -1 ? x : (ufpar[x] = uffind(ufpar[x]));
}
void ufjoin(int x, int y) {
  x = uffind(x), y = uffind(y);
  if (x == y) return;
  if (ufrnk[x] < ufrnk[y]) swap(x, y);
  ufpar[y] = x;
  if (ufrnk[x] == ufrnk[y]) ++ufrnk[x];
}
void run() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (m); ++i) {
    scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c);
    --e[i].a, --e[i].b, --e[i].c;
    if (e[i].a > e[i].b) swap(e[i].a, e[i].b);
  }
  sort(e, e + m);
  scanf("%d", &nq);
  for (int i = (0); i < (nq); ++i) {
    scanf("%d%d", &q[i].a, &q[i].b);
    --q[i].a, --q[i].b;
    if (q[i].a > q[i].b) swap(q[i].a, q[i].b);
    q[i].idx = i;
    q[i].id = (long long)q[i].a * n + q[i].b;
  }
  sort(q, q + nq);
  nextra = 0;
  for (int i = (0); i < (n); ++i) ufcol[i] = -1;
  for (int i = (0); i < (nq); ++i) ret[i] = 0;
  for (int i = 0, j = 0; i < m; i = j) {
    while (j < m && e[i].c == e[j].c) ++j;
    ncomp = 0;
    curcol = e[i].c;
    for (int k = (i); k < (j); ++k) ufjoin(e[k].a, e[k].b);
    if (j - i <= LIM) {
      for (int k = (0); k < (ncomp); ++k)
        for (int l = (k + 1); l < (ncomp); ++l) {
          int u = comp[k], v = comp[l];
          if (u > v) swap(u, v);
          if (uffind(u) != uffind(v)) continue;
          long long id = (long long)u * n + v;
          extra[nextra++] = id;
        }
    } else {
      for (int k = (0); k < (nq); ++k) {
        if (uffind(q[k].a) != uffind(q[k].b)) continue;
        ret[q[k].idx]++;
      }
    }
  }
  sort(extra, extra + nextra);
  for (int i = 0, j = 0, k = 0; i < nextra; i = j) {
    while (j < nextra && extra[i] == extra[j]) ++j;
    while (k < nq && q[k].id < extra[i]) ++k;
    while (k < nq && q[k].id == extra[i]) {
      ret[q[k].idx] += j - i;
      ++k;
    }
  }
  for (int i = (0); i < (nq); ++i) printf("%d\n", ret[i]);
}
int main() {
  run();
  return 0;
}
