#include <bits/stdc++.h>
using namespace std;
int dest = 0;
bool been[101];
vector<pair<int, int> > graph[101];
bool dfs(int a, int p, int& color) {
  if (a == dest) return true;
  if (been[a]) return false;
  been[a] = true;
  if (graph[a].size() == 0) return false;
  for (int i = 0; i < graph[a].size(); i++) {
    if (graph[a][i].first != p && graph[a][i].second == color &&
        dfs(graph[a][i].first, a, color))
      return true;
  }
  return false;
}
int main() {
  int n, m, a, b, c;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    graph[a].push_back(make_pair(b, c));
    graph[b].push_back(make_pair(a, c));
  }
  int q, u, v, counter = 0;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    dest = v;
    counter = 0;
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 101; k++) {
        been[k] = false;
      }
      if (dfs(u, 0, j)) counter++;
    }
    cout << counter << endl;
  }
  return 0;
}
