#include <bits/stdc++.h>
using namespace std;
vector<int> v[(int)(1e5 + 10)];
vector<pair<pair<int, int>, int> > arr[(int)(1e5 + 10)];
int n, m, k, i, j, x, y, z;
int s, q;
int last[(int)(1e5 + 10)];
map<int, int> ans[(int)(1e5 + 10)];
int H[(int)(1e5 + 10)];
int f(int x, int col, int s) {
  v[x].push_back(s);
  last[x] = s;
  int bgn = lower_bound(arr[x].begin(), arr[x].end(),
                        make_pair(make_pair(col, 0), 0)) -
            arr[x].begin();
  for (int i = bgn; i < arr[x].size(); i++)
    if (arr[x][i].first.first == col and !H[arr[x][i].first.second]) {
      H[arr[x][i].first.second] = 1;
      if (last[arr[x][i].second] == s) continue;
      f(arr[x][i].second, col, s);
    } else if (arr[x][i].first.first != col)
      break;
  return 0;
}
int main() {
  scanf("%d %d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d %d %d", &x, &y, &z);
    arr[x].push_back(make_pair(make_pair(z, i), y));
    arr[y].push_back(make_pair(make_pair(z, i), x));
  }
  for (i = 1; i <= n; i++) sort(arr[i].begin(), arr[i].end());
  for (i = 1; i <= n; i++)
    for (j = 0; j < arr[i].size(); j++) {
      if (!H[arr[i][j].first.second]) {
        f(i, arr[i][j].first.first, ++s);
      }
    }
  scanf("%d", &q);
  for (i = 1; i <= q; i++) {
    scanf("%d %d", &x, &y);
    if (v[x].size() > v[y].size()) swap(x, y);
    if (ans[x].find(y) != ans[x].end()) {
      printf("%d\n", ans[x][y]);
      continue;
    }
    int a = 0, b = 0, s = 0;
    for (j = 0; j < v[x].size(); j++) {
      if (binary_search(v[y].begin(), v[y].end(), v[x][j])) s++;
    }
    ans[x][y] = s;
    printf("%d\n", s);
  }
}
