#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<pair<int, int>>> g;
bool vis[100];
void dfs(int v, int color) {
  vis[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    if (!vis[g[v][i].first] && g[v][i].second == color) {
      dfs(g[v][i].first, g[v][i].second);
    }
  }
}
int main() {
  cin >> n >> m;
  g.resize(n);
  int from, to, color;
  for (int i = 0; i < m; i++) {
    cin >> from >> to >> color;
    from--;
    to--;
    g[from].push_back(make_pair(to, color));
    g[to].push_back(make_pair(from, color));
  }
  int q, amount = 0;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> from >> to;
    from--;
    to--;
    amount = 0;
    for (int k = 1; k <= 100; k++) {
      for (int j = 0; j < n; j++) {
        vis[j] = false;
      }
      dfs(from, k);
      if (vis[to]) {
        amount++;
      }
    }
    cout << amount << endl;
  }
}
