#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 111;
int cnt[MAX_N][MAX_N];
vector<int> l;
vector<pair<int, int> > gr[MAX_N];
int used[MAX_N];
inline void add(int v, int u, int c) { gr[v].push_back({u, c}); }
void dfs(int v, int c) {
  for (int i = 0; i < (int)l.size(); ++i) ++cnt[l[i]][v], ++cnt[v][l[i]];
  l.push_back(v);
  used[v] = c;
  for (int i = 0; i < (int)gr[v].size(); ++i) {
    if (gr[v][i].second != c) continue;
    int to = gr[v][i].first;
    if (used[to] == c) continue;
    dfs(to, c);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int v, u, c;
    cin >> v >> u >> c;
    add(v, u, c);
    add(u, v, c);
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (used[j] < i) {
        l.clear();
        dfs(j, i);
      }
    }
  }
  int q;
  cin >> q;
  while (q-- > 0) {
    int v, u;
    cin >> v >> u;
    cout << cnt[v][u] << '\n';
  }
  return 0;
}
