#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100, MaxM = 200;
int n, m, all, Q;
int pre[MaxM + 5], last[MaxN + 5], other[MaxM + 5], col[MaxM + 5];
bool f[MaxN + 5][MaxN + 5][MaxM + 5], vis[MaxN + 5];
void Build(int x, int y, int c) {
  pre[++all] = last[x];
  last[x] = all;
  other[all] = y;
  col[all] = c;
}
void Dfs(int orig, int x, int c) {
  int ed = last[x], dr;
  while (ed != -1) {
    dr = other[ed];
    if (col[ed] == c && !vis[dr]) {
      vis[dr] = true;
      f[orig][dr][c] = true;
      Dfs(orig, dr, c);
    }
    ed = pre[ed];
  }
}
void Init() {
  all = -1;
  memset(last, -1, sizeof(last));
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    Build(u, v, c);
    Build(v, u, c);
  }
  memset(f, 0, sizeof(f));
  for (int i = 1; i <= n; i++)
    for (int c = 1; c <= m; c++) {
      memset(vis, 0, sizeof(vis));
      vis[i] = true;
      Dfs(i, i, c);
    }
}
void Solve() {
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    int u, v, cnt = 0;
    scanf("%d%d", &u, &v);
    for (int c = 1; c <= m; c++)
      if (f[u][v][c]) cnt++;
    printf("%d\n", cnt);
  }
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    Init();
    Solve();
  }
  return 0;
}
