#include <bits/stdc++.h>
using namespace std;
vector<int> V[105][105];
int n, m;
int q;
int colors;
int viz[105];
void dfs(int color, int nod) {
  viz[nod] = 1;
  for (int i = 0; i < V[color][nod].size(); i++) {
    int vecin = V[color][nod][i];
    if (viz[vecin]) continue;
    dfs(color, vecin);
  }
}
void read() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    V[c][x].push_back(y);
    V[c][y].push_back(x);
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int x, y;
    cin >> x >> y;
    int colors = 0;
    for (int j = 1; j <= m; j++) {
      memset(viz, 0, sizeof(viz));
      dfs(j, x);
      if (viz[y]) colors++;
    }
    cout << colors << endl;
  }
}
int main() {
  read();
  return 0;
}
