#include <bits/stdc++.h>
using namespace std;
vector<vector<vector<int> > > gr;
int grcol[101] = {0};
int DFS(int s, int e, int c) {
  if (grcol[s] == 0) {
    grcol[s] = 1;
    if (s == e) {
      return 1;
    }
    int max = 0;
    for (int i = 0; i < gr[s][c].size(); i++) {
      if (DFS(gr[s][c][i], e, c) == 1) {
        max = 1;
      }
    }
    return max;
  }
  return 0;
}
int main() {
  int n, m;
  cin >> n >> m;
  gr.resize(n + 1);
  for (int i = 0; i <= n; i++) {
    gr[i].resize(m + 1);
  }
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    gr[a][c].push_back(b);
    gr[b][c].push_back(a);
  }
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int s, e;
    cin >> s >> e;
    int d = 0;
    for (int c = 1; c <= m; c++) {
      d = d + DFS(s, e, c);
      for (int i = 0; i <= n; i++) {
        grcol[i] = 0;
      }
    }
    cout << d << endl;
  }
  return 0;
}
