#include <bits/stdc++.h>
using namespace std;
int c[110][110];
int root(int k, int i) {
  return (c[k][i] < 0 ? i : c[k][i] = root(k, c[k][i]));
}
void un(int k, int i, int j) {
  i = root(k, i);
  j = root(k, j);
  if (i == j) return;
  if (c[k][i] > c[k][j]) swap(i, j);
  c[k][i] += c[k][j];
  c[k][j] = i;
}
int same(int k, int i, int j) {
  if (root(k, i) == root(k, j)) return 1;
  return 0;
}
int main() {
  memset(c, -1, sizeof(c));
  int n, m;
  cin >> n >> m;
  int i, j, a, b;
  while (m--) {
    cin >> i >> j >> a;
    un(a, i, j);
  }
  cin >> m;
  while (m--) {
    cin >> i >> j;
    int t = 0;
    for (int k = 1; k < 101; ++k) {
      if (same(k, i, j)) ++t;
    }
    cout << t << '\n';
  }
  return 0;
}
