#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int>> v[101];
bool connected[101][101][101];
bool visited[101];
void dfs(int start, int node, int color) {
  int i;
  visited[node] = true;
  if (start != node) connected[start][node][color] = true;
  for (i = 0; i < v[node].size(); i++) {
    if (visited[v[node][i].first] == true) continue;
    if (color == v[node][i].second)
      dfs(start, v[node][i].first, v[node][i].second);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int i, j, a, b, c;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b >> c;
    v[a].emplace_back(b, c);
    v[b].emplace_back(a, c);
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dfs(i, i, j);
      memset(visited, false, sizeof(visited));
    }
  }
  int q, u, v;
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> u >> v;
    int cnt = 0;
    for (j = 1; j <= m; j++) {
      if (connected[u][v][j] == true) cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
