#include <bits/stdc++.h>
using namespace std;
int n, m, us[101], f = 0, t = 0;
vector<int> v[101][101], ans;
void dfs(int x, int y, int zvet) {
  if (x == y) f = 1;
  int i, j;
  us[x] = 1;
  for (i = 0; i < v[x][zvet].size(); i++) {
    int to = v[x][zvet][i];
    if (us[to] == 0) dfs(to, y, zvet);
  }
}
int main() {
  int q, x, y, g, z, i, j;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    v[x][z].push_back(y);
    v[y][z].push_back(x);
  }
  cin >> q;
  for (i = 1; i <= q; i++) {
    int kol = 0;
    cin >> x >> y;
    t = 0;
    for (j = 0; j < 101; j++) {
      for (g = 0; g < 101; g++) us[g] = 0;
      f = 0;
      dfs(x, y, j);
      kol += f;
    }
    ans.push_back(kol);
  }
  for (i = 0; i < ans.size(); i++) cout << ans[i] << endl;
  return 0;
}
