#include <bits/stdc++.h>
using namespace std;
int v, u, c, q, n, q2, m, ans[110];
bool a[110][110][110], mark[110][110][110];
bool dfs(int i) {
  if (i == u) return true;
  mark[q2][c][i] = true;
  for (int x = 0; x < n; x++)
    if (mark[q2][c][x] == false && a[i][x][c] == true && dfs(x) == true)
      return true;
  return false;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a[x - 1][y - 1][z - 1] = a[y - 1][x - 1][z - 1] = true;
  }
  cin >> q;
  for (q2 = 0; q2 < q; q2++) {
    cin >> v >> u;
    v--;
    u--;
    for (c = 0; c < m; c++) ans[q2] += dfs(v);
  }
  for (int i = 0; i < q; i++) cout << ans[i] << '\n';
  return 0;
}
