#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int f[110];
int d[110][110];
bool used[110];
vector<pair<int, int> > v[110];
int getf(int x) {
  if (x == f[x])
    return x;
  else
    return f[x] = getf(f[x]);
}
void comb(int x, int y) {
  int xroot = getf(x), yroot = getf(y);
  f[xroot] = yroot;
}
int main() {
  scanf("%d %d", &n, &m);
  memset(used, false, sizeof(used));
  for (int i = 0, x, y, z; i < m; i++) {
    scanf("%d %d %d", &x, &y, &z);
    v[x].push_back(make_pair(y, z));
    used[z] = true;
  }
  memset(d, 0, sizeof(d));
  for (int c = 0; c <= m; c++)
    if (used[c]) {
      for (int i = 1; i <= n; i++) f[i] = i;
      for (int i = 1; i <= n; i++)
        for (int j = 0; j < v[i].size(); j++)
          if (v[i][j].second == c) comb(i, v[i][j].first);
      for (int i = 1; i <= n; i++) getf(i);
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
          if (f[i] == f[j]) d[i][j]++, d[j][i]++;
    }
  scanf("%d", &q);
  for (int i = 0, u, v; i < q; i++) {
    scanf("%d %d", &u, &v);
    printf("%d\n", d[u][v] / 2);
  }
  return 0;
}
