#include <bits/stdc++.h>
using namespace std;
int q, n, m;
const int maxn = 120;
bool vis[maxn];
int gr[maxn][maxn][maxn];
void dfs(int v, int c) {
  vis[v] = 1;
  for (int i = 1; i <= n; i++) {
    if (gr[v][i][c] && vis[i] == 0) {
      dfs(i, c);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    gr[a][b][c] = 1;
    gr[b][a][c] = 1;
  }
  cin >> q;
  while (q--) {
    int v, u, res = 0;
    cin >> v >> u;
    for (int i = 1; i <= m; i++) {
      memset(vis, 0, sizeof(vis));
      dfs(v, i);
      if (vis[u] == 1) {
        res++;
      }
    }
    cout << res << endl;
  }
  return 0;
}
