#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
using str = string;
template <class T>
using V = vector<T>;
template <class T1, class T2>
using umap = unordered_map<T1, T2>;
using pi = pair<int, int>;
using vi = V<int>;
int n, e, src, dest;
V<bool> visited;
V<V<pair<int, int>>> adj;
bool dfs(int node, int color);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> e;
  adj.resize(n);
  for (int i = 0; i < e; i++) {
    int a, b, col;
    cin >> a >> b >> col;
    a--;
    b--;
    col--;
    adj[a].push_back({b, col});
    adj[b].push_back({a, col});
  }
  int q;
  cin >> q;
  while (q--) {
    cin >> src >> dest;
    src--;
    dest--;
    int cnt = 0;
    for (int i = 0; i < e; i++) {
      visited.clear();
      visited.resize(n, 0);
      cnt += dfs(src, i);
    }
    cout << cnt << "\n";
  }
}
bool dfs(int node, int color) {
  visited[node] = 1;
  if (node == dest) return 1;
  for (auto u : adj[node])
    if (u.second == color && !visited[u.first])
      if (dfs(u.first, u.second)) return true;
  return false;
}
