#include <bits/stdc++.h>
int pre[105][105];
int find(int a, int c) {
  if (a == pre[c][a]) return a;
  return pre[c][a] = find(pre[c][a], c);
}
void unite(int a, int b, int c) {
  int x1, x2;
  x1 = find(a, c);
  x2 = find(b, c);
  if (x1 != x2) {
    pre[c][x2] = x1;
  }
}
int main() {
  for (int i = 0; i <= 104; i++)
    for (int j = 0; j <= 104; j++) pre[i][j] = j;
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    unite(a, b, c);
  }
  int q, num = 0;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    int r1, r2;
    for (int j = 1; j <= m; j++) {
      r1 = find(u, j);
      r2 = find(v, j);
      if (r1 == r2) num++;
    }
    printf("%d\n", num);
    num = 0;
  }
  return 0;
}
