#include <bits/stdc++.h>
using namespace std;
int visited[102];
vector<pair<int, int> > g[101];
void dfs(int color, int src, int dst) {
  visited[src] = 1;
  for (int i = 0; i < g[src].size(); i++) {
    if ((g[src])[i].second == color and visited[(g[src])[i].first] == 0) {
      dfs(color, (g[src])[i].first, dst);
    }
  }
}
int main() {
  int n, m, q, a, b, c, d;
  pair<int, int> pp;
  set<int> conj_colors;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
    conj_colors.insert(c);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    d = 0;
    for (std::set<int>::iterator it = conj_colors.begin();
         it != conj_colors.end(); it++) {
      memset(visited, 0, sizeof(visited));
      dfs(*it, a, b);
      if (visited[b] == 1) d++;
    }
    cout << d << endl;
  }
  return 0;
}
