#include <bits/stdc++.h>
using namespace std;
vector<int> vect[102][102];
int count;
int cc[102], vis[102];
void dfs(int u, int color) {
  vis[u] = 1;
  if (!vect[u][color].size()) {
    return;
  }
  for (int i = 0; i < vect[u][color].size(); i++) {
    if (!vis[vect[u][color][i]]) {
      dfs(vect[u][color][i], color);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    vect[a][c].push_back(b);
    vect[b][c].push_back(a);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int count = 0;
    int u, v;
    cin >> u >> v;
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 103; k++) {
        vis[k] = 0;
      }
      dfs(u, j);
      if (vis[v]) {
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}
