#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, q, u, v;
int a[N], b[N], c[N];
int cnt = 0;
unordered_set<int> colorset[N];
unordered_map<int, int> hm[N];
map<pair<int, int>, int> answers;
int par[N];
int find(int i) {
  while (par[i] != i) {
    par[i] = par[par[i]];
    i = par[i];
  }
  return i;
}
void merge(int u, int v) {
  int a = find(u), b = find(v);
  if (a == b) return;
  par[a] = b;
}
int query(int u, int v) {
  if (u > v) swap(u, v);
  if (answers.find({u, v}) != answers.end()) return answers[{u, v}];
  if (colorset[u].size() > colorset[v].size()) swap(u, v);
  int ans = 0;
  for (int col : colorset[u]) {
    if (colorset[v].count(col)) {
      ans += (find(hm[col][u]) == find(hm[col][v]));
    }
  }
  if (u > v) swap(u, v);
  return answers[{u, v}] = ans;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int i;
  for (i = 0; 0 < 2 * m ? i < 2 * m : i > 2 * m; 0 < 2 * m ? i += 1 : i -= 1) {
    par[i] = i;
  }
  for (i = 1; 1 < m + 1 ? i < m + 1 : i > m + 1; 1 < m + 1 ? i += 1 : i -= 1) {
    cin >> a[i] >> b[i] >> c[i];
    colorset[a[i]].insert(c[i]);
    colorset[b[i]].insert(c[i]);
    if (!hm[c[i]][a[i]]) {
      hm[c[i]][a[i]] = ++cnt;
    }
    if (!hm[c[i]][b[i]]) {
      hm[c[i]][b[i]] = ++cnt;
    }
    merge(hm[c[i]][a[i]], hm[c[i]][b[i]]);
  }
  cin >> q;
  for (i = 0; 0 < q ? i < q : i > q; 0 < q ? i += 1 : i -= 1) {
    cin >> u >> v;
    cout << query(u, v) << endl;
  }
  return 0;
}
