#include <bits/stdc++.h>
using namespace std;
int n, m, cn, q, ai, bi, ci, cnt;
vector<int> a[105][105];
bool v[105];
void f(int p, int q, int r) {
  int i;
  v[q] = true;
  if (q == r) {
    cnt++;
    return;
  }
  for (i = 0; i < a[p][q].size(); i++) {
    if (!v[a[p][q][i]]) f(p, a[p][q][i], r);
  }
}
int main() {
  int i, j, k;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &ai, &bi, &ci);
    a[ci][ai].push_back(bi);
    a[ci][bi].push_back(ai);
    if (ci > cn) cn = ci;
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    scanf("%d %d", &ai, &bi);
    cnt = 0;
    for (j = 1; j <= cn; j++) {
      for (k = 1; k <= n; k++) {
        v[k] = false;
      }
      f(j, ai, bi);
    }
    cout << cnt << endl;
  }
}
