#include <bits/stdc++.h>
using namespace std;
const int N = 1e2;
int n, m, q, vis[N], vid;
vector<int> graph[N][N];
bool dfs(int a, int b, int c) {
  if (a == b) return 1;
  if (vis[a] == vid) return 0;
  vis[a] = vid;
  for (int i = 0; i < (int)graph[a][c].size(); ++i) {
    if (dfs(graph[a][c][i], b, c)) return 1;
  }
  return 0;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0, a, b, c; i < m; ++i) {
    scanf("%d %d %d", &a, &b, &c);
    graph[--a][--c].emplace_back(--b);
    graph[b][c].emplace_back(a);
  }
  scanf("%d", &q);
  for (int i = 0, u, v; i < q; ++i) {
    scanf("%d %d", &u, &v);
    --u, --v;
    int res = 0;
    for (int j = 0; j < 100; ++j) {
      ++vid;
      res += dfs(u, v, j);
    }
    printf("%d\n", res);
  }
  return 0;
}
