#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e2 + 13;
int n, m, q, a, b, c, u, v;
int parent[MAXN][MAXN];
int findParent(int color, int num) {
  if (num == parent[color][num]) return num;
  return parent[color][num] = findParent(color, parent[color][num]);
}
void unionSet(int color, int node1, int node2) {
  int node1Parent = findParent(color, node1);
  int node2Parent = findParent(color, node2);
  if (node1Parent == node2Parent) return;
  parent[color][node1Parent] = node2Parent;
}
bool findSet(int color, int node1, int node2) {
  int node1Parent = findParent(color, node1);
  int node2Parent = findParent(color, node2);
  if (node1Parent == node2Parent) return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) parent[i + 1][j + 1] = j + 1;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    unionSet(c, a, b);
  }
  cin >> q;
  while (q--) {
    cin >> u >> v;
    int ans = 0;
    for (int i = 1; i <= m; i++)
      if (findSet(i, u, v)) ans++;
    cout << ans << "\n";
  }
  return 0;
}
