#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
int cnt, n, ans, t, m;
int color[N];
int prnt[N];
vector<pair<int, int> > l[105];
bool visited[105];
void dfs(int src, int dest, int color) {
  if (visited[src] == true) {
    return;
  }
  visited[src] = true;
  for (auto child : l[src]) {
    int next = child.first;
    int clr = child.second;
    if (clr == color && !visited[next]) {
      dfs(next, dest, clr);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.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;
    l[a].push_back({b, c});
    l[b].push_back({a, c});
  }
  int q;
  cin >> q;
  while (q--) {
    ans = 0;
    int u, v;
    cin >> u >> v;
    for (int i = 1; i <= m; i++) {
      memset(visited, 0, sizeof(visited));
      dfs(u, v, i);
      if (visited[v] == true) {
        ans++;
      }
    }
    cout << ans << " ";
  }
}
