#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
int die = 0;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N1 = 2e5 + 5;
const int N2 = 1e5 + 5;
map<pair<int, int>, int> mp, fcy;
struct edge {
  int to, nx, id;
} e[N1 << 1];
int ce = 1;
int fi[N1];
inline void add(int u, int v, int id) {
  e[++ce] = (edge){v, fi[u], id};
  fi[u] = ce;
}
int n, m, Q;
struct in {
  int u, v, col;
} s[N1];
int a[N1], b[N1];
int fa[N1];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void uone(int x, int y) {
  int tx = find(x), ty = find(y);
  fa[tx] = ty;
}
int q[N1];
int times[N1];
int ans[N1];
void sol1() {
  sort(s + 1, s + 1 + m, [](in a, in b) { return a.col < b.col; });
  int T = 0;
  for (int i = (1); i <= (m); i++) {
    int j = i;
    T++;
    while (j + 1 <= m && s[j + 1].col == s[i].col) j++;
    int tot = 0;
    for (int k = (i); k <= (j); k++) q[++tot] = s[k].u, q[++tot] = s[k].v;
    sort(q + 1, q + 1 + tot);
    tot = unique(q + 1, q + 1 + tot) - q - 1;
    for (int k = (1); k <= (tot); k++) fa[q[k]] = q[k], times[q[k]] = T;
    for (int k = (i); k <= (j); k++) uone(s[k].u, s[k].v);
    if (tot > sqrt(n)) {
      for (int k = (1); k <= (tot); k++) {
        int u = q[k];
        for (int j = fi[u]; j; j = e[j].nx) {
          int v = e[j].to;
          if (times[v] == T && find(u) == find(v)) ans[e[j].id]++;
        }
      }
    } else {
      for (int k1 = (1); k1 <= (tot); k1++)
        for (int k2 = (k1 + 1); k2 <= (tot); k2++)
          if (find(q[k1]) == find(q[k2]) &&
              fcy.find(pair<int, int>(q[k1], q[k2])) != fcy.end())
            mp[pair<int, int>(q[k1], q[k2])]++;
    }
    i = j;
  }
  for (int i = (1); i <= (Q); i++)
    printf("%d\n", ans[i] + mp[pair<int, int>(a[i], b[i])]);
}
void rd() {
  n = read(), m = read();
  for (int i = (1); i <= (m); i++)
    s[i].u = read(), s[i].v = read(), s[i].col = read();
  Q = read();
  for (int i = (1); i <= (Q); i++) {
    a[i] = read(), b[i] = read();
    if (a[i] > b[i]) swap(a[i], b[i]);
    add(a[i], b[i], i);
    fcy[pair<int, int>(a[i], b[i])] = 1;
  }
  sol1();
}
signed main() { rd(); }
