#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 123, inf = 1e9 + 123, mod = 1e9 + 7;
const long long INF = 1e18 + 123;
int n, m;
vector<pair<int, int> > g[N];
bool used[N];
bool dfs(int v, int cl, int first) {
  used[v] = 1;
  if (v == first) {
    return 1;
  }
  for (auto to : g[v]) {
    if (cl == to.second && !used[to.first] && dfs(to.first, cl, first)) {
      return 1;
    }
  }
  return 0;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].push_back({b, c}), g[b].push_back({a, c});
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int cnt = 0;
    for (int i = 1; i <= m; i++) {
      memset(used, 0, sizeof(used));
      if (dfs(u, i, v)) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
