#include <bits/stdc++.h>
using namespace std;
int f[101][101];
int find(int c, int x) {
  while (x != f[c][x]) x = f[c][x];
  return x;
}
int main() {
  int n, m, q;
  int u, v, c;
  int a, b, ans;
  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = 1; i <= m; i++)
      for (int j = 1; j <= n; j++) f[i][j] = j;
    for (int h = 1; h <= m; h++) {
      scanf("%d%d%d", &u, &v, &c);
      u = find(c, u);
      v = find(c, v);
      f[c][u] = v;
    }
    scanf("%d", &q);
    while (q--) {
      ans = 0;
      scanf("%d%d", &a, &b);
      int t1, t2;
      for (int l = 1; l <= m; l++) {
        t1 = find(l, a);
        t2 = find(l, b);
        if (t1 == t2) ans++;
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
