#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > edges[1000];
bool visited[1000];
int col[1000];
int n, m;
bool dfs(int s, int d, int c) {
  visited[s] = true;
  bool rs = true;
  for (int i = 0; i < edges[s].size(); i++) {
    int neighbor = edges[s][i].first;
    int color = edges[s][i].second;
    if (color == c && neighbor == d) {
      return true;
    } else if (color == c && !visited[neighbor]) {
      if (dfs(neighbor, d, c)) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    edges[--x].push_back(make_pair(--y, z));
    edges[y].push_back(make_pair(x, z));
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    int ans = 0;
    for (int c = 1; c <= 100; c++) {
      if (dfs(u, v, c)) {
        ans++;
      }
      for (int i = 0; i < n; i++) {
        visited[i] = false;
      }
    }
    cout << ans << endl;
  }
}
