#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 41;
int const B = 341;
int n, m, a[N], b[N], c[N], cntC[N], used[N], par[B][N], szLong;
vector<int> st, e[N], ver;
vector<pair<int, int> > edges[N];
map<pair<int, int>, int> add;
void pushAdd() {
  for (int i = 0; i < st.size(); ++i) {
    for (int j = i + 1; j < st.size(); ++j) {
      ++add[make_pair(min(st[i], st[j]), max(st[i], st[j]))];
    }
  }
  st.clear();
}
void dfs(int u, int p) {
  st.push_back(u);
  used[u] = 1;
  for (int i = 0; i < e[u].size(); ++i) {
    int to = e[u][i];
    if (to == p || used[to]) {
      continue;
    }
    dfs(to, u);
  }
}
void solveColor(int col) {
  for (int i = 0; i < edges[col].size(); ++i) {
    int fir, sec;
    fir = edges[col][i].first;
    sec = edges[col][i].second;
    ver.push_back(fir);
    ver.push_back(sec);
    e[fir].push_back(sec);
    e[sec].push_back(fir);
  }
  for (int i = 0; i < ver.size(); ++i) {
    if (!used[ver[i]]) {
      dfs(ver[i], ver[i]);
      pushAdd();
    }
  }
  for (int i = 0; i < ver.size(); ++i) {
    used[ver[i]] = 0;
    e[ver[i]].clear();
  }
  ver.clear();
}
void solveShort() {
  for (int i = 0; i < N; ++i) {
    if (edges[i].size() > 0 && edges[i].size() < B) {
      solveColor(i);
    }
  }
}
void initDsu(int col) {
  for (int i = 1; i <= n; ++i) {
    par[szLong][i] = i;
  }
}
int find(int id, int a) {
  if (par[id][a] == a) {
    return a;
  }
  return par[id][a] = find(id, par[id][a]);
}
void uni(int id, int a, int b) {
  a = find(id, a);
  b = find(id, b);
  if (a == b) {
    return;
  }
  if (a & 1) {
    par[id][b] = a;
  } else {
    par[id][a] = b;
  }
}
void solveLong() {
  for (int i = 0; i < N; ++i) {
    if (edges[i].size() >= B) {
      initDsu(i);
      for (int j = 0; j < edges[i].size(); ++j) {
        uni(szLong, edges[i][j].first, edges[i][j].second);
      }
      ++szLong;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    edges[c[i]].push_back(make_pair(a[i], b[i]));
  }
  solveShort();
  solveLong();
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int ui, vi;
    scanf("%d%d", &ui, &vi);
    int ans = add[make_pair(min(ui, vi), max(ui, vi))];
    for (int j = 0; j < szLong; ++j) {
      if (find(j, ui) == find(j, vi)) {
        ++ans;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
