#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 2;
const int MOD = 1 << 30;
const double pi = acos(-1);
struct line {
  double a, b, c;
};
struct point {
  double x, y;
};
void pointsToLine(point p1, point p2, line &l) {
  if (fabs(p1.x - p2.x) < 1e-2) {
    l.a = 1.0;
    l.b = 0.0;
    l.c = -p1.x;
  } else {
    l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x);
    l.b = 1.0;
    l.c = -(double)(l.a * p1.x) - p1.y;
  }
}
bool areParallel(line l1, line l2) {
  return ((fabs(l1.a - l2.a) < 1e-2) && (fabs(l2.b - l2.b) < 1e-2));
}
bool areSame(line l1, line l2) {
  return (areParallel(l1, l2) && (fabs(l1.c - l2.c) < 1e-2));
}
bool areIntersect(line l1, line l2, point &p) {
  if (areParallel(l1, l2)) return false;
  p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b);
  if (fabs(l1.b) > 1e-2)
    p.y = -(l1.a * p.x + l1.c);
  else
    p.y = -(l2.a * p.x + l2.c);
  return true;
}
vector<pair<int, int> > AdjList[N];
int n, m;
int vis[N];
void dfs(int u, int c) {
  vis[u] = true;
  for (int i = 0; i < (int)AdjList[u].size(); ++i) {
    pair<int, int> v = AdjList[u][i];
    if (!vis[v.first]) {
      if (v.second == c) dfs(v.first, v.second);
    }
  }
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    AdjList[u].push_back({v, w});
    AdjList[v].push_back({u, w});
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int ans = 0;
    for (int i = 1; i <= m; ++i) {
      memset(vis, false, sizeof vis);
      dfs(u, i);
      if (vis[v]) ans++;
    }
    cout << ans << '\n';
  }
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc = 1;
  while (tc--) solve();
}
