#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 4e18;
const int inf = 2e9;
const double eps = 1e-13;
const int N = 105;
int par[N][N];
int find(int c, int u) {
  if (par[c][u] != u) par[c][u] = find(c, par[c][u]);
  return par[c][u];
}
void merge(int c, int u, int v) {
  u = find(c, u);
  v = find(c, v);
  par[c][u] = v;
}
int main(void) {
  int n, m, i, j;
  for (i = 0; i < N; ++i)
    for (j = 0; j < N; ++j) par[i][j] = j;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    merge(c, a, b);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d %d", &a, &b);
    int ans = 0;
    for (i = 1; i <= m; ++i) ans += find(i, a) == find(i, b);
    printf("%d\n", ans);
  }
  return 0;
}
