#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long sq(long long x) { return (x * x) % MOD; }
long long modP(long long a, long long b) {
  return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}
int par[maxN];
map<pair<int, int>, int> memoize, mark;
vector<int> query[maxN], comp[maxN];
vector<pair<int, int> > G[maxN], Q;
void merge(int u, int v) {
  u = par[u];
  v = par[v];
  if (u == v) return;
  if (comp[u].size() > comp[v].size()) swap(u, v);
  for (auto a : comp[u])
    for (auto b : query[a])
      if (par[b] == v) memoize[make_pair(min(a, b), max(a, b))]++;
  for (auto a : comp[u]) {
    comp[v].push_back(a);
    par[a] = v;
  }
  comp[u].clear();
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  scanf("%d%d", &n, &m);
  int u, v, c;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &c);
    G[c].push_back({u, v});
  }
  for (int i = 0; i < maxN; i++) {
    par[i] = i;
    comp[i].push_back(i);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &u, &v);
    if (u > v) swap(u, v);
    Q.push_back({u, v});
    if (!mark[make_pair(u, v)]) {
      mark[make_pair(u, v)] = 1;
      query[u].push_back(v);
      query[v].push_back(u);
    }
  }
  for (int col = 1; col <= m; col++) {
    for (auto pp : G[col]) {
      merge(pp.first, pp.second);
    }
    for (auto pp : G[col]) {
      u = pp.first;
      v = pp.second;
      comp[u].clear();
      comp[v].clear();
      comp[u].push_back(u);
      comp[v].push_back(v);
      par[u] = u;
      par[v] = v;
    }
  }
  for (auto pp : Q) printf("%d\n", memoize[make_pair(pp.first, pp.second)]);
  return 0;
}
