#include <bits/stdc++.h>
using namespace std;
const long long mx = 1e6 + 9;
vector<pair<long long, long long> > adj[mx];
bool visited[mx];
set<long long> col;
long long flag;
void dfs(long long sor, long long des, long long cl) {
  if (sor == des) {
    flag = 1;
    return;
  }
  visited[sor] = true;
  for (long long i = 0; i < adj[sor].size(); i++) {
    long long v = adj[sor][i].first;
    long long c = adj[sor][i].second;
    if (visited[v] == false && cl == c) {
      dfs(v, des, c);
    }
  }
}
int main() {
  long long i, j, a, b, c, d, n, m, t, x, y, u, v, q;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> u >> v >> c;
    adj[u].push_back({v, c});
    adj[v].push_back({u, c});
    col.insert(c);
  }
  cin >> q;
  while (q--) {
    cin >> u >> v;
    long long cnt = 0;
    for (auto it : col) {
      c = it;
      flag = 0;
      memset(visited, 0, sizeof(visited));
      dfs(u, v, c);
      if (flag == 1) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
