#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m, q, x, y, z;
map<int, int> par[100010];
map<pair<int, int>, int> res;
vector<pair<int, int> > a[100010];
vector<int> g[100010];
vector<pair<pair<int, int>, int> > e;
int FIND(int k, int pos) {
  if (par[k].find(pos) == par[k].end() || par[k][pos] != pos)
    par[k][pos] = FIND(k, par[k][pos]);
  return par[k][pos];
}
void UNION(int k, int pos, int pos2) { par[k][FIND(k, pos)] = FIND(k, pos2); }
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &z);
    e.push_back(make_pair(make_pair(x, y), z));
    g[x].push_back(z);
    g[y].push_back(z);
  }
  for (int i = 1; i <= n; i++) {
    sort(g[i].begin(), g[i].end());
    g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
    for (int j = 0; j < g[i].size(); j++) par[g[i][j]][i] = i;
  }
  for (int i = 0; i < m; i++)
    UNION(e[i].second, e[i].first.first, e[i].first.second);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < g[i].size(); j++)
      a[i].push_back(make_pair(g[i][j], FIND(g[i][j], i)));
  cin >> q;
  for (int qn = 0; qn < q; qn++) {
    scanf("%d%d", &x, &y);
    if (a[x].size() > a[y].size()) swap(x, y);
    if (res.find(make_pair(x, y)) != res.end()) {
      cout << res[make_pair(x, y)] << endl;
      continue;
    }
    int ans = 0;
    for (int i = 0; i < a[x].size(); i++)
      if (binary_search(a[y].begin(), a[y].end(), a[x][i])) ans++;
    cout << ans << endl;
    res[make_pair(x, y)] = ans;
  }
  return 0;
}
