#include <bits/stdc++.h>
using namespace std;
int f;
bool vis[105];
void dfs(vector<vector<pair<int, int> > > g, int t, int u, int v, int c) {
  vis[t] = true;
  for (int i = 0; i < g[t].size(); i++) {
    if (g[t][i].second == c) {
      if (vis[g[t][i].first] == false) {
        if (g[t][i].first == v)
          f = 1;
        else if (g[t][i].first == u) {
        } else
          dfs(g, g[t][i].first, u, v, c);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, i, j, q;
  cin >> n >> m;
  vector<vector<pair<int, int> > > g(n + 1);
  while (m--) {
    int u, v, c;
    cin >> u >> v >> c;
    g[u].push_back({v, c});
    g[v].push_back({u, c});
  }
  cin >> q;
  while (q--) {
    int u, v;
    f = 0;
    set<int> s;
    cin >> u >> v;
    for (i = 0; i < g[u].size(); i++) {
      f = 0;
      for (j = 0; j < 105; j++) vis[j] = false;
      if (g[u][i].first == v)
        s.insert(g[u][i].second);
      else {
        dfs(g, g[u][i].first, u, v, g[u][i].second);
        if (f == 1) s.insert(g[u][i].second);
      }
    }
    cout << s.size() << endl;
  }
  return 0;
}
