#include <bits/stdc++.h>
using namespace std;
const int mxN = 101;
vector<pair<int, int> > G[mxN];
int vis[mxN];
void dfs(int u, int ci) {
  vis[u] = 1;
  for (pair<int, int> to : G[u]) {
    int v = to.first;
    int col = to.second;
    if (!vis[v] && ci == col) dfs(v, ci);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    G[u].push_back({v, c});
    G[v].push_back({u, c});
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int cnt = 0;
    for (int i = 1; i <= m; i++) {
      memset(vis, 0, sizeof(vis));
      dfs(u, i);
      if (vis[v]) cnt++;
    }
    cout << cnt << endl;
  }
}
