#include <bits/stdc++.h>
using namespace std;
int n, m;
void dfs(int u, int y, vector<set<int>> &visit,
         vector<vector<pair<int, int>>> &adj, int *coun, int color) {
  if (color > 0) visit[u].insert(color);
  int vector, c, i;
  if (u == y) {
    (*coun)++;
    return;
  }
  for (i = 0; i < adj[u].size(); i++) {
    pair<int, int> e = adj[u][i];
    vector = e.first, c = e.second;
    if (c == color || color == -1)
      if (visit[vector].find(c) == visit[vector].end()) {
        dfs(vector, y, visit, adj, coun, c);
      }
  }
  return;
}
int main() {
  int x, y, c, i, q;
  cin >> n >> m;
  vector<vector<pair<int, int>>> adj(n);
  for (i = 0; i < m; i++)
    scanf(" %d", &x), scanf(" %d", &y), scanf(" %d", &c),
        adj[x - 1].push_back({y - 1, c}), adj[y - 1].push_back({x - 1, c});
  cin >> q;
  while (q--) {
    scanf(" %d", &x), scanf(" %d", &y);
    vector<set<int>> visit(n);
    int count = 0;
    dfs(x - 1, y - 1, visit, adj, &count, -1);
    printf("%d\n", count);
  }
  return 0;
}
