#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long R = 7 + 1e9, NUMTESTCASE;
const long long NN = 10 + 1e6;
const double pi = acos(-1.0);
int di[8] = {1, 0, -1, 0, 1, -1, 1, -1}, dj[8] = {0, 1, 0, -1, 1, -1, -1, 1};
vector<pair<int, int>> Cols[NN];
map<int, int> Queries[NN];
int n, m, u, v, c, q, a, b, Ans[NN], Arr[NN], Mark[NN], Mark2[NN];
void Merge(int, int);
int Root(int);
void Initialize(vector<pair<int, int>> &Nodes) {
  ++a;
  queue<int> Queue;
  for (auto u : Nodes) {
    Arr[u.first] = u.first, Arr[u.second] = u.second;
    if (Mark[u.first] != a) Queue.push(u.first);
    if (Mark[u.second] != a) Queue.push(u.second);
    Mark[u.first] = Mark[u.second] = a;
  }
  for (auto u : Nodes) Merge(u.first, u.second);
  if (Queue.size() < 560) {
    vector<int> Node;
    while (Queue.size()) {
      Node.push_back(Queue.front());
      Queue.pop();
    }
    for (int i = (0); i <= (Node.size() - 1); ++i) {
      for (int j = (0); j <= (Node.size() - 1); ++j) {
        u = Node[i];
        v = Node[j];
        if (Queries[u].find(v) != Queries[u].end())
          Ans[Queries[u][v]] += Root(u) == Root(v);
      }
    }
    return;
  }
  while (Queue.size()) {
    u = Queue.front();
    Queue.pop();
    if (Queries[u].size())
      for (auto v : Queries[u]) Ans[v.second] += Root(v.first) == Root(u);
  }
}
int Root(int Ver) {
  if (Mark[Ver] != a) return INT_MAX;
  while (Ver != Arr[Ver]) {
    Arr[Ver] = Arr[Arr[Ver]];
    Ver = Arr[Ver];
  }
  return Ver;
}
void Merge(int u, int v) {
  u = Root(u);
  v = Root(v);
  if (u < v) swap(u, v);
  Arr[u] = v;
}
int main() {
  cin >> n >> m;
  for (int i = (1); i <= (m); ++i) {
    scanf("%d%d%d", &u, &v, &c);
    if (u > v) swap(u, v);
    Cols[c].push_back({u, v});
  }
  cin >> q;
  map<pair<int, int>, vector<int>> Temp;
  for (int i = (1); i <= (q); ++i) {
    scanf("%d%d", &u, &v);
    if (u < v) swap(u, v);
    Temp[{u, v}].push_back(i);
  }
  for (auto it : Temp)
    Queries[it.first.first][it.first.second] =
        Queries[it.first.second][it.first.first] = it.second.back();
  for (int c = (1); c <= (m); ++c)
    if (Cols[c].size()) Initialize(Cols[c]);
  for (auto it : Temp)
    for (int Res : it.second) Ans[Res] = Ans[it.second.back()];
  for (int i = (1); i <= (q); ++i) printf("%d\n", Ans[i] / 2);
  return 0;
}
