#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
int p[N][N], sz[N][N];
int find(int id, int x) {
  if (x == p[id][x]) return x;
  return p[id][x] = find(id, p[id][x]);
}
void join(int id, int x, int y) {
  x = find(id, x);
  y = find(id, y);
  if (x == y) return;
  if (sz[id][y] > sz[id][x]) swap(x, y);
  p[id][y] = x;
  sz[id][x] += sz[id][y];
}
void pre() {
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < N; j++) {
      p[i][j] = j;
      sz[i][j] = 1;
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  pre();
  int x, y, z;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    join(z, x, y);
  }
  int q;
  cin >> q;
  while (q--) {
    cin >> x >> y;
    int cnt = 0;
    for (int id = 1; id < N; id++) {
      cnt += (find(id, p[id][x]) == find(id, p[id][y]));
    }
    cout << cnt << "\n";
  }
  return 0;
}
