#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 2;
const long long INF = 5e8;
const long long Mod = 998244353;
const int SQRT = 444;
int n, m, q;
vector<pair<int, int> > E[N];
int ans[N];
map<int, int> queries[N];
int isLike[N];
vector<int> G[N];
int comp[N], num;
void DFS1(int u) {
  comp[u] = num;
  for (int v : G[u])
    if (!comp[v]) DFS1(v);
}
void Heavy(vector<pair<int, int> > &E) {
  for (int i = 1; i <= n; i++) G[i].clear(), comp[i] = 0;
  num = 0;
  for (pair<int, int> e : E)
    G[e.first].push_back(e.second), G[e.second].push_back(e.first);
  for (int i = 1; i <= n; i++)
    if (!comp[i]) num++, DFS1(i);
  for (int i = 1; i <= n; i++)
    for (auto j : queries[i])
      if (comp[i] == comp[j.first]) ans[j.second]++;
}
vector<int> nodes;
void DFS2(int u) {
  comp[u] = 1;
  nodes.push_back(u);
  for (int v : G[u])
    if (!comp[v]) DFS2(v);
}
void Light(vector<pair<int, int> > &E) {
  for (pair<int, int> e : E)
    G[e.first].clear(), G[e.second].clear(), comp[e.first] = comp[e.second] = 0;
  vector<int> cur;
  for (pair<int, int> e : E)
    G[e.first].push_back(e.second), G[e.second].push_back(e.first),
        cur.push_back(e.first), cur.push_back(e.second);
  for (int i : cur)
    if (!comp[i]) {
      nodes.clear();
      DFS2(i);
      for (int i = 0, u, v; i < nodes.size(); i++)
        for (int j = 0; j < i; j++) {
          u = nodes[i];
          v = nodes[j];
          if (u > v) swap(u, v);
          auto it = queries[u].find(v);
          if (it != queries[u].end()) ans[it->second]++;
        }
    }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    E[c].push_back({u, v});
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int u, v;
    cin >> u >> v;
    if (u > v) swap(u, v);
    auto it = queries[u].find(v);
    if (it != queries[u].end())
      isLike[i] = it->second;
    else
      isLike[i] = queries[u][v] = i;
  }
  for (int i = 1; i <= m; i++)
    if (E[i].size() > SQRT)
      Heavy(E[i]);
    else
      Light(E[i]);
  for (int i = 1; i <= q; i++) cout << ans[isLike[i]] << '\n';
  return 0;
}
