#include <bits/stdc++.h>
using namespace std;
int n, m, q, p[105][105], i, j;
int pp(int i, int v) { return p[i][v] == v ? v : pp(i, p[i][v]); }
int main() {
  for (i = 0; i < 105; i++)
    for (j = 0; j < 105; j++) p[i][j] = j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    p[c][pp(c, a)] = pp(c, b);
  }
  scanf("%d", &q);
  while (q--) {
    int u, v, ans = 0;
    scanf("%d%d", &u, &v);
    for (i = 0; i < 105; i++)
      if (pp(i, u) == pp(i, v)) ans++;
    printf("%d\n", ans);
  }
}
