#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 20;
int N, M, Q, dad[maxn];
bool used[maxn];
map<pair<int, int>, int> ans;
vector<pair<int, int> > v[maxn], w[maxn];
int find(int a) {
  if (dad[a] == a) return a;
  return dad[a] = find(dad[a]);
}
int main() {
  cin >> N >> M;
  for (int i = 1; i <= M; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    w[c].push_back(pair<int, int>(a, b));
  }
  for (int i = 1; i <= M; i++)
    if (w[i].size()) {
      for (int j = 0; j < (int)w[i].size(); j++)
        dad[w[i][j].first] = w[i][j].first,
        dad[w[i][j].second] = w[i][j].second;
      for (int j = 0; j < (int)w[i].size(); j++) {
        int a = w[i][j].first, b = w[i][j].second;
        a = find(a), b = find(b);
        dad[dad[a]] = dad[b];
      }
      for (int j = 0; j < (int)w[i].size(); j++) {
        int a = w[i][j].first, b = w[i][j].second;
        if (!used[a]) v[a].push_back(pair<int, int>(i, find(a))), used[a] = 1;
        if (!used[b]) v[b].push_back(pair<int, int>(i, find(b))), used[b] = 1;
      }
      for (int j = 0; j < (int)w[i].size(); j++) {
        int a = w[i][j].first, b = w[i][j].second;
        used[a] = used[b] = 0;
      }
    }
  cin >> Q;
  for (int q = 1; q <= Q; q++) {
    int a, b, res = 0;
    scanf("%d %d", &a, &b);
    if (v[a].size() > v[b].size()) swap(a, b);
    if (ans.count(pair<int, int>(a, b))) {
      printf("%d\n", ans[pair<int, int>(a, b)]);
      continue;
    }
    for (int i = 0; i < (int)v[a].size(); i++) {
      __typeof(v[a].begin()) it =
          lower_bound(v[b].begin(), v[b].end(), v[a][i]);
      if (it != v[b].end() && (*it) == v[a][i]) res++;
    }
    ans[pair<int, int>(a, b)] = res;
    printf("%d\n", res);
  }
  return 0;
}
