#include <bits/stdc++.h>
using namespace std;
int n, m;
int cnt[105][105];
vector<int> G[105][105];
bool was[105];
bool dfs(int v, int goal, int color) {
  was[v] = 1;
  if (v == goal) return true;
  for (int i = 0; i < G[v][color].size(); i++) {
    int &u = G[v][color][i];
    if (!was[u]) {
      if (dfs(u, goal, color)) return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a][c].push_back(b);
    G[b][c].push_back(a);
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    memset(was, 0, sizeof(was));
    int result = 0;
    for (int i = 1; i <= m; i++) {
      memset(was, 0, sizeof(was));
      result += dfs(a, b, i);
    }
    cout << result << endl;
  }
}
